Support -disallow-128-bit for math/big and math/rand

This commit is contained in:
gingerBill
2024-11-15 13:02:29 +00:00
parent c2d469954a
commit 15acbc933a
5 changed files with 21 additions and 4 deletions

View File

@@ -106,7 +106,7 @@ MAX_ITERATIONS_RANDOM_PRIME := 1_000_000
2) Optimizations thanks to precomputed masks wouldn't work.
*/
MATH_BIG_FORCE_64_BIT :: #config(MATH_BIG_FORCE_64_BIT, false)
MATH_BIG_FORCE_32_BIT :: #config(MATH_BIG_FORCE_32_BIT, false)
MATH_BIG_FORCE_32_BIT :: #config(MATH_BIG_FORCE_32_BIT, !ODIN_ALLOW_128_BIT)
when (MATH_BIG_FORCE_32_BIT && MATH_BIG_FORCE_64_BIT) { #panic("Cannot force 32-bit and 64-bit big backend simultaneously.") }
/*
@@ -209,7 +209,7 @@ Primality_Flags :: bit_set[Primality_Flag; u8]
- Must be large enough such that `init_integer` can store `u128` in the `Int` without growing.
*/
_MIN_DIGIT_COUNT :: max(3, ((size_of(u128) + _DIGIT_BITS) - 1) / _DIGIT_BITS)
_MIN_DIGIT_COUNT :: max(3, ((16 + _DIGIT_BITS) - 1) / _DIGIT_BITS)
#assert(_DEFAULT_DIGIT_COUNT >= _MIN_DIGIT_COUNT)
/*

View File

@@ -253,6 +253,7 @@ power_of_two :: proc(a: ^Int, power: int, allocator := context.allocator) -> (er
return #force_inline internal_int_power_of_two(a, power, allocator)
}
when ODIN_ALLOW_128_BIT {
int_get_u128 :: proc(a: ^Int, allocator := context.allocator) -> (res: u128, err: Error) {
/*
Check that `a` is usable.
@@ -270,6 +271,7 @@ int_get_i128 :: proc(a: ^Int, allocator := context.allocator) -> (res: i128, err
return int_get(a, i128, allocator)
}
get_i128 :: proc { int_get_i128, }
}
int_get_u64 :: proc(a: ^Int, allocator := context.allocator) -> (res: u64, err: Error) {
/*

View File

@@ -2282,6 +2282,7 @@ internal_int_power_of_two :: proc(a: ^Int, power: int, allocator := context.allo
return nil
}
when ODIN_ALLOW_128_BIT {
internal_int_get_u128 :: proc(a: ^Int) -> (res: u128, err: Error) {
return internal_int_get(a, u128)
}
@@ -2291,6 +2292,7 @@ internal_int_get_i128 :: proc(a: ^Int) -> (res: i128, err: Error) {
return internal_int_get(a, i128)
}
internal_get_i128 :: proc { internal_int_get_i128, }
}
internal_int_get_u64 :: proc(a: ^Int) -> (res: u64, err: Error) {
return internal_int_get(a, u64)

View File

@@ -363,8 +363,14 @@ radix_size :: proc(a: ^Int, radix: i8, zero_terminate := false, allocator := con
/* The "+1" here is the "+1" in "floor((la * k) / 2^29) + 1" */
/* n = n + 1 + EOS + sign */
size_, _ := internal_get(k, u128)
size = int(size_)
when ODIN_ALLOW_128_BIT {
size_, _ := internal_get(k, u128)
size = int(size_)
} else {
size_, _ := internal_get(k, u64)
size = int(size_)
}
}
/*

View File

@@ -133,6 +133,8 @@ Possible Output:
@(require_results)
uint64 :: proc(gen := context.random_generator) -> (val: u64) { return _random_u64(gen) }
when ODIN_ALLOW_128_BIT {
/*
Generates a random 128 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
@@ -159,6 +161,7 @@ uint128 :: proc(gen := context.random_generator) -> (val: u128) {
b := u128(_random_u64(gen))
return (a<<64) | b
}
}
/*
Generates a random 31 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
@@ -206,6 +209,7 @@ Possible Output:
*/
@(require_results) int63 :: proc(gen := context.random_generator) -> (val: i64) { return i64(uint64(gen) << 1 >> 1) }
when ODIN_ALLOW_128_BIT {
/*
Generates a random 127 bit value using the provided random number generator. If no generator is provided the global random number generator will be used.
The sign bit will always be set to 0, thus all generated numbers will be positive.
@@ -228,6 +232,7 @@ Possible Output:
*/
@(require_results) int127 :: proc(gen := context.random_generator) -> (val: i128) { return i128(uint128(gen) << 1 >> 1) }
}
/*
Generates a random 31 bit value in the range `[0, n)` using the provided random number generator. If no generator is provided the global random number generator will be used.
@@ -311,6 +316,7 @@ int63_max :: proc(n: i64, gen := context.random_generator) -> (val: i64) {
return v % n
}
when ODIN_ALLOW_128_BIT {
/*
Generates a random 127 bit value in the range `[0, n)` using the provided random number generator. If no generator is provided the global random number generator will be used.
@@ -351,6 +357,7 @@ int127_max :: proc(n: i128, gen := context.random_generator) -> (val: i128) {
}
return v % n
}
}
/*
Generates a random integer value in the range `[0, n)` using the provided random number generator. If no generator is provided the global random number generator will be used.