|
@@ -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);
|