With glibc built with ubsan it triggers:
UBSAN: Undefined behaviour in jrand48_r.c:29:34 left shift of 41612 by 16 cannot be represented in type 'int'
UBSAN: Undefined behaviour in erand48_r.c:39:45 left shift of 3972 by 20 cannot be represented in type 'int'
Fix by casting to uint32_t for the shift operation.
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);
+ temp.ieee.mantissa1 = (((uint32_t)xsubi[1] & 0xfff) << 20)
+ | ((uint32_t)xsubi[0] << 4);
/* Please note the lower 4 bits of mantissa1 are always 0. */
*result = temp.d - 1.0;
return -1;
/* Store the result. */
- *result = (int32_t) ((xsubi[2] << 16) | xsubi[1]);
+ *result = (int32_t) (((uint32_t)xsubi[2] << 16) | (uint32_t)xsubi[1]);
return 0;
}