Does gcc support 128-bit int on amd64?

2019-01-12 02:17发布

问题:

Does gcc support 128-bit int on amd64?

How to define it?

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

回答1:

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

Note: before version 4.6.0 they were named __int128_t and __uint128_t.



回答2:

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)



标签: gcc