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

Unified 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: samevalue.js tweak. 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 side-by-side diff with in-line comments
Download patch
Index: test/mjsunit/harmony/simd.js
diff --git a/test/mjsunit/harmony/simd.js b/test/mjsunit/harmony/simd.js
new file mode 100644
index 0000000000000000000000000000000000000000..c514a9d74db5221de247e2a40659d65c62e57eed
--- /dev/null
+++ b/test/mjsunit/harmony/simd.js
@@ -0,0 +1,300 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Flags: --harmony-simd --harmony-tostring --allow-natives-syntax
+// Flags: --noalways-opt
+
+function lanesForType(typeName) {
+ // The lane count follows the first 'x' in the type name, which begins with
+ // 'float', 'int', or 'bool'.
+ return Number.parseInt(typeName[typeName.indexOf('x') + 1]);
+}
+
+
+function isValidSimdString(string, value, type, lanes) {
+ var simdFn = SIMD[type],
+ parseFn =
+ type.indexOf('float') === 0 ? Number.parseFloat : Number.parseInt,
+ indexOfOpenParen = string.indexOf('(');
+ // Check prefix for correct type name.
+ if (string.substr(0, indexOfOpenParen).toUpperCase() !== type.toUpperCase())
+ return false;
+ // Remove type name and open parenthesis.
+ string = string.substr(indexOfOpenParen + 1);
+ var laneStrings = string.split(',');
+ if (laneStrings.length !== lanes)
+ return false;
+ for (var i = 0; i < lanes; i++) {
+ var fromString = parseFn(laneStrings[i]),
+ fromValue = simdFn.extractLane(value, i);
+ if (Math.abs(fromString - fromValue) > Number.EPSILON)
+ return false;
+ }
+ return true;
+}
+
+
+// Test for structural equivalence.
+function areEquivalent(type, lanes, a, b) {
+ var simdFn = SIMD[type];
+ for (var i = 0; i < lanes; i++) {
+ if (simdFn.extractLane(a, i) !== simdFn.extractLane(b, i))
+ return false;
+ }
+ return true;
+}
+
+
+// Tests for the global SIMD object.
+function TestSIMDObject() {
+ assertSame(typeof SIMD, 'object');
+ assertSame(SIMD.constructor, Object);
+ assertSame(Object.getPrototypeOf(SIMD), Object.prototype);
+ assertSame(SIMD + "", "[object SIMD]");
+}
+TestSIMDObject()
+
+// TestConstructor populates this with interesting values for the other tests.
+var values;
+
+// Test different forms of constructor calls. This test populates 'values' with
+// a variety of SIMD values as a side effect, which are used by other tests.
+function TestConstructor(type, lanes) {
+ var simdFn = SIMD[type];
+ assertFalse(Object === simdFn.prototype.constructor)
+ assertFalse(simdFn === Object.prototype.constructor)
+ assertSame(simdFn, simdFn.prototype.constructor)
+
+ values = []
+
+ // The constructor expects values for all lanes.
+ switch (type) {
+ case 'float32x4':
+ // The constructor expects values for all lanes.
+ assertThrows(function () { simdFn() }, TypeError)
+ assertThrows(function () { simdFn(0) }, TypeError)
+ assertThrows(function () { simdFn(0, 1) }, TypeError)
+ assertThrows(function () { simdFn(0, 1, 2) }, TypeError)
+
+ values.push(simdFn(1, 2, 3, 4))
+ values.push(simdFn(1, 2, 3, 4)) // test structural equivalence
+ values.push(simdFn(-0, NaN, 0, 0.5))
+ values.push(simdFn(-0, NaN, 0, 0.5)) // test structural equivalence
+ values.push(simdFn(3, 2, 1, 0))
+ values.push(simdFn(0, 0, 0, 0))
+ break
+ }
+ for (var i in values) {
+ assertSame(simdFn, values[i].__proto__.constructor)
+ assertSame(simdFn, Object(values[i]).__proto__.constructor)
+ assertSame(simdFn.prototype, values[i].__proto__)
+ assertSame(simdFn.prototype, Object(values[i]).__proto__)
+ }
+}
+
+
+function TestType(type, lanes) {
+ for (var i in values) {
+ assertEquals(type, typeof values[i])
+ assertTrue(typeof values[i] === type)
+ assertTrue(typeof Object(values[i]) === 'object')
+ assertEquals(null, %_ClassOf(values[i]))
+ assertEquals("Float32x4", %_ClassOf(Object(values[i])))
+ }
+}
+
+
+function TestPrototype(type, lanes) {
+ var simdFn = SIMD[type];
+ assertSame(Object.prototype, simdFn.prototype.__proto__)
+ for (var i in values) {
+ assertSame(simdFn.prototype, values[i].__proto__)
+ assertSame(simdFn.prototype, Object(values[i]).__proto__)
+ }
+}
+
+
+function TestValueOf(type, lanes) {
+ var simdFn = SIMD[type];
+ for (var i in values) {
+ assertTrue(values[i] === Object(values[i]).valueOf())
+ assertTrue(values[i] === values[i].valueOf())
+ assertTrue(simdFn.prototype.valueOf.call(Object(values[i])) === values[i])
+ assertTrue(simdFn.prototype.valueOf.call(values[i]) === values[i])
+ }
+}
+
+
+function TestGet(type, lanes) {
+ var simdFn = SIMD[type];
+ for (var i in values) {
+ assertEquals(undefined, values[i].a)
+ assertEquals(undefined, values[i]["a" + "b"])
+ assertEquals(undefined, values[i]["" + "1"])
+ assertEquals(undefined, values[i][42])
+ }
+}
+
+
+function TestToBoolean(type, lanes) {
+ for (var i in values) {
+ assertTrue(Boolean(Object(values[i])))
+ assertFalse(!Object(values[i]))
+ assertTrue(Boolean(values[i]).valueOf())
+ assertFalse(!values[i])
+ assertTrue(!!values[i])
+ assertTrue(values[i] && true)
+ assertFalse(!values[i] && false)
+ assertTrue(!values[i] || true)
+ assertEquals(1, values[i] ? 1 : 2)
+ assertEquals(2, !values[i] ? 1 : 2)
+ if (!values[i]) assertUnreachable();
+ if (values[i]) {} else assertUnreachable();
+ }
+}
+
+
+function TestToString(type, lanes) {
+ var simdFn = SIMD[type];
+ for (var i in values) {
+ assertEquals(values[i].toString(), String(values[i]))
+ assertTrue(isValidSimdString(values[i].toString(), values[i], type, lanes))
+ assertTrue(
+ isValidSimdString(Object(values[i]).toString(), values[i], type, lanes))
+ assertTrue(isValidSimdString(
+ simdFn.prototype.toString.call(values[i]), values[i], type, lanes))
+ }
+}
+
+
+function TestToNumber(type, lanes) {
+ for (var i in values) {
+ assertThrows(function() { Number(Object(values[i])) }, TypeError)
+ assertThrows(function() { +Object(values[i]) }, TypeError)
+ assertThrows(function() { Number(values[i]) }, TypeError)
+ assertThrows(function() { values[i] + 0 }, TypeError)
+ }
+}
+
+
+function TestEquality(type, lanes) {
+ // Every SIMD value should equal itself, and non-strictly equal its wrapper.
+ for (var i in values) {
+ assertSame(values[i], values[i])
+ assertEquals(values[i], values[i])
+ assertTrue(Object.is(values[i], values[i]))
+ assertTrue(values[i] === values[i])
+ assertTrue(values[i] == values[i])
+ assertFalse(values[i] === Object(values[i]))
+ assertFalse(Object(values[i]) === values[i])
+ assertFalse(values[i] == Object(values[i]))
+ assertFalse(Object(values[i]) == values[i])
+ assertTrue(values[i] === values[i].valueOf())
+ assertTrue(values[i].valueOf() === values[i])
+ assertTrue(values[i] == values[i].valueOf())
+ assertTrue(values[i].valueOf() == values[i])
+ assertFalse(Object(values[i]) === Object(values[i]))
+ assertEquals(Object(values[i]).valueOf(), Object(values[i]).valueOf())
+ }
+
+ // Test structural equivalence.
+ for (var i = 0; i < values.length; i++) {
+ for (var j = i + 1; j < values.length; j++) {
+ var a = values[i], b = values[j],
+ equivalent = areEquivalent(type, lanes, a, b);
+ assertSame(equivalent, a == b);
+ assertSame(equivalent, a === b);
+ }
+ }
+
+ // SIMD values should not be equal to any other kind of object.
+ var others = [347, 1.275, NaN, "string", null, undefined, {}, function() {}]
+ for (var i in values) {
+ for (var j in others) {
+ assertFalse(values[i] === others[j])
+ assertFalse(others[j] === values[i])
+ assertFalse(values[i] == others[j])
+ assertFalse(others[j] == values[i])
+ }
+ }
+}
+
+
+function TestComparison(type, lanes) {
+ var a = values[0], b = values[1];
+
+ function lt() { a < b; }
+ function gt() { a > b; }
+ function le() { a <= b; }
+ function ge() { a >= b; }
+ function lt_same() { a < a; }
+ function gt_same() { a > a; }
+ function le_same() { a <= a; }
+ function ge_same() { a >= a; }
+
+ var throwFuncs = [lt, gt, le, ge, lt_same, gt_same, le_same, ge_same];
+
+ for (var f of throwFuncs) {
+ assertThrows(f, TypeError);
+ %OptimizeFunctionOnNextCall(f);
+ assertThrows(f, TypeError);
+ assertThrows(f, TypeError);
+ }
+}
+
+
+// Test SIMD value wrapping/boxing over non-builtins.
+function TestCall(type, lanes) {
+ var simdFn = SIMD[type];
+ simdFn.prototype.getThisProto = function () {
+ return Object.getPrototypeOf(this);
+ }
+ for (var i in values) {
+ assertTrue(values[i].getThisProto() === simdFn.prototype)
+ }
+}
+
+
+function TestSIMDTypes() {
+ var types = [ 'float32x4' ];
+ for (var i = 0; i < types.length; ++i) {
+ var type = types[i],
+ lanes = lanesForType(type);
+ TestConstructor(type, lanes);
+ TestType(type, lanes);
+ TestPrototype(type, lanes);
+ TestValueOf(type, lanes);
+ TestGet(type, lanes);
+ TestToBoolean(type, lanes);
+ TestToString(type, lanes);
+ TestToNumber(type, lanes);
+ TestEquality(type, lanes);
+ TestComparison(type, lanes);
+ TestCall(type, lanes);
+ }
+}
+TestSIMDTypes()

Powered by Google App Engine
This is Rietveld 408576698