2021-08-31 18:21:00 +02:00
|
|
|
module big
|
|
|
|
|
2022-01-05 17:02:20 +01:00
|
|
|
import math
|
2021-08-31 18:21:00 +02:00
|
|
|
|
|
|
|
// Compares the magnitude of the two unsigned integers represented the given
|
|
|
|
// digit arrays. Returns -1 if a < b, 0 if a == b and +1 if a > b. Here
|
|
|
|
// a is operand_a and b is operand_b (for brevity).
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn compare_digit_array(operand_a []u32, operand_b []u32) int {
|
|
|
|
a_len := operand_a.len
|
|
|
|
b_len := operand_b.len
|
|
|
|
if a_len != b_len {
|
|
|
|
return if a_len < b_len { -1 } else { 1 }
|
|
|
|
}
|
|
|
|
// They have the same number of digits now
|
|
|
|
// Go from the most significant digit to the least significant one
|
|
|
|
for index := a_len - 1; index >= 0; index-- {
|
|
|
|
a_digit := operand_a[index]
|
|
|
|
b_digit := operand_b[index]
|
|
|
|
if a_digit != b_digit {
|
|
|
|
return if a_digit < b_digit { -1 } else { 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the digits in operand_a and operand_b and stores the result in sum.
|
|
|
|
// This function does not perform any allocation and assumes that the storage is
|
|
|
|
// large enough. It may affect the last element, based on the presence of a carry
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn add_digit_array(operand_a []u32, operand_b []u32, mut sum []u32) {
|
|
|
|
// Zero length cases
|
|
|
|
if operand_a.len == 0 {
|
|
|
|
for index in 0 .. operand_b.len {
|
|
|
|
sum[index] = operand_b[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if operand_b.len == 0 {
|
|
|
|
for index in 0 .. operand_a.len {
|
|
|
|
sum[index] = operand_a[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// First pass intersects with both operands
|
2022-01-05 17:02:20 +01:00
|
|
|
smaller_limit := math.min(operand_a.len, operand_b.len)
|
|
|
|
larger_limit := math.max(operand_a.len, operand_b.len)
|
2021-08-31 18:21:00 +02:00
|
|
|
mut a, mut b := if operand_a.len >= operand_b.len {
|
|
|
|
operand_a, operand_b
|
|
|
|
} else {
|
|
|
|
operand_b, operand_a
|
|
|
|
}
|
|
|
|
mut carry := u64(0)
|
|
|
|
for index in 0 .. smaller_limit {
|
|
|
|
partial := carry + a[index] + b[index]
|
|
|
|
sum[index] = u32(partial)
|
|
|
|
carry = u32(partial >> 32)
|
|
|
|
}
|
|
|
|
|
|
|
|
for index in smaller_limit .. larger_limit {
|
|
|
|
partial := carry + a[index]
|
|
|
|
sum[index] = u32(partial)
|
|
|
|
carry = u32(partial >> 32)
|
|
|
|
}
|
|
|
|
|
|
|
|
if carry == 0 {
|
|
|
|
sum.delete_last()
|
|
|
|
} else {
|
|
|
|
sum[larger_limit] = u32(carry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subtracts operand_b from operand_a and stores the difference in storage.
|
|
|
|
// It assumes operand_a contains the larger "integer" and that storage is
|
2021-09-09 13:56:20 +02:00
|
|
|
// the same size as operand_a and is 0
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn subtract_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
|
|
|
// Zero length cases
|
|
|
|
if operand_a.len == 0 {
|
|
|
|
// nothing to subtract from
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if operand_b.len == 0 {
|
|
|
|
// nothing to subtract
|
|
|
|
for index in 0 .. operand_a.len {
|
|
|
|
storage[index] = operand_a[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mut carry := false
|
|
|
|
for index in 0 .. operand_b.len {
|
|
|
|
mut a_digit := u64(operand_a[index])
|
|
|
|
b_digit := operand_b[index] + if carry { u64(1) } else { u64(0) }
|
|
|
|
carry = a_digit < b_digit
|
|
|
|
if carry {
|
|
|
|
a_digit += 0x100000000
|
|
|
|
}
|
|
|
|
storage[index] = u32(a_digit - b_digit)
|
|
|
|
}
|
|
|
|
|
|
|
|
for index in operand_b.len .. operand_a.len {
|
|
|
|
mut a_digit := u64(operand_a[index])
|
|
|
|
b_digit := if carry { u64(1) } else { u64(0) }
|
|
|
|
carry = a_digit < b_digit
|
|
|
|
if carry {
|
|
|
|
a_digit += 0x100000000
|
|
|
|
}
|
|
|
|
storage[index] = u32(a_digit - b_digit)
|
|
|
|
}
|
|
|
|
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 18:31:07 +02:00
|
|
|
const karatsuba_multiplication_limit = 1_000_000
|
|
|
|
|
|
|
|
// set limit to choose algorithm
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn multiply_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
|
|
|
if operand_a.len >= big.karatsuba_multiplication_limit
|
|
|
|
|| operand_b.len >= big.karatsuba_multiplication_limit {
|
|
|
|
karatsuba_multiply_digit_array(operand_a, operand_b, mut storage)
|
|
|
|
} else {
|
|
|
|
simple_multiply_digit_array(operand_a, operand_b, mut storage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 18:21:00 +02:00
|
|
|
// Multiplies the unsigned (non-negative) integers represented in a and b and the product is
|
|
|
|
// stored in storage. It assumes that storage has length equal to the sum of lengths
|
|
|
|
// of a and b. Length refers to length of array, that is, digit count.
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-09-16 18:31:07 +02:00
|
|
|
fn simple_multiply_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
2021-08-31 18:21:00 +02:00
|
|
|
for b_index in 0 .. operand_b.len {
|
|
|
|
mut carry := u64(0)
|
|
|
|
for a_index in 0 .. operand_a.len {
|
|
|
|
partial_product := u64(storage[a_index + b_index]) + carry +
|
|
|
|
u64(operand_a[a_index]) * u64(operand_b[b_index])
|
|
|
|
storage[a_index + b_index] = u32(partial_product)
|
|
|
|
carry = partial_product >> 32
|
|
|
|
}
|
|
|
|
if carry != 0 {
|
|
|
|
storage[b_index + operand_a.len] = u32(carry)
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stores the product of the unsigned (non-negative) integer represented in a and the digit in value
|
|
|
|
// in the storage array. It assumes storage is pre-initialised and populated with 0's
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn multiply_array_by_digit(operand_a []u32, value u32, mut storage []u32) {
|
|
|
|
if value == 0 {
|
2022-05-13 07:21:34 +02:00
|
|
|
storage.clear()
|
2021-08-31 18:21:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if value == 1 {
|
|
|
|
for index in 0 .. operand_a.len {
|
|
|
|
storage[index] = operand_a[index]
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
mut carry := u32(0)
|
|
|
|
for index in 0 .. operand_a.len {
|
|
|
|
product := u64(operand_a[index]) * value + carry
|
|
|
|
storage[index] = u32(product)
|
|
|
|
carry = u32(product >> 32)
|
|
|
|
}
|
|
|
|
if carry > 0 {
|
|
|
|
if storage.last() == 0 {
|
|
|
|
storage[operand_a.len] = carry
|
|
|
|
} else {
|
|
|
|
storage << carry
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Divides the non-negative integer in a by non-negative integer b and store the two results
|
|
|
|
// in quotient and remainder respectively. It is different from the rest of the functions
|
|
|
|
// because it assumes that quotient and remainder are empty zero length arrays. They can be
|
|
|
|
// made to have appropriate capacity though
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn divide_digit_array(operand_a []u32, operand_b []u32, mut quotient []u32, mut remainder []u32) {
|
|
|
|
cmp_result := compare_digit_array(operand_a, operand_b)
|
|
|
|
// a == b => q, r = 1, 0
|
|
|
|
if cmp_result == 0 {
|
|
|
|
quotient << 1
|
|
|
|
for quotient.len > 1 {
|
|
|
|
quotient.delete_last()
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
remainder.clear()
|
2021-08-31 18:21:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// a < b => q, r = 0, a
|
|
|
|
if cmp_result < 0 {
|
2022-05-13 07:21:34 +02:00
|
|
|
quotient.clear()
|
2021-08-31 18:21:00 +02:00
|
|
|
for index in 0 .. operand_a.len {
|
|
|
|
remainder << operand_a[index]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if operand_b.len == 1 {
|
|
|
|
divide_array_by_digit(operand_a, operand_b[0], mut quotient, mut remainder)
|
|
|
|
} else {
|
|
|
|
divide_array_by_array(operand_a, operand_b, mut quotient, mut remainder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Performs division on the non-negative dividend in a by the single digit divisor b. It assumes
|
2021-09-08 13:16:35 +02:00
|
|
|
// quotient and remainder are empty zero length arrays without previous allocation
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn divide_array_by_digit(operand_a []u32, divisor u32, mut quotient []u32, mut remainder []u32) {
|
|
|
|
if operand_a.len == 1 {
|
|
|
|
// 1 digit for both dividend and divisor
|
|
|
|
dividend := operand_a[0]
|
|
|
|
q := dividend / divisor
|
|
|
|
if q != 0 {
|
|
|
|
quotient << q
|
|
|
|
}
|
|
|
|
rem := dividend % divisor
|
|
|
|
if rem != 0 {
|
|
|
|
remainder << rem
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Dividend has more digits
|
|
|
|
mut rem := u64(0)
|
|
|
|
divisor64 := u64(divisor)
|
|
|
|
// Pad quotient to contain sufficient space
|
|
|
|
for _ in 0 .. operand_a.len {
|
|
|
|
quotient << 0
|
|
|
|
}
|
|
|
|
// Perform division step by step
|
|
|
|
for index := operand_a.len - 1; index >= 0; index-- {
|
|
|
|
dividend := (rem << 32) + operand_a[index]
|
|
|
|
quotient[index] = u32(dividend / divisor64)
|
|
|
|
rem = dividend % divisor64
|
|
|
|
}
|
|
|
|
// Remove leading zeros from quotient
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut quotient)
|
2021-08-31 18:21:00 +02:00
|
|
|
remainder << u32(rem)
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut remainder)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 18:31:07 +02:00
|
|
|
const newton_division_limit = 10_000
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn divide_array_by_array(operand_a []u32, operand_b []u32, mut quotient []u32, mut remainder []u32) {
|
|
|
|
if operand_a.len >= big.newton_division_limit {
|
|
|
|
newton_divide_array_by_array(operand_a, operand_b, mut quotient, mut remainder)
|
|
|
|
} else {
|
|
|
|
binary_divide_array_by_array(operand_a, operand_b, mut quotient, mut remainder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 18:21:00 +02:00
|
|
|
// Shifts the contents of the original array by the given amount of bits to the left.
|
|
|
|
// This function assumes that the amount is less than 32. The storage is expected to
|
|
|
|
// allocated with zeroes.
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn shift_digits_left(original []u32, amount u32, mut storage []u32) {
|
|
|
|
mut leftover := u32(0)
|
|
|
|
offset := 32 - amount
|
|
|
|
for index in 0 .. original.len {
|
|
|
|
value := leftover | (original[index] << amount)
|
|
|
|
leftover = (original[index] & (u32(-1) << offset)) >> offset
|
|
|
|
storage[index] = value
|
|
|
|
}
|
|
|
|
if leftover != 0 {
|
|
|
|
storage << leftover
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shifts the contents of the original array by the given amount of bits to the right.
|
|
|
|
// This function assumes that the amount is less than 32. The storage is expected to
|
|
|
|
// be allocated with zeroes.
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn shift_digits_right(original []u32, amount u32, mut storage []u32) {
|
|
|
|
mut moveover := u32(0)
|
|
|
|
mask := (u32(1) << amount) - 1
|
|
|
|
offset := 32 - amount
|
|
|
|
for index := original.len - 1; index >= 0; index-- {
|
|
|
|
value := (moveover << offset) | (original[index] >> amount)
|
|
|
|
moveover = original[index] & mask
|
|
|
|
storage[index] = value
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn bitwise_or_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
|
|
|
lower, upper, bigger := if operand_a.len < operand_b.len {
|
|
|
|
operand_a.len, operand_b.len, operand_b
|
|
|
|
} else {
|
|
|
|
operand_b.len, operand_a.len, operand_a
|
|
|
|
}
|
|
|
|
for index in 0 .. lower {
|
|
|
|
storage[index] = operand_a[index] | operand_b[index]
|
|
|
|
}
|
|
|
|
for index in lower .. upper {
|
|
|
|
storage[index] = bigger[index]
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn bitwise_and_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
2022-01-05 17:02:20 +01:00
|
|
|
lower := math.min(operand_a.len, operand_b.len)
|
2021-08-31 18:21:00 +02:00
|
|
|
for index in 0 .. lower {
|
|
|
|
storage[index] = operand_a[index] & operand_b[index]
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn bitwise_xor_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
|
|
|
lower, upper, bigger := if operand_a.len < operand_b.len {
|
|
|
|
operand_a.len, operand_b.len, operand_b
|
|
|
|
} else {
|
|
|
|
operand_b.len, operand_a.len, operand_a
|
|
|
|
}
|
|
|
|
for index in 0 .. lower {
|
|
|
|
storage[index] = operand_a[index] ^ operand_b[index]
|
|
|
|
}
|
|
|
|
for index in lower .. upper {
|
|
|
|
storage[index] = bigger[index]
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 07:21:34 +02:00
|
|
|
[direct_array_access]
|
2021-08-31 18:21:00 +02:00
|
|
|
fn bitwise_not_digit_array(original []u32, mut storage []u32) {
|
|
|
|
for index in 0 .. original.len {
|
|
|
|
storage[index] = ~original[index]
|
|
|
|
}
|
2022-05-13 07:21:34 +02:00
|
|
|
shrink_tail_zeros(mut storage)
|
2021-08-31 18:21:00 +02:00
|
|
|
}
|