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

前端 未结 4 534
误落风尘
误落风尘 2020-12-03 14:27

I was wondering if any fellow SO\'s could recommend a good light-weight fixed size integer type (128-bit or even 256-bit, possibly even template parametrized) library.

相关标签:
4条回答
  • 2020-12-03 14:58

    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

    0 讨论(0)
  • 2020-12-03 15:00

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

    0 讨论(0)
  • 2020-12-03 15:07

    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;
    
    0 讨论(0)
  • 2020-12-03 15:09

    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.

    0 讨论(0)
提交回复
热议问题