ref: cd97ca1d0d636c4c4b4bb3ff9e687e858551a889
parent: bf9480715e20b52f96625603cca8b0b3abbd6317
author: Ori Bernstein <[email protected]>
date: Mon Sep 15 20:32:40 EDT 2014
Clean up bigint code a bit.
--- a/libstd/bigint.myr
+++ b/libstd/bigint.myr
@@ -112,9 +112,10 @@
or
2 + a.dig.len * 32/3.32...
or
- 2 + a.dig.len * 11
+ 2 + a.dig.len * 10
+ plus one for the - sign.
*/
- buf = slalloc(2 + a.dig.len * 13)
+ buf = slalloc(3 + a.dig.len * 10)
n = bigbfmt(buf, a, base)
-> buf[:n]
}
@@ -254,25 +255,24 @@
-> `Before
elif a.sign > b.sign
-> `After
- else
- /* the one with more digits has greater magnitude */
- if a.dig.len > b.dig.len
+ elif a.dig.len > b.dig.len
+ -> signedorder(a.sign)
+ elif a.dig.len < b.dig.len
+ -> signedorder(-a.sign)
+ /* otherwise, the one with the first larger digit is bigger */
+ for i = a.dig.len; i > 0; i--
+ if a.dig[i - 1] > b.dig[i - 1]
-> signedorder(a.sign)
+ elif b.dig[i - 1] > a.dig[i - 1]
+ -> signedorder(a.sign)
;;
- /* otherwise, the one with the first larger digit is bigger */
- for i = a.dig.len; i > 0; i--
- if a.dig[i - 1] > b.dig[i - 1]
- -> signedorder(a.sign)
- elif b.dig[i - 1] > a.dig[i - 1]
- -> signedorder(a.sign)
- ;;
- ;;
;;
+ ;;
-> `Equal
}
const signedorder = {sign
- if sign < 0
+ if sign == -1
-> `Before
else
-> `After
@@ -468,7 +468,8 @@
q.dig[j - 1] = (((carry << 32) + aj)/b0) castto(uint32)
carry = (carry << 32) + aj - (q.dig[j-1] castto(uint64))*b0
;;
- -> (trim(q), trim(mkbigint(carry castto(int32))))
+ q = trim(q)
+ -> (q, trim(mkbigint(carry castto(int32))))
;;
u = bigdup(a)
@@ -683,12 +684,15 @@
/* creates a bigint on the stack; should not be modified. */
const bigdigit = {v, isneg : bool, val : uint64, dig
+ v.sign = 1
if isneg
val = -val
+ v.sign = -1
;;
if val == 0
+ v.sign = 0
v.dig = [][:]
- elif val > Base
+ elif val < Base
v.dig = dig[:1]
v.dig[0] = val castto(uint32)
else