summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/ion/testFloat32-correctness.js
blob: 8321e6bfab48270285d055e56cd39c850e66a8a4 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
setJitCompilerOption("ion.warmup.trigger", 50);

var f32 = new Float32Array(10);

function test(setup, f) {
    if (f === undefined) {
        f = setup;
        setup = function(){};
    }
    setup();
    for(var n = 200; n; --n) {
        f();
    }
}

// Basic arithmetic
function setupBasicArith() {
    f32[0] = -Infinity;
    f32[1] = -1;
    f32[2] = -0;
    f32[3] = 0;
    f32[4] = 1.337;
    f32[5] = 42;
    f32[6] = Infinity;
    f32[7] = NaN;
}
function basicArith() {
    for (var i = 0; i < 7; ++i) {
        var opf = Math.fround(f32[i] + f32[i+1]);
        var opd = (1 / (1 / f32[i])) + f32[i+1];
        assertFloat32(opf, true);
        assertFloat32(opd, false);
        assertEq(opf, Math.fround(opd));

        opf = Math.fround(f32[i] - f32[i+1]);
        opd = (1 / (1 / f32[i])) - f32[i+1];
        assertFloat32(opf, true);
        assertFloat32(opd, false);
        assertEq(opf, Math.fround(opd));

        opf = Math.fround(f32[i] * f32[i+1]);
        opd = (1 / (1 / f32[i])) * f32[i+1];
        assertFloat32(opf, true);
        assertFloat32(opd, false);
        assertEq(opf, Math.fround(opd));

        opf = Math.fround(f32[i] / f32[i+1]);
        opd = (1 / (1 / f32[i])) / f32[i+1];
        assertFloat32(opf, true);
        assertFloat32(opd, false);
        assertEq(opf, Math.fround(opd));
    }
}
test(setupBasicArith, basicArith);

// MAbs
function setupAbs() {
    f32[0] = -0;
    f32[1] = 0;
    f32[2] = -3.14159;
    f32[3] = 3.14159;
    f32[4] = -Infinity;
    f32[5] = Infinity;
    f32[6] = NaN;
}
function abs() {
    for(var i = 0; i < 7; ++i) {
        assertEq( Math.fround(Math.abs(f32[i])), Math.abs(f32[i]) );
    }
}
test(setupAbs, abs);

// MSqrt
function setupSqrt() {
    f32[0] = 0;
    f32[1] = 1;
    f32[2] = 4;
    f32[3] = -1;
    f32[4] = Infinity;
    f32[5] = NaN;
    f32[6] = 13.37;
}
function sqrt() {
    for(var i = 0; i < 7; ++i) {
        var sqrtf = Math.fround(Math.sqrt(f32[i]));
        var sqrtd = 1 + Math.sqrt(f32[i]) - 1; // force no float32 by chaining arith ops
        assertEq( sqrtf, Math.fround(sqrtd) );
    }
}
test(setupSqrt, sqrt);

// MMinMax
function setupMinMax() {
    f32[0] = -0;
    f32[1] = 0;
    f32[2] = 1;
    f32[3] = 4;
    f32[4] = -1;
    f32[5] = Infinity;
    f32[6] = NaN;
    f32[7] = 13.37;
    f32[8] = -Infinity;
    f32[9] = Math.pow(2,31) - 1;
}
function minMax() {
    for(var i = 0; i < 9; ++i) {
        for(var j = 0; j < 9; j++) {
            var minf = Math.fround(Math.min(f32[i], f32[j]));
            var mind = 1 / (1 / Math.min(f32[i], f32[j])); // force no float32 by chaining arith ops
            assertFloat32(minf, true);
            assertFloat32(mind, false);
            assertEq( minf, Math.fround(mind) );

            var maxf = Math.fround(Math.max(f32[i], f32[j]));
            var maxd = 1 / (1 / Math.max(f32[i], f32[j])); // force no float32 by chaining arith ops
            assertFloat32(maxf, true);
            assertFloat32(maxd, false);
            assertEq( maxf, Math.fround(maxd) );
        }
    }
}
test(setupMinMax, minMax);

// MTruncateToInt32
// The only way to get a MTruncateToInt32 with a Float32 input is to use Math.imul
function setupTruncateToInt32() {
    f32[0] = -1;
    f32[1] = 4;
    f32[2] = 5.13;
}
function truncateToInt32() {
    assertEq( Math.imul(f32[0], f32[1]), Math.imul(-1, 4) );
    assertEq( Math.imul(f32[1], f32[2]), Math.imul(4, 5) );
}
test(setupTruncateToInt32, truncateToInt32);

// MCompare
function comp() {
    for(var i = 0; i < 9; ++i) {
        assertEq( f32[i] < f32[i+1], true );
    }
}
function setupComp() {
    f32[0] = -Infinity;
    f32[1] = -1;
    f32[2] = -0.01;
    f32[3] = 0;
    f32[4] = 0.01;
    f32[5] = 1;
    f32[6] = 10;
    f32[7] = 13.37;
    f32[8] = 42;
    f32[9] = Infinity;
}
test(setupComp, comp);

// MNot
function setupNot() {
    f32[0] = -0;
    f32[1] = 0;
    f32[2] = 1;
    f32[3] = NaN;
    f32[4] = Infinity;
    f32[5] = 42;
    f32[6] = -23;
}
function not() {
    assertEq( !f32[0], true );
    assertEq( !f32[1], true );
    assertEq( !f32[2], false );
    assertEq( !f32[3], true );
    assertEq( !f32[4], false );
    assertEq( !f32[5], false );
    assertEq( !f32[6], false );
}
test(setupNot, not);

// MToNumberInt32
var str = "can haz cheezburger? okthxbye;";
function setupToInt32() {
    f32[0] = 0;
    f32[1] = 1;
    f32[2] = 2;
    f32[3] = 4;
    f32[4] = 5;
}
function testToInt32() {
    assertEq(str[f32[0]], 'c');
    assertEq(str[f32[1]], 'a');
    assertEq(str[f32[2]], 'n');
    assertEq(str[f32[3]], 'h');
    assertEq(str[f32[4]], 'a');
}
test(setupToInt32, testToInt32);

function setupBailoutToInt32() {
    f32[0] = .5;
}
function testBailoutToInt32() {
    assertEq(typeof str[f32[0]], 'undefined');
}
test(setupBailoutToInt32, testBailoutToInt32);

// MMath (no trigo - see also testFloat32-trigo.js
function assertNear(a, b) {
    var r = (a != a && b != b) || Math.abs(a-b) < 1e-1 || a === b;
    if (!r) {
        print('Precision error: ');
        print(new Error().stack);
        print('Got', a, ', expected near', b);
        assertEq(false, true);
    }
}

function setupOtherMath() {
    setupComp();
    f32[8] = 4.2;
}
function otherMath() {
    for (var i = 0; i < 9; ++i) {
        assertNear(Math.fround(Math.exp(f32[i])), Math.exp(f32[i]));
        assertNear(Math.fround(Math.log(f32[i])), Math.log(f32[i]));
    }
};
test(setupOtherMath, otherMath);

function setupFloor() {
    f32[0] = -5.5;
    f32[1] = -0.5;
    f32[2] = 0;
    f32[3] = 1.5;
}
function setupFloorDouble() {
    f32[4] = NaN;
    f32[5] = -0;
    f32[6] = Infinity;
    f32[7] = -Infinity;
    f32[8] = Math.pow(2,31); // too big to fit into a int
}
function testFloor() {
    for (var i = 0; i < 4; ++i) {
        var f = Math.floor(f32[i]);
        assertFloat32(f, false); // f is an int32

        var g = Math.floor(-0 + f32[i]);
        assertFloat32(g, false);

        assertEq(f, g);
    }
}
function testFloorDouble() {
    for (var i = 4; i < 9; ++i) {
        var f = Math.fround(Math.floor(f32[i]));
        assertFloat32(f, true);

        var g = Math.floor(-0 + f32[i]);
        assertFloat32(g, false);

        assertEq(f, g);
    }
}
test(setupFloor, testFloor);
test(setupFloorDouble, testFloorDouble);

function setupRound() {
    f32[0] = -5.5;
    f32[1] = -0.6;
    f32[2] = 1.5;
    f32[3] = 1;
}
function setupRoundDouble() {
    f32[4] = NaN;
    f32[5] = -0.49;          // rounded to -0
    f32[6] = Infinity;
    f32[7] = -Infinity;
    f32[8] = Math.pow(2,31); // too big to fit into a int
    f32[9] = -0;
}
function testRound() {
    for (var i = 0; i < 4; ++i) {
        var r32 = Math.round(f32[i]);
        assertFloat32(r32, false); // r32 is an int32

        var r64 = Math.round(-0 + f32[i]);
        assertFloat32(r64, false);

        assertEq(r32, r64);
    }
}
function testRoundDouble() {
    for (var i = 4; i < 10; ++i) {
        var r32 = Math.fround(Math.round(f32[i]));
        assertFloat32(r32, true);

        var r64 = Math.round(-0 + f32[i]);
        assertFloat32(r64, false);

        assertEq(r32, r64);
    }
}
test(setupRound, testRound);
test(setupRoundDouble, testRoundDouble);

function setupCeil() {
    f32[0] = -5.5;
    f32[1] = -1.5;
    f32[2] = 0;
    f32[3] = 1.5;
}
function setupCeilDouble() {
    f32[4] = NaN;
    f32[5] = -0;
    f32[6] = Infinity;
    f32[7] = -Infinity;
    f32[8] = Math.pow(2,31); // too big to fit into a int
}
function testCeil() {
    for(var i = 0; i < 2; ++i) {
        var f = Math.ceil(f32[i]);
        assertFloat32(f, false);

        var g = Math.ceil(-0 + f32[i]);
        assertFloat32(g, false);

        assertEq(f, g);
    }
}
function testCeilDouble() {
    for(var i = 4; i < 9; ++i) {
        var f = Math.fround(Math.ceil(f32[i]));
        assertFloat32(f, true);

        var g = Math.ceil(-0 + f32[i]);
        assertFloat32(g, false);

        assertEq(f, g);
    }
}
test(setupCeil, testCeil);
test(setupCeilDouble, testCeilDouble);