Skip to content

Commit e1622af

Browse files
committed
Optimize llvm_cttz_i32() to remove the clumsy cttz_i8 lookup table to simplify code - (probably faster too, though did not benchmark; the arithmetic only form is greatly preferred anyway)
1 parent d54eb4e commit e1622af

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed

emscripten.py

-5
Original file line numberDiff line numberDiff line change
@@ -1259,11 +1259,6 @@ def create_basic_vars(exported_implemented_functions, forwarded_json, metadata,
12591259
basic_vars = ['DYNAMICTOP_PTR', 'tempDoublePtr', 'ABORT']
12601260
if not (settings['WASM'] and settings['SIDE_MODULE']):
12611261
basic_vars += ['STACKTOP', 'STACK_MAX']
1262-
if metadata.get('preciseI64MathUsed'):
1263-
basic_vars += ['cttz_i8']
1264-
else:
1265-
if forwarded_json['Functions']['libraryFunctions'].get('_llvm_cttz_i32'):
1266-
basic_vars += ['cttz_i8']
12671262
if settings['RELOCATABLE']:
12681263
if not (settings['WASM'] and settings['SIDE_MODULE']):
12691264
basic_vars += ['gb', 'fb']

src/library.js

+2-26
Original file line numberDiff line numberDiff line change
@@ -1067,37 +1067,13 @@ LibraryManager.library = {
10671067
return ret | 0;
10681068
},
10691069

1070-
llvm_cttz_i32__deps: [function() {
1071-
function cttz(x) {
1072-
for (var i = 0; i < 8; i++) {
1073-
if (x & (1 << i)) {
1074-
return i;
1075-
}
1076-
}
1077-
return 8;
1078-
}
1079-
if (SIDE_MODULE) return ''; // uses it from the parent
1080-
1081-
#if USE_PTHREADS
1082-
return 'var cttz_i8; if (ENVIRONMENT_IS_PTHREAD) cttz_i8 = PthreadWorkerInit.cttz_i8; else PthreadWorkerInit.cttz_i8 = cttz_i8 = allocate([' + range(256).map(function(x) { return cttz(x) }).join(',') + '], "i8", ALLOC_STATIC);';
1083-
#else
1084-
return 'var cttz_i8 = allocate([' + range(256).map(function(x) { return cttz(x) }).join(',') + '], "i8", ALLOC_STATIC);';
1085-
#endif
1086-
}],
10871070
#if WASM == 0 // binaryen will convert these calls to wasm anyhow
10881071
llvm_cttz_i32__asm: true,
10891072
#endif
10901073
llvm_cttz_i32__sig: 'ii',
10911074
llvm_cttz_i32: function(x) {
1092-
x = x|0;
1093-
var ret = 0;
1094-
ret = {{{ makeGetValueAsm('cttz_i8', 'x & 0xff', 'i8') }}};
1095-
if ((ret|0) < 8) return ret|0;
1096-
ret = {{{ makeGetValueAsm('cttz_i8', '(x >> 8)&0xff', 'i8') }}};
1097-
if ((ret|0) < 8) return (ret + 8)|0;
1098-
ret = {{{ makeGetValueAsm('cttz_i8', '(x >> 16)&0xff', 'i8') }}};
1099-
if ((ret|0) < 8) return (ret + 16)|0;
1100-
return ({{{ makeGetValueAsm('cttz_i8', 'x >>> 24', 'i8') }}} + 24)|0;
1075+
x = x | 0;
1076+
return x ? (31 - (Math_clz32((x ^ (x - 1))) | 0) | 0) : 32;
11011077
},
11021078

11031079
llvm_cttz_i64__deps: ['llvm_cttz_i32'],

tests/core/test_llvm_intrinsics.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ int main(void) {
6565
printf("%d,%d\n", (int)llvm_ctpop_i64((0x3101ULL << 32) | 1),
6666
llvm_ctpop_i32(0x3101));
6767

68+
printf("llvm_cttz_i32:\n");
69+
printf("(0, 0)=%d\n", llvm_cttz_i32(0, 0));
70+
printf("(1, 0)=%d\n", llvm_cttz_i32(1, 0));
71+
printf("(2, 0)=%d\n", llvm_cttz_i32(2, 0));
72+
printf("(0x0000FFFF, 0)=%d\n", llvm_cttz_i32(0x0000FFFF, 0));
73+
printf("(0x7FFF0000, 0)=%d\n", llvm_cttz_i32(0x7FFF0000, 0));
74+
printf("(0xFFFF0000, 0)=%d\n", llvm_cttz_i32(0xFFFF0000, 0));
75+
printf("(0x7FFFFFFF, 0)=%d\n", llvm_cttz_i32(0x7FFFFFFF, 0));
76+
printf("(0xFFFFFFFE, 0)=%d\n", llvm_cttz_i32(0xFFFFFFFE, 0));
77+
printf("(0xFFFFFFFF, 0)=%d\n", llvm_cttz_i32(0xFFFFFFFF, 0));
6878
printf("small ctlz: %d,%d\n", (int)llvm_ctlz_i8(2, 0), llvm_ctlz_i16(2, 0));
6979

7080
printf("llvm_ctpop_i32:\n");

tests/core/test_llvm_intrinsics.out

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ c5,de,15,8a
55
23,21
66
40,10
77
5,4
8+
llvm_cttz_i32:
9+
(0, 0)=32
10+
(1, 0)=0
11+
(2, 0)=1
12+
(0x0000FFFF, 0)=0
13+
(0x7FFF0000, 0)=16
14+
(0xFFFF0000, 0)=16
15+
(0x7FFFFFFF, 0)=0
16+
(0xFFFFFFFE, 0)=1
17+
(0xFFFFFFFF, 0)=0
818
small ctlz: 6,14
919
llvm_ctpop_i32:
1020
22

tests/test_other.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7976,7 +7976,7 @@ def test(filename, expectations):
79767976
0, [], ['tempDoublePtr', 'waka'], 8, 0, 0), # totally empty!
79777977
# but we don't metadce with linkable code! other modules may want it
79787978
(['-O3', '-s', 'MAIN_MODULE=1'],
7979-
1542, ['invoke_i'], ['waka'], 496958, 168, 2558),
7979+
1541, ['invoke_i'], ['waka'], 496958, 168, 2558),
79807980
])
79817981

79827982
print('test on a minimal pure computational thing')

0 commit comments

Comments
 (0)