-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest16inf.cpp
More file actions
208 lines (187 loc) · 5.11 KB
/
Copy pathtest16inf.cpp
File metadata and controls
208 lines (187 loc) · 5.11 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
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
//exhaustive test converting fp to xlns16
//check edge cases near subnormal, inf
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
//uncomment for ideal sb and db:
//#define xlns16_ideal
//should be tested both w and w/o xlns16_table
#define xlns16_table
#include "xlns16.cpp"
//these are now in xlns16.cpp:
//#define xlns16_pos_inf 0x7FFF
//#define xlns16_neg_inf 0xFFFF
//used for debug (identical to new def of fp2xlns16)
xlns16 myfp2xlns16(double x)
{
if ((x>-2.938747e-39)&&(x<2.938747e-39))
return(xlns16_zero);
else if (x> 3.40282286e+38)
return(xlns16_pos_inf);
else if (x< -3.40282286e+38)
return(xlns16_neg_inf);
else if (x > 0.0)
return xlns16_abs((xlns16_signed) ((log(x)/log(2.0))*xlns16_scale))
^xlns16_logsignmask;
else
return (((xlns16_signed) ((log(fabs(x))/log(2.0))*xlns16_scale))
|xlns16_signmask)^xlns16_logsignmask;
}
void testcvt(double x)
{
printf("%e converts to %04x and back to %e\n",x,fp2xlns16(x),xlns162fp(fp2xlns16(x)));
}
void test_pos_norm_floats()
{
float x, eps;
int ix;
xlns16 lx;
bool ok;
eps = 1e-2;
ok = 0;
for (ix = 0x00800000; ix<=0x7F800000; ix++)
{ if ((ix & 0x007FFFFF)==0)
{ if (ok)
printf(" ok");
ok = 1;
printf("\nnormal %08x",ix);
}
x = * ((float *) (&ix));
lx = fp2xlns16(x);
if (fabs(x-xlns162fp(lx))/x>eps)
{ ok=0;
printf("%08x %e %e rerr=%e\n",ix,x,xlns162fp(lx),fabs(x-xlns162fp(lx))/x);
}
}
if (ok)
printf(" ok\n\n");
}
void test_neg_norm_floats()
{
float x, eps;
int ix;
xlns16 lx;
bool ok;
eps = 1e-2;
ok = 0;
for (ix = 0x80800000; ix<=0xFF800000; ix++)
{ if ((ix & 0x007FFFFF)==0)
{ if (ok)
printf(" ok");
ok = 1;
printf("\nnormal %08x",ix);
}
x = * ((float *) (&ix));
lx = fp2xlns16(x);
if (fabs(x-xlns162fp(lx))/x>eps)
{ ok=0;
printf("%08x %e %e rerr=%e\n",ix,x,xlns162fp(lx),fabs(x-xlns162fp(lx))/x);
}
}
if (ok)
printf(" ok\n\n");
}
void test_pos_subnorm_floats()
{
float x, eps;
int ix;
xlns16 lx;
eps = 1e-2;
printf("pos subnormals\n");
for (ix =0x00000000; ix<=0x00800000; ix++)
{
x = * ((float *) (&ix));
lx = fp2xlns16(x);
if (fabs(x-xlns162fp(lx))>eps)
printf("%08x %e l=%08x %e aerr=%e\n",ix,x,lx,xlns162fp(lx),fabs(x-xlns162fp(lx)));
}
}
void test_neg_subnorm_floats()
{
float x, eps;
int ix;
xlns16 lx;
eps = 1e-2;
printf("neg subnormals\n");
for (ix =0x80000000; ix<=0x80800000; ix++)
{
x = * ((float *) (&ix));
lx = fp2xlns16(x);
if (fabs(x-xlns162fp(lx))>eps)
printf("%08x %e l=%08x %e aerr=%e\n",ix,x,lx,xlns162fp(lx),fabs(x-xlns162fp(lx)));
}
}
int main(void)
{
test_neg_subnorm_floats();
test_pos_subnorm_floats();
test_pos_norm_floats();
test_neg_norm_floats();
testcvt(-1.0/0.0);
testcvt(-1e100);
testcvt(-3.40283e+38);
testcvt(-3.40282e+38);
testcvt(-3.402812e+38);
testcvt(-3.402811e+38);
testcvt(-3.4e+38);
testcvt(-3.0e+38);
testcvt(-2.0);
testcvt(-1.0);
testcvt(-0.5);
testcvt(-3.0e-39);
testcvt(-2.9388e-39);
testcvt(-2.938740e-39);
testcvt(-2.938739e-39);
testcvt(-2.938738e-39);
testcvt(-2.938737e-39);
testcvt(-2.938736e-39);
testcvt(-2.938735877e-39);
testcvt(-2.9e-39);
testcvt(-1e-100);
testcvt(-0.0);
testcvt(0.0);
testcvt(1e-100);
testcvt(2.9e-39);
testcvt(2.938735877e-39);
testcvt(2.938736e-39);
testcvt(2.938737e-39);
testcvt(2.938738e-39);
testcvt(2.938739e-39);
testcvt(2.938740e-39);
testcvt(2.938741e-39);
testcvt(2.938742e-39);
testcvt(2.938743e-39);
testcvt(2.938744e-39);
testcvt(2.938745e-39);
testcvt(2.938746e-39);
testcvt(2.938747e-39);
testcvt(2.9387471e-39);
testcvt(2.9387472e-39);
testcvt(2.9387473e-39);
testcvt(2.9387474e-39);
testcvt(2.9387475e-39);
testcvt(2.9387476e-39);
testcvt(2.9387477e-39);
testcvt(2.9387478e-39);
testcvt(2.9387479e-39);
testcvt(2.938748e-39);
testcvt(2.938750e-39);
testcvt(2.938760e-39);
testcvt(2.938770e-39);
testcvt(2.938780e-39);
testcvt(2.938790e-39);
testcvt(2.9388e-39);
testcvt(3.0e-39);
testcvt(0.5);
testcvt(1.0);
testcvt(2.0);
testcvt(3.0e+38);
testcvt(3.4e+38);
testcvt(3.402811e+38);
testcvt(3.402812e+38);
testcvt(3.40282e+38);
testcvt(3.40283e+38);
testcvt(1e100);
testcvt(1.0/0.0);
return 1;
}