ref: beec45c577268917e3eee0454876b356c6f7249c
parent: 61903e15585a167f4d5f27b1d0ec812503e821e6
author: Ori Bernstein <[email protected]>
date: Thu Jan 23 06:14:26 EST 2014
Use correct shift in bigdiv. Test it.
--- a/libstd/bigint.myr
+++ b/libstd/bigint.myr
@@ -50,6 +50,8 @@
const bigshri : (a : bigint#, b : uint64 -> bigint#)
;;
+const Base = 0x100000000ul
+
const mkbigint = {v
var a
a = zalloc()
@@ -398,7 +400,7 @@
m = u.dig.len
n = v.dig.len
- shift = nlz(v.dig[n - 1])
+ shift = 32-nlz(v.dig[n - 1])
bigshli(u, shift)
bigshli(v, shift)
for j = m - n; j >= 0; j--
@@ -412,10 +414,10 @@
qhat = (x + y)/z
rhat = (x + y) - (qhat * z)
:divagain
- if qhat > 0xfffffffful || qhat * w > ((rhat << 32) + w)
+ if qhat >= Base || (qhat * w) > ((rhat << 32) + w)
qhat--
rhat += z
- if rhat <= 0xfffffffful
+ if rhat < Base
goto divagain
;;
;;
--- a/test/bigint.myr
+++ b/test/bigint.myr
@@ -23,4 +23,23 @@
n = std.bigfmt(buf[:], a)
std.put("%s\n", buf[:n])
+
+ match std.bigparse("1234_5678_1234_6789_6666_7777_8888")
+ | `std.Some val: a = val
+ | `std.None: std.die("Failed to parse a\n")
+ ;;
+ match std.bigparse("1234_5678_1234_6789_6666_7777")
+ | `std.Some val: b = val
+ | `std.None: std.die("Failed to parse b\n")
+ ;;
+
+ n = std.bigfmt(buf[:], a)
+ std.put("%s\n", buf[:n])
+ n = std.bigfmt(buf[:], b)
+ std.put("%s\n", buf[:n])
+
+ std.bigdiv(a, b)
+
+ n = std.bigfmt(buf[:], a)
+ std.put("%s\n", buf[:n])
}