|
| 1 | +function modBy1(x) { return x % 1; } |
| 2 | +function modBy2(x) { return x % 2; } |
| 3 | +function modBy8(x) { return x % 8; } |
| 4 | +function modByMinus4(x) { return x % -4; } |
| 5 | +function modByHalf(x) { return x % 0.5; } |
| 6 | +function modBy2p52(x) { return x % 4503599627370496; } |
| 7 | +function modBy2p53(x) { return x % 9007199254740992; } |
| 8 | +function modBy2p1023(x) { return x % 8.98846567431158e+307; } |
| 9 | +function modByMinus2p31(x) { return x % -2147483648; } |
| 10 | +function modBy2p31(x) { return x % 2147483648; } |
| 11 | +function modBy2p32(x) { return x % 4294967296; } |
| 12 | +function modBy3(x) { return x % 3; } |
| 13 | +noInline(modBy1); |
| 14 | +noInline(modBy2); |
| 15 | +noInline(modBy8); |
| 16 | +noInline(modByMinus4); |
| 17 | +noInline(modByHalf); |
| 18 | +noInline(modBy2p52); |
| 19 | +noInline(modBy2p53); |
| 20 | +noInline(modBy2p1023); |
| 21 | +noInline(modByMinus2p31); |
| 22 | +noInline(modBy2p31); |
| 23 | +noInline(modBy2p32); |
| 24 | +noInline(modBy3); |
| 25 | + |
| 26 | +function reference(x, y) { return x % y; } |
| 27 | +noInline(reference); |
| 28 | + |
| 29 | +const tests = [ |
| 30 | + [modBy1, 1], |
| 31 | + [modBy2, 2], |
| 32 | + [modBy8, 8], |
| 33 | + [modByMinus4, -4], |
| 34 | + [modByHalf, 0.5], |
| 35 | + [modBy2p52, 4503599627370496], |
| 36 | + [modBy2p53, 9007199254740992], |
| 37 | + [modBy2p1023, 8.98846567431158e+307], |
| 38 | + [modByMinus2p31, -2147483648], |
| 39 | + [modBy2p31, 2147483648], |
| 40 | + [modBy2p32, 4294967296], |
| 41 | + [modBy3, 3], |
| 42 | +]; |
| 43 | + |
| 44 | +const inputs = [ |
| 45 | + 0, -0, 0.5, -0.5, 1, -1, 1.5, -1.5, 2, -2, 3.75, -3.75, 4, -4, 7.999999999999999, -8, 8.000000000000002, |
| 46 | + 0.1, -0.1, 1e-310, -1e-310, 5e-324, -5e-324, 2.2250738585072014e-308, -2.2250738585072014e-308, |
| 47 | + 123456789.123, -123456789.123, 4503599627370495.5, -4503599627370495.5, 4503599627370497, -4503599627370497, |
| 48 | + 9007199254740991, -9007199254740991, 9007199254740994, -9007199254740994, 1e300, -1e300, 1.7976931348623157e308, |
| 49 | + -1.7976931348623157e308, Infinity, -Infinity, NaN, |
| 50 | + -3, 5, -7, 9, -16, 1073741824, -1073741824, 2147483647, -2147483647, -2147483648, 2147483648, -2147483649, |
| 51 | +]; |
| 52 | + |
| 53 | +let seed = 1; |
| 54 | +function random() { |
| 55 | + seed = (seed * 1103515245 + 12345) % 2147483648; |
| 56 | + return seed / 2147483648; |
| 57 | +} |
| 58 | +for (let i = 0; i < 100; ++i) |
| 59 | + inputs.push((random() - 0.5) * Math.pow(2, Math.floor(random() * 120) - 40)); |
| 60 | + |
| 61 | +for (let i = 0; i < testLoopCount; ++i) { |
| 62 | + const input = inputs[i % inputs.length]; |
| 63 | + for (const [func, divisor] of tests) { |
| 64 | + const actual = func(input); |
| 65 | + const expected = reference(input, divisor); |
| 66 | + if (!Object.is(actual, expected)) |
| 67 | + throw new Error(`${func.name}(${input}): expected ${expected} but got ${actual}`); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function modBy4Generic(x) { return x % 4; } |
| 72 | +noInline(modBy4Generic); |
| 73 | + |
| 74 | +const genericInputs = ["-6", "7.5", true, null, undefined, { valueOf() { return -12.5; } }, -8, 2.5]; |
| 75 | +for (let i = 0; i < testLoopCount; ++i) { |
| 76 | + const input = genericInputs[i % genericInputs.length]; |
| 77 | + const actual = modBy4Generic(input); |
| 78 | + const expected = reference(input, 4); |
| 79 | + if (!Object.is(actual, expected)) |
| 80 | + throw new Error(`modBy4Generic(${String(input)}): expected ${expected} but got ${actual}`); |
| 81 | +} |
0 commit comments