Numerical types outside cstdint

I regularly used types from cstdint (e.g. uint32_t) in my code, but now they do not quite meet my needs, especially regarding templates.

Is there a way to specify an integer type that is twice the size of the template argument? When my template is passed to uint32_t, I need to create it uint64_t for one variable inside the function. Perhaps more difficult when uint64_t passed, I need to create "uint128_t". I could do this with an array of two template arguments, but then I cannot pass this array to other template functions. This is a critical piece of code (I'm doing cryptography).

In this regard, is there any other header that I can include (in order of preference: standard, boost, other) that gives me 128-bit integers? This question seems to answer this specific part: The fastest 128-bit integer library

Is there a way to indicate that I want to use the largest integer that does not exceed a certain size? This maximum size will also be a function of sizeof (T).

+4
source share
1 answer

"Extended arithmetic" is a drawback of the C language family. It is not possible to get an integer processor overflow flag, so there is no portable way to write an optimal 128-bit integer class.

For better performance (to compete with other crypto libraries), you may need a static library with a custom assembly inside. Unfortunately, I do not know a portable (wide-pore) interface to this.

If you just need a card from each fundamental type with N bits with 2N bits, then do a simple metafunction:

template< typename half > struct double_bits; template<> struct double_bits< std::uint8_t > { typedef std::uint16_t type; }; template<> struct double_bits< std::uint16_t > { typedef std::uint32_t type; }; template<> struct double_bits< std::uint32_t > { typedef std::uint64_t type; }; 
+5
source

Source: https://habr.com/ru/post/1383950/


All Articles