Skip to content

Commit 9fe928d

Browse files
committed
make ctpop_i32 and 64 asm.js methods
1 parent 5c829bf commit 9fe928d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/library.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -916,20 +916,24 @@ LibraryManager.library = {
916916
{{{ makeStructuralReturn(['ret', '0']) }}};
917917
},
918918

919+
llvm_ctpop_i32__asm: true,
920+
llvm_ctpop_i32__sig: 'ii',
919921
llvm_ctpop_i32: function(x) {
920922
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
921-
x = x|0;
922-
x = x - ((x >>> 1) & 0x55555555)|0;
923-
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333)|0;
924-
925-
// next line is asm.js friendly, but unfortunatelly gives WRONG result
926-
// return (((x + (x >>> 4) & 0xF0F0F0F)|0 * 0x1010101) >>> 24)|0;
927-
return (((x + (x >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24)|0;
923+
// http://bits.stephan-brumme.com/countBits.html
924+
x = x | 0;
925+
x = x - ((x >>> 1) & 0x55555555) | 0;
926+
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333) | 0;
927+
return (Math_imul((x + (x >>> 4) & 252645135 /* 0xF0F0F0F, but hits uglify parse bug? */), 0x1010101) >>> 24) | 0;
928928
},
929929

930930
llvm_ctpop_i64__deps: ['llvm_ctpop_i32'],
931+
llvm_ctpop_i64__asm: true,
932+
llvm_ctpop_i64__sig: 'iii',
931933
llvm_ctpop_i64: function(l, h) {
932-
return _llvm_ctpop_i32(l) + _llvm_ctpop_i32(h);
934+
l = l | 0;
935+
h = h | 0;
936+
return (_llvm_ctpop_i32(l) | 0) + (_llvm_ctpop_i32(h) | 0) | 0;
933937
},
934938

935939
llvm_trap: function() {

0 commit comments

Comments
 (0)