-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAX_adder.cpp
396 lines (339 loc) · 9.15 KB
/
AX_adder.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <random>
#include <iostream>
#include <time.h>
//#include <values.h>
#define mask 0xFFF
#define bitnum 12
#define emask 0x7FFFFF
/* endianness testing */
const int EndianTest = 0x04030201;
#define LITTLE_ENDIAN() (*((const char *) &EndianTest) == 0x01)
/* extract nth LSB from object stored in lvalue x */
#define GET_BIT(x, n) ((((const char *) &x)[LITTLE_ENDIAN() ? (n) / CHARBITS : sizeof(x) - (n) / CHARBITS - 1] >> ((n) % CHARBITS)) & 0x01)
#define PUT_BIT(x, n) (putchar(GET_BIT((x), (n)) ? '1' : '0'))
#define not_real_number printf("Not real number\n"); \
exit(1);
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 generator(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<int> RandomExponent(0, 254);
std::uniform_int_distribution<int> RandomMantissa(0, 0x7FFFFF);
std::uniform_int_distribution<int> RandomSign(0, 1);
int nnn = 1;
int checknum = 0;
//Union 사용
typedef union {
float f;
struct {
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} parts;
} float_cast;
float_cast makeFP();
float_cast FPAdder(float_cast a, float_cast b, int case_num);
float_cast makeFP() {
//exponet => 127(7F) ~ -128(80)
//mantissa => 524287(7FFFF) ~ 0(0)
float_cast num;
num.parts.exponent = RandomExponent(generator); //0~255 -> 2^(exponent - 126) * 1.(mantissa)
//if exponent == 255 -> inf
num.parts.mantissa = RandomMantissa(generator);
num.parts.sign = RandomSign(generator);
return num;
}
void mantissa_cal(float_cast &z, float_cast &x, float_cast &y, int subEx) {
z.parts.exponent = x.parts.exponent;
if (subEx > 23) //shift 가 mantissa의 23비트 넘어서면 0으로 초기화!
y.parts.mantissa = 0;
else {
//a.parts.mantissa >>= abs(subEx);
if (y.parts.exponent != 0)
y.parts.mantissa = (y.parts.mantissa >> 1) | 0x400000;
if (subEx > 1)
y.parts.mantissa >>= subEx - 1;
}
}
unsigned int LOA(unsigned int a, unsigned int b, int mode)
{
unsigned int m, n, sum;
int carry;
m = a & mask;
n = b & mask;
sum = m | n;
carry = (m >> bitnum - 1) & (n >> bitnum - 1);
if (mode == 0) //sum
sum += (a - m) + (b - n) + (carry << bitnum);
else
sum = (a - m) - (b - n) + (carry << bitnum);
return sum;
}
unsigned int ETA1(unsigned int a, unsigned int b, int mode)
{
unsigned int M, N, m, n, inaccuratePart = 0, r, sum = 0;
int carry, imask = 0x800;
M = a & mask;
N = b & mask;
while (1) {
m = a & imask;
n = b & imask;
r = m ^ n;
inaccuratePart += r;
if (m == imask && n == imask) {
inaccuratePart += imask - 1;
break;
}
imask /= 2;
if (imask == 0)
break;
}
if (mode == 0)
sum = (a - M) + (b - N) + inaccuratePart;
else
sum = (a - M) - (b - N) + inaccuratePart;
return sum;
}
unsigned int APP_sum_cal(float_cast &z, float_cast x, float_cast y, int caseNum)
{
unsigned int sum = 0, temp;
unsigned int esum = 0;
if (x.parts.exponent < y.parts.exponent) {
if (caseNum == 1) //LOA
sum = LOA((y.parts.mantissa | 0x800000), (x.parts.mantissa), 1);
else if (caseNum == 2) //ETA1
sum = ETA1((y.parts.mantissa | 0x800000), (x.parts.mantissa), 1);
z.parts.sign = y.parts.sign;
int cnt = 0;
unsigned int temp;
temp = sum;
if (sum <= 0x7FFFFF) {
for (cnt = 1; temp & 0x400000 ? 0 : 1; cnt++)
temp <<= 1;
if (cnt >= 3) {
sum <<= cnt - 3;
}
else {
sum >>= 3 - cnt;
}
}
sum &= 0x7FFFFF;
int tempSum = z.parts.exponent - cnt;
if (tempSum <= 0) {
z.parts.exponent = 0;
sum >>= (abs(tempSum) + 1);
sum |= (0x400000 >> abs(tempSum));
}
else
z.parts.exponent -= cnt;
}
else {
if (caseNum == 1) //LOA
sum = LOA(x.parts.mantissa, y.parts.mantissa, 1);
else if (caseNum == 2) //ETA1
sum = ETA1(x.parts.mantissa, y.parts.mantissa, 1);
z.parts.sign = x.parts.sign;
}
return sum;
}
float_cast AXAdder(float_cast a, float_cast b, int caseNum) {
//먼저 두 값이 real number인지 판단해야한다. (inf, -inf, 0, -0, NAN)
//0 FF 000000 -> inf, 1 FF 000000 -> -inf, 00000 -> 0, 100000 -> -0
//입력값이 INF일 수 가있나?
if (a.parts.exponent == 0xFF && a.parts.sign == 0) //a is inf
{
not_real_number
}
else if (a.parts.exponent == 0xFF && a.parts.sign == 1)//a is -inf
{
not_real_number
}
if (b.parts.exponent == 0xFF && b.parts.sign == 0) //b is inf
{
not_real_number
}
else if (b.parts.exponent == 0xFF && b.parts.sign == 1)//b is -inf
{
not_real_number
}
float_cast z; //return 값
z.parts.sign = 0;
unsigned int sum = 0;
int ext_bit[3] = { 0, }; // guard, round, sticky bit
int subEx_tmp = a.parts.exponent - b.parts.exponent;
int subEx = abs(subEx_tmp);
if (subEx != 0) {//exponents equal
checknum = 1;
if (a.parts.exponent > b.parts.exponent) {// a's exponent > b's exponent => shift mantissa right
//b.parts.exponent = a.parts.exponent;
mantissa_cal(z, a, b, subEx);
z.parts.sign = a.parts.sign;
}
else {// a's exponent < b's exponent => shift mantissa right
//a.parts.exponent = b.parts.exponent;
mantissa_cal(z, b, a, subEx);
z.parts.sign = b.parts.sign;
}
}
else {
z.parts.exponent = a.parts.exponent;
if (a.parts.sign == b.parts.sign)
z.parts.sign = a.parts.sign;
else {
if (a.parts.mantissa < b.parts.mantissa)
z.parts.sign = b.parts.sign;
else
z.parts.sign = a.parts.sign;
}
}
switch (caseNum) {
case 1: //LOA
if (a.parts.sign == b.parts.sign)
sum = LOA(a.parts.mantissa, b.parts.mantissa, 0);
else {
if (a.parts.mantissa > b.parts.mantissa) {
if (subEx != 0)
sum = APP_sum_cal(z, a, b, caseNum);
else
sum = LOA(a.parts.mantissa, b.parts.mantissa, 1);
}
else {
if (subEx != 0)
sum = APP_sum_cal(z, b, a, caseNum);
else
sum = LOA(b.parts.mantissa, a.parts.mantissa, 1);;
}
}
break;
case 2: //ETA1
if (a.parts.sign == b.parts.sign)
sum = ETA1(a.parts.mantissa, b.parts.mantissa, 0);
else {
if (a.parts.mantissa > b.parts.mantissa) {
if (subEx != 0)
sum = APP_sum_cal(z, a, b, caseNum);
else
sum = ETA1(a.parts.mantissa, b.parts.mantissa, 1);
}
else {
if (subEx != 0)
sum = APP_sum_cal(z, b, a, caseNum);
else
sum = ETA1(b.parts.mantissa, a.parts.mantissa, 1);
}
}
break;
}
//mantissa + mantissa가 23비트가 넘어가버리면 자동으로 잘라버림! (왜냐면 union이니깐)
//따라서 우리가 직접 넘어가는 carry값을 처리해줘야한다.
if (sum > 0x7FFFFF) {
if (subEx == 0) {
if (z.parts.exponent != 0) {
z.parts.mantissa = sum >> 1;
}
else {
//z.parts.mantissa = sum & 0x3FFFFF;
z.parts.mantissa = sum;
}
z.parts.exponent++;
}
else {
z.parts.mantissa = (sum >> 1) & 0x3FFFFF;
z.parts.exponent++;
}
}
else {
if (subEx == 0) {
if (a.parts.sign == b.parts.sign) {
if (z.parts.exponent != 0) {
z.parts.mantissa = sum >> 1;
z.parts.exponent++;
}
else
z.parts.mantissa = sum;
}
else {
int cnt = 1;
if (z.parts.exponent != 0) {
for (cnt = 1; sum & 0x400000 ? 0 : 1; cnt++) {
if (z.parts.exponent - cnt == 0)
break;
sum <<= 1;
}
if (z.parts.exponent - cnt == 0)
z.parts.mantissa = sum;
else
z.parts.mantissa = (sum << 1) & 0x7FFFFF;
z.parts.exponent -= cnt;
}
else
z.parts.mantissa = sum;
}
}
else {
z.parts.mantissa = sum;
}
}
//overflow!
if (z.parts.mantissa == 0 && (z.parts.exponent >= 0xFF)) {
//printf("\noverflow!\n");
z.parts.mantissa = 1 << 23;
z.parts.exponent = 255;
return z;
//z.parts.mantissa <<= 1;
//z.parts.exponent--;
//z.parts.mantissa += ext_bit[2];
//ext_bit[2] = ext_bit[1];
//ext_bit[1] = 0;
}
//underflow!
else if (z.parts.exponent >= 0xFF) {
//printf("\nunderflow!\n");
z.parts.mantissa = 1 << 23;
z.parts.exponent = 255;
return z;
//ext_bit[2] = z.parts.mantissa & 1;
//ext_bit[1] = ext_bit[2];
//ext_bit[0] = ext_bit[0] | ext_bit[1];
//z.parts.mantissa >>= 1;
//z.parts.exponent++;
}
return z;
}
int main(void) {
float_cast A, B;
float_cast orgAns, loa, eta1;
FILE* input = fopen("input.txt", "r");
//FILE* output = fopen("Errorlist.txt", "w");
int cnt = 0;
printf("A\t\t+\t\tB\t =\torgANS\t\tLOA\t\tETA1\n");
printf("**********************************************************************\n");
while (nnn<=10) {
// fscanf(input, "%f %f ", &A.f, &B.f);
//A, B 직접 지정
/*A.f = 0.603934;
B.f = -4.830974;*/
A = makeFP();
B = makeFP();
orgAns.f = A.f + B.f;
loa = AXAdder(A, B, 1);
eta1 = AXAdder(A, B, 2);
if (checknum == 0) {
printf("%d: %e + %e = %e, %e, %e\n", nnn, A.f, B.f, orgAns.f, loa.f, eta1.f);
printf("\n\n******************************\n");
}
else {
checknum = 0;
printf("++%d: %e + %e = %e, %e, %e\n", nnn, A.f, B.f, orgAns.f, loa.f, eta1.f);
printf("\n\n******************************\n");
}
/*fprintf(output, "%f\t%f\n", (loa.f / orgAns.f), (eta1.f / orgAns.f));*/
/*if ((loa.f / orgAns.f) > 2 || (loa.f / orgAns.f) < 0.5)
printf("\n\n%e\t%e\n\n", A.f, B.f);*/
nnn++;
}
//fclose(output);
fclose(input);
}