Question

Does gcc support 128-bit int on amd64?

How to define it?

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

Was it helpful?

Solution

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

OTHER TIPS

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top