mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 23:28:48 +00:00
Add shortcut for unsigned_x/power_of_two -> unsigned_x >> log2(power_of_two)
This commit is contained in:
@@ -80,6 +80,13 @@ gb_internal gb_inline bool is_power_of_two(i64 x) {
|
|||||||
return !(x & (x-1));
|
return !(x & (x-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gb_internal gb_inline bool is_power_of_two_u64(u64 x) {
|
||||||
|
if (x == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !(x & (x-1));
|
||||||
|
}
|
||||||
|
|
||||||
gb_internal int isize_cmp(isize x, isize y) {
|
gb_internal int isize_cmp(isize x, isize y) {
|
||||||
if (x < y) {
|
if (x < y) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -1153,6 +1153,17 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L
|
|||||||
return zero;
|
return zero;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (!is_signed && lb_sizeof(type) <= 8) {
|
||||||
|
u64 v = cast(u64)LLVMConstIntGetZExtValue(rhs);
|
||||||
|
if (v == 1) {
|
||||||
|
return lhs;
|
||||||
|
} else if (is_power_of_two_u64(v)) {
|
||||||
|
u64 n = floor_log2(v);
|
||||||
|
LLVMValueRef bits = LLVMConstInt(type, n, false);
|
||||||
|
return LLVMBuildLShr(p->builder, lhs, bits, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return call(p->builder, lhs, rhs, "");
|
return call(p->builder, lhs, rhs, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user