raz / test / run / IndexParam.raz

154 lines · 3.8 KiB · raw · history · last change 87e3c487

1(* An element of a var or const array parameter, of 1, 2, 4 or 8 bytes,
2 is read and written as the operand [pointer + index * size], the
3 index checked first; here in a leaf, which keeps the pointer in rdi or
4 rsi, and in a procedure that calls, which keeps it in a callee-saved
5 register. IndexParamOff has the same with no checks. An element of
6 another size is added up. The last index is out of range. *)
7module IndexParam;
8import Out, IndexParamOff;
9
10const N = 8;
11
12type
13 Bytes = IndexParamOff.Bytes;
14 Shorts = IndexParamOff.Shorts;
15 Words = IndexParamOff.Words;
16 Longs = IndexParamOff.Longs;
17 Odd = record a, b, c: U8; end; (* 3 bytes *)
18 Pair = record tag: U8; v: [N]I32; end; (* v at an offset *)
19
20var
21 bs: Bytes;
22 ss: Shorts;
23 ws: Words;
24 ls, ds: Longs;
25 os: [N]Odd;
26 pr: Pair;
27
28proc sumBytes(const a: Bytes): INT;
29var i, t: INT;
30begin
31 t := 0;
32 for i := 0 to N - 1 do t := t + a[i] + INT(a[N - 1 - i]) * 2; end;
33 for i := 0 to N div 2 - 1 do t := t + a[2 * i + 1] + a[a[i] mod N]; end;
34 return t;
35end;
36
37proc sumShorts(const a: Shorts): INT;
38var i, t: INT;
39begin
40 t := 0;
41 for i := 1 to N - 1 do t := t + INT(a[i]) * a[i - 1]; end;
42 return t;
43end;
44
45proc sumWords(const a: Words): U32;
46var i: INT; t: U32;
47begin
48 t := 0;
49 for i := 0 to N - 2 do t := t + a[i + 1] xor a[i]; end;
50 return t;
51end;
52
53proc double(var a: Longs; const d: Longs);
54var i: INT;
55begin
56 for i := 1 to N - 1 do a[i] := a[i - 1] * 2 + d[i]; end;
57end;
58
59(* as double, but calling between: the pointers are kept past calls *)
60proc doubleSay(var a: Longs; const d: Longs);
61var i: INT;
62begin
63 for i := 1 to N - 1 do
64 a[i] := a[i - 1] * 2 + d[i];
65 Out.int(a[i]);
66 Out.char(' ');
67 end;
68 Out.ln;
69end;
70
71proc swap(var a: Bytes);
72var i: INT; c: U8;
73begin
74 for i := 0 to N div 2 - 1 do
75 c := a[i];
76 a[i] := a[N - 1 - i];
77 a[N - 1 - i] := c;
78 end;
79end;
80
81(* the elements of 3 bytes, added up as before *)
82proc sumOdd(var a: [N]Odd): INT;
83var i, t: INT;
84begin
85 t := 0;
86 for i := 0 to N - 1 do
87 a[i].c := U8(a[N - 1 - i].a + 1);
88 t := t + a[i].a + INT(a[i].b) * a[i].c;
89 end;
90 return t;
91end;
92
93(* an array at an offset in a record parameter, read and written *)
94proc pairSum(var p: Pair): INT;
95var i, t: INT;
96begin
97 t := 0;
98 for i := 0 to N - 1 do p.v[i] := I32(i - 3) * I32(p.tag); end;
99 for i := 1 to N - 1 do t := t + INT(p.v[i - 1]) * 10 + INT(p.v[i]); end;
100 return t;
101end;
102
103(* a's element at k, which may be out of range *)
104proc at(const a: Shorts; k: INT): INT;
105begin
106 return a[k + 1];
107end;
108
109proc reset;
110var i: INT;
111begin
112 for i := 0 to N - 1 do
113 bs[i] := U8(i * 37 + 5);
114 ss[i] := I16(i * 1000 - 3500);
115 ws[i] := U32(i) * 9E3779B9H;
116 ls[i] := I64(i + 1);
117 ds[i] := I64(i * i) - 20;
118 os[i].a := U8(i); os[i].b := U8(i + 1); os[i].c := 0;
119 end;
120end;
121
122proc main*;
123var i: INT;
124begin
125 reset();
126 Out.int(sumBytes(bs)); Out.char(' ');
127 Out.int(sumShorts(ss)); Out.char(' ');
128 Out.int(I64(sumWords(ws))); Out.ln;
129 Out.int(IndexParamOff.sumBytes(bs)); Out.char(' ');
130 Out.int(IndexParamOff.sumShorts(ss)); Out.char(' ');
131 Out.int(I64(IndexParamOff.sumWords(ws))); Out.ln;
132 double(ls, ds);
133 for i := 0 to N - 1 do Out.int(ls[i]); Out.char(' '); end;
134 Out.ln;
135 reset();
136 IndexParamOff.double(ls, ds);
137 for i := 0 to N - 1 do Out.int(ls[i]); Out.char(' '); end;
138 Out.ln;
139 reset();
140 doubleSay(ls, ds);
141 swap(bs);
142 for i := 0 to N - 1 do Out.int(I64(bs[i])); Out.char(' '); end;
143 Out.ln;
144 IndexParamOff.swap(bs);
145 for i := 0 to N - 1 do Out.int(I64(bs[i])); Out.char(' '); end;
146 Out.ln;
147 Out.int(sumOdd(os)); Out.ln;
148 pr.tag := 7;
149 Out.int(pairSum(pr)); Out.ln;
150 Out.int(at(ss, 6)); Out.ln;
151 Out.int(at(ss, 7)); Out.ln;
152end;
153
154end IndexParam.