xref: /aosp_15_r20/external/brotli/c/enc/fast_log.h (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
1*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2013 Google Inc. All Rights Reserved.
2*f4ee7fbaSAndroid Build Coastguard Worker 
3*f4ee7fbaSAndroid Build Coastguard Worker    Distributed under MIT license.
4*f4ee7fbaSAndroid Build Coastguard Worker    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5*f4ee7fbaSAndroid Build Coastguard Worker */
6*f4ee7fbaSAndroid Build Coastguard Worker 
7*f4ee7fbaSAndroid Build Coastguard Worker /* Utilities for fast computation of logarithms. */
8*f4ee7fbaSAndroid Build Coastguard Worker 
9*f4ee7fbaSAndroid Build Coastguard Worker #ifndef BROTLI_ENC_FAST_LOG_H_
10*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_ENC_FAST_LOG_H_
11*f4ee7fbaSAndroid Build Coastguard Worker 
12*f4ee7fbaSAndroid Build Coastguard Worker #include <math.h>
13*f4ee7fbaSAndroid Build Coastguard Worker 
14*f4ee7fbaSAndroid Build Coastguard Worker #include "../common/platform.h"
15*f4ee7fbaSAndroid Build Coastguard Worker #include <brotli/types.h>
16*f4ee7fbaSAndroid Build Coastguard Worker 
17*f4ee7fbaSAndroid Build Coastguard Worker #if defined(__cplusplus) || defined(c_plusplus)
18*f4ee7fbaSAndroid Build Coastguard Worker extern "C" {
19*f4ee7fbaSAndroid Build Coastguard Worker #endif
20*f4ee7fbaSAndroid Build Coastguard Worker 
Log2FloorNonZero(size_t n)21*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE uint32_t Log2FloorNonZero(size_t n) {
22*f4ee7fbaSAndroid Build Coastguard Worker #if defined(BROTLI_BSR32)
23*f4ee7fbaSAndroid Build Coastguard Worker   return BROTLI_BSR32((uint32_t)n);
24*f4ee7fbaSAndroid Build Coastguard Worker #else
25*f4ee7fbaSAndroid Build Coastguard Worker   uint32_t result = 0;
26*f4ee7fbaSAndroid Build Coastguard Worker   while (n >>= 1) result++;
27*f4ee7fbaSAndroid Build Coastguard Worker   return result;
28*f4ee7fbaSAndroid Build Coastguard Worker #endif
29*f4ee7fbaSAndroid Build Coastguard Worker }
30*f4ee7fbaSAndroid Build Coastguard Worker 
31*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_LOG2_TABLE_SIZE 256
32*f4ee7fbaSAndroid Build Coastguard Worker 
33*f4ee7fbaSAndroid Build Coastguard Worker /* A lookup table for small values of log2(int) to be used in entropy
34*f4ee7fbaSAndroid Build Coastguard Worker    computation. */
35*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_INTERNAL extern const double kBrotliLog2Table[BROTLI_LOG2_TABLE_SIZE];
36*f4ee7fbaSAndroid Build Coastguard Worker 
37*f4ee7fbaSAndroid Build Coastguard Worker /* Visual Studio 2012 and Android API levels < 18 do not have the log2()
38*f4ee7fbaSAndroid Build Coastguard Worker  * function defined, so we use log() and a multiplication instead. */
39*f4ee7fbaSAndroid Build Coastguard Worker #if !defined(BROTLI_HAVE_LOG2)
40*f4ee7fbaSAndroid Build Coastguard Worker #if ((defined(_MSC_VER) && _MSC_VER <= 1700) || \
41*f4ee7fbaSAndroid Build Coastguard Worker      (defined(__ANDROID_API__) && __ANDROID_API__ < 18))
42*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_HAVE_LOG2 0
43*f4ee7fbaSAndroid Build Coastguard Worker #else
44*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_HAVE_LOG2 1
45*f4ee7fbaSAndroid Build Coastguard Worker #endif
46*f4ee7fbaSAndroid Build Coastguard Worker #endif
47*f4ee7fbaSAndroid Build Coastguard Worker 
48*f4ee7fbaSAndroid Build Coastguard Worker #define LOG_2_INV 1.4426950408889634
49*f4ee7fbaSAndroid Build Coastguard Worker 
50*f4ee7fbaSAndroid Build Coastguard Worker /* Faster logarithm for small integers, with the property of log2(0) == 0. */
FastLog2(size_t v)51*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE double FastLog2(size_t v) {
52*f4ee7fbaSAndroid Build Coastguard Worker   if (v < BROTLI_LOG2_TABLE_SIZE) {
53*f4ee7fbaSAndroid Build Coastguard Worker     return kBrotliLog2Table[v];
54*f4ee7fbaSAndroid Build Coastguard Worker   }
55*f4ee7fbaSAndroid Build Coastguard Worker #if !(BROTLI_HAVE_LOG2)
56*f4ee7fbaSAndroid Build Coastguard Worker   return log((double)v) * LOG_2_INV;
57*f4ee7fbaSAndroid Build Coastguard Worker #else
58*f4ee7fbaSAndroid Build Coastguard Worker   return log2((double)v);
59*f4ee7fbaSAndroid Build Coastguard Worker #endif
60*f4ee7fbaSAndroid Build Coastguard Worker }
61*f4ee7fbaSAndroid Build Coastguard Worker 
62*f4ee7fbaSAndroid Build Coastguard Worker #if defined(__cplusplus) || defined(c_plusplus)
63*f4ee7fbaSAndroid Build Coastguard Worker }  /* extern "C" */
64*f4ee7fbaSAndroid Build Coastguard Worker #endif
65*f4ee7fbaSAndroid Build Coastguard Worker 
66*f4ee7fbaSAndroid Build Coastguard Worker #endif  /* BROTLI_ENC_FAST_LOG_H_ */
67