128 bit arithmetic on x64 in C

陌路散爱 提交于 2019-12-06 05:06:44

问题


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.)


回答1:


gcc has __uint128_t and __int128_t as extensions.




回答2:


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

http://gmplib.org/



来源:https://stackoverflow.com/questions/5288915/128-bit-arithmetic-on-x64-in-c

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