This repository was archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbinary8_gen.cpp
More file actions
103 lines (92 loc) · 2.73 KB
/
binary8_gen.cpp
File metadata and controls
103 lines (92 loc) · 2.73 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
/**
* Emanuele Ruffaldi (C) 2017
* Templated C++ Posit
*/
#include <typeinfo>
#include <fstream>
#include <iostream>
#include "binary8.hpp"
#include "anyfloat.hpp"
#define SIGNEX(v, sb) ((v) | (((v) & (1 << (sb))) ? ~((1 << (sb))-1) : 0))
int main(int argc, char const *argv[])
{
// template <int expbits, int fractionbits, class value_t, class holder_t, class impl_t>
// match the posit8 from posit8.hpp
using X=anyfloat_emu<BINARY8_EXP,BINARY8_MAN,int8_t,uint8_t, float>;
union Q {
float f;
uint32_t i;
} ;
int8_t opadd[256*256],opmul[256*256],opdiv[256*256],opinv[256],opsquare[256],opexp2[256],ophalf[256];
uint32_t op2float[256];
for(int16_t s = -128 ; s < 128; s++)
{
X x;
x.v = s;
int32_t i = ((unsigned int)(uint16_t)s) & 0xFF;
float fx(x);
Q fxq;
fxq.f = fx;
opinv[i] = x.inv().v;
opsquare[i] = X(fx*fx).v;
op2float[i] = fxq.i;
ophalf[i] = X(fx/2).v;
opexp2[i] = X(exp2(fx)).v;
//std::cout << "twice " << (X)(fx*2) << " half " << (X)(fx/2) << std::endl;
}
for(int16_t s1 = -128 ; s1 < 128; s1++)
{
X x1;
x1.v = s1;
int32_t i1 = ((unsigned int)(uint8_t)s1) & 0xFF;
float f1 = uint32_to_float(op2float[i1]);
for(int16_t s2 = -128 ; s2 < 128; s2++)
{
X x2;
x2.v = s2;
int32_t i2 = ((unsigned int)(uint8_t)s2) & 0xFF;
float f2 = uint32_to_float(op2float[i2]);
opadd[i1*256+i2] = X(f1+f2).v;
opmul[i1*256+i2] = X(f1*f2).v;
opdiv[i1*256+i2] = X(f1/f2).v;
//opadd[i*256+j] = (x*y).v;
}
}
std::ofstream onf(argc == 1 ? "binary8_tbl.cpp" : argv[1]);
onf << "#include <stdint.h>\nnamespace binary8ns {\n";
onf << "// type is " << typeid(X).name() << std::endl;
onf << "int8_t opadd[] = {\n";
for(int i = 0; i < 256*256; i++)
onf << (int)(opadd[i]) << ",";
onf << "}; " << std::endl;
// emit the numbers as C file
onf << "int8_t opmul[] = {\n";
for(int i = 0; i < 256*256; i++)
onf << (int)(opmul[i]) << ",";
onf << "}; " << std::endl;
onf << "int8_t opdiv[] = {\n";
for(int i = 0; i < 256*256; i++)
onf << (int)(opdiv[i]) << ",";
onf << "}; " << std::endl;
onf << "int8_t opinv[] = {\n";
for(int i = 0; i < 256; i++)
onf << (int)(opinv[i]) << ",";
onf << "}; " << std::endl;
onf << "int8_t opsquare[] = {\n";
for(int i = 0; i < 256; i++)
onf << (int)(opsquare[i]) << ",";
onf << "}; " << std::endl;
onf << "int8_t opexp2[] = {\n";
for(int i = 0; i < 256; i++)
onf << (int)(opexp2[i]) << ",";
onf << "}; " << std::endl;
onf << "int8_t ophalf[] = {\n";
for(int i = 0; i < 256; i++)
onf << (int)(ophalf[i]) << ",";
onf << "}; " << std::endl;
onf << "uint32_t op2float[] = {\n";
for(int i = 0; i < 256; i++)
onf << (op2float[i]) << ",";
onf << "};} " << std::endl;
return 0;
}