Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::multiprecision::uint128_t sizeof is 24

Basic math (128 / 8 = 16) speaks differently. I'm kinda disappointed and want some answers - since from what I've been used to, that notation(type_num_of_bytes_t) describes not just the amount of data you can put into the variable, but also cross-platform fixed variable size, and the latter is IMHO even more important. What am I doing wrong?

#include "boost/multiprecision/cpp_int.hpp"
using boost::multiprecision::uint128_t;

...

qDebug() << sizeof(uint128_t);

Output: 24.

I'm using standard x86/64 architecture CPU, compiling with vs2013 on Windows.

UPDATE: boost version is 1.61.

like image 408
Leontyev Georgiy Avatar asked Dec 24 '22 21:12

Leontyev Georgiy


1 Answers

cpp_int 1.6.1

When used at fixed precision, the size of this type is always one machine word larger than you would expect for an N-bit integer: the extra word stores both the sign, and how many machine words in the integer are actually in use. The latter is an optimisation for larger fixed precision integers, so that a 1024-bit integer has almost the same performance characteristics as a 128-bit integer, rather than being 4 times slower for addition and 16 times slower for multiplication (assuming the values involved would always fit in 128 bits). Typically this means you can use an integer type wide enough for the "worst case scenario" with only minor performance degradation even if most of the time the arithmetic could in fact be done with a narrower type.

The extra machine word (on x86/64 8 bytes) makes the size 24 instead of the expected 16.

like image 120
lcs Avatar answered Dec 29 '22 04:12

lcs