New RNG triggers the undefined behaviour sanitizer
When running the code with GCC's undefined behaviour sanitizer (-fsanitize=undefined
), we get a series of error messages coming from the RNG. Specifically, we get:
../src/random.h:110:50: runtime error: left shift of 2053 by 20 places cannot be represented in type 'int'
where the 2053
will be different for every call.
This corresponds to this line in the code:
INLINE static double inl_erand48(uint16_t xsubi[3]) {
union ieee754_double temp;
/* Compute next state. */
inl_drand48_iterate(xsubi);
/* Construct a positive double with the 48 random bits distributed over
its fractional part so the resulting FP number is [0.0,1.0). */
temp.ieee.negative = 0;
temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4); // < ----- HERE ----
/* Please note the lower 4 bits of mantissa1 are always 0. */
return temp.d - 1.0;
}
We indeed bitshift by 20 something that is only 16 bits wide. However, I think we overflow on purpose here.
Silencing the warning somehow would be good but that shouldn't be at the cost of not overflowing since this is part of the RNG strategy. Telling the compiler that we are adults and know what we are doing would be the best choice.
Edited by Matthieu Schaller