Debian Codegraph @

js/src/jit-test/tests/typedarray/uint8clamped-round-half-to-even.js

raw
1 // |jit-test| test-also=--no-sse4
2
3 // Return the next Number value in direction to +Infinity.
4 function nextUp(num) {
5 if (!Number.isFinite(num)) {
6 return num;
7 }
8 if (num === 0) {
9 return Number.MIN_VALUE;
10 }
11
12 let f64 = new Float64Array([num]);
13 let u64 = new BigUint64Array(f64.buffer);
14 u64[0] += (num < 0 ? -1n : 1n);
15 return f64[0];
16 }
17
18 // Return the next Number value in direction to -Infinity.
19 function nextDown(num) {
20 if (!Number.isFinite(num)) {
21 return num;
22 }
23 if (num === 0) {
24 return -Number.MIN_VALUE;
25 }
26
27 let f64 = new Float64Array([num]);
28 let u64 = new BigUint64Array(f64.buffer);
29 u64[0] += (num < 0 ? 1n : -1n);
30 return f64[0];
31 }
32
33 function Clamped(num) {
34 let uint8Clamped = new Uint8ClampedArray(1);
35
36 // Note: Assignment to ensure JIT code path for ClampDoubleToUint8.
37 uint8Clamped[0] = num;
38
39 return uint8Clamped[0];
40 }
41
42 Math.clamp = (v, min, max) => Math.min(Math.max(v, min), max);
43
44 // Test numbers around integer inputs.
45 for (let i = -1; i <= 255 + 1; ++i) {
46 let num = i;
47
48 assertEq(Clamped(num), Math.clamp(i, 0, 255));
49
50 let down = num;
51 for (let j = 0; j < 10; ++j) {
52 down = nextDown(down);
53 assertEq(Clamped(down), Math.clamp(i, 0, 255));
54 }
55
56 let up = num;
57 for (let j = 0; j < 10; ++j) {
58 up = nextUp(up);
59 assertEq(Clamped(up), Math.clamp(i, 0, 255));
60 }
61 }
62
63 // Test numbers around the half-way case.
64 for (let i = -1; i <= 255 + 1; ++i) {
65 let num = i + 0.5;
66
67 assertEq(Clamped(num), Math.clamp(i + (i & 1), 0, 255));
68
69 let down = num;
70 for (let j = 0; j < 10; ++j) {
71 down = nextDown(down);
72 assertEq(Clamped(down), Math.clamp(i, 0, 255));
73 }
74
75 let up = num;
76 for (let j = 0; j < 10; ++j) {
77 up = nextUp(up);
78 assertEq(Clamped(up), Math.clamp(i + 1, 0, 255));
79 }
80 }