-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFToIN.v
179 lines (146 loc) · 7.76 KB
/
NFToIN.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
Require Import Kami.AllNotations FpuKami.Definitions.
Section Definitions.
Open Scope nat.
Variable intWidthMinus2 expWidthMinus2 sigWidthMinus2: nat.
Local Notation intWidthMinus1 := (intWidthMinus2 + 1).
Local Notation intWidth := (intWidthMinus1 + 1).
Local Notation expWidthMinus1 := (expWidthMinus2 + 1).
Local Notation expWidth := (expWidthMinus1 + 1).
Local Notation sigWidthMinus1 := (sigWidthMinus2 + 1).
Local Notation sigWidth := (sigWidthMinus1 + 1).
Variable expWidth_prop: expWidthMinus2 >= 2.
Variable expWidthMinus2_plus4_gt_sigWidth: 2 ^ expWidthMinus2 + 4 > sigWidth.
Section Ty.
Variable ty: Kind -> Type.
Section Convert.
Definition NFToINInput := STRUCT_TYPE {
"inNF" :: NF expWidthMinus2 sigWidthMinus2;
"roundingMode" :: Bit 3;
"signedOut" :: Bool
}.
Definition NFToINOutput := STRUCT_TYPE {
"outIN" :: Bit intWidth;
"flags" :: ExceptionFlags
}.
Open Scope kami_expr.
Variable input: NFToINInput @# ty.
Definition roundingMode_near_even := input @% "roundingMode" == $$ round_near_even.
Definition roundingMode_minMag := input @% "roundingMode" == $$ round_minMag.
Definition roundingMode_min := input @% "roundingMode" == $$ round_min.
Definition roundingMode_max := input @% "roundingMode" == $$ round_max.
Definition roundingMode_odd := input @% "roundingMode" == $$ round_odd.
Definition roundingMode_near_maxMag := input @% "roundingMode" == $$ round_near_maxMag.
Definition signedOut := input @% "signedOut" .
Definition inNF := input @% "inNF".
Definition inExp := inNF @% "sExp".
Definition inSig := inNF @% "sig".
Definition inSign := inNF @% "sign".
Definition bigIntSz: nat :=(2 ^ expWidthMinus1).
Definition ZeroExpand n sz `(i: Expr ty (SyntaxKind (Bit n)))
:Expr ty (SyntaxKind (Bit (n + sz))).
Proof.
refine (castBits _ ({< i, $$(wzero sz) >})); abstract (intuition auto with *).
Defined.
Lemma expWidth_ge_sigWidth_local:
(2 ^ expWidthMinus1 > sigWidth)%nat.
Proof.
rewrite ?Nat.pow_add_r; simpl.
assert (sth: (2 ^ expWidthMinus2 >= 4)%nat). {
pose proof (@Nat.pow_le_mono_r 2 _ _ ltac:(lia) expWidth_prop).
assumption.
}
lia.
Qed.
Open Scope kami_action.
Definition NFToIN_expr
: NFToINOutput ## ty.
Proof.
refine (
LETC isExpPositive <- (inExp >=s $0);
LETC leadingOneSig: Bit sigWidth <- {< $$ WO~1, inSig >};
LETC bigSig: Bit bigIntSz <- castBits _ (ZeroExpand (bigIntSz - sigWidth) #leadingOneSig);
LETC tailSig: Bit sigWidth <- (IF #isExpPositive || inNF @% "isZero"
then #leadingOneSig << (inExp+$1)
else (IF inExp == ($0 - $1)
then #leadingOneSig
else #leadingOneSig >> $$WO~1));
LETC isTailMiddle : Bool <- (#tailSig == {< $$WO~1, $$ (wzero sigWidthMinus1)>});
LETC isTailGtMiddle : Bool <- (#tailSig > {< $$WO~1, $$ (wzero sigWidthMinus1)>});
LETC correctSig: Bit bigIntSz <- #bigSig >> ($bigIntSz - inExp - $1);
LETC truncSig <- match Compare_dec.lt_dec intWidth bigIntSz with
| left _ => UniBit (TruncLsb (intWidth) (bigIntSz - intWidth)) (castBits _ #correctSig)
| _ => castBits _ (ZeroExtend (intWidth - bigIntSz) #correctSig)
end;
LETC truncSigHead <- match Compare_dec.lt_dec intWidth bigIntSz with
| left _ => UniBit (TruncMsb (intWidth) (bigIntSz - intWidth)) (castBits _ #correctSig)
| _ => $ 0
end;
LETC isTruncSigOdd: Bool <- UniBit (TruncLsb 1 intWidthMinus1) (castBits _ #truncSig) == $1 ;
LETC roundIncr <-
((roundingMode_min && inSign && #tailSig != $0)
|| (roundingMode_max && !inSign && #tailSig != $0)
|| (roundingMode_near_even && ((#isTailMiddle && #isTruncSigOdd)
||#isTailGtMiddle))
|| (roundingMode_near_maxMag && (#isTailMiddle || #isTailGtMiddle)));
LETC roundIN : Bit intWidth <- (IF #roundIncr then #truncSig + $1 else #truncSig);
LETC roundOrJammIN: (Bit intWidth) <-
(IF (roundingMode_odd && #tailSig != $0)
then castBits _ ({< UniBit (TruncMsb 1 intWidthMinus1) (castBits _ #roundIN), Const ty WO~1 >})
else #roundIN);
LETC signedIN <-
IF inSign
then $0 - #roundOrJammIN
else #roundOrJammIN;
LETC overflowBiasOut: Bit (expWidth + 1) <-
ZeroExtend _ (IF signedOut
then $1
else $0);
LETC overflowBiasSign: Bit (expWidth + 1) <-
ZeroExtend _ (IF inSign
then $1
else $0);
LETC overflow_afterRound <- !inSign
&& (((#signedIN <s $0) && signedOut)
|| (!signedOut
&& #roundIncr
&& (#truncSig == $$(wones intWidth))));
LETC overflow_exp <- ((inExp >s $intWidthMinus1 - #overflowBiasOut)
&& !(inSign
&& (#signedIN == {<$$WO~1, $$(wzero intWidthMinus1)>})
&& inExp == $intWidthMinus1));
LETC roundsToZero <- (#signedIN == $0) && (inExp <=s $0);
LETC invalidExec <-
(inNF @% "isNaN"
|| inNF @% "isInf"
|| #overflow_exp
|| #overflow_afterRound
|| (inSign && !signedOut && !#roundsToZero));
LETC finalIN <-
(IF !#invalidExec
then #signedIN
else (IF signedOut
then (IF inSign && !(inNF @% "isNaN")
then {<$$ WO~1, $$(wzero intWidthMinus1)>}
else {<$$ WO~0, $$(wones intWidthMinus1)>})
else (IF inSign && !(inNF @% "isNaN")
then $0
else $$(wones intWidth))));
LETC outFlags: ExceptionFlags <- STRUCT {
"invalid" ::= (#invalidExec);
"infinite" ::= $$false;
"overflow" ::= $$false;
"underflow" ::= $$false;
"inexact" ::= !#invalidExec && (#tailSig != $0)
};
LETC output: NFToINOutput <- STRUCT {
"outIN" ::= #finalIN;
"flags" ::= #outFlags
};
RetE #output);
abstract (unfold bigIntSz in *; pose proof expWidth_ge_sigWidth_local; simpl; lia).
Defined.
Definition NFToIN_action: ActionT ty (NFToINOutput)
:= convertLetExprSyntax_ActionT NFToIN_expr.
End Convert.
End Ty.
End Definitions.