bitmath.h 6.33 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* libFLAC - Free Lossless Audio Codec library
2
 * Copyright (C) 2001-2009  Josh Coalson
Thomas Zander's avatar
Thomas Zander committed
3
 * Copyright (C) 2011-2016  Xiph.Org Foundation
Josh Coalson's avatar
Josh Coalson committed
4
 *
5
6
7
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Josh Coalson's avatar
Josh Coalson committed
8
 *
9
10
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
Josh Coalson's avatar
Josh Coalson committed
11
 *
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Xiph.org Foundation nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson's avatar
Josh Coalson committed
31
32
33
34
35
36
 */

#ifndef FLAC__PRIVATE__BITMATH_H
#define FLAC__PRIVATE__BITMATH_H

#include "FLAC/ordinals.h"
37
#include "FLAC/assert.h"
Josh Coalson's avatar
Josh Coalson committed
38

39
#include "share/compat.h"
40

Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
41
#if defined(_MSC_VER)
42
43
44
45
#include <intrin.h> /* for _BitScanReverse* */
#endif

/* Will never be emitted for MSVC, GCC, Intel compilers */
46
static inline uint32_t FLAC__clz_soft_uint32(FLAC__uint32 word)
47
{
48
	static const uint8_t byte_to_unary_table[] = {
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	};
66

Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
67
68
69
70
	return word > 0xffffff ? byte_to_unary_table[word >> 24] :
		word > 0xffff ? byte_to_unary_table[word >> 16] + 8 :
		word > 0xff ? byte_to_unary_table[word >> 8] + 16 :
		byte_to_unary_table[word] + 24;
71
72
}

73
static inline uint32_t FLAC__clz_uint32(FLAC__uint32 v)
74
75
{
/* Never used with input 0 */
76
	FLAC__ASSERT(v > 0);
77
#if defined(__INTEL_COMPILER)
78
	return _bit_scan_reverse(v) ^ 31U;
79
80
#elif defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
/* 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);
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
83
#elif defined(_MSC_VER)
84
	{
85
		uint32_t idx;
86
87
88
		_BitScanReverse(&idx, v);
		return idx ^ 31U;
	}
89
#else
90
	return FLAC__clz_soft_uint32(v);
91
92
93
#endif
}

94
/* Used when 64-bit bsr/clz is unavailable; can use 32-bit bsr/clz when possible */
95
static inline uint32_t FLAC__clz_soft_uint64(FLAC__uint64 word)
96
97
98
99
100
{
	return (FLAC__uint32)(word>>32) ? FLAC__clz_uint32((FLAC__uint32)(word>>32)) :
		FLAC__clz_uint32((FLAC__uint32)word) + 32;
}

101
static inline uint32_t FLAC__clz_uint64(FLAC__uint64 v)
102
103
104
105
106
107
108
{
	/* Never used with input 0 */
	FLAC__ASSERT(v > 0);
#if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
	return __builtin_clzll(v);
#elif (defined(__INTEL_COMPILER) || defined(_MSC_VER)) && (defined(_M_IA64) || defined(_M_X64))
	{
109
		uint32_t idx;
110
111
112
113
114
115
116
117
118
		_BitScanReverse64(&idx, v);
		return idx ^ 63U;
	}
#else
	return FLAC__clz_soft_uint64(v);
#endif
}

/* These two functions work with input 0 */
119
static inline uint32_t FLAC__clz2_uint32(FLAC__uint32 v)
120
{
121
122
123
	if (!v)
		return 32;
	return FLAC__clz_uint32(v);
124
125
}

126
static inline uint32_t FLAC__clz2_uint64(FLAC__uint64 v)
127
128
129
130
131
132
{
	if (!v)
		return 64;
	return FLAC__clz_uint64(v);
}

133
134
/* An example of what FLAC__bitmath_ilog2() computes:
 *
135
 * ilog2( 0) = assertion failure
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 * ilog2( 1) = 0
 * ilog2( 2) = 1
 * ilog2( 3) = 1
 * ilog2( 4) = 2
 * ilog2( 5) = 2
 * ilog2( 6) = 2
 * ilog2( 7) = 2
 * ilog2( 8) = 3
 * ilog2( 9) = 3
 * ilog2(10) = 3
 * ilog2(11) = 3
 * ilog2(12) = 3
 * ilog2(13) = 3
 * ilog2(14) = 3
 * ilog2(15) = 3
 * ilog2(16) = 4
 * ilog2(17) = 4
 * ilog2(18) = 4
 */
155

156
static inline uint32_t FLAC__bitmath_ilog2(FLAC__uint32 v)
157
{
158
	FLAC__ASSERT(v > 0);
159
#if defined(__INTEL_COMPILER)
160
	return _bit_scan_reverse(v);
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
161
#elif defined(_MSC_VER)
162
	{
163
		uint32_t idx;
164
165
166
		_BitScanReverse(&idx, v);
		return idx;
	}
167
#else
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
168
	return FLAC__clz_uint32(v) ^ 31U;
169
#endif
170
171
}

172
static inline uint32_t FLAC__bitmath_ilog2_wide(FLAC__uint64 v)
173
{
174
	FLAC__ASSERT(v > 0);
175
#if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
176
	return __builtin_clzll(v) ^ 63U;
177
/* Sorry, only supported in x64/Itanium.. and both have fast FPU which makes integer-only encoder pointless */
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
178
#elif (defined(__INTEL_COMPILER) || defined(_MSC_VER)) && (defined(_M_IA64) || defined(_M_X64))
179
	{
180
		uint32_t idx;
181
182
183
		_BitScanReverse64(&idx, v);
		return idx;
	}
184
#else
185
/*  Brain-damaged compilers will use the fastest possible way that is,
186
187
	de Bruijn sequences (http://supertech.csail.mit.edu/papers/debruijn.pdf)
	(C) Timothy B. Terriberry (tterribe@xiph.org) 2001-2009 CC0 (Public domain).
188
*/
189
	{
190
		static const uint8_t DEBRUIJN_IDX64[64]={
191
192
193
194
195
196
197
198
199
200
201
202
			0, 1, 2, 7, 3,13, 8,19, 4,25,14,28, 9,34,20,40,
			5,17,26,38,15,46,29,48,10,31,35,54,21,50,41,57,
			63, 6,12,18,24,27,33,39,16,37,45,47,30,53,49,56,
			62,11,23,32,36,44,52,55,61,22,43,51,60,42,59,58
		};
		v|= v>>1;
		v|= v>>2;
		v|= v>>4;
		v|= v>>8;
		v|= v>>16;
		v|= v>>32;
		v= (v>>1)+1;
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
203
		return DEBRUIJN_IDX64[v*FLAC__U64L(0x218A392CD3D5DBF)>>58&0x3F];
204
	}
205
206
#endif
}
207

208
uint32_t FLAC__bitmath_silog2(FLAC__int64 v);
Josh Coalson's avatar
Josh Coalson committed
209
210

#endif