Bug 1312411: Add integer specialization of TypedArrayCompare. r=evilpie
authorAndré Bargull <andre.bargull@gmail.com>
Mon, 24 Oct 2016 09:46:19 -0700
changeset 319825 7b9c260074e76d3501c4bff39137cf382753e9d0
parent 319824 862a1036317f3c0ec5d5925a4366b2cbf442198a
child 319826 ea39ecf49414c920b402938cd1ddef62dbb18c01
push id20748
push userphilringnalda@gmail.com
push dateFri, 28 Oct 2016 03:39:55 +0000
treeherderfx-team@715360440695 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersevilpie
bugs1312411
milestone52.0a1
Bug 1312411: Add integer specialization of TypedArrayCompare. r=evilpie
js/src/builtin/TypedArray.js
--- a/js/src/builtin/TypedArray.js
+++ b/js/src/builtin/TypedArray.js
@@ -1066,43 +1066,66 @@ function TypedArraySome(callbackfn/*, th
         if (testResult)
             return true;
     }
 
     // Step 10.
     return false;
 }
 
-// ES6 draft 20151210 22.2.3.26
+// ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
+// 22.2.3.26 TypedArray SortCompare abstract operation
 // Cases are ordered according to likelihood of occurrence
 // as opposed to the ordering in the spec.
 function TypedArrayCompare(x, y) {
     // Step 1.
     assert(typeof x === "number" && typeof y === "number",
            "x and y are not numbers.");
 
-    // Steps 6 - 7.
+    // Step 2 (Implemented in TypedArraySort).
+
+    // Step 6.
+    if (x < y)
+        return -1;
+
+    // Step 7.
+    if (x > y)
+        return 1;
+
+    // Steps 8-9.
+    if (x === 0 && y === 0)
+        return (1/x > 0 ? 1 : 0) - (1/y > 0 ? 1 : 0);
+
+    // Steps 3-4.
+    if (Number_isNaN(x))
+        return Number_isNaN(y) ? 0 : 1;
+
+    // Steps 5, 10.
+    return Number_isNaN(y) ? -1 : 0;
+}
+
+// TypedArray SortCompare specialization for integer values.
+function TypedArrayCompareInt(x, y) {
+    // Step 1.
+    assert(typeof x === "number" && typeof y === "number",
+           "x and y are not numbers.");
+    assert((x === (x|0) || x === (x>>>0)) && (y === (y|0) || y === (y>>>0)),
+           "x and y are not int32/uint32 numbers.");
+
+    // Step 2 (Implemented in TypedArraySort).
+
+    // Steps 6-7.
     var diff = x - y;
     if (diff)
         return diff;
 
-    // Steps 8 - 10.
-    if (x === 0 && y === 0)
-        return (1/x > 0 ? 1 : 0) - (1/y > 0 ? 1 : 0);
-
-    // Step 2. Implemented in TypedArraySort
+    // Steps 3-5, 8-9 (Not applicable when sorting integer values).
 
-    // Step 3.
-    if (Number_isNaN(x) && Number_isNaN(y))
-        return 0;
-
-    // Steps 4 - 5.
-    if (Number_isNaN(x) || Number_isNaN(y))
-        return Number_isNaN(x) ? 1 : -1;
-
+    // Step 10.
+    return 0;
 }
 
 // ES6 draft 20151210 22.2.3.26 %TypedArray%.prototype.sort ( comparefn ).
 function TypedArraySort(comparefn) {
     // This function is not generic.
 
     // Step 1.
     var obj = this;
@@ -1127,23 +1150,23 @@ function TypedArraySort(comparefn) {
 
     if (comparefn === undefined) {
         // CountingSort doesn't invoke the comparator function.
         if (IsUint8TypedArray(obj)) {
             return CountingSort(obj, len, false /* signed */);
         } else if (IsInt8TypedArray(obj)) {
             return CountingSort(obj, len, true /* signed */);
         } else if (IsUint16TypedArray(obj)) {
-            return RadixSort(obj, len, buffer, 2 /* nbytes */, false /* signed */, false /* floating */, TypedArrayCompare);
+            return RadixSort(obj, len, buffer, 2 /* nbytes */, false /* signed */, false /* floating */, TypedArrayCompareInt);
         } else if (IsInt16TypedArray(obj)) {
-            return RadixSort(obj, len, buffer, 2 /* nbytes */, true /* signed */, false /* floating */, TypedArrayCompare);
+            return RadixSort(obj, len, buffer, 2 /* nbytes */, true /* signed */, false /* floating */, TypedArrayCompareInt);
         } else if (IsUint32TypedArray(obj)) {
-            return RadixSort(obj, len, buffer, 4 /* nbytes */, false /* signed */, false /* floating */, TypedArrayCompare);
+            return RadixSort(obj, len, buffer, 4 /* nbytes */, false /* signed */, false /* floating */, TypedArrayCompareInt);
         } else if (IsInt32TypedArray(obj)) {
-            return RadixSort(obj, len, buffer, 4 /* nbytes */, true /* signed */, false /* floating */, TypedArrayCompare);
+            return RadixSort(obj, len, buffer, 4 /* nbytes */, true /* signed */, false /* floating */, TypedArrayCompareInt);
         } else if (IsFloat32TypedArray(obj)) {
             return RadixSort(obj, len, buffer, 4 /* nbytes */, true /* signed */, true /* floating */, TypedArrayCompare);
         }
         return QuickSort(obj, len, TypedArrayCompare);
     }
 
     // To satisfy step 2 from TypedArray SortCompare described in 22.2.3.26
     // the user supplied comparefn is wrapped.