Skip to content

Commit b3073b9

Browse files
authored
math.big: optimize subtract_digit_array() (#25142)
1 parent b502d52 commit b3073b9

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

‎vlib/math/big/array_ops.v‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,21 @@ fn subtract_digit_array(operand_a []u64, operand_b []u64, mut storage []u64) {
8282
for index in 0 .. operand_a.len {
8383
storage[index] = operand_a[index]
8484
}
85+
return
8586
}
8687

87-
mut carry := false
88+
mut borrow := u64(0)
8889
for index in 0 .. operand_b.len {
89-
mut a_digit := operand_a[index]
90-
b_digit := operand_b[index] + if carry { u64(1) } else { u64(0) }
91-
carry = a_digit < b_digit
92-
if carry {
93-
a_digit = a_digit | (u64(1) << digit_bits)
94-
}
95-
storage[index] = a_digit - b_digit
90+
a := operand_a[index]
91+
b := operand_b[index] + borrow
92+
diff := a - b
93+
borrow = (diff >> digit_bits) & 1
94+
storage[index] = diff + (borrow << digit_bits)
9695
}
97-
9896
for index in operand_b.len .. operand_a.len {
99-
mut a_digit := operand_a[index]
100-
b_digit := if carry { u64(1) } else { u64(0) }
101-
carry = a_digit < b_digit
102-
if carry {
103-
a_digit = a_digit | (u64(1) << digit_bits)
104-
}
105-
storage[index] = a_digit - b_digit
97+
diff := operand_a[index] - borrow
98+
borrow = (diff >> digit_bits) & 1
99+
storage[index] = diff + (borrow << digit_bits)
106100
}
107101

108102
shrink_tail_zeros(mut storage)

0 commit comments

Comments
 (0)