]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/ng_assembly/num/word32.ma
freescle porting, work in progress
[helm.git] / helm / software / matita / contribs / ng_assembly / num / word32.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 (* ********************************************************************** *)
16 (*                          Progetto FreeScale                            *)
17 (*                                                                        *)
18 (*   Sviluppato da: Cosimo Oliboni, oliboni@cs.unibo.it                   *)
19 (*     Cosimo Oliboni, oliboni@cs.unibo.it                                *)
20 (*                                                                        *)
21 (* ********************************************************************** *)
22
23 include "num/word16.ma".
24
25 (* ***** *)
26 (* DWORD *)
27 (* ***** *)
28
29 nrecord word32 : Type ≝
30  {
31  w32h: word16;
32  w32l: word16
33  }.
34
35 (* \langle \rangle *)
36 notation "〈x.y〉" non associative with precedence 80
37  for @{ 'mk_word32 $x $y }.
38 interpretation "mk_word32" 'mk_word32 x y = (mk_word32 x y).
39
40 (* operatore = *)
41 ndefinition eq_w32 ≝ λdw1,dw2.(eq_w16 (w32h dw1) (w32h dw2)) ⊗ (eq_w16 (w32l dw1) (w32l dw2)).
42
43 (* operatore < *)
44 ndefinition lt_w32 ≝
45 λdw1,dw2:word32.match lt_w16 (w32h dw1) (w32h dw2) with
46  [ true ⇒ true
47  | false ⇒ match gt_w16 (w32h dw1) (w32h dw2) with
48   [ true ⇒ false
49   | false ⇒ lt_w16 (w32l dw1) (w32l dw2) ]].
50
51 (* operatore ≤ *)
52 ndefinition le_w32 ≝ λdw1,dw2:word32.(eq_w32 dw1 dw2) ⊕ (lt_w32 dw1 dw2).
53
54 (* operatore > *)
55 ndefinition gt_w32 ≝ λdw1,dw2:word32.⊖ (le_w32 dw1 dw2).
56
57 (* operatore ≥ *)
58 ndefinition ge_w32 ≝ λdw1,dw2:word32.⊖ (lt_w32 dw1 dw2).
59
60 (* operatore and *)
61 ndefinition and_w32 ≝
62 λdw1,dw2:word32.mk_word32 (and_w16 (w32h dw1) (w32h dw2)) (and_w16 (w32l dw1) (w32l dw2)).
63
64 (* operatore or *)
65 ndefinition or_w32 ≝
66 λdw1,dw2:word32.mk_word32 (or_w16 (w32h dw1) (w32h dw2)) (or_w16 (w32l dw1) (w32l dw2)).
67
68 (* operatore xor *)
69 ndefinition xor_w32 ≝
70 λdw1,dw2:word32.mk_word32 (xor_w16 (w32h dw1) (w32h dw2)) (xor_w16 (w32l dw1) (w32l dw2)).
71
72 (* operatore rotazione destra con carry *)
73 ndefinition rcr_w32 ≝
74 λdw:word32.λc:bool.match rcr_w16 (w32h dw) c with
75  [ pair wh' c' ⇒ match rcr_w16 (w32l dw) c' with
76   [ pair wl' c'' ⇒ pair … (mk_word32 wh' wl') c'' ]]. 
77
78 (* operatore shift destro *)
79 ndefinition shr_w32 ≝
80 λdw:word32.match rcr_w16 (w32h dw) false with
81  [ pair wh' c' ⇒ match rcr_w16 (w32l dw) c' with
82   [ pair wl' c'' ⇒ pair … (mk_word32 wh' wl') c'' ]].
83
84 (* operatore rotazione destra *)
85 ndefinition ror_w32 ≝
86 λdw:word32.match rcr_w16 (w32h dw) false with
87  [ pair wh' c' ⇒ match rcr_w16 (w32l dw) c' with
88   [ pair wl' c'' ⇒ match c'' with
89    [ true ⇒ mk_word32 (or_w16 (mk_word16 (mk_byte8 x8 x0) (mk_byte8 x0 x0)) wh') wl'
90    | false ⇒ mk_word32 wh' wl' ]]].
91
92 (* operatore rotazione destra n-volte *)
93 nlet rec ror_w32_n (dw:word32) (n:bitrigesim) (r:rec_bitrigesim n) on r ≝
94  match r with
95   [ bi_O ⇒ dw
96   | bi_S t n' ⇒ ror_w32_n (ror_w32 dw) t n' ].
97
98 (* operatore rotazione sinistra con carry *)
99 ndefinition rcl_w32 ≝
100 λdw:word32.λc:bool.match rcl_w16 (w32l dw) c with
101  [ pair wl' c' ⇒ match rcl_w16 (w32h dw) c' with
102   [ pair wh' c'' ⇒ pair … (mk_word32 wh' wl') c'' ]]. 
103
104 (* operatore shift sinistro *)
105 ndefinition shl_w32 ≝
106 λdw:word32.match rcl_w16 (w32l dw) false with
107  [ pair wl' c' ⇒ match rcl_w16 (w32h dw) c' with
108   [ pair wh' c'' ⇒ pair … (mk_word32 wh' wl') c'' ]].
109
110 (* operatore rotazione sinistra *)
111 ndefinition rol_w32 ≝
112 λdw:word32.match rcl_w16 (w32l dw) false with
113  [ pair wl' c' ⇒ match rcl_w16 (w32h dw) c' with
114   [ pair wh' c'' ⇒ match c'' with
115    [ true ⇒ mk_word32 wh' (or_w16 (mk_word16 (mk_byte8 x0 x0) (mk_byte8 x0 x1)) wl')
116    | false ⇒ mk_word32 wh' wl' ]]].
117
118 (* operatore rotazione sinistra n-volte *)
119 nlet rec rol_w32_n (dw:word32) (n:bitrigesim) (r:rec_bitrigesim n) on r ≝
120  match r with
121   [ bi_O ⇒ dw
122   | bi_S t n' ⇒ rol_w32_n (rol_w32 dw) t n' ].
123
124 (* operatore not/complemento a 1 *)
125 ndefinition not_w32 ≝
126 λdw:word32.mk_word32 (not_w16 (w32h dw)) (not_w16 (w32l dw)).
127
128 (* operatore somma con data+carry → data+carry *)
129 ndefinition plus_w32_dc_dc ≝
130 λdw1,dw2:word32.λc:bool.
131  match plus_w16_dc_dc (w32l dw1) (w32l dw2) c with
132   [ pair l c ⇒ match plus_w16_dc_dc (w32h dw1) (w32h dw2) c with
133    [ pair h c' ⇒ pair … 〈h.l〉 c' ]].
134
135 (* operatore somma con data+carry → data *)
136 ndefinition plus_w32_dc_d ≝
137 λdw1,dw2:word32.λc:bool.
138  match plus_w16_dc_dc (w32l dw1) (w32l dw2) c with
139   [ pair l c ⇒ 〈plus_w16_dc_d (w32h dw1) (w32h dw2) c.l〉 ].
140
141 (* operatore somma con data+carry → c *)
142 ndefinition plus_w32_dc_c ≝
143 λdw1,dw2:word32.λc:bool.
144  plus_w16_dc_c (w32h dw1) (w32h dw2) (plus_w16_dc_c (w32l dw1) (w32l dw2) c).
145
146 (* operatore somma con data → data+carry *)
147 ndefinition plus_w32_d_dc ≝
148 λdw1,dw2:word32.
149  match plus_w16_d_dc (w32l dw1) (w32l dw2) with
150   [ pair l c ⇒ match plus_w16_dc_dc (w32h dw1) (w32h dw2) c with
151    [ pair h c' ⇒ pair … 〈h.l〉 c' ]].
152
153 (* operatore somma con data → data *)
154 ndefinition plus_w32_d_d ≝
155 λdw1,dw2:word32.
156  match plus_w16_d_dc (w32l dw1) (w32l dw2) with
157   [ pair l c ⇒ 〈plus_w16_dc_d (w32h dw1) (w32h dw2) c.l〉 ].
158
159 (* operatore somma con data → c *)
160 ndefinition plus_w32_d_c ≝
161 λdw1,dw2:word32.
162  plus_w16_dc_c (w32h dw1) (w32h dw2) (plus_w16_d_c (w32l dw1) (w32l dw2)).
163
164 (* operatore Most Significant Bit *)
165 ndefinition MSB_w32 ≝ λdw:word32.eq_ex x8 (and_ex x8 (b8h (w16h (w32h dw)))).
166
167 (* operatore predecessore *)
168 ndefinition pred_w32 ≝
169 λdw:word32.match eq_w16 (w32l dw) (mk_word16 (mk_byte8 x0 x0) (mk_byte8 x0 x0)) with
170  [ true ⇒ mk_word32 (pred_w16 (w32h dw)) (pred_w16 (w32l dw))
171  | false ⇒ mk_word32 (w32h dw) (pred_w16 (w32l dw)) ].
172
173 (* operatore successore *)
174 ndefinition succ_w32 ≝
175 λdw:word32.match eq_w16 (w32l dw) (mk_word16 (mk_byte8 xF xF) (mk_byte8 xF xF)) with
176  [ true ⇒ mk_word32 (succ_w16 (w32h dw)) (succ_w16 (w32l dw))
177  | false ⇒ mk_word32 (w32h dw) (succ_w16 (w32l dw)) ].
178
179 (* operatore neg/complemento a 2 *)
180 ndefinition compl_w32 ≝
181 λdw:word32.match MSB_w32 dw with
182  [ true ⇒ succ_w32 (not_w32 dw)
183  | false ⇒ not_w32 (pred_w32 dw) ].
184
185 (* 
186    operatore moltiplicazione senza segno: b*b=[0x00000000,0xFFFE0001]
187    ... in pratica (〈a:b〉*〈c:d〉) = (a*c)<<16+(a*d)<<8+(b*c)<<8+(b*d)
188 *)
189 ndefinition mul_w16 ≝
190 λw1,w2:word16.match w1 with
191 [ mk_word16 b1h b1l ⇒ match w2 with
192 [ mk_word16 b2h b2l ⇒ match mul_b8 b1l b2l with
193 [ mk_word16 t1_h t1_l ⇒ match mul_b8 b1h b2l with
194 [ mk_word16 t2_h t2_l ⇒ match mul_b8 b2h b1l with
195 [ mk_word16 t3_h t3_l ⇒ match mul_b8 b1h b2h with
196 [ mk_word16 t4_h t4_l ⇒
197  plus_w32_d_d
198   (plus_w32_d_d
199    (plus_w32_d_d 〈〈〈x0,x0〉:t3_h〉.〈t3_l:〈x0,x0〉〉〉 〈〈〈x0,x0〉:t2_h〉.〈t2_l:〈x0,x0〉〉〉) 〈〈t4_h:t4_l〉.〈〈x0,x0〉:〈x0,x0〉〉〉)〈〈〈x0,x0〉:〈x0,x0〉〉.〈t1_h:t1_l〉〉
200 ]]]]]].
201
202 (* divisione senza segno (secondo la logica delle ALU): (quoziente resto) overflow *)
203 nlet rec div_w16_aux (divd:word32) (divs:word32) (molt:word16) (q:word16) (c:exadecim) (rc:rec_exadecim c) on rc ≝
204   let w' ≝ plus_w32_d_d divd (compl_w32 divs) in
205    match rc with
206    [ ex_O ⇒ match le_w32 divs divd with
207     [ true ⇒ triple … (or_w16 molt q) (w32l w') (⊖ (eq_w16 (w32h w') 〈〈x0,x0〉:〈x0,x0〉〉))
208     | false ⇒ triple … q (w32l divd) (⊖ (eq_w16 (w32h divd) 〈〈x0,x0〉:〈x0,x0〉〉)) ]
209    | ex_S t c' ⇒ match le_w32 divs divd with
210     [ true ⇒ div_w16_aux w' (ror_w32 divs) (ror_w16 molt) (or_w16 molt q) t c'
211     | false ⇒ div_w16_aux divd (ror_w32 divs) (ror_w16 molt) q t c' ]].
212
213 ndefinition div_w16 ≝
214 λw:word32.λb:word16.match eq_w16 b 〈〈x0,x0〉:〈x0,x0〉〉 with
215 (* 
216    la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato
217 *)
218  [ true ⇒ triple … 〈〈xF,xF〉:〈xF,xF〉〉 (w32l w) true
219  | false ⇒ match eq_w32 w 〈〈〈x0,x0〉:〈x0,x0〉〉.〈〈x0,x0〉:〈x0,x0〉〉〉 with
220 (* 0 diviso qualsiasi cosa diverso da 0 da' q=0 r=0 o=false *)
221   [ true ⇒ triple … 〈〈x0,x0〉:〈x0,x0〉〉 〈〈x0,x0〉:〈x0,x0〉〉 false
222 (* 1) e' una divisione sensata che produrra' overflow/risultato *)
223 (* 2) parametri: dividendo, divisore, moltiplicatore, quoziente, contatore *)
224 (* 3) ad ogni ciclo il divisore e il moltiplicatore vengono scalati di 1 a dx *)
225 (* 4) il moltiplicatore e' la quantita' aggiunta al quoziente se il divisore *)
226 (*    puo' essere sottratto al dividendo *) 
227   | false ⇒ div_w16_aux w (rol_w32_n 〈〈〈x0,x0〉:〈x0,x0〉〉.b〉 ? (bit_to_recbit t0F)) 〈〈x8,x0〉:〈x0,x0〉〉 〈〈x0,x0〉:〈x0,x0〉〉 ? (ex_to_recex xF) ]].
228
229 (* operatore x in [inf,sup] *)
230 ndefinition inrange_w32 ≝
231 λx,inf,sup:word32.(le_w32 inf sup) ⊗ (ge_w32 x inf) ⊗ (le_w32 x sup).
232
233 (* iteratore sulle word *)
234 ndefinition forall_w32 ≝
235  λP.
236   forall_w16 (λbh.
237   forall_w16 (λbl.
238    P (mk_word32 bh bl ))).