aboutsummaryrefslogtreecommitdiffstats
path: root/riscV/ExtValues.v
blob: d79604f7bdd10734e221b5c9280ca076c4ba03e3 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Require Import Coqlib.
Require Import Integers.
Require Import Values.
Require Import Floats.
Require Import Memory.
Require Import Lia.

Axiom address_of_ptr : mem -> block -> ptrofs -> int64.
Axiom ptr_of_address : mem -> int64 -> option (block*ptrofs).
Axiom ptr_address_correct :
  forall m b ofs, (ptr_of_address m (address_of_ptr m b ofs)) = Some (b, ofs).

Definition int64_of_value m v : int64 :=
  match v with
  | Vint x => Int64.repr (Int.signed x)
  | Vlong x => x
  | Vsingle x => Int64.repr (Int.signed (Float32.to_bits x))
  | Vfloat x => Float.to_bits x
  | Vptr b ofs => address_of_ptr m b ofs
  | Vundef => Int64.zero
  end.

Inductive vtype := VTint | VTlong | VTsingle | VTfloat | VTptr | VTundef.

Definition vtype_of v :=
  match v with
  | Vint _ => VTint
  | Vlong _ => VTlong
  | Vfloat _ => VTfloat
  | Vsingle _ => VTsingle
  | Vptr _ _ => VTptr
  | Vundef => VTundef
  end.

Definition value_of_int64 (ty : vtype) (m : mem) (l : int64) : val :=
  match ty with
  | VTint => Vint (Int.repr (Int64.signed l))
  | VTlong => Vlong l
  | VTsingle => Vsingle (Float32.of_bits (Int.repr (Int64.signed l)))
  | VTfloat => Vfloat (Float.of_bits l)
  | VTptr => match ptr_of_address m l with
                | Some(b, ofs) => Vptr b ofs
                | None => Vundef
             end
  | VTundef => Vundef
  end.

Remark min_signed_order: Int64.min_signed <= Int.min_signed.
Proof.
  cbv. discriminate.
Qed.

Remark max_signed_order: Int.max_signed <= Int64.max_signed.
Proof.
  cbv. discriminate.
Qed.

Lemma int64_of_value_correct :
  forall m v,
    value_of_int64 (vtype_of v) m (int64_of_value m v) = v.
Proof.
  destruct v; cbn; trivial; f_equal.
  - rewrite Int64.signed_repr.
    apply Int.repr_signed. 
    pose proof (Int.signed_range i) as RANGE.
    pose proof min_signed_order.
    pose proof max_signed_order.
    lia.
  - apply Float.of_to_bits.
  - rewrite Int64.signed_repr.
    { rewrite Int.repr_signed.
      apply Float32.of_to_bits. }
    pose proof (Int.signed_range (Float32.to_bits f)) as RANGE.
    pose proof min_signed_order.
    pose proof max_signed_order.
    lia.
  - pose proof (ptr_address_correct m b i).
    destruct (ptr_of_address m (address_of_ptr m b i)).
    + inv H. trivial.
    + discriminate.
Qed.
    
Definition bits_of_float x :=
  match x with
  | Vfloat f => Vlong (Float.to_bits f)
  | _ => Vundef
  end.

Definition bits_of_single x :=
  match x with
  | Vsingle f => Vint (Float32.to_bits f)
  | _ => Vundef
  end.

Definition float_of_bits x :=
  match x with
  | Vlong f => Vfloat (Float.of_bits f)
  | _ => Vundef
  end.

Definition single_of_bits x :=
  match x with
  | Vint f => Vsingle (Float32.of_bits f)
  | _ => Vundef
  end.

Definition bitwise_select_int b vtrue vfalse :=
  Val.or (Val.and (Val.neg b) vtrue)
         (Val.and (Val.sub b Vone) vfalse).

Lemma bitwise_select_int_true :
  forall vtrue vfalse,
    bitwise_select_int (Val.of_optbool (Some true)) (Vint vtrue) (Vint vfalse)
    = Vint vtrue.
Proof.
  intros. cbn. f_equal.
  change (Int.neg Int.one) with Int.mone.
  rewrite Int.and_commut.
  rewrite Int.and_mone.
  rewrite Int.sub_idem.
  rewrite Int.and_commut.
  rewrite Int.and_zero.
  apply Int.or_zero.
Qed.

Lemma bitwise_select_int_false :
  forall vtrue vfalse,
    bitwise_select_int (Val.of_optbool (Some false)) (Vint vtrue) (Vint vfalse)
    = Vint vfalse.
Proof.
  intros. cbn. f_equal.
  rewrite Int.neg_zero.
  rewrite Int.and_commut.
  rewrite Int.and_zero.
  rewrite Int.sub_zero_r.
  change (Int.neg Int.one) with Int.mone.
  rewrite Int.and_commut.
  rewrite Int.and_mone.
  rewrite Int.or_commut.
  apply Int.or_zero.
Qed.

Definition bitwise_select_long b vtrue vfalse :=
  let b' := Val.longofint b in
  Val.orl (Val.andl (Val.negl b') vtrue)
          (Val.andl (Val.subl b' (Vlong Int64.one)) vfalse).

Lemma bitwise_select_long_true :
  forall vtrue vfalse,
    bitwise_select_long (Val.of_optbool (Some true)) (Vlong vtrue) (Vlong vfalse)
    = Vlong vtrue.
Proof.
  intros. cbn. f_equal.
  change (Int64.neg Int64.one) with Int64.mone.
  rewrite Int64.and_commut.
  rewrite Int64.and_mone.
  rewrite Int64.sub_idem.
  rewrite Int64.and_commut.
  rewrite Int64.and_zero.
  apply Int64.or_zero.
Qed.

Lemma bitwise_select_long_false :
  forall vtrue vfalse,
    bitwise_select_long (Val.of_optbool (Some false)) (Vlong vtrue) (Vlong vfalse)
    = Vlong vfalse.
Proof.
  intros. cbn. f_equal.
  change (Int64.repr (Int.signed Int.zero)) with Int64.zero.
  rewrite Int64.neg_zero.
  rewrite Int64.and_commut.
  rewrite Int64.and_zero.
  rewrite Int64.sub_zero_r.
  change (Int64.neg Int64.one) with Int64.mone.
  rewrite Int64.and_commut.
  rewrite Int64.and_mone.
  rewrite Int64.or_commut.
  apply Int64.or_zero.
Qed.

(*
Compute (Int.unsigned (Float32.to_bits (Float32.of_int (Int.repr 4200)))).
Compute (Int64.unsigned (Float.to_bits (Float.of_int (Int.repr 4233)))).
*)