1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef FLAC__PRIVATE__BITMATH_H
34 #define FLAC__PRIVATE__BITMATH_H
36 #include "FLAC/ordinals.h"
37 #include "FLAC/assert.h"
39 #include "share/compat.h"
42 #include <intrin.h> /* for _BitScanReverse* */
45 /* Will never be emitted for MSVC, GCC, Intel compilers */
46 static inline unsigned int FLAC__clz_soft_uint32(FLAC__uint32 word)
48 static const unsigned char byte_to_unary_table[] = {
49 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
50 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
51 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
54 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
55 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 return word > 0xffffff ? byte_to_unary_table[word >> 24] :
68 word > 0xffff ? byte_to_unary_table[word >> 16] + 8 :
69 word > 0xff ? byte_to_unary_table[word >> 8] + 16 :
70 byte_to_unary_table[word] + 24;
73 static inline unsigned int FLAC__clz_uint32(FLAC__uint32 v)
75 /* Never used with input 0 */
77 #if defined(__INTEL_COMPILER)
78 return _bit_scan_reverse(v) ^ 31U;
79 #elif defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
80 /* This will translate either to (bsr ^ 31U), clz , ctlz, cntlz, lzcnt depending on
81 * -march= setting or to a software routine in exotic machines. */
82 return __builtin_clz(v);
83 #elif defined(_MSC_VER)
86 _BitScanReverse(&idx, v);
90 return FLAC__clz_soft_uint32(v);
94 /* Used when 64-bit bsr/clz is unavailable; can use 32-bit bsr/clz when possible */
95 static inline unsigned int FLAC__clz_soft_uint64(FLAC__uint64 word)
97 return (FLAC__uint32)(word>>32) ? FLAC__clz_uint32((FLAC__uint32)(word>>32)) :
98 FLAC__clz_uint32((FLAC__uint32)word) + 32;
101 static inline unsigned int FLAC__clz_uint64(FLAC__uint64 v)
103 /* Never used with input 0 */
105 #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
106 return __builtin_clzll(v);
107 #elif (defined(__INTEL_COMPILER) || defined(_MSC_VER)) && (defined(_M_IA64) || defined(_M_X64))
110 _BitScanReverse64(&idx, v);
114 return FLAC__clz_soft_uint64(v);
118 /* These two functions work with input 0 */
119 static inline unsigned int FLAC__clz2_uint32(FLAC__uint32 v)
123 return FLAC__clz_uint32(v);
126 static inline unsigned int FLAC__clz2_uint64(FLAC__uint64 v)
130 return FLAC__clz_uint64(v);
133 /* An example of what FLAC__bitmath_ilog2() computes:
135 * ilog2( 0) = assertion failure
156 static inline unsigned FLAC__bitmath_ilog2(FLAC__uint32 v)
159 #if defined(__INTEL_COMPILER)
160 return _bit_scan_reverse(v);
161 #elif defined(_MSC_VER)
164 _BitScanReverse(&idx, v);
168 return FLAC__clz_uint32(v) ^ 31U;
172 static inline unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v)
175 #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
176 return __builtin_clzll(v) ^ 63U;
177 /* Sorry, only supported in x64/Itanium.. and both have fast FPU which makes integer-only encoder pointless */
178 #elif (defined(__INTEL_COMPILER) || defined(_MSC_VER)) && (defined(_M_IA64) || defined(_M_X64))
181 _BitScanReverse64(&idx, v);
185 /* Brain-damaged compilers will use the fastest possible way that is,
186 de Bruijn sequences (http://supertech.csail.mit.edu/papers/debruijn.pdf)
187 (C) Timothy B. Terriberry (tterribe@xiph.org) 2001-2009 CC0 (Public domain).
190 static const unsigned char DEBRUIJN_IDX64[64]={
191 0, 1, 2, 7, 3,13, 8,19, 4,25,14,28, 9,34,20,40,
192 5,17,26,38,15,46,29,48,10,31,35,54,21,50,41,57,
193 63, 6,12,18,24,27,33,39,16,37,45,47,30,53,49,56,
194 62,11,23,32,36,44,52,55,61,22,43,51,60,42,59,58
203 return DEBRUIJN_IDX64[v*FLAC__U64L(0x218A392CD3D5DBF)>>58&0x3F];
208 unsigned FLAC__bitmath_silog2(FLAC__int64 v);