raz / conform / run / Pow2.raz

68 lines · 2.3 KiB · raw · history · last change 264716cb

1(* div, mod and * by a power of two are shifts; they must agree with
2 idiv and imul, which a divisor in a variable still gets. *)
3module Pow2;
4import Out;
5
6var bad: I64;
7
8proc check(s: []const U8; x, got, want: I64);
9begin
10 if got # want then
11 Out.str(s); Out.str(" "); Out.int(x); Out.str(": ");
12 Out.int(got); Out.str(" # "); Out.int(want); Out.ln;
13 bad := bad + 1;
14 end;
15end;
16
17proc i64s(x: I64);
18var d1, d2, d8, d62: I64;
19begin
20 d1 := 1; d2 := 2; d8 := 8; d62 := 4000000000000000H;
21 check("div 1", x, x div 1, x div d1);
22 check("mod 1", x, x mod 1, x mod d1);
23 check("div 2", x, x div 2, x div d2);
24 check("mod 2", x, x mod 2, x mod d2);
25 check("div 8", x, x div 8, x div d8);
26 check("mod 8", x, x mod 8, x mod d8);
27 check("div 2^62", x, x div 4000000000000000H, x div d62);
28 check("mod 2^62", x, x mod 4000000000000000H, x mod d62);
29 check("* 8", x, x * 8, x * d8);
30 check("div -8", x, x div (0 - 8), x div (0 - d8));
31end;
32
33proc u64s(x: U64);
34var d2, d8, d62: U64;
35begin
36 d2 := 2; d8 := 8; d62 := 4000000000000000H;
37 check("u div 2", I64(x), I64(x div 2), I64(x div d2));
38 check("u mod 2", I64(x), I64(x mod 2), I64(x mod d2));
39 check("u div 8", I64(x), I64(x div 8), I64(x div d8));
40 check("u mod 8", I64(x), I64(x mod 8), I64(x mod d8));
41 check("u div 2^62", I64(x), I64(x div 4000000000000000H), I64(x div d62));
42 check("u mod 2^62", I64(x), I64(x mod 4000000000000000H), I64(x mod d62));
43end;
44
45proc main*;
46var i: I64; b: I8; d: I8; w: U8; dw: U8;
47begin
48 for i := -300 to 300 do i64s(i); u64s(U64(i)); end;
49 i64s(07FFFFFFFFFFFFFFFH); i64s(-07FFFFFFFFFFFFFFFH - 1);
50 i64s(4000000000000001H); i64s(-4000000000000001H);
51 u64s(0FFFFFFFFFFFFFFFFH); u64s(8000000000000000H);
52 d := 8; dw := 8;
53 for i := -128 to 127 do
54 b := I8(i); w := U8(i + 128);
55 check("i8 div 8", i, I64(b div 8), I64(b div d));
56 check("i8 mod 8", i, I64(b mod 8), I64(b mod d));
57 check("i8 * 8", i, I64(b * 8), I64(b * d));
58 check("u8 div 8", i, I64(w div 8), I64(w div dw));
59 check("u8 mod 8", i, I64(w mod 8), I64(w mod dw));
60 check("u8 * 8", i, I64(w * 8), I64(w * dw));
61 end;
62 i := -7;
63 Out.str("-7 div 2 = "); Out.int(i div 2);
64 Out.str(", -7 mod 2 = "); Out.int(i mod 2); Out.ln;
65 Out.int(bad); Out.str(" wrong\n");
66end;
67
68end Pow2.