OLD | NEW |
1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 // Utilities for fast computation of logarithms. | 7 /* Utilities for fast computation of logarithms. */ |
8 | 8 |
9 #ifndef BROTLI_ENC_FAST_LOG_H_ | 9 #ifndef BROTLI_ENC_FAST_LOG_H_ |
10 #define BROTLI_ENC_FAST_LOG_H_ | 10 #define BROTLI_ENC_FAST_LOG_H_ |
11 | 11 |
12 #include <assert.h> | |
13 #include <math.h> | 12 #include <math.h> |
14 | 13 |
15 #include "./types.h" | 14 #include <brotli/types.h> |
| 15 #include <brotli/port.h> |
16 | 16 |
17 namespace brotli { | 17 #if defined(__cplusplus) || defined(c_plusplus) |
| 18 extern "C" { |
| 19 #endif |
18 | 20 |
19 static inline uint32_t Log2FloorNonZero(size_t n) { | 21 static BROTLI_INLINE uint32_t Log2FloorNonZero(size_t n) { |
20 #ifdef __GNUC__ | 22 #ifdef __GNUC__ |
21 return 31u ^ static_cast<uint32_t>(__builtin_clz(static_cast<uint32_t>(n))); | 23 return 31u ^ (uint32_t)__builtin_clz((uint32_t)n); |
22 #else | 24 #else |
23 uint32_t result = 0; | 25 uint32_t result = 0; |
24 while (n >>= 1) result++; | 26 while (n >>= 1) result++; |
25 return result; | 27 return result; |
26 #endif | 28 #endif |
27 } | 29 } |
28 | 30 |
29 // A lookup table for small values of log2(int) to be used in entropy | 31 /* A lookup table for small values of log2(int) to be used in entropy |
30 // computation. | 32 computation. |
31 // | 33 |
32 // ", ".join(["%.16ff" % x for x in [0.0]+[log2(x) for x in range(1, 256)]]) | 34 ", ".join(["%.16ff" % x for x in [0.0]+[log2(x) for x in range(1, 256)]]) */ |
33 static const float kLog2Table[] = { | 35 static const float kLog2Table[] = { |
34 0.0000000000000000f, 0.0000000000000000f, 1.0000000000000000f, | 36 0.0000000000000000f, 0.0000000000000000f, 1.0000000000000000f, |
35 1.5849625007211563f, 2.0000000000000000f, 2.3219280948873622f, | 37 1.5849625007211563f, 2.0000000000000000f, 2.3219280948873622f, |
36 2.5849625007211561f, 2.8073549220576042f, 3.0000000000000000f, | 38 2.5849625007211561f, 2.8073549220576042f, 3.0000000000000000f, |
37 3.1699250014423126f, 3.3219280948873626f, 3.4594316186372978f, | 39 3.1699250014423126f, 3.3219280948873626f, 3.4594316186372978f, |
38 3.5849625007211565f, 3.7004397181410922f, 3.8073549220576037f, | 40 3.5849625007211565f, 3.7004397181410922f, 3.8073549220576037f, |
39 3.9068905956085187f, 4.0000000000000000f, 4.0874628412503400f, | 41 3.9068905956085187f, 4.0000000000000000f, 4.0874628412503400f, |
40 4.1699250014423122f, 4.2479275134435852f, 4.3219280948873626f, | 42 4.1699250014423122f, 4.2479275134435852f, 4.3219280948873626f, |
41 4.3923174227787607f, 4.4594316186372973f, 4.5235619560570131f, | 43 4.3923174227787607f, 4.4594316186372973f, 4.5235619560570131f, |
42 4.5849625007211570f, 4.6438561897747244f, 4.7004397181410926f, | 44 4.5849625007211570f, 4.6438561897747244f, 4.7004397181410926f, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 7.8703647195834048f, 7.8765169465650002f, 7.8826430493618425f, | 114 7.8703647195834048f, 7.8765169465650002f, 7.8826430493618425f, |
113 7.8887432488982601f, 7.8948177633079446f, 7.9008668079807496f, | 115 7.8887432488982601f, 7.8948177633079446f, 7.9008668079807496f, |
114 7.9068905956085187f, 7.9128893362299619f, 7.9188632372745955f, | 116 7.9068905956085187f, 7.9128893362299619f, 7.9188632372745955f, |
115 7.9248125036057813f, 7.9307373375628867f, 7.9366379390025719f, | 117 7.9248125036057813f, 7.9307373375628867f, 7.9366379390025719f, |
116 7.9425145053392399f, 7.9483672315846778f, 7.9541963103868758f, | 118 7.9425145053392399f, 7.9483672315846778f, 7.9541963103868758f, |
117 7.9600019320680806f, 7.9657842846620870f, 7.9715435539507720f, | 119 7.9600019320680806f, 7.9657842846620870f, 7.9715435539507720f, |
118 7.9772799234999168f, 7.9829935746943104f, 7.9886846867721664f, | 120 7.9772799234999168f, 7.9829935746943104f, 7.9886846867721664f, |
119 7.9943534368588578f | 121 7.9943534368588578f |
120 }; | 122 }; |
121 | 123 |
122 // Faster logarithm for small integers, with the property of log2(0) == 0. | 124 #define LOG_2_INV 1.4426950408889634 |
123 static inline double FastLog2(size_t v) { | 125 |
| 126 /* Faster logarithm for small integers, with the property of log2(0) == 0. */ |
| 127 static BROTLI_INLINE double FastLog2(size_t v) { |
124 if (v < sizeof(kLog2Table) / sizeof(kLog2Table[0])) { | 128 if (v < sizeof(kLog2Table) / sizeof(kLog2Table[0])) { |
125 return kLog2Table[v]; | 129 return kLog2Table[v]; |
126 } | 130 } |
127 #if defined(_MSC_VER) && _MSC_VER <= 1700 | 131 #if (defined(_MSC_VER) && _MSC_VER <= 1700) || \ |
128 // Visual Studio 2012 does not have the log2() function defined, so we use | 132 (defined(__ANDROID_API__) && __ANDROID_API__ < 18) |
129 // log() and a multiplication instead. | 133 /* Visual Studio 2012 and Android API levels < 18 do not have the log2() |
130 static const double kLog2Inv = 1.4426950408889634f; | 134 * function defined, so we use log() and a multiplication instead. */ |
131 return log(static_cast<double>(v)) * kLog2Inv; | 135 return log((double)v) * LOG_2_INV; |
132 #else | 136 #else |
133 return log2(static_cast<double>(v)); | 137 return log2((double)v); |
134 #endif | 138 #endif |
135 } | 139 } |
136 | 140 |
137 } // namespace brotli | 141 #if defined(__cplusplus) || defined(c_plusplus) |
| 142 } /* extern "C" */ |
| 143 #endif |
138 | 144 |
139 #endif // BROTLI_ENC_FAST_LOG_H_ | 145 #endif /* BROTLI_ENC_FAST_LOG_H_ */ |
OLD | NEW |