-
Notifications
You must be signed in to change notification settings - Fork 0
/
Round.v
358 lines (284 loc) · 21.3 KB
/
Round.v
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
Require Import Kami.AllNotations.
Require Import FpuKami.Definitions.
Definition Pair (A B: Kind) := (STRUCT_TYPE {
"fst" :: A;
"snd" :: B
}).
Section RoundAny.
Variable ty: Kind -> Type.
Variable inExpWidthMinus2 inSigWidthMinus2: nat.
Local Notation inExpWidthMinus1 := (inExpWidthMinus2 + 1).
Local Notation inExpWidth := (inExpWidthMinus1 + 1).
Local Notation inSigWidthMinus1 := (inSigWidthMinus2 + 1).
Local Notation inSigWidth := (inSigWidthMinus1 + 1).
Variable outExpWidthMinus2 outSigWidthMinus2: nat.
Local Notation outExpWidthMinus1 := (outExpWidthMinus2 + 1).
Local Notation outExpWidth := (outExpWidthMinus1 + 1).
Local Notation outSigWidthMinus1 := (outSigWidthMinus2 + 1).
Local Notation outSigWidth := (outSigWidthMinus1 + 1).
Definition RoundInput := STRUCT_TYPE {
"in" :: NF inExpWidthMinus2 inSigWidthMinus2;
"afterRounding" :: Bool;
"roundingMode" :: Bit 3
}.
Variable underflowInExpSltMinus underflowToZeroInExpSltMinus overflowExpGlt: nat.
Open Scope kami_expr.
Variable input : RoundInput @# ty.
Definition ignoreMsb {n:nat} (x: Expr ty (SyntaxKind (Bit (n+1)))) :=
UniBit (TruncLsb n 1) x.
Open Scope kami_action.
Definition RoundNF_expr: LetExprSyntax ty (OpOutput outExpWidthMinus2 outSigWidthMinus2).
refine (
LETE roundingMode_near_even <- RetE (input @% "roundingMode" == $$ round_near_even);
LETE roundingMode_minMag <- RetE (input @% "roundingMode" == $$ round_minMag);
LETE roundingMode_min <- RetE (input @% "roundingMode" == $$ round_min);
LETE roundingMode_max <- RetE (input @% "roundingMode" == $$ round_max);
LETE roundingMode_odd <- RetE (input @% "roundingMode" == $$ round_odd);
LETE roundingMode_near_maxMag <- RetE (input @% "roundingMode" == $$ round_near_maxMag);
LETE nfIn <- RetE (input @% "in");
LETE inSign <- RetE (#nfIn @% "sign");
LETE inSig <- RetE (#nfIn @% "sig");
LETE inExp : Bit (inExpWidth + 1) <- RetE (#nfIn @% "sExp");
LETE subnormal <- RetE match Compare_dec.lt_dec outSigWidthMinus2 inSigWidthMinus2 with
| left _ => (#inExp <s (* $2 - $(pow2 outExpWidthMinus1) *) $0 - $underflowInExpSltMinus)
| _ => $$false
end;
LETE subNormDist <- RetE (IF #subnormal
then ($2 - $(2 ^ outExpWidthMinus1) - #inExp)
else $0);
LETE leadingOneSig: Bit inSigWidth <-
RetE ({<$$WO~1, #inSig>});
LETE signedSubnormShift <- RetE (IF $outSigWidth <s #subNormDist
then $0
else $outSigWidthMinus1 - #subNormDist);
LETE tailSig: Bit inSigWidth <- RetE (IF $outSigWidth <s #subNormDist
then (#leadingOneSig >> $$(WO~1))
else (#leadingOneSig << ($outSigWidthMinus1 - #subNormDist + $1)));
LETE underflowsToZero <- RetE (#inExp <s (* $2 - $(pow2 outExpWidthMinus1) - $outSigWidthMinus1 *) $0 - $ underflowToZeroInExpSltMinus);
LETE isTailMiddle : Bool <-
RetE (#tailSig == {< $$WO~1, $$ (wzero inSigWidthMinus1)>});
LETE isTailGtMiddle : Bool <-
RetE (#tailSig > {< $$WO~1, $$ (wzero inSigWidthMinus1)>});
LETE overflow <- RetE match Compare_dec.le_dec outExpWidthMinus2 inExpWidthMinus2 with
| left _ => (#inExp >s (* $(pow2 outExpWidthMinus1) - $1 *) $ overflowExpGlt)
| _ => $$false
end;
LETE roundToInf: Bool <-
RetE (#roundingMode_near_even
|| #roundingMode_near_maxMag
|| (#roundingMode_min && #inSign)
|| (#roundingMode_max && !#inSign));
LETE inexact_underflow: Bool <-
RetE (!(#nfIn @% "isZero")
&& !(#nfIn @% "isNaN")
&& !(#nfIn @% "isInf")
&& (#tailSig != $0
|| #underflowsToZero));
LETE inexact <-
RetE match Compare_dec.lt_dec outExpWidthMinus2 inExpWidthMinus2,
Compare_dec.lt_dec outSigWidthMinus2 inSigWidthMinus2
with
| right _, right _ => $$ false
| _, _ => (!(#nfIn @% "isInf") && !(#nfIn @% "isNaN")) && (#overflow || #inexact_underflow)
end;
LETE expSigPlus1RoundSig : Pair (Pair Bool Bool) (Bit outSigWidthMinus1) <-
match Compare_dec.lt_dec outSigWidthMinus2 inSigWidthMinus2 with
| left isLt =>
LETE truncSig:
Bit outSigWidth <-
RetE ((UniBit (TruncMsb
(inSigWidthMinus1 - outSigWidthMinus1)
(outSigWidth))
(castBits _ #leadingOneSig)) >> #subNormDist);
LETE isTruncSigOdd: Bool <-
RetE (UniBit (TruncLsb 1 outSigWidthMinus1)
(castBits _ #truncSig) == $1) ;
LETE sigPlus1 <-
RetE ((#roundingMode_min && #inSign && #inexact)
|| (#roundingMode_max && !#inSign && #inexact)
|| (#roundingMode_near_even && ((#isTailMiddle && #isTruncSigOdd)
||#isTailGtMiddle))
|| (#roundingMode_near_maxMag && (#isTailMiddle || #isTailGtMiddle)));
LETE roundSig : Bit outSigWidth <- RetE (IF #sigPlus1 then #truncSig + $1 else #truncSig);
LETE roundOrJammSig: (Bit outSigWidth) <-
RetE
(IF (#roundingMode_odd && #inexact)
then castBits _ ({< UniBit (TruncMsb 1 outSigWidthMinus1) (castBits _ #roundSig), Const ty WO~1 >})
else #roundSig);
LETE treatedSig: Bit (outSigWidthMinus1) <-
RetE (IF !(#nfIn @% "isNaN") && #overflow && !#roundToInf (* == roundToMaxMag *)
then $$(wones outSigWidthMinus1)
else ignoreMsb (#roundOrJammSig << #subNormDist));
LETE expPlus1RoundSig : Pair (Pair Bool Bool) (Bit outSigWidthMinus1) <-
RetE (STRUCT {
"fst" ::= (STRUCT {
"fst" ::=
(IF #subnormal
then (#truncSig != $0) && ((#roundSig << #subNormDist) == $0)
else (#truncSig != $0) && (#roundSig == $0)) ;
"snd" ::= #sigPlus1
});
"snd" ::= #treatedSig });
RetE #expPlus1RoundSig
| _ => RetE (STRUCT {
"fst" ::= (STRUCT {
"fst" ::= $$ false ;
"snd" ::= $$ false
});
"snd" ::= castBits _ ({<#inSig, Const ty (wzero (outSigWidthMinus1 - inSigWidthMinus1))>}) })
end;
LETE expPlus1 : Bool <- RetE (#expSigPlus1RoundSig @% "fst" @% "fst");
LETE sigRoundedUp : Bool <- RetE (#expSigPlus1RoundSig @% "fst" @% "snd" || (#roundingMode_odd && #inexact));
LETE roundSig : Bit outSigWidthMinus1 <- RetE (#expSigPlus1RoundSig @% "snd" );
LETE roundExp : Bit (outExpWidth + 1) <-
match Compare_dec.le_dec outExpWidthMinus2 inExpWidthMinus2 with
| left isLt1 =>
LETE truncExp : Bit (outExpWidth + 1) <-
RetE (UniBit (TruncLsb (outExpWidth + 1) (inExpWidth - outExpWidth))
(castBits _ #inExp));
LETE treatedExp : Bit (outExpWidth+1) <-
RetE (IF #overflow
then castBits _ (ZeroExtend 2 (Const ty (wones outExpWidthMinus1)))
else (IF #underflowsToZero
then (IF #sigRoundedUp
then $1 - $(2 ^ outExpWidthMinus1) - $outSigWidthMinus2
else $1 - $(2 ^ outExpWidthMinus1) - $outSigWidthMinus1)
else #truncExp));
RetE (IF #expPlus1 then (#treatedExp + $1) else #treatedExp)
| right _ => RetE (castBits _ (SignExtend (outExpWidth - inExpWidth) #inExp))
end;
LETE unboundedExpPlus1: Bool <-
match Compare_dec.lt_dec outSigWidthMinus2 inSigWidthMinus2 with
| left isLt =>
LETE truncSig:
Bit outSigWidth <-
RetE ((UniBit (TruncMsb
(inSigWidthMinus1 - outSigWidthMinus1)
(outSigWidth))
(castBits _ #leadingOneSig)));
LETE isTruncSigOdd: Bool <-
RetE ((UniBit (TruncLsb 1 outSigWidthMinus1)
(castBits _ #truncSig)) == $1) ;
LETE tailSigUE <-
RetE ((UniBit (TruncLsb
(inSigWidthMinus1 - outSigWidthMinus1)
(outSigWidth))
(castBits _ #leadingOneSig)));
LETE isTailMiddleUE <- RetE (#tailSigUE == (castBits _ ({< $$WO~1, $$(wzero (inSigWidthMinus1 - outSigWidth)) >})));
LETE isTailGtMiddleUE <- RetE (#tailSigUE > (castBits _ ({< $$WO~1, $$(wzero (inSigWidthMinus1 - outSigWidth)) >})));
LETE sigPlus1 <-
RetE ((#roundingMode_min && #inSign && (#tailSigUE != $0))
|| (#roundingMode_max && !#inSign && (#tailSigUE != $0))
|| (#roundingMode_near_even && ((#isTailMiddleUE && #isTruncSigOdd)
||#isTailGtMiddleUE))
|| (#roundingMode_near_maxMag && (#isTailMiddleUE || #isTailGtMiddleUE)));
RetE (IF #sigPlus1
then ((#truncSig != $0)
&& ((#truncSig+$1) == $0))
else ($$false))
| _ => RetE ($$ false)
end;
LETE afterRoundUnderflow <- RetE ((#roundExp <s (* $2 - $(2 ^ outExpWidthMinus1) *) $0 - $ underflowInExpSltMinus)
|| !#unboundedExpPlus1
&&(#inExp == ($1 - $(2 ^ outExpWidthMinus1))));
LETE treatedSubnormal <-
RetE (IF input @% "afterRounding"
then #afterRoundUnderflow
else #subnormal);
LETE underflow <- RetE (#treatedSubnormal && #inexact_underflow);
LETE overflow_afterRound <- RetE (#overflow || (#roundExp >s (* $( 2 ^ outExpWidthMinus1) - $1 *) $overflowExpGlt));
LETE outNF: NF outExpWidthMinus2 outSigWidthMinus2 <-
RetE (STRUCT {
"isNaN" ::= #nfIn @% "isNaN";
"isInf" ::= !(#nfIn @% "isNaN") && (#nfIn @% "isInf" ||
(!(#nfIn @% "isNaN") && #overflow_afterRound && #roundToInf));
"isZero" ::= !(#nfIn @% "isNaN") && !(# nfIn @% "isInf") && (#nfIn @% "isZero"||
match Compare_dec.le_dec outExpWidthMinus2 inExpWidthMinus2 with
| left _ => #underflowsToZero && !#sigRoundedUp
| right _ => $$ false
end);
"sign" ::= #nfIn @% "sign";
"sExp" ::= #roundExp;
"sig" ::= #roundSig
});
LETE isSignalingNaN <- RetE ((UniBit (TruncMsb _ 1 ) (#nfIn @% "sig")) == $0);
LETE outFlags: ExceptionFlags <-
RetE (STRUCT {
"invalid" ::= (#nfIn @% "isNaN") && #isSignalingNaN;
"infinite" ::= $$false;
"overflow" ::= #overflow_afterRound && !(#nfIn @% "isInf") && !(#nfIn @% "isNaN") && !(#nfIn @% "isZero");
"underflow" ::= #underflow && !(#nfIn @% "isZero");
"inexact" ::= #inexact && !(#nfIn @% "isZero")
});
LETE outNF_final : NF _ _ <-
RetE ((#outNF @%[ "sign" <- #outNF @% "sign" && !(#outNF @% "isNaN")]
@%[ "sig" <- IF #outNF @% "isNaN" then {< $$ WO~1, $ 0 >} else #outNF @% "sig"]));
LETE RoundNF: OpOutput outExpWidthMinus2 outSigWidthMinus2 <-
RetE (STRUCT {
"out" ::= #outNF_final;
"exceptionFlags" ::= #outFlags });
RetE #RoundNF
);
abstract lia.
Defined.
Definition RoundNF_action: ActionT ty (OpOutput outExpWidthMinus2 outSigWidthMinus2) :=
convertLetExprSyntax_ActionT RoundNF_expr.
Close Scope kami_action.
End RoundAny.
Section RoundDef.
Variable ty: Kind -> Type.
Variable inExpWidthMinus2 inSigWidthMinus2: nat.
Variable outExpWidthMinus2 outSigWidthMinus2: nat.
Variable underflowInExpSltMinus underflowToZeroInExpSltMinus overflowExpGlt: nat.
Variable input : (RoundInput inExpWidthMinus2 inSigWidthMinus2) @# ty.
Local Notation outExpWidthMinus1 := (outExpWidthMinus2 + 1).
Local Notation outSigWidthMinus1 := (outSigWidthMinus2 + 1).
Definition RoundNF_def_expr: LetExprSyntax ty (OpOutput outExpWidthMinus2 outSigWidthMinus2) :=
RoundNF_expr outExpWidthMinus2 outSigWidthMinus2
(2 ^ outExpWidthMinus1 - 2)
(2 ^ outExpWidthMinus1 + outSigWidthMinus1 - 2)
(2 ^ outExpWidthMinus1 - 1)
input.
Definition RoundNF_def_action: ActionT ty (OpOutput outExpWidthMinus2 outSigWidthMinus2) :=
convertLetExprSyntax_ActionT RoundNF_def_expr.
End RoundDef.
Section RoundDef1.
Variable ty: Kind -> Type.
Variable inExpWidthMinus2 inSigWidthMinus2: nat.
Variable outExpWidthMinus2 outSigWidthMinus2: nat.
Variable input : (RoundInput inExpWidthMinus2 inSigWidthMinus2) @# ty.
Variable invalidExc infiniteExc: Bool @# ty.
Definition RoundRecFN := STRUCT_TYPE
{ "outNF" :: NF outExpWidthMinus2 outSigWidthMinus2 ;
"outFN" :: FN outExpWidthMinus2 outSigWidthMinus2 ;
"outRawFloat" :: RawFloat outExpWidthMinus2 outSigWidthMinus2 ;
"out" :: RecFN outExpWidthMinus2 outSigWidthMinus2 ;
"exceptionFlags" :: ExceptionFlags }.
Local Open Scope kami_expr.
Definition RoundNF_to_RecFN_def_expr: LetExprSyntax ty RoundRecFN :=
LETE inp : NF inExpWidthMinus2 inSigWidthMinus2 <- RetE (input @% "in") ;
LETE modInput : RoundInput inExpWidthMinus2 inSigWidthMinus2 <- RetE (STRUCT { "in" ::= STRUCT { "isNaN" ::= (invalidExc || (#inp @% "isNaN")) ;
"isInf" ::= (infiniteExc || (#inp @% "isInf")) ;
"isZero" ::= #inp @% "isZero" ;
"sign" ::= #inp @% "sign" ;
"sExp" ::= #inp @% "sExp" ;
"sig" ::= #inp @% "sig" } ;
"afterRounding" ::= input @% "afterRounding" ;
"roundingMode" ::= input @% "roundingMode"});
LETE val: OpOutput outExpWidthMinus2 outSigWidthMinus2 <- RoundNF_def_expr outExpWidthMinus2 outSigWidthMinus2 #modInput;
LETE valOut : NF outExpWidthMinus2 outSigWidthMinus2 <- RetE (#val @% "out") ;
LETE valExcept : ExceptionFlags <- RetE (#val @% "exceptionFlags") ;
LETE final: RecFN outExpWidthMinus2 outSigWidthMinus2 <- RetE (getRecFN_from_NF #valOut);
LETE valFN : FN outExpWidthMinus2 outSigWidthMinus2 <- RetE (getFN_from_NF #valOut);
LETE finalVal: RoundRecFN <- RetE (STRUCT { "outNF" ::= #valOut;
"outFN" ::= #valFN;
"outRawFloat" ::= (getRawFloat_from_FN #valFN);
"out" ::= #final;
"exceptionFlags" ::= STRUCT {"invalid" ::= invalidExc ;
"infinite" ::= infiniteExc ;
"overflow" ::= #valExcept @% "overflow" ;
"underflow" ::= #valExcept @% "underflow" ;
"inexact" ::= #valExcept @% "inexact" }});
RetE #finalVal.
Local Close Scope kami_expr.
End RoundDef1.