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

Commit 3c4747d

Browse files
committed
Elementwise select.
This renames the existing bitwise select to bitselect, and introduces a new elementwise select, following the convention for boolean values in SIMD of examining the sign bit.
1 parent 89eea8d commit 3c4747d

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-9
lines changed

src/ecmascript_simd.js

+61-6
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,32 @@ SIMD.float32x4.greaterThan = function(t, other) {
675675
/**
676676
* @param {int32x4} t Selector mask. An instance of int32x4
677677
* @param {float32x4} trueValue Pick lane from here if corresponding
678-
* selector lane is 0xFFFFFFFF
678+
* selector lane is true
679679
* @param {float32x4} falseValue Pick lane from here if corresponding
680-
* selector lane is 0x0
680+
* selector lane is false
681681
* @return {float32x4} Mix of lanes from trueValue or falseValue as
682682
* indicated
683683
*/
684684
SIMD.float32x4.select = function(t, trueValue, falseValue) {
685+
checkInt32x4(t);
686+
checkFloat32x4(trueValue);
687+
checkFloat32x4(falseValue);
688+
return SIMD.float32x4(_PRIVATE.tobool(t.x) ? trueValue.x : falseValue.x,
689+
_PRIVATE.tobool(t.y) ? trueValue.y : falseValue.y,
690+
_PRIVATE.tobool(t.z) ? trueValue.z : falseValue.z,
691+
_PRIVATE.tobool(t.w) ? trueValue.w : falseValue.w);
692+
}
693+
694+
/**
695+
* @param {int32x4} t Selector mask. An instance of int32x4
696+
* @param {float32x4} trueValue Pick bit from here if corresponding
697+
* selector bit is 1
698+
* @param {float32x4} falseValue Pick bit from here if corresponding
699+
* selector bit is 0
700+
* @return {float32x4} Mix of bits from trueValue or falseValue as
701+
* indicated
702+
*/
703+
SIMD.float32x4.bitselect = function(t, trueValue, falseValue) {
685704
checkInt32x4(t);
686705
checkFloat32x4(trueValue);
687706
checkFloat32x4(falseValue);
@@ -1240,13 +1259,30 @@ SIMD.float64x2.greaterThan = function(t, other) {
12401259
/**
12411260
* @param {int32x4} t Selector mask. An instance of int32x4
12421261
* @param {float64x2} trueValue Pick lane from here if corresponding
1243-
* selector lanes are 0xFFFFFFFF
1262+
* selector lane is true
12441263
* @param {float64x2} falseValue Pick lane from here if corresponding
1245-
* selector lanes are 0x0
1264+
* selector lane is false
12461265
* @return {float64x2} Mix of lanes from trueValue or falseValue as
12471266
* indicated
12481267
*/
12491268
SIMD.float64x2.select = function(t, trueValue, falseValue) {
1269+
checkInt32x4(t);
1270+
checkFloat64x2(trueValue);
1271+
checkFloat64x2(falseValue);
1272+
return SIMD.float32x4(_PRIVATE.tobool(t.x) ? trueValue.x : falseValue.x,
1273+
_PRIVATE.tobool(t.y) ? trueValue.y : falseValue.y);
1274+
}
1275+
1276+
/**
1277+
* @param {int32x4} t Selector mask. An instance of int32x4
1278+
* @param {float64x2} trueValue Pick bit from here if corresponding
1279+
* selector bit is 1
1280+
* @param {float64x2} falseValue Pick bit from here if corresponding
1281+
* selector bit is 0
1282+
* @return {float64x2} Mix of bits from trueValue or falseValue as
1283+
* indicated
1284+
*/
1285+
SIMD.float64x2.bitselect = function(t, trueValue, falseValue) {
12501286
checkInt32x4(t);
12511287
checkFloat64x2(trueValue);
12521288
checkFloat64x2(falseValue);
@@ -1489,13 +1525,32 @@ SIMD.int32x4.shuffleMix = function(t1, t2, mask) {
14891525
/**
14901526
* @param {int32x4} t Selector mask. An instance of int32x4
14911527
* @param {int32x4} trueValue Pick lane from here if corresponding
1492-
* selector lane is 0xFFFFFFFF
1528+
* selector lane is true
14931529
* @param {int32x4} falseValue Pick lane from here if corresponding
1494-
* selector lane is 0x0
1530+
* selector lane is false
14951531
* @return {int32x4} Mix of lanes from trueValue or falseValue as
14961532
* indicated
14971533
*/
14981534
SIMD.int32x4.select = function(t, trueValue, falseValue) {
1535+
checkInt32x4(t);
1536+
checkInt32x4(trueValue);
1537+
checkInt32x4(falseValue);
1538+
return SIMD.int32x4(_PRIVATE.tobool(t.x) ? trueValue.x : falseValue.x,
1539+
_PRIVATE.tobool(t.y) ? trueValue.y : falseValue.y,
1540+
_PRIVATE.tobool(t.z) ? trueValue.z : falseValue.z,
1541+
_PRIVATE.tobool(t.w) ? trueValue.w : falseValue.w);
1542+
}
1543+
1544+
/**
1545+
* @param {int32x4} t Selector mask. An instance of int32x4
1546+
* @param {int32x4} trueValue Pick bit from here if corresponding
1547+
* selector bit is 1
1548+
* @param {int32x4} falseValue Pick bit from here if corresponding
1549+
* selector bit is 0
1550+
* @return {int32x4} Mix of bits from trueValue or falseValue as
1551+
* indicated
1552+
*/
1553+
SIMD.int32x4.bitselect = function(t, trueValue, falseValue) {
14991554
checkInt32x4(t);
15001555
checkInt32x4(trueValue);
15011556
checkInt32x4(falseValue);

src/ecmascript_simd_tests.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ test('float32x4 comparisons', function() {
535535
});
536536

537537
test('float32x4 select', function() {
538-
var m = SIMD.int32x4.bool(true, true, false, false);
538+
var m = SIMD.int32x4(0xaaaaaaaa, 0xaaaaaaaa, 0x55555555, 0x55555555);
539539
var t = SIMD.float32x4(1.0, 2.0, 3.0, 4.0);
540540
var f = SIMD.float32x4(5.0, 6.0, 7.0, 8.0);
541541
var s = SIMD.float32x4.select(m, t, f);
@@ -545,6 +545,17 @@ test('float32x4 select', function() {
545545
equal(8.0, s.w);
546546
});
547547

548+
test('float32x4 bitselect', function() {
549+
var m = SIMD.int32x4(0xaaaaaaaa, 0xaaaaaaaa, 0x55555555, 0x55555555);
550+
var t = SIMD.float32x4(1.0, 2.0, 3.0, 4.0);
551+
var f = SIMD.float32x4(5.0, 6.0, 7.0, 8.0);
552+
var s = SIMD.float32x4.bitselect(m, t, f);
553+
equal(7.737125245533627e+25, s.x);
554+
equal(3.0, s.y);
555+
equal(7.0, s.z);
556+
equal(2.0, s.w);
557+
});
558+
548559
test('float32x4 int32x4 bit conversion', function() {
549560
var m = SIMD.int32x4(0x3F800000, 0x40000000, 0x40400000, 0x40800000);
550561
var n = SIMD.float32x4.fromInt32x4Bits(m);
@@ -1620,14 +1631,23 @@ test('float64x2 comparisons', function() {
16201631
});
16211632

16221633
test('float64x2 select', function() {
1623-
var m = SIMD.int32x4.bool(true, true, false, false);
1634+
var m = SIMD.int32x4(0xaaaaaaaa, 0x55555555);
16241635
var t = SIMD.float64x2(1.0, 2.0);
16251636
var f = SIMD.float64x2(3.0, 4.0);
16261637
var s = SIMD.float64x2.select(m, t, f);
16271638
equal(1.0, s.x);
16281639
equal(4.0, s.y);
16291640
});
16301641

1642+
test('float64x2 bitselect', function() {
1643+
var m = SIMD.int32x4(0xaaaaaaaa, 0x55555555);
1644+
var t = SIMD.float64x2(1.0, 2.0);
1645+
var f = SIMD.float64x2(3.0, 4.0);
1646+
var s = SIMD.float64x2.bitselect(m, t, f);
1647+
equal(7.475396213323176e-206, s.x);
1648+
equal(4.0, s.y);
1649+
});
1650+
16311651
test('float64x2 load', function() {
16321652
var a = new Float64Array(8);
16331653
for (var i = 0; i < a.length; i++) {
@@ -2150,7 +2170,7 @@ test('int32x4 shiftRightLogicalByScalar', function() {
21502170
});
21512171

21522172
test('int32x4 select', function() {
2153-
var m = SIMD.int32x4.bool(true, true, false, false);
2173+
var m = SIMD.int32x4(0xaaaaaaaa, 0xaaaaaaaa, 0x55555555, 0x55555555);
21542174
var t = SIMD.int32x4(1, 2, 3, 4);
21552175
var f = SIMD.int32x4(5, 6, 7, 8);
21562176
var s = SIMD.int32x4.select(m, t, f);
@@ -2160,6 +2180,17 @@ test('int32x4 select', function() {
21602180
equal(8, s.w);
21612181
});
21622182

2183+
test('int32x4 bitselect', function() {
2184+
var m = SIMD.int32x4(0xaaaaaaaa, 0xaaaaaaaa, 0x55555555, 0x55555555);
2185+
var t = SIMD.int32x4(1, 2, 3, 4);
2186+
var f = SIMD.int32x4(5, 6, 7, 8);
2187+
var s = SIMD.int32x4.bitselect(m, t, f);
2188+
equal(5, s.x);
2189+
equal(6, s.y);
2190+
equal(3, s.z);
2191+
equal(12, s.w);
2192+
});
2193+
21632194
test('int32x4 load', function() {
21642195
var a = new Int32Array(8);
21652196
for (var i = 0; i < a.length; i++) {

0 commit comments

Comments
 (0)