summaryrefslogtreecommitdiff
path: root/lib/mono_rewrites.sail
blob: 3190003f012a2cec1012bbecf30f510f6fc18699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(* Definitions for use with the -mono_rewrites option *)

(* External definitions not in the usual asl prelude *)

val "shiftleft" : forall 'n 'o ('ord : Order).
    (vector('o, 'n, 'ord, bit), int) -> vector('o, 'n, 'ord, bit) effect pure

overload operator << = {shiftleft}

val "shiftright" : forall 'n 'o ('ord : Order).
    (vector('o, 'n, 'ord, bit), int) -> vector('o, 'n, 'ord, bit) effect pure

overload operator >> = {shiftright}

val "extz" : forall 'n 'm 'o 'p. (atom('p),atom('m),vector('o, 'n, dec, bit)) -> vector('p, 'm, dec, bit) effect pure
val extzv : forall 'n 'm 'o 'p. vector('o, 'n, dec, bit) -> vector('p, 'm, dec, bit) effect pure
function extzv(v) = extz(sizeof('p), sizeof('m),v)

(* Definitions for the rewrites *)

val slice_mask : forall 'n, 'n >= 0. (int, int) -> bits('n) effect pure
function slice_mask(i,l) =
  let one : bits('n) = extzv(0b1) in
  ((one << l) - one) << i

val is_zero_subrange : forall 'n, 'n >= 0.
  (bits('n), int, int) -> bool effect pure

function is_zero_subrange (xs, i, j) = {
  (xs & slice_mask(j, i-j)) == extzv(0b0)
}

val is_ones_subrange : forall 'n, 'n >= 0.
  (bits('n), int, int) -> bool effect pure

function is_ones_subrange (xs, i, j) = {
  let m : bits('n) = slice_mask(j,j-i) in
  (xs & m) == m
}

val slice_slice_concat : forall 'n 'm 'p 'q 'r, 'r = 'p + 'q & 'n >= 0 & 'm >= 0 & 'p >= 0 & 'q >= 0.
  (bits('n), int, atom('p), bits('m), int, atom('q)) -> bits('r) effect pure

function slice_slice_concat (xs, i, l, ys, i', l') = {
  let xs = (xs & slice_mask(i,l)) >> i in
  let ys = (ys & slice_mask(i',l')) >> i' in
  extzv(xs) << l' | extzv(ys)
}

(* Assumes initial vectors are of equal size *)

val subrange_subrange_eq : forall 'n, 'n >= 0.
  (bits('n), int, int, bits('n), int, int) -> bool effect pure

function subrange_subrange_eq (xs, i, j, ys, i', j') = {
  let xs = (xs & slice_mask(j,i-j)) >> j in
  let ys = (ys & slice_mask(j',i'-j')) >> j' in
  xs == ys
}

val subrange_subrange_concat : forall 'n 'o 'p 'm 'q 'r 's, 's = 'o - ('p - 1) + 'q - ('r - 1) & 'n >= 0 & 'm >= 0.
  (bits('n), atom('o), atom('p), bits('m), atom('q), atom('r)) -> bits('s) effect pure

function subrange_subrange_concat (xs, i, j, ys, i', j') = {
  let xs = (xs & slice_mask(j,i-j)) >> j in
  let ys = (ys & slice_mask(j',i'-j')) >> j' in
  extzv(xs) << i' - (j' - 1) | extzv(ys)
}