forked from github-linguist/linguist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalfAndDouble.bb
147 lines (93 loc) · 2.52 KB
/
HalfAndDouble.bb
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
Local bk = CreateBank(8)
PokeFloat bk, 0, -1
Print Bin(PeekInt(bk, 0))
Print %1000000000000000
Print Bin(1 Shl 31)
Print $1f
Print $ff
Print $1f + (127 - 15)
Print Hex(%01111111100000000000000000000000)
Print Hex(~%11111111100000000000000000000000)
Print Bin(FloatToHalf(-2.5))
Print HalfToFloat(FloatToHalf(-200000000000.0))
Print Bin(FToI(-2.5))
WaitKey
End
; Half-precision (16-bit) arithmetic library
;============================================
Global Half_CBank_
Function FToI(f#)
If Half_CBank_ = 0 Then Half_CBank_ = CreateBank(4)
PokeFloat Half_CBank_, 0, f
Return PeekInt(Half_CBank_, 0)
End Function
Function HalfToFloat#(h)
Local signBit, exponent, fraction, fBits
signBit = (h And 32768) <> 0
exponent = (h And %0111110000000000) Shr 10
fraction = (h And %0000001111111111)
If exponent = $1F Then exponent = $FF : ElseIf exponent Then exponent = (exponent - 15) + 127
fBits = (signBit Shl 31) Or (exponent Shl 23) Or (fraction Shl 13)
If Half_CBank_ = 0 Then Half_CBank_ = CreateBank(4)
PokeInt Half_CBank_, 0, fBits
Return PeekFloat(Half_CBank_, 0)
End Function
Function FloatToHalf(f#)
Local signBit, exponent, fraction, fBits
If Half_CBank_ = 0 Then Half_CBank_ = CreateBank(4)
PokeFloat Half_CBank_, 0, f
fBits = PeekInt(Half_CBank_, 0)
signBit = (fBits And (1 Shl 31)) <> 0
exponent = (fBits And $7F800000) Shr 23
fraction = fBits And $007FFFFF
If exponent
exponent = exponent - 127
If Abs(exponent) > $1F
If exponent <> ($FF - 127) Then fraction = 0
exponent = $1F * Sgn(exponent)
Else
exponent = exponent + 15
EndIf
exponent = exponent And %11111
EndIf
fraction = fraction Shr 13
Return (signBit Shl 15) Or (exponent Shl 10) Or fraction
End Function
Function HalfAdd(l, r)
End Function
Function HalfSub(l, r)
End Function
Function HalfMul(l, r)
End Function
Function HalfDiv(l, r)
End Function
Function HalfLT(l, r)
End Function
Function HalfGT(l, r)
End Function
; Double-precision (64-bit) arithmetic library)
;===============================================
Global DoubleOut[1], Double_CBank_
Function DoubleToFloat#(d[1])
End Function
Function FloatToDouble(f#)
End Function
Function IntToDouble(i)
End Function
Function SefToDouble(s, e, f)
End Function
Function DoubleAdd(l, r)
End Function
Function DoubleSub(l, r)
End Function
Function DoubleMul(l, r)
End Function
Function DoubleDiv(l, r)
End Function
Function DoubleLT(l, r)
End Function
Function DoubleGT(l, r)
End Function
;~IDEal Editor Parameters:
;~F#1A#20#2F
;~C#Blitz3D