问题
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.
I've had a look at GMP and co, they care great, yet are a bit too large for my purposes, I'm interested in simple header only solutions at this point. Performance is important and the target architecture will be x86 and x86-64, also a reasonable license (aka nothing GPL or LGPL).
回答1:
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;
回答2:
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.
回答3:
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
回答4:
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.)
来源:https://stackoverflow.com/questions/5242819/c-128-256-bit-fixed-size-integer-types