Is there a 128 bit integer in gcc?

喜欢而已 提交于 2019-11-26 05:54:30

问题


I want a 128 bit integer because I want to store results of multiplication of two 64 bit numbers. Is there any such thing in gcc 4.4 and above?


回答1:


A 128-bit integer type is only ever available on 64-bit targets, so you need to check for availability even if you have already detected a recent GCC version. In theory gcc could support TImode integers on machines where it would take 4x 32-bit registers to hold one, but I don't think there are any cases where it does.


GCC 4.6 and later has a __int128 / unsigned __int128 defined as a built-in type. Use
#ifdef __SIZEOF_INT128__ to detect it.

GCC 4.1 and later define __int128_t and __uint128_t in <stdint.h>. On recent compilers, this is presumably defined in terms of __int128. (You still need to #include <stdint.h> if you want to use the __int128_t name instead of __int128.)

I tested on the Godbolt compiler explorer for the first versions of compilers to support each of these 3 things (on x86-64). Godbolt only goes back to gcc4.1, ICC13, and clang3.0, so I've used <= 4.1 to indicate that the actual first support might have been even earlier.

         legacy               recommended(?)    |  One way of detecting support
        __uint128_t   |  [unsigned]  __int128   |  #ifdef __SIZEOF_INT128__
gcc        <=  4.1    |       4.6               |     4.6
clang      <=  3.0    |       3.1               |     3.3
ICC        <=  13     |     <= 13               |     16.  (Godbolt doesn't have 14 or 15)

If you compile for a 32-bit architecture like ARM, or x86 with -m32, no 128-bit integer type is supported with even the newest version of any of these compilers. So you need to detect support before using, if it's possible for your code to work at all without it.

The only direct CPP macro I'm aware of for detecting it is __SIZEOF_INT128__, but unfortunately some old compiler versions support it without defining it. (And there's no macro for __uint128_t, only the gcc4.6 style unsigned __int128). How to know if __uint128_t is defined

Some people still use ancient compiler versions like gcc4.4 on RHEL (RedHat Enterprise Linux), or similar crusty old systems. If you care about obsolete gcc versions like that, you probably want to stick to __uint128_t. And maybe detect 64-bitness in terms of sizeof(int_fast32_t) which for some reason is 8 on some 64-bit ISAs. But not on ILP32 ISAs like x32 or ILP32 AArch64, so maybe just check sizeof(void*) if __SIZEOF_INT128__ isn't defined.

There might be some 64-bit ISAs where gcc doesn't define __int128, or maybe even some 32-bit ISAs where gcc does define __int128, but I'm not aware of any.

As comments on another answer here point out, the GCC internals are integer TI mode. (Tetra-integer = 4x width of int, vs. DImode = double width vs. SImode = plain int.) As the GCC manual points out, __int128 is supported on targets that support a 128-bit integer mode (TImode).

typedef unsigned uint128_t __attribute__ ((mode (TI)));

Random fact: ICC19 -E -dM defines:

#define __GLIBCXX_TYPE_INT_N_0 __int128
#define __GLIBCXX_BITSIZE_INT_N_0 128

The test function was:

#include <stdint.h>

#define uint128_t __uint128_t
//#define uint128_t unsigned __int128

uint128_t mul64(uint64_t a, uint64_t b) {
    return (uint128_t)a * b;
}

compilers that support it all compile it efficiently, to

    mov       rax, rdi
    mul       rsi
    ret                          # return in RDX:RAX



回答2:


Ah, big integers are not C's forte.

GCC does have an unsigned __int128/__int128 type, starting from version 4.something (not sure here). I do seem to recall, however, that there was a __int128_t def before that.

These are only available on 64-bit targets.

(Editor's note: this answer used to claim that gcc defined uint128_t and int128_t. None of the versions I tested on the Godbolt compiler explorer define those types without leading __, from gcc4.1 to 8.2 , or clang or ICC.)




回答3:


You could use a library which handles arbitrary or large precision values, such as the GNU MP Bignum Library.



来源:https://stackoverflow.com/questions/16088282/is-there-a-128-bit-integer-in-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!