Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #334 from stoklund/coerce-index
Browse files Browse the repository at this point in the history
Coerce load/store indexes with ToNumber.
  • Loading branch information
littledan committed Mar 14, 2016
2 parents 0d28cdc + 1d876c9 commit 612e80b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/ecmascript_simd.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ function clamp(a, min, max) {

// SIMD implementation functions

function simdCoerceIndex(index) {
index = +index;
if (index != Math.floor(index))
throw new RangeError("SIMD index must be an integer");
return index;
}

function simdCheckLaneIndex(index, lanes) {
if (!isInt32(index))
throw new TypeError('Lane index must be an int32');
Expand Down Expand Up @@ -292,8 +299,7 @@ function simdShiftOp(type, op, a, bits) {
function simdLoad(type, tarray, index, count) {
if (!isTypedArray(tarray))
throw new TypeError("The 1st argument must be a typed array.");
if (!isInt32(index))
throw new TypeError("The 2nd argument must be an Int32.");
index = simdCoerceIndex(index);
var bpe = tarray.BYTES_PER_ELEMENT;
var bytes = count * type.laneSize;
if (index < 0 || (index * bpe + bytes) > tarray.byteLength)
Expand All @@ -316,8 +322,7 @@ function simdLoad(type, tarray, index, count) {
function simdStore(type, tarray, index, a, count) {
if (!isTypedArray(tarray))
throw new TypeError("The 1st argument must be a typed array.");
if (!isInt32(index))
throw new TypeError("The 2nd argument must be an Int32.");
index = simdCoerceIndex(index);
var bpe = tarray.BYTES_PER_ELEMENT;
var bytes = count * type.laneSize;
if (index < 0 || (index * bpe + bytes) > tarray.byteLength)
Expand Down
25 changes: 25 additions & 0 deletions src/ecmascript_simd_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,18 @@ function testLoad(type, name, count) {
var a = loadFn(buf, i);
checkValue(type, a, function(index) { return index < count ? i + index : 0; });
}

// Test index coercions.
// Unlike typedArray[index], non-canonical strings are allowed here.
checkValue(type, loadFn(buf, "0"), function(index) { return index < count ? index : 0; });
checkValue(type, loadFn(buf, " -0.0 "), function(index) { return index < count ? index : 0; });
checkValue(type, loadFn(buf, "00"), function(index) { return index < count ? index : 0; });
checkValue(type, loadFn(buf, false), function(index) { return index < count ? index : 0; });
checkValue(type, loadFn(buf, null), function(index) { return index < count ? index : 0; });
checkValue(type, loadFn(buf, "01"), function(index) { return index < count ? 1 + index : 0; });
checkValue(type, loadFn(buf, " +1e0"), function(index) { return index < count ? 1 + index : 0; });
checkValue(type, loadFn(buf, true), function(index) { return index < count ? 1 + index : 0; });

// Test the 2 possible over-alignments.
var f64 = new Float64Array(ab);
var stride = 8 / type.laneSize;
Expand All @@ -673,6 +685,9 @@ function testLoad(type, name, count) {
throws(function () { loadFn(buf, index); });
}
testIndexCheck(buf, -1);
testIndexCheck(buf, 0.7);
testIndexCheck(buf, -0.1);
testIndexCheck(buf, NaN);
testIndexCheck(buf, bufSize / type.laneSize - count + 1);
testIndexCheck(buf.buffer, 1);
testIndexCheck(buf, "a");
Expand All @@ -696,6 +711,16 @@ function testStore(type, name, count) {
storeFn(buf, i, a);
ok(checkBuffer(i));
}

// Test index coercions.
storeFn(buf, "0", a); ok(checkBuffer(0));
storeFn(buf, "01", a); ok(checkBuffer(1));
storeFn(buf, " -0.0 ", a); ok(checkBuffer(0));
storeFn(buf, " +1e0", a); ok(checkBuffer(1));
storeFn(buf, false, a); ok(checkBuffer(0));
storeFn(buf, true, a); ok(checkBuffer(1));
storeFn(buf, null, a); ok(checkBuffer(0));

// Test the 2 over-alignments.
var f64 = new Float64Array(ab);
var stride = 8 / type.laneSize;
Expand Down

0 comments on commit 612e80b

Please sign in to comment.