raz / commit e8ec426b

commit
e8ec426beeb0c090053e4007d70600e6e7de1c63
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0c519645
Math.divMod: 128 bits divided by 64, by one div instruction; hi >= d stops with a message

patch · log

6 files changed, +67 -1

doc/raz-lib.md (history) modified +1 -0

@@ -838,6 +838,7 @@
838838 proc bits*(x: F64): U64; (* the bits of x, as IEEE 754 lays them out *)
839839 proc fromBits*(u: U64): F64; (* the F64 whose bits are u *)
840840 proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *)
841+proc divMod*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi < d *)
841842 proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *)
842843 ```
843844

lib/Math.raz (history) modified +51 -1

@@ -7,7 +7,7 @@
77
88 Left out: sinh, cosh, fmod and hypot, until a program needs them. *)
99 module Math;
10-import Check;
10+import Check, Rt;
1111
1212 const
1313 pi* = 3.141592653589793; (* the F64 nearest to pi *)
@@ -442,6 +442,24 @@
442442 ret;
443443 end mul;
444444
445+(* hi, lo divided by d, by the machine's one instruction; it faults if hi >= d. *)
446+proc div128(hi, lo, d: U64): (U64, U64);
447+code
448+ mov rcx, rdx;
449+ mov rdx, rdi;
450+ mov rax, rsi;
451+ div rcx;
452+ ret;
453+end div128;
454+
455+(* hi, lo (a 128-bit number, hi its high word) divided by d: the quotient and the remainder.
456+ hi >= d, whose quotient doesn't fit a word, and d = 0 stop the program, as a division by zero does. *)
457+proc divMod*(hi, lo, d: U64): (U64, U64);
458+begin
459+ if hi >= d then Rt.stop("Math.divMod: hi >= d, the quotient doesn't fit a word"); end;
460+ return div128(hi, lo, d);
461+end divMod;
462+
445463 (* 64 bits of t from bit s on, the first bit of t[0] being 0. *)
446464 proc window(t: []U64; s: INT): U64;
447465 var q, r: INT;
@@ -1031,6 +1049,37 @@
10311049 end;
10321050
10331051 (* The module's test. *)
1052+(* divMod against mul: the product of a and b, divided by b, gives a back. *)
1053+proc testDiv;
1054+var a, b, hi, lo, q, r, x: U64;
1055+ i: INT;
1056+begin
1057+ q, r := divMod(0, 7, 2);
1058+ Check.ok((q = 3) and (r = 1), "Math.divMod small");
1059+ q, r := divMod(0, 12345, 1);
1060+ Check.ok((q = 12345) and (r = 0), "Math.divMod by 1");
1061+ q, r := divMod(5, 9, 6);
1062+ Check.ok((q = 0D555555555555556H) and (r = 5), "Math.divMod known");
1063+ q, r := divMod(0FFFFFFFFFFFFFFFEH, 0FFFFFFFFFFFFFFFFH, 0FFFFFFFFFFFFFFFFH);
1064+ Check.ok((q = 0FFFFFFFFFFFFFFFFH) and (r = 0FFFFFFFFFFFFFFFEH), "Math.divMod maximal hi");
1065+ x := 88172645463325252;
1066+ for i := 1 to 20 do
1067+ x := x xor (x shl 13); x := x xor (x shr 7); x := x xor (x shl 17);
1068+ a := x;
1069+ x := x xor (x shl 13); x := x xor (x shr 7); x := x xor (x shl 17);
1070+ b := x | 1;
1071+ lo, hi := mul(a, b);
1072+ q, r := divMod(hi, lo, b);
1073+ Check.ok((q = a) and (r = 0), "Math.divMod of a product");
1074+ if b > 1 then
1075+ r := b - 1;
1076+ if lo + r >= lo then q, r := divMod(hi, lo + r, b); (* no carry out of lo *)
1077+ Check.ok((q = a) and (r = b - 1), "Math.divMod with a remainder");
1078+ end;
1079+ end;
1080+ end;
1081+end;
1082+
10341083 proc main*;
10351084 var inf, nan, hard: F64;
10361085 begin
@@ -1087,6 +1136,7 @@
10871136 or not negative(roundEven(-0.5)) or negative(roundEven(0.5)) or (roundEven(0.49999999999999994) # 0.0)
10881137 or (roundEven(4503599627370495.5) # 4503599627370496.0) or (roundEven(4503599627370497.0) # 4503599627370497.0)
10891138 or (roundEven(-1.0E300) # -1.0E300) or (roundEven(inf) # inf) or (roundEven(nan) = roundEven(nan))), "Math.roundEven");
1139+ testDiv;
10901140 testToReal;
10911141 end;
10921142

test/run/MathDivFull.expect (history) added +0 -0

An empty file.

test/run/MathDivFull.raz (history) added +13 -0

@@ -0,0 +1,13 @@
1+module MathDivFull;
2+(* Math.divMod with hi >= d, whose quotient doesn't fit a word, stops the
3+ program as a division by zero does *)
4+import Math, Out;
5+
6+proc main*;
7+var q, r: U64;
8+begin
9+ q, r := Math.divMod(5, 0, 5);
10+ Out.hex(q + r, 1);
11+end;
12+
13+end MathDivFull.

test/run/MathDivFull.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/MathDivFull.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+Math.divMod: hi >= d, the quotient doesn't fit a word