diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-03-11 11:48:09 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-03-11 11:48:09 -0300 |
| commit | e3388ebfad4a566c933b2b1562cfa9f40a8afc13 (patch) | |
| tree | e35ff74969885932b9d04c5e4f2557d20607b88a | |
| parent | 9e3db70482e927e5992cccdb5a40424b33ad0a29 (diff) | |
| download | lua-e3388ebfad4a566c933b2b1562cfa9f40a8afc13.tar.gz lua-e3388ebfad4a566c933b2b1562cfa9f40a8afc13.tar.bz2 lua-e3388ebfad4a566c933b2b1562cfa9f40a8afc13.zip | |
more explicit casts when converting an integer to a random float
(to ensure computations are done with all bits)
| -rw-r--r-- | lmathlib.c | 8 |
1 files changed, 5 insertions, 3 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmathlib.c,v 1.122 2018/03/09 15:05:13 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.123 2018/03/09 19:23:39 roberto Exp roberto $ |
| 3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -373,7 +373,7 @@ static I xorshift128plus (I *state) { | |||
| 373 | /* do not need bits from higher half */ | 373 | /* do not need bits from higher half */ |
| 374 | #define maskHF 0 | 374 | #define maskHF 0 |
| 375 | #define maskLOW (~(~1U << (FIGS - 1))) /* use FIG bits */ | 375 | #define maskLOW (~(~1U << (FIGS - 1))) /* use FIG bits */ |
| 376 | #define shiftFIG (0.5 / (1U << (FIGS - 1))) /* 2^(-FIG) */ | 376 | #define shiftFIG (l_mathop(0.5) / (1U << (FIGS - 1))) /* 2^(-FIG) */ |
| 377 | 377 | ||
| 378 | #else /* 32 < FIGS <= 64 */ | 378 | #else /* 32 < FIGS <= 64 */ |
| 379 | 379 | ||
| @@ -393,7 +393,9 @@ static I xorshift128plus (I *state) { | |||
| 393 | #define twoto32 l_mathop(4294967296.0) /* 2^32 */ | 393 | #define twoto32 l_mathop(4294967296.0) /* 2^32 */ |
| 394 | 394 | ||
| 395 | static lua_Number I2d (I x) { | 395 | static lua_Number I2d (I x) { |
| 396 | return ((x.h & maskHF) * twoto32 + (x.l & maskLOW)) * shiftFIG; | 396 | lua_Number h = (lua_Number)(x.h & maskHF); |
| 397 | lua_Number l = (lua_Number)(x.l & maskLOW); | ||
| 398 | return (h * twoto32 + l) * shiftFIG; | ||
| 397 | } | 399 | } |
| 398 | 400 | ||
| 399 | static lua_Unsigned I2UInt (I x) { | 401 | static lua_Unsigned I2UInt (I x) { |
