C++ 128/256-bit fixed size integer types

喜夏-厌秋 提交于 2019-11-28 10:01:10

The Boost library has data types as part of multiprecision library, for types ranging from 128 to 1024 bits.

#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;

int128_t mySignedInt128 = -1;
uint128_t myUnsignedInt128 = 2;
int256_t mySignedInt256 = -3;
uint256_t myUnsignedInt256 = 4;
int512_t mySignedInt512 = -5;
uint512_t myUnsignedInt512 = 6;
int1024_t mySignedInt1024 = -7;
uint1024_t myUnsignedInt1024 = 8;

The Xint library is currently under review to become part of Boost. Although it is discussed rather controversially and the outcome of the review is not clear yet, the library fulfills some of your requirements:

  • header only
  • reasonable license

One of the points that are discussed during the review is performance though. If accepted as an official Boost library, I expect performance issues to be addressed promptly.

So I would give it a try: Code, Documentation.

Some native 128-bit types are available on certain platforms, you tend to be limited by the architecture. For example __m128 is available for SSE2?

http://msdn.microsoft.com/en-us/library/ayeb3ayc.aspx

Also listed as __int128 in this ABI:

http://www.x86-64.org/documentation/abi-0.99.pdf

However the preferred naming of uint128_t and uint256_t can be found in SHOGUN, a "large scale machine learning toolbox with focus on especially Support Vector Machines (SVM)"

http://www.shogun-toolbox.org/doc/index.html

Depending on your requirements, the STL class bitset might fit your needs. It responds to all the bit-manipulation operators that integer types do (<<,| etc.), but unfortunately not to arithmetic operators like + or *. Its size is fixed at compile time via a template parameter. Another unfortunate thing is that the API provides no way to get at the underlying binary representation (e.g. for streaming it), which might seriously limit its usefulness.

(I know this is an old question, but this answer might help others.)

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