Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: test/mjsunit/harmony/simd.js

Issue 1219943002: Expose SIMD.Float32x4 type to Javascript. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Remove SIMD_OBJECT from native context fields. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 // Flags: --harmony-simd --harmony-tostring --allow-natives-syntax
29 // Flags: --noalways-opt
30
31 function lanesForType(typeName) {
32 // The lane count follows the first 'x' in the type name, which begins with
33 // 'float', 'int', or 'bool'.
34 return Number.parseInt(typeName[typeName.indexOf('x') + 1]);
35 }
36
37
38 function isValidSimdString(string, value, type, lanes) {
39 var simdFn = SIMD[type],
40 parseFn =
41 type.indexOf('float') === 0 ? Number.parseFloat : Number.parseInt,
42 indexOfOpenParen = string.indexOf('(');
43 // Check prefix for correct type name.
44 if (string.substr(0, indexOfOpenParen).toUpperCase() !== type.toUpperCase())
45 return false;
46 // Remove type name and open parenthesis.
47 string = string.substr(indexOfOpenParen + 1);
48 var laneStrings = string.split(',');
49 if (laneStrings.length !== lanes)
50 return false;
51 for (var i = 0; i < lanes; i++) {
52 var fromString = parseFn(laneStrings[i]),
53 fromValue = simdFn.extractLane(value, i);
54 if (Math.abs(fromString - fromValue) > Number.EPSILON)
55 return false;
56 }
57 return true;
58 }
59
60
61 // Test for structural equivalence.
62 function areEquivalent(type, lanes, a, b) {
63 var simdFn = SIMD[type];
64 for (var i = 0; i < lanes; i++) {
65 if (simdFn.extractLane(a, i) !== simdFn.extractLane(b, i))
66 return false;
67 }
68 return true;
69 }
70
71
72 // Tests for the global SIMD object.
73 function TestSIMDObject() {
74 assertSame(typeof SIMD, 'object');
75 assertSame(SIMD.constructor, Object);
76 assertSame(Object.getPrototypeOf(SIMD), Object.prototype);
77 assertSame(SIMD + "", "[object SIMD]");
78 }
79 TestSIMDObject()
80
81 // TestConstructor populates this with interesting values for the other tests.
82 var values;
83
84 // Test different forms of constructor calls. This test populates 'values' with
85 // a variety of SIMD values as a side effect, which are used by other tests.
86 function TestConstructor(type, lanes) {
87 var simdFn = SIMD[type];
88 assertFalse(Object === simdFn.prototype.constructor)
89 assertFalse(simdFn === Object.prototype.constructor)
90 assertSame(simdFn, simdFn.prototype.constructor)
91
92 values = []
93
94 // The constructor expects values for all lanes.
95 switch (type) {
96 case 'float32x4':
97 // The constructor expects values for all lanes.
98 assertThrows(function () { simdFn() }, TypeError)
99 assertThrows(function () { simdFn(0) }, TypeError)
100 assertThrows(function () { simdFn(0, 1) }, TypeError)
101 assertThrows(function () { simdFn(0, 1, 2) }, TypeError)
102
103 values.push(simdFn(1, 2, 3, 4))
104 values.push(simdFn(1, 2, 3, 4)) // test structural equivalence
105 values.push(simdFn(-0, NaN, 0, 0.5))
106 values.push(simdFn(-0, NaN, 0, 0.5)) // test structural equivalence
107 values.push(simdFn(3, 2, 1, 0))
108 values.push(simdFn(0, 0, 0, 0))
109 break
110 }
111 for (var i in values) {
112 assertSame(simdFn, values[i].__proto__.constructor)
113 assertSame(simdFn, Object(values[i]).__proto__.constructor)
114 assertSame(simdFn.prototype, values[i].__proto__)
115 assertSame(simdFn.prototype, Object(values[i]).__proto__)
116 }
117 }
118
119
120 function TestType(type, lanes) {
121 for (var i in values) {
122 assertEquals(type, typeof values[i])
123 assertTrue(typeof values[i] === type)
124 assertTrue(typeof Object(values[i]) === 'object')
125 assertEquals(null, %_ClassOf(values[i]))
126 assertEquals("Float32x4", %_ClassOf(Object(values[i])))
127 }
128 }
129
130
131 function TestPrototype(type, lanes) {
132 var simdFn = SIMD[type];
133 assertSame(Object.prototype, simdFn.prototype.__proto__)
134 for (var i in values) {
135 assertSame(simdFn.prototype, values[i].__proto__)
136 assertSame(simdFn.prototype, Object(values[i]).__proto__)
137 }
138 }
139
140
141 function TestValueOf(type, lanes) {
142 var simdFn = SIMD[type];
143 for (var i in values) {
144 assertTrue(values[i] === Object(values[i]).valueOf())
145 assertTrue(values[i] === values[i].valueOf())
146 assertTrue(simdFn.prototype.valueOf.call(Object(values[i])) === values[i])
147 assertTrue(simdFn.prototype.valueOf.call(values[i]) === values[i])
148 }
149 }
150
151
152 function TestGet(type, lanes) {
153 var simdFn = SIMD[type];
154 for (var i in values) {
155 assertEquals(undefined, values[i].a)
156 assertEquals(undefined, values[i]["a" + "b"])
157 assertEquals(undefined, values[i]["" + "1"])
158 assertEquals(undefined, values[i][42])
159 }
160 }
161
162
163 function TestToBoolean(type, lanes) {
164 for (var i in values) {
165 assertTrue(Boolean(Object(values[i])))
166 assertFalse(!Object(values[i]))
167 assertTrue(Boolean(values[i]).valueOf())
168 assertFalse(!values[i])
169 assertTrue(!!values[i])
170 assertTrue(values[i] && true)
171 assertFalse(!values[i] && false)
172 assertTrue(!values[i] || true)
173 assertEquals(1, values[i] ? 1 : 2)
174 assertEquals(2, !values[i] ? 1 : 2)
175 if (!values[i]) assertUnreachable();
176 if (values[i]) {} else assertUnreachable();
177 }
178 }
179
180
181 function TestToString(type, lanes) {
182 var simdFn = SIMD[type];
183 for (var i in values) {
184 assertEquals(values[i].toString(), String(values[i]))
185 assertTrue(isValidSimdString(values[i].toString(), values[i], type, lanes))
186 assertTrue(
187 isValidSimdString(Object(values[i]).toString(), values[i], type, lanes))
188 assertTrue(isValidSimdString(
189 simdFn.prototype.toString.call(values[i]), values[i], type, lanes))
190 }
191 }
192
193
194 function TestToNumber(type, lanes) {
195 for (var i in values) {
196 assertThrows(function() { Number(Object(values[i])) }, TypeError)
197 assertThrows(function() { +Object(values[i]) }, TypeError)
198 assertThrows(function() { Number(values[i]) }, TypeError)
199 assertThrows(function() { values[i] + 0 }, TypeError)
200 }
201 }
202
203
204 function TestEquality(type, lanes) {
205 // Every SIMD value should equal itself, and non-strictly equal its wrapper.
206 for (var i in values) {
207 assertSame(values[i], values[i])
208 assertEquals(values[i], values[i])
209 assertTrue(Object.is(values[i], values[i]))
210 assertTrue(values[i] === values[i])
211 assertTrue(values[i] == values[i])
212 assertFalse(values[i] === Object(values[i]))
213 assertFalse(Object(values[i]) === values[i])
214 assertFalse(values[i] == Object(values[i]))
215 assertFalse(Object(values[i]) == values[i])
216 assertTrue(values[i] === values[i].valueOf())
217 assertTrue(values[i].valueOf() === values[i])
218 assertTrue(values[i] == values[i].valueOf())
219 assertTrue(values[i].valueOf() == values[i])
220 assertFalse(Object(values[i]) === Object(values[i]))
221 assertEquals(Object(values[i]).valueOf(), Object(values[i]).valueOf())
222 }
223
224 // Test structural equivalence.
225 for (var i = 0; i < values.length; i++) {
226 for (var j = i + 1; j < values.length; j++) {
227 var a = values[i], b = values[j],
228 equivalent = areEquivalent(type, lanes, a, b);
229 assertSame(equivalent, a == b);
230 assertSame(equivalent, a === b);
231 }
232 }
233
234 // SIMD values should not be equal to any other kind of object.
235 var others = [347, 1.275, NaN, "string", null, undefined, {}, function() {}]
236 for (var i in values) {
237 for (var j in others) {
238 assertFalse(values[i] === others[j])
239 assertFalse(others[j] === values[i])
240 assertFalse(values[i] == others[j])
241 assertFalse(others[j] == values[i])
242 }
243 }
244 }
245
246
247 function TestComparison(type, lanes) {
248 var a = values[0], b = values[1];
249
250 function lt() { a < b; }
251 function gt() { a > b; }
252 function le() { a <= b; }
253 function ge() { a >= b; }
254 function lt_same() { a < a; }
255 function gt_same() { a > a; }
256 function le_same() { a <= a; }
257 function ge_same() { a >= a; }
258
259 var throwFuncs = [lt, gt, le, ge, lt_same, gt_same, le_same, ge_same];
260
261 for (var f of throwFuncs) {
262 assertThrows(f, TypeError);
263 %OptimizeFunctionOnNextCall(f);
264 assertThrows(f, TypeError);
265 assertThrows(f, TypeError);
266 }
267 }
268
269
270 // Test SIMD value wrapping/boxing over non-builtins.
271 function TestCall(type, lanes) {
272 var simdFn = SIMD[type];
273 simdFn.prototype.getThisProto = function () {
274 return Object.getPrototypeOf(this);
275 }
276 for (var i in values) {
277 assertTrue(values[i].getThisProto() === simdFn.prototype)
278 }
279 }
280
281
282 function TestSIMDTypes() {
283 var types = [ 'float32x4' ];
284 for (var i = 0; i < types.length; ++i) {
285 var type = types[i],
286 lanes = lanesForType(type);
287 TestConstructor(type, lanes);
288 TestType(type, lanes);
289 TestPrototype(type, lanes);
290 TestValueOf(type, lanes);
291 TestGet(type, lanes);
292 TestToBoolean(type, lanes);
293 TestToString(type, lanes);
294 TestToNumber(type, lanes);
295 TestEquality(type, lanes);
296 TestComparison(type, lanes);
297 TestCall(type, lanes);
298 }
299 }
300 TestSIMDTypes()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698