128 bit arithmetic on x64 in C

2020-07-18 01:58发布

When implementing bignums on x86, obviously the most efficient choice for digit size is 32 bits. However, you need arithmetic up to twice the digit size (i.e. 32+32=33, 32*32=64, 64/32=32). Fortunately, not only does x86 provide this, but it's also accessible from portable C (uint64_t).

Similarly, on x64 it would be desirable to use 64-bit digits. This would require 128 bit arithmetic (i.e. 64+64=65, 64*64=128, 128/64=64). Fortunately, x64 provides this. Unfortunately, it's not accessible from portable C, though obviously one could dip into assembly.

So my question is whether it's accessible from nonportable C. Do any C compilers on x64 provide access to this, and if so, what's the syntax?

(Note that I'm not talking about 128 bit vectors that are strictly treated as collections of 32 or 64 bit words with no carry propagation between them, but about actual 128 bit integer operations.)

2条回答
一纸荒年 Trace。
2楼-- · 2020-07-18 02:38

You may want to check the GNU Multiple Precision Arithmetic Library:

http://gmplib.org/

查看更多
甜甜的少女心
3楼-- · 2020-07-18 02:44

gcc has __uint128_t and __int128_t as extensions.

查看更多
登录 后发表回答