-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatTesting.py
269 lines (252 loc) · 8.92 KB
/
FloatTesting.py
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
from MyFloat import MyFloat
import numpy as np
import operator
import random as rand
import itertools
# I am intentionally hitting failure cases to make sure I handle them properly
np.seterr(all='ignore')
op2str = {
operator.add: '+',
operator.sub: '-',
operator.mul: '*',
operator.truediv: '/',
operator.neg: '-unary',
np.sin: 'sin',
}
randomizedCases = 10_000
class FloatTesting:
def __init__(self, typeToTest=np.float16, manualCases=True, utahCases=False,
randomCases=False, previouslyFailedRandomCases=False, specialCases=False,
specialAndRandomCases1=True, specialAndRandomCases2=True):
self.cases1 = []
self.cases2 = []
self.typeToTest = typeToTest
self.operators2 = [
operator.add,
operator.sub,
operator.mul,
operator.truediv
]
self.operators1 = [
operator.neg,
# np.sin,
]
if manualCases:
self.cases2.extend(self.buildManualCases())
if utahCases:
self.cases2.extend(self.buildUtahCases())
if randomCases:
self.cases2.extend(self.buildRandomCases())
if previouslyFailedRandomCases:
self.cases2.extend(self.buildPreviouslyFailedRandomCases())
if specialCases:
self.cases2.extend(self.buildSpecialCases())
if specialAndRandomCases1:
self.cases1.extend(self.buildSpecialAndRandomCases1())
if specialAndRandomCases2:
self.cases2.extend(self.buildSpecialAndRandomCases2())
def buildManualCases(self):
return [
[6e-8, 2e-7],
[1, 1.5],
[1, 3],
[2, 4],
[6.09e-05, 0.995],
[1e-07, 0.995],
[6e-08, 6.09e-05],
[6.104e-05, 6.11e-05],
[3.05e-5, 3.05e-5],
[6e-08, 0.9995],
[0.9995, -1.0],
[300, 700],
[1.0, 1.0],
[0.5874, -0.1327],
[2050, 3],
[0.7217, 1.865E-3],
[1.0/3.0, 2.0/3.0],
[2048, 3],
[1, 0],
[1, -2],
[1, -1],
[1.001, 1.0],
[0.0, 0.0],
[0.0, -0.0],
[-0.0, 0.0],
[-0.0, -0.0],
[np.infty - np.infty, np.nan], # NaN from invalid calc + plain NaN
[np.nan, np.infty - np.infty], # plain NaN + NaN from invalid calc
[3, 7],
[30, 70],
[300, 700],
[3000, 7000],
[3, 7000],
[1, 2047],
[2, 2047],
[-27, np.infty],
[np.infty, np.infty],
[np.infty, -np.infty],
[-np.infty, -np.infty],
[-np.infty, np.infty],
[np.NaN, 0],
[3.052E-5, 3.052E-5], #subnormal + subnormal = normal
[6.104E-5, -3.052E-5], #normal + subnormal = subnormal
[6.104E-5, -6.11E-5], #normal + normal = subnormal
]
def buildUtahCases(self):
utahCases = []
with open('UtahActualCalculationErrors.txt') as f:
utahCases.extend([[self.typeToTest(x) for x in line.split(' ')] for line in f.readlines()])
return utahCases
def buildRandomCases(self):
randomCases = []
for _ in range(randomizedCases):
randomCases.append([self.RandFloat(), self.RandFloat()])
return randomCases
def buildPreviouslyFailedRandomCases(self):
previouslyFailedRandomCases = []
with open('RandomTestFailures.txt') as f:
previouslyFailedRandomCases.extend([[self.typeToTest(x) for x in line.split(' ')] for line in f.readlines()])
return previouslyFailedRandomCases
def buildSpecialCases(self):
f = MyFloat(valueFormat=self.typeToTest)
def getNegative(toNegate):
assert isinstance(toNegate, MyFloat)
negated = MyFloat(toNegate.original)
negated.S = '1' if negated.S == '0' else '0'
negated.Reinterpret()
return negated
specialValues = [
f.Zero(),
f.MinSubNorm(),
f.NextMinSubNorm(),
f.MidSubNorm(),
f.PrevMaxSubNorm(),
f.MaxSubNorm(),
f.MinNorm(),
f.NextMinNorm(),
f.PrevOne(),
f.One(),
f.NextOne(),
f.MidNorm(),
f.PrevMaxNorm(),
f.MaxNorm(),
f.Infinity(),
f.DefaultNaN()
]
specialValues = specialValues + [getNegative(x) for x in specialValues]
return [[lhs.original, rhs.original] for lhs, rhs in itertools.combinations_with_replacement(specialValues, 2)]
def buildSpecialAndRandomCases1(self):
f = MyFloat(valueFormat=self.typeToTest)
def getNegative(toNegate):
assert isinstance(toNegate, MyFloat)
negated = MyFloat(toNegate.original)
negated.S = '1' if negated.S == '0' else '0'
negated.Reinterpret()
return negated
specialValues = [
f.Zero(),
f.MinSubNorm(),
f.NextMinSubNorm(),
f.MidSubNorm(),
f.PrevMaxSubNorm(),
f.MaxSubNorm(),
f.MinNorm(),
f.NextMinNorm(),
f.PrevOne(),
f.One(),
f.NextOne(),
f.MidNorm(),
f.PrevMaxNorm(),
f.MaxNorm(),
f.Infinity(),
f.DefaultNaN()
]
specialValues += [MyFloat(self.RandFloat()) for _ in range(50)]
specialValues = specialValues + [getNegative(x) for x in specialValues]
return [x.original for x in specialValues]
def buildSpecialAndRandomCases2(self):
f = MyFloat(valueFormat=self.typeToTest)
def getNegative(toNegate):
assert isinstance(toNegate, MyFloat)
negated = MyFloat(toNegate.original)
negated.S = '1' if negated.S == '0' else '0'
negated.Reinterpret()
return negated
specialValues = [
f.Zero(),
f.MinSubNorm(),
f.NextMinSubNorm(),
f.MidSubNorm(),
f.PrevMaxSubNorm(),
f.MaxSubNorm(),
f.MinNorm(),
f.NextMinNorm(),
f.PrevOne(),
f.One(),
f.NextOne(),
f.MidNorm(),
f.PrevMaxNorm(),
f.MaxNorm(),
f.Infinity(),
f.DefaultNaN()
]
specialValues += [MyFloat(self.RandFloat()) for _ in range(50)]
specialValues = specialValues + [getNegative(x) for x in specialValues]
return [[lhs.original, rhs.original] for lhs, rhs in itertools.combinations_with_replacement(specialValues, 2)]
def RandFloat(self):
return MyFloat.bin2floatStatic(
self.typeToTest,
format(
rand.getrandbits(np.dtype(self.typeToTest).itemsize * 8),
'01b'))
def Test2Op(self):
for operation in self.operators2:
passed = 0
total = 0
for a, b in self.cases2:
A = MyFloat(a, self.typeToTest)
B = MyFloat(b, self.typeToTest)
C = operation(A, B)
mine = C.original
actual = operation(A.original, B.original)
if C.EqualsFloatBits(actual):
passed += 1
# print('----------------')
# print(A, op2str[operation], B)
# print('Matches: ', MyFloat(actual))
pass
else:
print('----------------')
print(A, op2str[operation], B)
# print(A.original, B.original)
print('FAILED: ', C, ' Actual: ', MyFloat(actual))
pass
total += 1
print(op2str[operation] + ':', str(100*passed/total) + '%')
def Test1Op(self):
for operation in self.operators1:
passed = 0
total = 0
for a in self.cases1:
A = MyFloat(a, self.typeToTest)
C = operation(A)
mine = C.original
actual = operation(A.original)
if C.EqualsFloatBits(actual):
passed += 1
# print('----------------')
# print(op2str[operation], A)
# print('Matches: ', MyFloat(actual))
pass
else:
print('----------------')
print(op2str[operation], A)
print('FAILED: ', C, ' Actual: ', MyFloat(actual))
pass
total += 1
print(op2str[operation] + ':', str(100*passed/total) + '%')
def RunTesting(self):
self.Test1Op()
self.Test2Op()
if __name__ == '__main__':
FloatTesting(typeToTest=np.float16).RunTesting()