diff options
| author | gingerBill <bill@gingerbill.org> | 2022-11-11 11:41:28 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2022-11-11 11:41:28 +0000 |
| commit | 0d37da54b4937a19bf581dc001271eb16ac86daf (patch) | |
| tree | 4fd44cf3c2331442d3e22d9dad4d6360e97ee829 /src/llvm_backend_stmt.cpp | |
| parent | 5d47e2a166ada673ed08784f47120abe2433f742 (diff) | |
Add minor optimization for `lb_map_cell_index_static`
Diffstat (limited to 'src/llvm_backend_stmt.cpp')
| -rw-r--r-- | src/llvm_backend_stmt.cpp | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index ec7a162cb..c8f244181 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -382,12 +382,20 @@ lbValue lb_map_cell_index_static(lbProcedure *p, Type *type, lbValue cells_ptr, return lb_emit_ptr_offset(p, elems_ptr, index); } - // TOOD(bill): N power of two optimization to use >> and & + lbValue cell_index = {}; + lbValue data_index = {}; lbValue size_const = lb_const_int(p->module, t_uintptr, size); lbValue len_const = lb_const_int(p->module, t_uintptr, len); - lbValue cell_index = lb_emit_arith(p, Token_Quo, index, len_const, t_uintptr); - lbValue data_index = lb_emit_arith(p, Token_Mod, index, len_const, t_uintptr); + + if (is_power_of_two(len)) { + u64 log2_len = floor_log2(cast(u64)len); + cell_index = log2_len == 0 ? index : lb_emit_arith(p, Token_Shr, index, lb_const_int(p->module, t_uintptr, log2_len), t_uintptr); + data_index = lb_emit_arith(p, Token_And, index, lb_const_int(p->module, t_uintptr, len-1), t_uintptr); + } else { + cell_index = lb_emit_arith(p, Token_Quo, index, len_const, t_uintptr); + data_index = lb_emit_arith(p, Token_Mod, index, len_const, t_uintptr); + } lbValue elems_ptr = lb_emit_conv(p, cells_ptr, t_uintptr); lbValue cell_offset = lb_emit_arith(p, Token_Mul, size_const, cell_index, t_uintptr); |