| | @@ -7,7 +7,7 @@ |
| 7 | 7 | |
| 8 | 8 | Left out: sinh, cosh, fmod and hypot, until a program needs them. *) |
| 9 | 9 | module Math; |
| 10 | | -import Check; |
| 10 | +import Check, Rt; |
| 11 | 11 | |
| 12 | 12 | const |
| 13 | 13 | pi* = 3.141592653589793; (* the F64 nearest to pi *) |
| | @@ -442,6 +442,24 @@ |
| 442 | 442 | ret; |
| 443 | 443 | end mul; |
| 444 | 444 | |
| 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 | + |
| 445 | 463 | (* 64 bits of t from bit s on, the first bit of t[0] being 0. *) |
| 446 | 464 | proc window(t: []U64; s: INT): U64; |
| 447 | 465 | var q, r: INT; |
| | @@ -1031,6 +1049,37 @@ |
| 1031 | 1049 | end; |
| 1032 | 1050 | |
| 1033 | 1051 | (* 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 | + |
| 1034 | 1083 | proc main*; |
| 1035 | 1084 | var inf, nan, hard: F64; |
| 1036 | 1085 | begin |
| | @@ -1087,6 +1136,7 @@ |
| 1087 | 1136 | or not negative(roundEven(-0.5)) or negative(roundEven(0.5)) or (roundEven(0.49999999999999994) # 0.0) |
| 1088 | 1137 | or (roundEven(4503599627370495.5) # 4503599627370496.0) or (roundEven(4503599627370497.0) # 4503599627370497.0) |
| 1089 | 1138 | or (roundEven(-1.0E300) # -1.0E300) or (roundEven(inf) # inf) or (roundEven(nan) = roundEven(nan))), "Math.roundEven"); |
| 1139 | + testDiv; |
| 1090 | 1140 | testToReal; |
| 1091 | 1141 | end; |
| 1092 | 1142 | |