aboutsummaryrefslogtreecommitdiff
path: root/src/big_int.cpp
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2025-01-06 13:43:01 +0000
committerGitHub <noreply@github.com>2025-01-06 13:43:01 +0000
commit6e49bbb66853b5d824ac5bbd534ae3e81c4f39aa (patch)
tree50886a3be8f2fcfab053e07cfe9e15f50fa5f9f6 /src/big_int.cpp
parentbd96cd0af761994210018ca647eb843dfeb71494 (diff)
parent98efb03934b464a1b23759b5695a12ff37588357 (diff)
Merge branch 'master' into d3d11-annotations
Diffstat (limited to 'src/big_int.cpp')
-rw-r--r--src/big_int.cpp21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/big_int.cpp b/src/big_int.cpp
index e350687b4..8e476f090 100644
--- a/src/big_int.cpp
+++ b/src/big_int.cpp
@@ -62,6 +62,7 @@ gb_internal void big_int_shl (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_shr (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_mul (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_mul_u64(BigInt *dst, BigInt const *x, u64 y);
+gb_internal void big_int_exp_u64(BigInt *dst, BigInt const *x, u64 y, bool *success);
gb_internal void big_int_quo_rem(BigInt const *x, BigInt const *y, BigInt *q, BigInt *r);
gb_internal void big_int_quo (BigInt *z, BigInt const *x, BigInt const *y);
@@ -250,9 +251,7 @@ gb_internal void big_int_from_string(BigInt *dst, String const &s, bool *success
exp *= 10;
exp += v;
}
- for (u64 x = 0; x < exp; x++) {
- big_int_mul_eq(dst, &b);
- }
+ big_int_exp_u64(dst, &b, exp, success);
}
if (is_negative) {
@@ -328,6 +327,18 @@ gb_internal void big_int_mul_u64(BigInt *dst, BigInt const *x, u64 y) {
big_int_dealloc(&d);
}
+gb_internal void big_int_exp_u64(BigInt *dst, BigInt const *x, u64 y, bool *success) {
+ if (y > INT_MAX) {
+ *success = false;
+ return;
+ }
+
+ // Note: The cutoff for square-multiply being faster than the naive
+ // for loop is when exp > 4, but it probably isn't worth adding
+ // a fast path.
+ mp_err err = mp_expt_n(x, int(y), dst);
+ *success = err == MP_OKAY;
+}
gb_internal void big_int_mul(BigInt *dst, BigInt const *x, BigInt const *y) {
mp_mul(x, y, dst);
@@ -621,3 +632,7 @@ gb_internal String big_int_to_string(gbAllocator allocator, BigInt const *x, u64
}
return make_string(cast(u8 *)buf.data, buf.count);
}
+
+gb_internal int big_int_log2(BigInt const *x) {
+ return mp_count_bits(x) - 1;
+}