blob: b7dd90da64648c31d58508b49e3e75db7a3b0be7 (
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
Require Import ZArith NArith NPeano.
Require Import QhasmCommon.
Require Export Bedrock.Word Bedrock.Nomega.
Delimit Scope nword_scope with w.
Local Open Scope nword_scope.
Notation "& x" := (wordToN x) (at level 30) : nword_scope.
Notation "** x" := (NToWord _ x) (at level 30) : nword_scope.
Section Util.
Definition convS {n m} (x: word n) (H: n = m): word m.
refine (eq_rect _ (fun B0 : Set => B0) x _ _).
abstract (subst; intuition).
Defined.
Definition low {k n: nat} (p: (k <= n)%nat) (w: word n): word k.
refine (split1 k (n - k) (convS w _)); abstract omega.
Defined.
Definition high {k n: nat} (p: (k <= n)%nat) (w: word n): word (n - k).
refine (split2 k (n - k) (convS w _)); abstract omega.
Defined.
Definition extend {k n: nat} (p: (k <= n)%nat) (w: word k): word n.
refine (convS (zext w (n - k)) _); abstract omega.
Defined.
Definition shiftr {n} (w: word n) (k: nat): word n.
refine match (le_dec k n) with
| left p => extend _ (@high k _ _ w)
| right _ => wzero n
end; abstract nomega.
Defined.
Definition mask {n} (k: nat) (w: word n): word n :=
match (le_dec k n) with
| left p => extend p (low p w)
| right _ => w
end.
Definition Nge_dec (x y: N) :
{(x >= y)%N} + {(x < y)%N}.
refine (
let c := (x ?= y)%N in
match c as c' return c = c' -> _ with
| Lt => fun _ => right _
| _ => fun _ => left _
end eq_refl); abstract (
unfold c in *; unfold N.lt, N.ge;
repeat match goal with
| [ H: (_ ?= _)%N = _ |- _] =>
rewrite H; intuition; try inversion H
| [ H: Eq = _ |- _] => inversion H
| [ H: Gt = _ |- _] => inversion H
| [ H: Lt = _ |- _] => inversion H
end).
Defined.
Definition overflows (n: nat) (x: N) := Nge_dec x (Npow2 n).
Definition ind (b: bool): N := if b then 1%N else 0%N.
Definition ind' {A B} (b: {A} + {B}): N := if b then 1%N else 0%N.
Definition break {n} (m: nat) (x: word n): word m * word (n - m).
refine match (le_dec m n) with
| left p => (extend _ (low p x), extend _ (@high m n _ x))
| right p => (extend _ x, extend _ WO)
end; try abstract intuition.
Defined.
Definition addWithCarry {n} (x y: word n) (c: bool): word n :=
x ^+ y ^+ (natToWord _ (if c then 1 else 0)).
Definition omap {A B} (x: option A) (f: A -> option B) :=
match x with
| Some y => f y
| _ => None
end.
Notation "A <- X ; B" := (omap X (fun A => B)) (at level 70, right associativity).
Definition orElse {T} (d: T) (o: option T): T :=
match o with
| Some v => v
| None => d
end.
End Util.
Close Scope nword_scope.
|