From 4cbcb3ace7c0d066e8d5105e4b3582300f93d533 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 10 Aug 2025 17:53:07 +0100 Subject: [PATCH] Add shortcut for `unsigned_x/power_of_two` -> `unsigned_x >> log2(power_of_two)` --- src/common.cpp | 7 +++++++ src/llvm_backend_expr.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/common.cpp b/src/common.cpp index 53848cacf..5b007bf2c 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -80,6 +80,13 @@ gb_internal gb_inline bool is_power_of_two(i64 x) { 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) { if (x < y) { return -1; diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 13c6082fb..f922c6359 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -1153,6 +1153,17 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L return zero; } } 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, ""); } }