-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp-main-test.c
113 lines (106 loc) · 2.61 KB
/
fp-main-test.c
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
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <assert.h>
#include <math.h>
#include "fp-soft-variable.h"
void error(char * msg)
{
printf ("Error %s\n", msg);
printf ("./fp-main-test p [FPNumber]+\n");
printf ("./fp-main-test [+|*] FPNumber1 FPNumber2 Precision [Result]\n");
exit(1);
}
void printPrecision()
{
int i;
printf ("Prec. :");
for (i = 0; i < 24; i++) printf ("%4d ", i);
printf ("\nFmul32 :");
for (i = 0; i < 24; i++) printf ("%4lld ", fpGetmulCount(i));
printf ("\nFadd32 :");
for (i = 0; i < 24; i++) printf ("%4lld ", fpGetaddCount(i));
printf ("\nFsub32 :");
for (i = 0; i < 24; i++) printf ("%4lld ", fpGetsubCount(i));
printf ("\n");
}
int main (int argc, char* argv[])
{
float_cast a, b, c;
double_cast d;
int i, tmp;
printf ("float_cast %u float_cast %u \n", sizeof (float_cast), sizeof (double_cast));
assert ((32/8) == sizeof (float_cast));
assert ((64/8) == sizeof (double_cast));
if (argc < 2)
error ("Missing argument");
if (argv[1][0] == 'p')
{
for (i = 2; i < argc; i++)
{
a.f = (float) atof (argv[i]);
d.d = atof (argv[i]);
PrF(a);
PrD(d);
}
}
else /* Operation */
{
if (argc < 5)
error ("Missing arguments");
a.f = (float) atof (argv[2]);
b.f = (float) atof (argv[3]);
c.i = 0;
tmp = atoi(argv[4]);
if ((tmp < 0) || (tmp > 23))
error ("Incorrect precision 0 < p < 24");
fpInitCount();
fpSetPrecision (tmp);
#define PF(V) printFloat (V, #V)
PrF(a);
PrF(b);
PrF(c);
switch (argv[1][0])
{
case '*':
c.f = a.f * b.f;
PF(c);
c.i = mulsf3_classical (a.i, b.i);
PF(c);
break;
case '+':
c.f = a.f + b.f;
PF(c);
c.i = addsf3_classical (a.i, b.i);
PF(c);
break;
case '-':
c.f = a.f - b.f;
PF(c);
b.i ^= LONG32_SIGNBIT_MASK;
c.i = subsf3_classical (a.i, b.i);
PF(c);
break;
case '/':
error ("/ not yet implemented");
break;
default:
error ("Unknown command");
break;
}
if (argc > 5)
{
float result, diff;
result = atof (argv[5]);
diff = fabs(result - c.f);
printf ("%f %f %f\n", c.f, result, diff);
if (diff < 1e-6)
return 0;
else
return 1;
}
}
printPrecision();
return 0;
}