mirror of
https://github.com/odin-lang/Odin.git
synced 2025-12-29 17:34:34 +00:00
big: Add ilog2.
This commit is contained in:
@@ -213,16 +213,6 @@ int_to_byte_little :: proc(v: ^Int) {
|
||||
demo :: proc() {
|
||||
a, b, c, d, e, f, res := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{};
|
||||
defer destroy(a, b, c, d, e, f, res);
|
||||
|
||||
set(a, 42);
|
||||
set(b, 6);
|
||||
set(c, 131);
|
||||
|
||||
if err := internal_int_exponent_mod(res, a, b, c); err != nil {
|
||||
fmt.printf("Error: %v\n", err);
|
||||
}
|
||||
|
||||
print("res: ", res);
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
|
||||
@@ -1437,7 +1437,7 @@ _private_int_factorial_binary_split :: proc(res: ^Int, n: int, allocator := cont
|
||||
internal_one(inner, false) or_return;
|
||||
internal_one(outer, false) or_return;
|
||||
|
||||
bits_used := int(_DIGIT_TYPE_BITS - intrinsics.count_leading_zeros(n));
|
||||
bits_used := ilog2(n);
|
||||
|
||||
for i := bits_used; i >= 0; i -= 1 {
|
||||
start := (n >> (uint(i) + 1)) + 1 | 1;
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
*/
|
||||
package math_big
|
||||
|
||||
import "core:intrinsics"
|
||||
|
||||
/*
|
||||
===========================
|
||||
User-level routines
|
||||
@@ -383,6 +385,10 @@ digit_log :: proc(a: DIGIT, base: DIGIT) -> (log: int, err: Error) {
|
||||
}
|
||||
log :: proc { int_log, digit_log, };
|
||||
|
||||
ilog2 :: proc(value: $T) -> (log2: T) {
|
||||
return (size_of(T) * 8) - intrinsics.count_leading_zeros(value);
|
||||
}
|
||||
|
||||
/*
|
||||
Calculate `dest = base^power` using a square-multiply algorithm.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user