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