summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/warp/min-max-foldsTo-3.js
blob: dd52ef2ffb590baebbce167af1f0aa54b562b0cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
with ({}); // Don't inline anything into the top-level script.

const numbers = [
  -Infinity, -10, -5, -2, -1, -0.5, 0, 0.5, 1, 2, 5, 10, Infinity, NaN,
];

// Test all supported types (Int32, Float32, Float64).
const converters = [
  x => `${x}`,
  x => `(${x}|0)`,
  x => `Math.fround(${x})`,
  x => `numberToDouble(${x})`,
];

// Fold min(x, min(x, y)) to min(x, y).
for (let cvt of converters) {
  let x = cvt("x");
  let y = cvt("y");
  let c = Function("a", `return ${cvt("a")}`);

  let min1 = Function("x, y", `return Math.min(${x}, Math.min(${x}, ${y}))`);
  let min2 = Function("x, y", `return Math.min(${y}, Math.min(${x}, ${y}))`);
  let min3 = Function("x, y", `return Math.min(Math.min(${x}, ${y}), ${x})`);
  let min4 = Function("x, y", `return Math.min(Math.min(${x}, ${y}), ${y})`);

  for (let i = 0; i < 20; ++i) {
    for (let j = 0; j < numbers.length; ++j) {
      for (let k = 0; k < numbers.length; ++k) {
        let x = numbers[j];
        let y = numbers[k]
        let r1 = min1(x, y);
        let r2 = min2(x, y);
        let r3 = min3(x, y);
        let r4 = min4(x, y);

        // Convert to the correct type before computing the expected results.
        x = c(x);
        y = c(y);

        assertEq(r1, Math.min(x, Math.min(x, y)));
        assertEq(r1, Math.min(x, y));

        assertEq(r2, Math.min(y, Math.min(x, y)));
        assertEq(r2, Math.min(x, y));

        assertEq(r3, Math.min(Math.min(x, y), x));
        assertEq(r3, Math.min(x, y));

        assertEq(r4, Math.min(Math.min(x, y), y));
        assertEq(r4, Math.min(x, y));
      }
    }
  }
}

// Fold max(x, max(x, y)) to max(x, y).
for (let cvt of converters) {
  let x = cvt("x");
  let y = cvt("y");
  let c = Function("a", `return ${cvt("a")}`);

  let max1 = Function("x, y", `return Math.max(${x}, Math.max(${x}, ${y}))`);
  let max2 = Function("x, y", `return Math.max(${y}, Math.max(${x}, ${y}))`);
  let max3 = Function("x, y", `return Math.max(Math.max(${x}, ${y}), ${x})`);
  let max4 = Function("x, y", `return Math.max(Math.max(${x}, ${y}), ${y})`);

  for (let i = 0; i < 20; ++i) {
    for (let j = 0; j < numbers.length; ++j) {
      for (let k = 0; k < numbers.length; ++k) {
        let x = numbers[j];
        let y = numbers[k]
        let r1 = max1(x, y);
        let r2 = max2(x, y);
        let r3 = max3(x, y);
        let r4 = max4(x, y);

        // Convert to the correct type before computing the expected results.
        x = c(x);
        y = c(y);

        assertEq(r1, Math.max(x, Math.max(x, y)));
        assertEq(r1, Math.max(x, y));

        assertEq(r2, Math.max(y, Math.max(x, y)));
        assertEq(r2, Math.max(x, y));

        assertEq(r3, Math.max(Math.max(x, y), x));
        assertEq(r3, Math.max(x, y));

        assertEq(r4, Math.max(Math.max(x, y), y));
        assertEq(r4, Math.max(x, y));
      }
    }
  }
}

// Fold max(x, min(x, y)) = x.
for (let cvt of converters) {
  let x = cvt("x");
  let y = cvt("y");
  let c = Function("a", `return ${cvt("a")}`);

  let maxmin1 = Function("x, y", `return Math.max(${x}, Math.min(${x}, ${y}))`);
  let maxmin2 = Function("x, y", `return Math.max(${y}, Math.min(${x}, ${y}))`);
  let maxmin3 = Function("x, y", `return Math.max(Math.min(${x}, ${y}), ${x})`);
  let maxmin4 = Function("x, y", `return Math.max(Math.min(${x}, ${y}), ${y})`);

  for (let i = 0; i < 20; ++i) {
    for (let j = 0; j < numbers.length; ++j) {
      for (let k = 0; k < numbers.length; ++k) {
        let x = numbers[j];
        let y = numbers[k]
        let r1 = maxmin1(x, y);
        let r2 = maxmin2(x, y);
        let r3 = maxmin3(x, y);
        let r4 = maxmin4(x, y);

        // Convert to the correct type before computing the expected results.
        x = c(x);
        y = c(y);

        assertEq(r1, Math.max(x, Math.min(x, y)));
        assertEq(r1, Number.isNaN(y) ? NaN : x);

        assertEq(r2, Math.max(y, Math.min(x, y)));
        assertEq(r2, Number.isNaN(x) ? NaN : y);

        assertEq(r3, Math.max(Math.min(x, y), x));
        assertEq(r3, Number.isNaN(y) ? NaN : x);

        assertEq(r4, Math.max(Math.min(x, y), y));
        assertEq(r4, Number.isNaN(x) ? NaN : y);
      }
    }
  }
}

// Fold min(x, max(x, y)) = x.
for (let cvt of converters) {
  let x = cvt("x");
  let y = cvt("y");
  let c = Function("a", `return ${cvt("a")}`);

  let minmax1 = Function("x, y", `return Math.min(${x}, Math.max(${x}, ${y}))`);
  let minmax2 = Function("x, y", `return Math.min(${y}, Math.max(${x}, ${y}))`);
  let minmax3 = Function("x, y", `return Math.min(Math.max(${x}, ${y}), ${x})`);
  let minmax4 = Function("x, y", `return Math.min(Math.max(${x}, ${y}), ${y})`);

  for (let i = 0; i < 20; ++i) {
    for (let j = 0; j < numbers.length; ++j) {
      for (let k = 0; k < numbers.length; ++k) {
        let x = numbers[j];
        let y = numbers[k]
        let r1 = minmax1(x, y);
        let r2 = minmax2(x, y);
        let r3 = minmax3(x, y);
        let r4 = minmax4(x, y);

        // Convert to the correct type before computing the expected results.
        x = c(x);
        y = c(y);

        assertEq(r1, Math.min(x, Math.max(x, y)));
        assertEq(r1, Number.isNaN(y) ? NaN : x);

        assertEq(r2, Math.min(y, Math.max(x, y)));
        assertEq(r2, Number.isNaN(x) ? NaN : y);

        assertEq(r3, Math.min(Math.max(x, y), x));
        assertEq(r3, Number.isNaN(y) ? NaN : x);

        assertEq(r4, Math.min(Math.max(x, y), y));
        assertEq(r4, Number.isNaN(x) ? NaN : y);
      }
    }
  }
}