1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

math.big: remove import math dependency (math.big only uses min/max/abs)

This commit is contained in:
Delyan Angelov 2022-11-29 14:21:16 +02:00
parent 6ef3dfdea9
commit 952f4644c5
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 43 additions and 23 deletions

View File

@ -1,7 +1,5 @@
module big
import math
// 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).
@ -42,8 +40,8 @@ fn add_digit_array(operand_a []u32, operand_b []u32, mut sum []u32) {
}
// First pass intersects with both operands
smaller_limit := math.min(operand_a.len, operand_b.len)
larger_limit := math.max(operand_a.len, operand_b.len)
smaller_limit := imin(operand_a.len, operand_b.len)
larger_limit := imax(operand_a.len, operand_b.len)
mut a, mut b := if operand_a.len >= operand_b.len {
operand_a, operand_b
} else {
@ -311,7 +309,7 @@ fn bitwise_or_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
[direct_array_access]
fn bitwise_and_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
lower := math.min(operand_a.len, operand_b.len)
lower := imin(operand_a.len, operand_b.len)
for index in 0 .. lower {
storage[index] = operand_a[index] & operand_b[index]
}

View File

@ -1,13 +1,10 @@
module big
import math
import math.bits
import strings
import strconv
const (
digit_array = '0123456789abcdefghijklmnopqrstuvwxyz'.bytes()
)
const digit_array = '0123456789abcdefghijklmnopqrstuvwxyz'.bytes()
// big.Integer
// -----------
@ -55,7 +52,7 @@ pub fn integer_from_int(value int) Integer {
return zero_int
}
return Integer{
digits: [u32(math.abs(value))]
digits: [u32(iabs(value))]
signum: int_signum(value)
}
}
@ -327,7 +324,7 @@ pub fn (integer Integer) - (subtrahend Integer) Integer {
fn (integer Integer) add(addend Integer) Integer {
a := integer.digits
b := addend.digits
mut storage := []u32{len: math.max(a.len, b.len) + 1}
mut storage := []u32{len: imax(a.len, b.len) + 1}
add_digit_array(a, b, mut storage)
return Integer{
signum: integer.signum
@ -585,7 +582,7 @@ pub fn (mut a Integer) set_bit(i u32, value bool) {
pub fn (a Integer) bitwise_or(b Integer) Integer {
check_sign(a)
check_sign(b)
mut result := []u32{len: math.max(a.digits.len, b.digits.len)}
mut result := []u32{len: imax(a.digits.len, b.digits.len)}
bitwise_or_digit_array(a.digits, b.digits, mut result)
return Integer{
digits: result
@ -597,7 +594,7 @@ pub fn (a Integer) bitwise_or(b Integer) Integer {
pub fn (a Integer) bitwise_and(b Integer) Integer {
check_sign(a)
check_sign(b)
mut result := []u32{len: math.max(a.digits.len, b.digits.len)}
mut result := []u32{len: imax(a.digits.len, b.digits.len)}
bitwise_and_digit_array(a.digits, b.digits, mut result)
return Integer{
digits: result
@ -620,7 +617,7 @@ pub fn (a Integer) bitwise_not() Integer {
pub fn (a Integer) bitwise_xor(b Integer) Integer {
check_sign(a)
check_sign(b)
mut result := []u32{len: math.max(a.digits.len, b.digits.len)}
mut result := []u32{len: imax(a.digits.len, b.digits.len)}
bitwise_xor_digit_array(a.digits, b.digits, mut result)
return Integer{
digits: result
@ -941,7 +938,7 @@ fn gcd_binary(x Integer, y Integer) Integer {
mut az := a.msb()
bz := b.msb()
shift := math.min(az, bz)
shift := umin(az, bz)
b = b.rshift(bz)
for a.signum != 0 {

26
vlib/math/big/min_max.v Normal file
View File

@ -0,0 +1,26 @@
module big
[inline]
fn imax(a int, b int) int {
return if a > b { a } else { b }
}
[inline]
fn imin(a int, b int) int {
return if a < b { a } else { b }
}
[inline]
fn umax(a u32, b u32) u32 {
return if a > b { a } else { b }
}
[inline]
fn umin(a u32, b u32) u32 {
return if a < b { a } else { b }
}
[inline]
fn iabs(v int) int {
return if v > 0 { v } else { -v }
}

View File

@ -1,6 +1,5 @@
module big
import math
import math.bits
import strings
@ -113,7 +112,7 @@ fn karatsuba_multiply_digit_array(operand_a []u32, operand_b []u32, mut storage
}
// thanks to the base cases we can pass zero-length arrays to the mult func
half := math.max(operand_a.len, operand_b.len) / 2
half := imax(operand_a.len, operand_b.len) / 2
a_l := operand_a[0..half]
a_h := operand_a[half..]
mut b_l := []u32{}
@ -132,8 +131,8 @@ fn karatsuba_multiply_digit_array(operand_a []u32, operand_b []u32, mut storage
mut p_3 := []u32{len: a_l.len + b_l.len + 1}
multiply_digit_array(a_l, b_l, mut p_3)
mut tmp_1 := []u32{len: math.max(a_h.len, a_l.len) + 1}
mut tmp_2 := []u32{len: math.max(b_h.len, b_l.len) + 1}
mut tmp_1 := []u32{len: imax(a_h.len, a_l.len) + 1}
mut tmp_2 := []u32{len: imax(b_h.len, b_l.len) + 1}
add_digit_array(a_h, a_l, mut tmp_1)
add_digit_array(b_h, b_l, mut tmp_2)
@ -288,8 +287,8 @@ fn rshift_digits_in_place(mut a []u32, amount int) {
fn add_in_place(mut a []u32, b []u32) {
len_a := a.len
len_b := b.len
max := math.max(len_a, len_b)
min := math.min(len_a, len_b)
max := imax(len_a, len_b)
min := imin(len_a, len_b)
mut carry := u64(0)
for index in 0 .. min {
partial := carry + a[index] + b[index]
@ -316,8 +315,8 @@ fn add_in_place(mut a []u32, b []u32) {
fn subtract_in_place(mut a []u32, b []u32) {
len_a := a.len
len_b := b.len
max := math.max(len_a, len_b)
min := math.min(len_a, len_b)
max := imax(len_a, len_b)
min := imin(len_a, len_b)
mut carry := u32(0)
mut new_carry := u32(0)
for index in 0 .. min {