-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogSample.cpp
162 lines (129 loc) · 4.45 KB
/
LogSample.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
#include<cmath>
#include<vector>
struct LogScale {
template<typename F>
LogScale(float imin, float imax, F f) {
frexpf(imin,&nMin); --nMin;
frexpf(imax,&nMax);
values.resize(size(),0.);
for (int n=nMin; n<=nMax; ++n)
values[n-nMin] = f(ldexpf(1.,n));
}
int size() const { return nMax-nMin +1;}
static std::pair<int, float> bin(float x) {
const float l2 = 5*log10(2.);
const float p1 = l2;
const float p2 = -l2/2.;
const float p3 = l2/3.;
std::pair<int, float> res;
float f = frexpf(x,&res.first)-1.f;
res.second = f*(p1+f*(p2+p3*f));
return res;
}
float value(float x) const {
std::pair<int, float> res = bin(x);
return values[res.first-nMin] + res.second*(values[res.first-nMin] - values[res.first-nMin-1]);
}
int nMin;
int nMax;
std::vector<float> values;
};
float compute(float x, LogScale const & s) {
return s.value(x);
}
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<iomanip>
void look(float x) {
int e;
float f = ::frexpf(x,&e);
std::cout << x << " exp " << e << " res " << f << std::endl;
union {
float val;
int x;
} tmp;
tmp.val = x;
const int log_2 = ((tmp.x >> 23) & 255) - 127;//exponent
tmp.x &= 0x7FFFFF; //mantissa
// 0xFF800000;
std::cout << "exp " << log_2 << " mant as int " << std::hex << tmp.x
<< " mant as float " << std::dec << (tmp.x|0x800000)*::pow(2.,-23) << std::endl;
// approx
int n = 10;
int mask = 0x1 << (23-n-1);
tmp.val = x;
int rounding = (tmp.x & mask) ? 1 : 0;
int m = (tmp.x & 0x7FFFFF) >> (23-n);
tmp.x &= 0xFF800000; tmp.x |=(m<<23-n);
std::cout << "approx " << tmp.val << " " << std::hex << (tmp.x & 0x7FFFFF) << std::dec << " " << m;
tmp.val = x;
int m2 = (tmp.x & 0x7FFFFF) >> (23-n-1);
tmp.x &= 0xFF800000; tmp.x |=(m2<<23-n-1);
std::cout << " " << tmp.val << " " << std::hex << (tmp.x & 0x7FFFFF) << std::dec << " " << m2 << std::endl;
// if ( m2 & 0x1 ) {
m+=rounding; tmp.val = x; tmp.x &= 0xFF800000; tmp.x |=(m<<23-n);
std::cout << "better " << std::hex << tmp.val << " " << (tmp.x & 0x7FFFFF) << std::dec << " " << m << std::endl;
// }
}
int main() {
std::cout << "have a look inside a float" << std::endl;
look(0.f);
look(0.99999f);
look(1.f);
look(2.f);
look(3.f);
look(std::sqrt(3.3));
look(std::sqrt(333444.e14));
look(std::sqrt(333444.e-14));
LogScale scale(10.e-2,10.e5,::log10f);
std::cout << "size " << scale.values.size() << " " << scale.nMin << " " << scale.nMax << std::endl;
std::cout << std::endl;
int n = 5;
union {
float numlog;
int x;
} tmp;
tmp.x = 0x3F800000; //set the exponent to 0 so numlog=1.0
int incr = 1 << (23-n); //amount to increase the mantissa
int p=std::pow(2.,n);
std:: cout << p << ": ";
for(int i=0;i<p;++i)
{
std::cout << tmp.numlog << ", " << ::log2(tmp.numlog) << " " ;
//lookup_table[i] = std::log2(tmp.numlog); //save the log value
tmp.x += incr;
}
std:: cout << std::endl;
std:: cout << std::endl;
std::cout << scale.bin(0.04f).second << std::endl;
std::cout << scale.bin(0.4f).second << std::endl;
std::cout << scale.bin(1000.f).second << std::endl;
std::cout << scale.bin(1024.f).second << std::endl;
std::cout << scale.bin(1024.01f).second << std::endl;
std::cout << scale.bin(1200.f).second << std::endl;
std::cout << scale.bin(1536.f).second << std::endl;
std::cout << scale.bin(1900.f).second << std::endl;
std::cout << scale.bin(2047.99f).second << std::endl;
std::cout << std::endl;
std::cout << scale.bin(0.04f).first << std::endl;
std::cout << scale.bin(0.4f).first << std::endl;
std::cout << scale.bin(1000.f).first << std::endl;
std::cout << scale.bin(1024.f).first << std::endl;
std::cout << scale.bin(1024.01f).first << std::endl;
std::cout << scale.bin(1200.f).first << std::endl;
std::cout << scale.bin(1536.f).first << std::endl;
std::cout << scale.bin(1900.f).first << std::endl;
std::cout << scale.bin(2047.99f).first << std::endl;
std::cout << scale.bin(10000.f).first << std::endl;
std::cout << std::endl;
std::cout << scale.value(0.1f) << std::endl;
std::cout << scale.value(1.f) << std::endl;
std::cout << scale.value(100.f) << std::endl;
std::cout << scale.value(1000.f) << std::endl;
std::cout << scale.value(1024.f) << std::endl;
std::cout << scale.value(1024.01f) << std::endl;
std::cout << scale.value(1200.01f) << std::endl;
std::cout << scale.value(10000.f) << std::endl;
return 0;
}