Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does gcc support 128-bit int on amd64? [duplicate]

Does gcc support 128-bit int on amd64?

How to define it?

How to use scanf/printf to read/write it?

like image 522
Yichao Zhou Avatar asked Jul 25 '10 14:07

Yichao Zhou


People also ask

Is there a 128-bit integer limit?

The 128-bit data type can handle up to 31 significant digits (compared to 17 handled by the 64-bit long double).

What is the 128-bit limit?

Representation. 128-bit processors could be used for addressing directly up to 2128 (over 3.40×1038) bytes, which would greatly exceed the total data captured, created, or replicated on Earth as of 2018, which has been estimated to be around 33 zettabytes (over 274 bytes).

How do I store a 128-bit number?

If you only need to store it then you can store it in a byte array like "char num128[16]". If you need to manipulate it you need to use big numbers library like GMP. Show activity on this post. It is not possible to store it in one primitive data type, so we have to be slightly more creative.

How do you write a 128-bit integer?

Simply write __int128 for a signed 128-bit integer, or unsigned __int128 for an unsigned 128-bit integer. There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.


2 Answers

GCC supports built-in __int128 and unsigned __int128 types (on 64-bit platforms only), but it looks like formatting support for 128-bit integers is less common in libc.

Note: <stdint.h> defines __int128_t and __uint128_t on versions before gcc4.6. See also Is there a 128 bit integer in gcc? for a table of gcc/clang/ICC versions.

How to know if __uint128_t is defined for detecting __int128

like image 146
rkhayrov Avatar answered Sep 24 '22 15:09

rkhayrov


void f(__int128* res, __int128* op1, __int128* op2) {     *res = *op1 + *op2; } 

Save to test.c and compile with:

$ gcc -c -O3 test.c $ objdump -d -M intel test.o 

You get:

mov    rcx, rdx mov    rax, [rsi] mov    rdx, [rsi+0x8]  add    rax, [rcx] adc    rdx, [rcx+0x8]  mov    [rdi], rax mov    [rdi+0x8], rdx 

As you can see the __int128 type is supported by keeping two 64-bits in sequence and then operating on them with the typical big int pattern of using two instructions, for example ADD and then ADC (add with carry)

like image 43
Andrew Tomazos Avatar answered Sep 24 '22 15:09

Andrew Tomazos