forked from hxim/paq8px
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIlog.hpp
46 lines (39 loc) · 984 Bytes
/
Ilog.hpp
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
#ifndef PAQ8PX_ILOG_HPP
#define PAQ8PX_ILOG_HPP
#include "Array.hpp"
/**
* ilog(x) = round(log2(x) * 16), 0 <= x < 64K
*/
class Ilog {
public:
static Ilog& getInstance();
[[nodiscard]] auto log(uint16_t x) const -> int;
private:
/**
* Compute lookup table by numerical integration of 1/x
*/
Ilog() {
uint32_t x = 14155776;
for( uint32_t i = 2; i < 65536; ++i ) {
x += 774541002 / (i * 2 - 1); // numerator is 2^29/ln 2
t[i] = x >> 24U;
}
}
/**
* Copy constructor is private so that it cannot be called
*/
Ilog(Ilog const & /*unused*/) {}
/**
* Assignment operator is private so that it cannot be called
*/
auto operator=(Ilog const & /*unused*/) -> Ilog & { return *this; }
Array<uint8_t> t = Array<uint8_t>(65536);
};
/**
* llog(x) accepts 32 bits
* @param x
* @return
*/
auto llog(uint32_t x) -> int;
auto bitCount(uint32_t v) -> uint32_t;
#endif //PAQ8PX_ILOG_HPP