Skip to content

Commit 7ff9b2f

Browse files
rakudramaCommit Bot
authored and
Commit Bot
committed
[js_runtime] Use Math.clz32
Change-Id: Ibe6598587151d7c5e0595cffde4b35d024f63c15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/244861 Reviewed-by: Nicholas Shahan <[email protected]> Commit-Queue: Stephen Adams <[email protected]>
1 parent 8a978ff commit 7ff9b2f

File tree

2 files changed

+2
-93
lines changed

2 files changed

+2
-93
lines changed

sdk/lib/_internal/js_dev_runtime/private/js_number.dart

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,7 @@ class JSNumber extends Interceptor implements int, double {
435435

436436
@notNull
437437
static int _clz32(@notNull int uint32) {
438-
// TODO(sra): Use `Math.clz32(uint32)` (not available on IE11).
439-
return 32 - _bitCount(_spread(uint32));
438+
return JS('!', 'Math.clz32(#)', uint32);
440439
}
441440

442441
// Returns pow(this, e) % m.
@@ -581,54 +580,6 @@ class JSNumber extends Interceptor implements int, double {
581580
return _binaryGcd(x, y, false);
582581
}
583582

584-
// Assumes i is <= 32-bit and unsigned.
585-
@notNull
586-
static int _bitCount(@notNull int i) {
587-
// See "Hacker's Delight", section 5-1, "Counting 1-Bits".
588-
589-
// The basic strategy is to use "divide and conquer" to
590-
// add pairs (then quads, etc.) of bits together to obtain
591-
// sub-counts.
592-
//
593-
// A straightforward approach would look like:
594-
//
595-
// i = (i & 0x55555555) + ((i >> 1) & 0x55555555);
596-
// i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
597-
// i = (i & 0x0F0F0F0F) + ((i >> 4) & 0x0F0F0F0F);
598-
// i = (i & 0x00FF00FF) + ((i >> 8) & 0x00FF00FF);
599-
// i = (i & 0x0000FFFF) + ((i >> 16) & 0x0000FFFF);
600-
//
601-
// The code below removes unnecessary &'s and uses a
602-
// trick to remove one instruction in the first line.
603-
604-
i = _shru(i, 0) - (_shru(i, 1) & 0x55555555);
605-
i = (i & 0x33333333) + (_shru(i, 2) & 0x33333333);
606-
i = 0x0F0F0F0F & (i + _shru(i, 4));
607-
i += _shru(i, 8);
608-
i += _shru(i, 16);
609-
return (i & 0x0000003F);
610-
}
611-
612-
@notNull
613-
static int _shru(int value, int shift) =>
614-
JS<int>('!', '# >>> #', value, shift);
615-
@notNull
616-
static int _shrs(int value, int shift) =>
617-
JS<int>('!', '# >> #', value, shift);
618-
@notNull
619-
static int _ors(int a, int b) => JS<int>('!', '# | #', a, b);
620-
621-
// Assumes i is <= 32-bit
622-
@notNull
623-
static int _spread(@notNull int i) {
624-
i = _ors(i, _shrs(i, 1));
625-
i = _ors(i, _shrs(i, 2));
626-
i = _ors(i, _shrs(i, 4));
627-
i = _ors(i, _shrs(i, 8));
628-
i = _shru(_ors(i, _shrs(i, 16)), 0);
629-
return i;
630-
}
631-
632583
@notNull
633584
int operator ~() => JS<int>('!', r'(~#) >>> 0', this);
634585
}

sdk/lib/_internal/js_runtime/lib/js_number.dart

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,7 @@ class JSInt extends JSNumber implements int {
531531
}
532532

533533
static int _clz32(int uint32) {
534-
// TODO(sra): Use `Math.clz32(uint32)` (not available on IE11).
535-
return 32 - _bitCount(_spread(uint32));
534+
return JS('JSUInt31', 'Math.clz32(#)', uint32);
536535
}
537536

538537
// Returns pow(this, e) % m.
@@ -684,47 +683,6 @@ class JSInt extends JSNumber implements int {
684683
return _binaryGcd(x, y, false);
685684
}
686685

687-
// Assumes i is <= 32-bit and unsigned.
688-
static int _bitCount(int i) {
689-
// See "Hacker's Delight", section 5-1, "Counting 1-Bits".
690-
691-
// The basic strategy is to use "divide and conquer" to
692-
// add pairs (then quads, etc.) of bits together to obtain
693-
// sub-counts.
694-
//
695-
// A straightforward approach would look like:
696-
//
697-
// i = (i & 0x55555555) + ((i >> 1) & 0x55555555);
698-
// i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
699-
// i = (i & 0x0F0F0F0F) + ((i >> 4) & 0x0F0F0F0F);
700-
// i = (i & 0x00FF00FF) + ((i >> 8) & 0x00FF00FF);
701-
// i = (i & 0x0000FFFF) + ((i >> 16) & 0x0000FFFF);
702-
//
703-
// The code below removes unnecessary &'s and uses a
704-
// trick to remove one instruction in the first line.
705-
706-
i = _shru(i, 0) - (_shru(i, 1) & 0x55555555);
707-
i = (i & 0x33333333) + (_shru(i, 2) & 0x33333333);
708-
i = 0x0F0F0F0F & (i + _shru(i, 4));
709-
i += _shru(i, 8);
710-
i += _shru(i, 16);
711-
return (i & 0x0000003F);
712-
}
713-
714-
static int _shru(int value, int shift) => JS('int', '# >>> #', value, shift);
715-
static int _shrs(int value, int shift) => JS('int', '# >> #', value, shift);
716-
static int _ors(int a, int b) => JS('int', '# | #', a, b);
717-
718-
// Assumes i is <= 32-bit
719-
static int _spread(int i) {
720-
i = _ors(i, _shrs(i, 1));
721-
i = _ors(i, _shrs(i, 2));
722-
i = _ors(i, _shrs(i, 4));
723-
i = _ors(i, _shrs(i, 8));
724-
i = _shru(_ors(i, _shrs(i, 16)), 0);
725-
return i;
726-
}
727-
728686
Type get runtimeType => int;
729687

730688
int operator ~() => JS('JSUInt32', r'(~#) >>> 0', this);

0 commit comments

Comments
 (0)