forked from coder-mike/FixedPoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduce_test.py
More file actions
182 lines (162 loc) · 7.9 KB
/
produce_test.py
File metadata and controls
182 lines (162 loc) · 7.9 KB
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
from fixedpoint import FixedPoint
from random import random
from tqdm import tqdm
output_file = "tests.txt"
NUM_TESTS = 1e2
MIN_BOUND = -100
MAX_BOUND = 100
# Let's say that every line in the file will be a test
# If the line starts with #, it means it is an assignment test
# If the line starts with a +, it means it is an addition test
# If the line starts with a -, it means it is a subtraction test
# If the line starts with a *, it means it is a multiplication test
# If the line starts with a /, it means it is a division test
# If the line starts with a --, it is a unary minus test
# If the line starts with a ==, it is an equality test
# If the line starts with a <, it is a less than test
# If the line starts with a >, it is a greater than test
# If the line starts with a !, it means the next two numbers are the new integer and fractional bits
# Do I need anything else?
OVERFLOW_MODE = 'clamp'
ROUNDING_MODE = 'down'
def assignment_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
# fixed_num = FXnum(num, fxpt)
# write to file
with open(output_file, 'a') as f:
f.write(f"# {num} {float((FixedPoint(num, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')))}\n")
def addition_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num1 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
num2 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
fixed_num1 = FixedPoint(num1, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
fixed_num2 = FixedPoint(num2, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
#fixed_num1.clamp(n-1)
#fixed_num2.clamp(n-1)
summ = fixed_num1 + fixed_num2
summ = float(summ)
summ = FixedPoint(summ, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
summ = float(summ)
# write to file
with open(output_file, 'a') as f:
f.write(f"+ {num1} {num2} {summ}\n")
def subtraction_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num1 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
num2 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
fixed_num1 = FixedPoint(num1, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
fixed_num2 = FixedPoint(num2, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
sub = float(FixedPoint(float(fixed_num1 - fixed_num2), True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore'))
# write to file
with open(output_file, 'a') as f:
f.write(f"- {num1} {num2} {sub}\n")
def multiplication_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num1 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
num2 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
fixed_num1 = FixedPoint(num1, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
fixed_num2 = FixedPoint(num2, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
mult = float(FixedPoint(float(fixed_num1 * fixed_num2), True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore'))
# write to file
with open(output_file, 'a') as f:
f.write(f"* {num1} {num2} {mult}\n")
def division_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num1 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
num2 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
fixed_num1 = FixedPoint(num1, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
fixed_num2 = FixedPoint(num2, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
if(fixed_num2 == 0):
continue
# write to file
with open(output_file, 'a') as f:
f.write(f"/ {num1} {num2} {fixed_num1 / fixed_num2}\n")
def unary_minus_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
fixed_num =float( FixedPoint(num, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore'))
# write to file
with open(output_file, 'a') as f:
f.write(f"( {num} {-fixed_num}\n")
def equality_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num1 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
num2 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
fixed_num1 = FixedPoint(num1, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
fixed_num2 = FixedPoint(num2, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
# write to file
with open(output_file, 'a') as f:
f.write(f"== {num1} {num2} {fixed_num1 == fixed_num2}\n")
def less_than_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num1 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
num2 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
fixed_num1 = FixedPoint(num1, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
fixed_num2 = FixedPoint(num2, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
# write to file
with open(output_file, 'a') as f:
f.write(f"< {num1} {num2} {fixed_num1 < fixed_num2}\n")
def greater_than_test(n, m):
# use tqdm for loop
for _ in range(int(NUM_TESTS)):
# produce a random number
num1 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
num2 = (random() * (MAX_BOUND - MIN_BOUND)) + MIN_BOUND
# convert to fixed point
fixed_num1 = FixedPoint(num1, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
fixed_num2 = FixedPoint(num2, True, n, m, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
# write to file
with open(output_file, 'a') as f:
f.write(f"> {num1} {num2} {fixed_num1 > fixed_num2}\n")
def test_num(N, M):
with open(output_file, 'a') as f:
f.write(f"! {N} {M}\n")
assignment_test(N, M)
addition_test(N, M)
subtraction_test(N, M)
multiplication_test(N, M)
#division_test(N, M)
unary_minus_test(N, M)
#equality_test(N, M)
#less_than_test(N, M)
#greater_than_test(N, M)
#N=6
#custom_fxpt = FXfamily(n_bits=32, n_intbits=N)
for N in tqdm(range(8, 9)):
for M in tqdm(range(8, 9)):
#print("N: ", N, "M: ", M)
test_num(N, M)
"""
# FAIL -130.622 + -211.598 == 125.379 + 44.406 == -86.215 != 169.785
# example
custom_fixed = FixedPoint(-130.622, True, m=8, n=8, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
print(float(custom_fixed))
num2 = FixedPoint(-211.598, True, m=8, n=8, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')
print(float(num2))
print(float(FixedPoint(float(custom_fixed + num2), True, m=8, n=8, overflow=OVERFLOW_MODE, rounding=ROUNDING_MODE, overflow_alert='ignore')))
"""