Commit a3709d47 authored by cwhan.tunz's avatar cwhan.tunz Committed by Commit bot

[typedarrays] No callbacks for fast sorting of integer typed arrays

- No callbacks for std::sort of integer typed arrays when user-defined
  comparison is not given
- Use template function, instead of macro, for comparison function
- Do not sort if the array size is less than or equal to 1

BUG=v8:5953

Review-Url: https://codereview.chromium.org/2726153003
Cr-Commit-Position: refs/heads/master@{#43565}
parent 092222c7
...@@ -336,27 +336,24 @@ RUNTIME_FUNCTION(Runtime_TypedArraySetFastCases) { ...@@ -336,27 +336,24 @@ RUNTIME_FUNCTION(Runtime_TypedArraySetFastCases) {
namespace { namespace {
#define TYPED_ARRAY_SORT_COMPAREFN(Type, type, TYPE, ctype, size) \ template <typename T>
bool compare##Type(ctype x, ctype y) { \ bool CompareNum(T x, T y) {
if (x < y) { \ if (x < y) {
return true; \ return true;
} else if (x > y) { \ } else if (x > y) {
return false; \ return false;
} else { \ } else if (!std::is_integral<T>::value) {
double _x = x, _y = y; \ double _x = x, _y = y;
if (x == 0 && x == y) { \ if (x == 0 && x == y) {
/* -0.0 is less than +0.0 */ \ /* -0.0 is less than +0.0 */
return std::signbit(_x) && !std::signbit(_y); \ return std::signbit(_x) && !std::signbit(_y);
} else if (!std::isnan(_x) && std::isnan(_y)) { \ } else if (!std::isnan(_x) && std::isnan(_y)) {
/* number is less than NaN */ \ /* number is less than NaN */
return true; \ return true;
} \ }
} \
return false; \
} }
return false;
TYPED_ARRAYS(TYPED_ARRAY_SORT_COMPAREFN) }
#undef TYPED_ARRAY_SORT_COMPAREFN
} // namespace } // namespace
...@@ -376,15 +373,19 @@ RUNTIME_FUNCTION(Runtime_TypedArraySortFast) { ...@@ -376,15 +373,19 @@ RUNTIME_FUNCTION(Runtime_TypedArraySortFast) {
if (V8_UNLIKELY(array->WasNeutered())) return *array; if (V8_UNLIKELY(array->WasNeutered())) return *array;
size_t length = array->length_value(); size_t length = array->length_value();
if (length == 0) return *array; if (length <= 1) return *array;
switch (array->type()) { switch (array->type()) {
#define TYPED_ARRAY_SORT(Type, type, TYPE, ctype, size) \ #define TYPED_ARRAY_SORT(Type, type, TYPE, ctype, size) \
case kExternal##Type##Array: { \ case kExternal##Type##Array: { \
ctype* backing_store = \ ctype* backing_store = \
static_cast<ctype*>(array->GetBuffer()->backing_store()); \ static_cast<ctype*>(array->GetBuffer()->backing_store()); \
std::sort(backing_store, backing_store + length, compare##Type); \ if (kExternal##Type##Array == kExternalFloat64Array || \
break; \ kExternal##Type##Array == kExternalFloat32Array) \
std::sort(backing_store, backing_store + length, CompareNum<ctype>); \
else \
std::sort(backing_store, backing_store + length); \
break; \
} }
TYPED_ARRAYS(TYPED_ARRAY_SORT) TYPED_ARRAYS(TYPED_ARRAY_SORT)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment