make sure RAND_MAX is a proper value

This commit is contained in:
Alon Zakai 2014-02-01 16:49:00 -08:00
Родитель f5c957b289
Коммит 737de35442
2 изменённых файлов: 6 добавлений и 1 удалений

Просмотреть файл

@ -3458,7 +3458,7 @@ LibraryManager.library = {
rand_r: function(seedp) {
seedp = seedp|0;
var val = 0;
val = ((Math_imul({{{ makeGetValueAsm('seedp', 0, 'i32') }}}, 31010991)|0) + 0x676e6177 ) & {{{ cDefine('RAND_MAX') }}};
val = ((Math_imul({{{ makeGetValueAsm('seedp', 0, 'i32') }}}, 31010991)|0) + 0x676e6177 ) & {{{ cDefine('RAND_MAX') }}}; // assumes RAND_MAX is in bit mask form (power of 2 minus 1)
{{{ makeSetValueAsm('seedp', 0, 'val', 'i32') }}};
return val|0;
},

Просмотреть файл

@ -3552,8 +3552,13 @@ ok
def test_rand(self):
src = r'''#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int main()
{
// we need RAND_MAX to be a bitmask (power of 2 minus 1). this assertions guarantees
// if RAND_MAX changes the test failure will focus attention on that issue here.
assert(RAND_MAX == 0x7fffffff);
srand(0xdeadbeef);
for(int i = 0; i < 10; ++i)
printf("%d\n", rand());