| 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. *) |
| 4 | module IndexParamOff; |
| 5 | import SYSTEM; |
| 6 | check off; |
| 7 | |
| 8 | const N = 8; |
| 9 | |
| 10 | type |
| 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 *) |
| 17 | proc sumBytes*(const a: Bytes): INT; |
| 18 | var i, t: INT; |
| 19 | begin |
| 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; |
| 24 | end; |
| 25 | |
| 26 | proc sumShorts*(const a: Shorts): INT; |
| 27 | var i, t: INT; |
| 28 | begin |
| 29 | t := 0; |
| 30 | for i := 1 to N - 1 do t := t + INT(a[i]) * a[i - 1]; end; |
| 31 | return t; |
| 32 | end; |
| 33 | |
| 34 | proc sumWords*(const a: Words): U32; |
| 35 | var i: INT; t: U32; |
| 36 | begin |
| 37 | t := 0; |
| 38 | for i := 0 to N - 2 do t := t + a[i + 1] xor a[i]; end; |
| 39 | return t; |
| 40 | end; |
| 41 | |
| 42 | (* each element of a made twice the one before, plus d[i] *) |
| 43 | proc double*(var a: Longs; const d: Longs); |
| 44 | var i: INT; |
| 45 | begin |
| 46 | for i := 1 to N - 1 do a[i] := a[i - 1] * 2 + d[i]; end; |
| 47 | end; |
| 48 | |
| 49 | (* the bytes of a turned round, two at a time from either end *) |
| 50 | proc swap*(var a: Bytes); |
| 51 | var i: INT; c: U8; |
| 52 | begin |
| 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; |
| 58 | end; |
| 59 | |
| 60 | end IndexParamOff. |