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

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: 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
30 function isValidSimdString(string, value, type, lanes) {
31 var simdFn = SIMD[type],
32 parseFn =
33 type.indexOf('float') === 0 ? Number.parseFloat : Number.parseInt;
34 var laneStrings = string.split(',');
35 if (laneStrings.length !== lanes)
36 return false;
37 for (var i = 0; i < lanes; i++) {
38 var fromString = parseFn(laneStrings[i]),
39 fromValue = simdFn.extractLane(value, i);
40 if (Math.abs(fromString - fromValue) > Number.EPSILON)
41 return false;
42 }
43 return true;
44 }
45
46
rossberg 2015/07/02 13:35:43 Add tests for the SIMD object itself, e.g.: asser
bbudge 2015/07/06 23:59:05 Thanks. Done.
47 // Test different forms of constructor calls.
48 function TestConstructor(type, lanes, values) {
49 var simdFn = SIMD[type];
50 assertFalse(Object === simdFn.prototype.constructor)
51 assertFalse(simdFn === Object.prototype.constructor)
52 assertSame(simdFn, simdFn.prototype.constructor)
53
54 // Constructors expect values for all lanes.
55 assertThrows(function () { simdFn() }, TypeError)
56 assertThrows(function () { simdFn(0) }, TypeError)
57 switch (lanes) {
58 case 4:
59 values.push(simdFn(0, 1, 2, 3));
60 values.push(simdFn(-0, NaN, 0, 0.5));
61 // Constructors expect values for all lanes.
62 assertThrows(function () { simdFn(0, 1) }, TypeError)
63 assertThrows(function () { simdFn(0, 1, 2) }, TypeError)
64 break
65 }
66 for (var i in values) {
67 assertSame(simdFn, values[i].__proto__.constructor)
68 assertSame(simdFn, Object(values[i]).__proto__.constructor)
rossberg 2015/07/02 13:35:43 assertSame(simdFn.prototype, value[i].__proto__)
bbudge 2015/07/06 23:59:05 Done.
69 }
70 }
71
72
73 function TestType(type, lanes, values) {
74 for (var i in values) {
75 assertEquals(type, typeof values[i])
76 assertTrue(typeof values[i] === type)
77 assertTrue(typeof Object(values[i]) === 'object')
78 assertEquals(null, %_ClassOf(values[i]))
79 assertEquals("Float32x4", %_ClassOf(Object(values[i])))
80 }
81 }
82
83
84 function TestPrototype(type, lanes, values) {
85 var simdFn = SIMD[type];
86 assertSame(Object.prototype, simdFn.prototype.__proto__)
87 for (var i in values) {
88 assertSame(simdFn.prototype, values[i].__proto__)
89 assertSame(simdFn.prototype, Object(values[i]).__proto__)
90 }
91 }
92
93
94 function TestValueOf(type, lanes, values) {
95 var simdFn = SIMD[type];
96 for (var i in values) {
97 assertTrue(values[i] === Object(values[i]).valueOf())
98 assertTrue(values[i] === values[i].valueOf())
99 assertTrue(simdFn.prototype.valueOf.call(Object(values[i])) === values[i])
100 assertTrue(simdFn.prototype.valueOf.call(values[i]) === values[i])
101 }
102 }
103
104
105 function TestGet(type, lanes, values) {
106 var simdFn = SIMD[type];
107 for (var i in values) {
108 assertEquals(undefined, values[i].a)
109 assertEquals(undefined, values[i]["a" + "b"])
110 assertEquals(undefined, values[i]["" + "1"])
111 assertEquals(undefined, values[i][42])
112 }
113 }
114
115
116 function TestToBoolean(type, lanes, values) {
117 for (var i in values) {
118 assertTrue(Boolean(Object(values[i])))
119 assertFalse(!Object(values[i]))
120 assertTrue(Boolean(values[i]).valueOf())
121 assertFalse(!values[i])
122 assertTrue(!!values[i])
123 assertTrue(values[i] && true)
124 assertFalse(!values[i] && false)
125 assertTrue(!values[i] || true)
126 assertEquals(1, values[i] ? 1 : 2)
127 assertEquals(2, !values[i] ? 1 : 2)
128 if (!values[i]) assertUnreachable();
129 if (values[i]) {} else assertUnreachable();
130 }
131 }
132
133
134 function TestToString(type, lanes, values) {
135 var simdFn = SIMD[type];
136 for (var i in values) {
137 assertEquals(values[i].toString(), String(values[i]))
138 assertTrue(isValidSimdString(values[i].toString(), values[i], type, lanes))
139 assertTrue(
140 isValidSimdString(Object(values[i]).toString(), values[i], type, lanes))
141 assertTrue(isValidSimdString(
142 simdFn.prototype.toString.call(values[i]), values[i], type, lanes))
143 }
144 }
145
146
147 function TestToNumber(type, lanes, values) {
148 for (var i in values) {
149 assertThrows(function() { Number(Object(values[i])) }, TypeError)
150 assertThrows(function() { +Object(values[i]) }, TypeError)
151 assertThrows(function() { Number(values[i]) }, TypeError)
152 assertThrows(function() { values[i] + 0 }, TypeError)
153 }
154 }
155
156
157 function TestEquality(type, lanes, values) {
158 // Every SIMD value should equal itself, and non-strictly equal its wrapper.
rossberg 2015/07/02 13:35:43 This is lacking tests that make sure that equality
bbudge 2015/07/06 23:59:05 Added tests for structural equality semantics.
159 for (var i in values) {
160 assertSame(values[i], values[i])
161 assertEquals(values[i], values[i])
162 assertTrue(Object.is(values[i], values[i]))
163 assertTrue(values[i] === values[i])
164 assertTrue(values[i] == values[i])
165 assertFalse(values[i] === Object(values[i]))
166 assertFalse(Object(values[i]) === values[i])
167 assertFalse(values[i] == Object(values[i]))
168 assertFalse(Object(values[i]) == values[i])
169 assertTrue(values[i] === values[i].valueOf())
170 assertTrue(values[i].valueOf() === values[i])
171 assertTrue(values[i] == values[i].valueOf())
172 assertTrue(values[i].valueOf() == values[i])
173 assertFalse(Object(values[i]) === Object(values[i]))
174 assertEquals(Object(values[i]).valueOf(), Object(values[i]).valueOf())
175 }
176
177 // SIMD values should not equal other SIMD values.
178 for (var i = 0; i < values.length; ++i) {
179 for (var j = i + 1; j < values.length; ++j) {
180 assertFalse(Object.is(values[i], values[j]))
181 assertFalse(values[i] === values[j])
182 assertFalse(values[i] == values[j])
183 }
184 }
185
186 // SIMD values should not be equal to any other kind of object.
187 var others = [347, 1.275, NaN, "string", null, undefined, {}, function() {}]
188 for (var i in values) {
189 for (var j in others) {
190 assertFalse(values[i] === others[j])
191 assertFalse(others[j] === values[i])
192 assertFalse(values[i] == others[j])
193 assertFalse(others[j] == values[i])
194 }
195 }
196 }
197
198
199 function TestComparison(type, lanes, values) {
200 var a = values[0], b = values[1];
201
202 function lt() { a < b; }
203 function gt() { a > b; }
204 function le() { a <= b; }
205 function ge() { a >= b; }
206 function lt_same() { a < a; }
207 function gt_same() { a > a; }
208 function le_same() { a <= a; }
209 function ge_same() { a >= a; }
210
211 var throwFuncs = [lt, gt, le, ge, lt_same, gt_same, le_same, ge_same];
212
213 for (var f of throwFuncs) {
214 assertThrows(f, TypeError);
215 %OptimizeFunctionOnNextCall(f);
216 assertThrows(f, TypeError);
217 assertThrows(f, TypeError);
218 }
219 }
220
221
222 // Test SIMD value wrapping/boxing over non-builtins.
223 function TestCall(type, lanes, values) {
224 var simdFn = SIMD[type];
225 simdFn.prototype.getThisProto = function () {
226 return Object.getPrototypeOf(this);
227 }
228 for (var i in values) {
229 assertTrue(values[i].getThisProto() === simdFn.prototype)
230 }
231 }
232
233
234 function TestAll() {
235 var types = [ 'float32x4' ];
236
237 function lanesForType(typeName) {
238 return Number.parseInt(typeName[typeName.length - 1]);
239 }
240
241 for (var i = 0; i < types.length; ++i) {
242 var type = types[i],
243 lanes = lanesForType(type);
244 var values = [];
245 TestConstructor(type, lanes, values);
246 TestType(type, lanes, values);
247 TestPrototype(type, lanes, values);
248 TestValueOf(type, lanes, values);
249 TestGet(type, lanes, values);
250 TestToBoolean(type, lanes, values);
251 TestToString(type, lanes, values);
252 TestToNumber(type, lanes, values);
253 TestEquality(type, lanes, values);
254 TestComparison(type, lanes, values);
255 TestCall(type, lanes, values);
256 }
257 }
258 TestAll()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698