forked from WebKit/WebKit-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith-log2-on-various-types.js
237 lines (204 loc) · 7.01 KB
/
arith-log2-on-various-types.js
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
//@ skip if not $jitTests
//@ defaultNoEagerRun
"use strict";
let log2OfHalf = Math.log2(0.5);
let validInputTestCases = [
// input as string, expected result as string.
["undefined", "NaN"],
["null", "-Infinity"],
["1", "0"],
["0", "-Infinity"],
["-0.", "-Infinity"],
["0.5", "" + log2OfHalf],
["Math.PI", "" + Math.log2(Math.PI)],
["Infinity", "Infinity"],
["-Infinity", "NaN"],
["NaN", "NaN"],
["\"WebKit\"", "NaN"],
["\"0.5\"", "" + log2OfHalf],
["{ valueOf: () => { return 0.5; } }", "" + log2OfHalf],
];
let validInputTypedTestCases = validInputTestCases.map((element) => { return [eval("(" + element[0] + ")"), eval(element[1])] });
function isIdentical(result, expected)
{
if (expected === expected) {
if (result !== expected)
return false;
if (!expected)
return (1 / expected) === (1 / result);
return true;
}
return result !== result;
}
// Test Math.log2() without arguments.
function opaqueLog2NoArgument() {
return Math.log2();
}
noInline(opaqueLog2NoArgument);
noOSRExitFuzzing(opaqueLog2NoArgument);
function testNoArgument() {
for (let i = 0; i < 1e4; ++i) {
let output = opaqueLog2NoArgument();
if (output === output) {
throw "Failed opaqueLog2NoArgument";
}
}
if (numberOfDFGCompiles(opaqueLog2NoArgument) > 1)
throw "The call without arguments should never exit.";
}
testNoArgument();
// Test Math.log2() with a very polymorphic input. All test cases are seen at each iteration.
function opaqueAllTypesLog2(argument) {
return Math.log2(argument);
}
noInline(opaqueAllTypesLog2);
noOSRExitFuzzing(opaqueAllTypesLog2);
function testAllTypesCall() {
for (let i = 0; i < 1e3; ++i) {
for (let testCaseInput of validInputTypedTestCases) {
let output = opaqueAllTypesLog2(testCaseInput[0]);
if (!isIdentical(output, testCaseInput[1]))
throw "Failed testAllTypesCall for input " + testCaseInput[0] + " expected " + testCaseInput[1] + " got " + output;
}
}
if (numberOfDFGCompiles(opaqueAllTypesLog2) > 2)
throw "We should have detected log2() was polymorphic and generated a generic version.";
}
testAllTypesCall();
// Test Math.log2() on a completely typed input. Every call see only one type.
function testSingleTypeCall() {
for (let testCaseInput of validInputTestCases) {
eval(`
function opaqueLog2(argument) {
return Math.log2(argument);
}
noInline(opaqueLog2);
noOSRExitFuzzing(opaqueLog2);
for (let i = 0; i < 1e4; ++i) {
if (!isIdentical(opaqueLog2(${testCaseInput[0]}), ${testCaseInput[1]})) {
throw "Failed testSingleTypeCall()";
}
}
if (numberOfDFGCompiles(opaqueLog2) > 1)
throw "We should have compiled a single log2 for the expected type.";
`);
}
}
testSingleTypeCall();
// Test Math.log2() on constants
function testConstant() {
for (let testCaseInput of validInputTestCases) {
eval(`
function opaqueLog2OnConstant() {
return Math.log2(${testCaseInput[0]});
}
noInline(opaqueLog2OnConstant);
noOSRExitFuzzing(opaqueLog2OnConstant);
for (let i = 0; i < 1e4; ++i) {
if (!isIdentical(opaqueLog2OnConstant(), ${testCaseInput[1]})) {
throw "Failed testConstant()";
}
}
if (numberOfDFGCompiles(opaqueLog2OnConstant) > 1)
throw "We should have compiled a single log2 for the expected type.";
`);
}
}
testConstant();
// Verify we call valueOf() exactly once per call.
function opaqueLog2ForSideEffects(argument) {
return Math.log2(argument);
}
noInline(opaqueLog2ForSideEffects);
noOSRExitFuzzing(opaqueLog2ForSideEffects);
function testSideEffect() {
let testObject = {
counter: 0,
valueOf: function() { ++this.counter; return 0.2; }
};
let log2Result = Math.log2(0.2);
for (let i = 0; i < 1e4; ++i) {
if (opaqueLog2ForSideEffects(testObject) !== log2Result)
throw "Incorrect result in testSideEffect()";
}
if (testObject.counter !== 1e4)
throw "Failed testSideEffect()";
if (numberOfDFGCompiles(opaqueLog2ForSideEffects) > 1)
throw "opaqueLog2ForSideEffects() is predictable, it should only be compiled once.";
}
testSideEffect();
// Verify log2() is not subject to CSE if the argument has side effects.
function opaqueLog2ForCSE(argument) {
return Math.log2(argument) + Math.log2(argument) + Math.log2(argument);
}
noInline(opaqueLog2ForCSE);
noOSRExitFuzzing(opaqueLog2ForCSE);
function testCSE() {
let testObject = {
counter: 0,
valueOf: function() { ++this.counter; return 0.2; }
};
let log2Result = Math.log2(0.2);
let threelog2Result = log2Result + log2Result + log2Result;
for (let i = 0; i < 1e4; ++i) {
if (opaqueLog2ForCSE(testObject) !== threelog2Result)
throw "Incorrect result in testCSE()";
}
if (testObject.counter !== 3e4)
throw "Failed testCSE()";
if (numberOfDFGCompiles(opaqueLog2ForCSE) > 1)
throw "opaqueLog2ForCSE() is predictable, it should only be compiled once.";
}
testCSE();
// Verify log2() is not subject to DCE if the argument has side effects.
function opaqueLog2ForDCE(argument) {
Math.log2(argument);
}
noInline(opaqueLog2ForDCE);
noOSRExitFuzzing(opaqueLog2ForDCE);
function testDCE() {
let testObject = {
counter: 0,
valueOf: function() { ++this.counter; return 0.2; }
};
for (let i = 0; i < 1e4; ++i) {
opaqueLog2ForDCE(testObject);
}
if (testObject.counter !== 1e4)
throw "Failed testDCE()";
if (numberOfDFGCompiles(opaqueLog2ForDCE) > 1)
throw "opaqueLog2ForDCE() is predictable, it should only be compiled once.";
}
testDCE();
// Test exceptions in the argument.
function testException() {
let counter = 0;
function opaqueLog2WithException(argument) {
let result = Math.log2(argument);
++counter;
return result;
}
noInline(opaqueLog2WithException);
let testObject = { valueOf: () => { return 0.1; } };
let log2Result = Math.log2(0.1);
// Warm up without exception.
for (let i = 0; i < 1e3; ++i) {
if (opaqueLog2WithException(testObject) !== log2Result)
throw "Incorrect result in opaqueLog2WithException()";
}
let testThrowObject = { valueOf: () => { throw testObject; return 0.1; } };
for (let i = 0; i < 1e2; ++i) {
try {
if (opaqueLog2WithException(testThrowObject) !== 8)
throw "This code should not be reached!!";
} catch (e) {
if (e !== testObject) {
throw "Wrong object thrown from opaqueLog2WithException."
}
}
}
if (counter !== 1e3) {
throw "Invalid count in testException()";
}
}
testException();