raz / test / run / mods / IndexParamOff.raz

60 lines · 1.3 KiB · raw · history · last change 264716cb

1(* IndexParam's procedures again, with no checks: an element of an array
2 parameter of 1, 2, 4 or 8 bytes is read and written as the operand
3 [pointer + index * size], where the index comes with no check. *)
4module IndexParamOff;
5import SYSTEM;
6check off;
7
8const N = 8;
9
10type
11 Bytes* = [N]U8;
12 Shorts* = [N]I16;
13 Words* = [N]U32;
14 Longs* = [N]I64;
15
16(* a sum of a's elements, picked by indices worked out several ways *)
17proc sumBytes*(const a: Bytes): INT;
18var i, t: INT;
19begin
20 t := 0;
21 for i := 0 to N - 1 do t := t + a[i] + INT(a[N - 1 - i]) * 2; end;
22 for i := 0 to N div 2 - 1 do t := t + a[2 * i + 1] + a[a[i] mod N]; end;
23 return t;
24end;
25
26proc sumShorts*(const a: Shorts): INT;
27var i, t: INT;
28begin
29 t := 0;
30 for i := 1 to N - 1 do t := t + INT(a[i]) * a[i - 1]; end;
31 return t;
32end;
33
34proc sumWords*(const a: Words): U32;
35var i: INT; t: U32;
36begin
37 t := 0;
38 for i := 0 to N - 2 do t := t + a[i + 1] xor a[i]; end;
39 return t;
40end;
41
42(* each element of a made twice the one before, plus d[i] *)
43proc double*(var a: Longs; const d: Longs);
44var i: INT;
45begin
46 for i := 1 to N - 1 do a[i] := a[i - 1] * 2 + d[i]; end;
47end;
48
49(* the bytes of a turned round, two at a time from either end *)
50proc swap*(var a: Bytes);
51var i: INT; c: U8;
52begin
53 for i := 0 to N div 2 - 1 do
54 c := a[i];
55 a[i] := a[N - 1 - i];
56 a[N - 1 - i] := c;
57 end;
58end;
59
60end IndexParamOff.