OLD | NEW |
1 /* libFLAC - Free Lossless Audio Codec library | 1 /* libFLAC - Free Lossless Audio Codec library |
2 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson | 2 * Copyright (C) 2001-2009 Josh Coalson |
| 3 * Copyright (C) 2011-2014 Xiph.Org Foundation |
3 * | 4 * |
4 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
6 * are met: | 7 * are met: |
7 * | 8 * |
8 * - Redistributions of source code must retain the above copyright | 9 * - Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
10 * | 11 * |
11 * - Redistributions in binary form must reproduce the above copyright | 12 * - Redistributions in binary form must reproduce the above copyright |
12 * notice, this list of conditions and the following disclaimer in the | 13 * notice, this list of conditions and the following disclaimer in the |
(...skipping 13 matching lines...) Expand all Loading... |
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ | 31 */ |
31 | 32 |
32 #ifndef FLAC__PRIVATE__BITMATH_H | 33 #ifndef FLAC__PRIVATE__BITMATH_H |
33 #define FLAC__PRIVATE__BITMATH_H | 34 #define FLAC__PRIVATE__BITMATH_H |
34 | 35 |
35 #include "FLAC/ordinals.h" | 36 #include "FLAC/ordinals.h" |
| 37 #include "FLAC/assert.h" |
36 | 38 |
37 unsigned FLAC__bitmath_ilog2(FLAC__uint32 v); | 39 /* for CHAR_BIT */ |
38 unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v); | 40 #include <limits.h> |
| 41 #include "share/compat.h" |
| 42 |
| 43 #if defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 44 #include <intrin.h> /* for _BitScanReverse* */ |
| 45 #endif |
| 46 |
| 47 /* Will never be emitted for MSVC, GCC, Intel compilers */ |
| 48 static inline unsigned int FLAC__clz_soft_uint32(unsigned int word) |
| 49 { |
| 50 static const unsigned char byte_to_unary_table[] = { |
| 51 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
| 52 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 53 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 54 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 58 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 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, |
| 65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 67 }; |
| 68 |
| 69 return (word) > 0xffffff ? byte_to_unary_table[(word) >> 24] : |
| 70 (word) > 0xffff ? byte_to_unary_table[(word) >> 16] + 8 : |
| 71 (word) > 0xff ? byte_to_unary_table[(word) >> 8] + 16 : |
| 72 byte_to_unary_table[(word)] + 24; |
| 73 } |
| 74 |
| 75 static inline unsigned int FLAC__clz_uint32(FLAC__uint32 v) |
| 76 { |
| 77 /* Never used with input 0 */ |
| 78 FLAC__ASSERT(v > 0); |
| 79 #if defined(__INTEL_COMPILER) |
| 80 return _bit_scan_reverse(v) ^ 31U; |
| 81 #elif defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >=
4)) |
| 82 /* This will translate either to (bsr ^ 31U), clz , ctlz, cntlz, lzcnt depending
on |
| 83 * -march= setting or to a software routine in exotic machines. */ |
| 84 return __builtin_clz(v); |
| 85 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 86 { |
| 87 unsigned long idx; |
| 88 _BitScanReverse(&idx, v); |
| 89 return idx ^ 31U; |
| 90 } |
| 91 #else |
| 92 return FLAC__clz_soft_uint32(v); |
| 93 #endif |
| 94 } |
| 95 |
| 96 /* This one works with input 0 */ |
| 97 static inline unsigned int FLAC__clz2_uint32(FLAC__uint32 v) |
| 98 { |
| 99 if (!v) |
| 100 return 32; |
| 101 return FLAC__clz_uint32(v); |
| 102 } |
| 103 |
| 104 /* An example of what FLAC__bitmath_ilog2() computes: |
| 105 * |
| 106 * ilog2( 0) = assertion failure |
| 107 * ilog2( 1) = 0 |
| 108 * ilog2( 2) = 1 |
| 109 * ilog2( 3) = 1 |
| 110 * ilog2( 4) = 2 |
| 111 * ilog2( 5) = 2 |
| 112 * ilog2( 6) = 2 |
| 113 * ilog2( 7) = 2 |
| 114 * ilog2( 8) = 3 |
| 115 * ilog2( 9) = 3 |
| 116 * ilog2(10) = 3 |
| 117 * ilog2(11) = 3 |
| 118 * ilog2(12) = 3 |
| 119 * ilog2(13) = 3 |
| 120 * ilog2(14) = 3 |
| 121 * ilog2(15) = 3 |
| 122 * ilog2(16) = 4 |
| 123 * ilog2(17) = 4 |
| 124 * ilog2(18) = 4 |
| 125 */ |
| 126 |
| 127 static inline unsigned FLAC__bitmath_ilog2(FLAC__uint32 v) |
| 128 { |
| 129 FLAC__ASSERT(v > 0); |
| 130 #if defined(__INTEL_COMPILER) |
| 131 return _bit_scan_reverse(v); |
| 132 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 133 { |
| 134 unsigned long idx; |
| 135 _BitScanReverse(&idx, v); |
| 136 return idx; |
| 137 } |
| 138 #else |
| 139 return sizeof(FLAC__uint32) * CHAR_BIT - 1 - FLAC__clz_uint32(v); |
| 140 #endif |
| 141 } |
| 142 |
| 143 |
| 144 #ifdef FLAC__INTEGER_ONLY_LIBRARY /* Unused otherwise */ |
| 145 |
| 146 static inline unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v) |
| 147 { |
| 148 FLAC__ASSERT(v > 0); |
| 149 #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4
)) |
| 150 return sizeof(FLAC__uint64) * CHAR_BIT - 1 - __builtin_clzll(v); |
| 151 /* Sorry, only supported in x64/Itanium.. and both have fast FPU which makes int
eger-only encoder pointless */ |
| 152 #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && (defined(_M_IA64) || defined(
_M_X64)) |
| 153 { |
| 154 unsigned long idx; |
| 155 _BitScanReverse64(&idx, v); |
| 156 return idx; |
| 157 } |
| 158 #else |
| 159 /* Brain-damaged compilers will use the fastest possible way that is, |
| 160 de Bruijn sequences (http://supertech.csail.mit.edu/papers/debruijn.pdf) |
| 161 (C) Timothy B. Terriberry (tterribe@xiph.org) 2001-2009 CC0 (Public domain). |
| 162 */ |
| 163 { |
| 164 static const unsigned char DEBRUIJN_IDX64[64]={ |
| 165 0, 1, 2, 7, 3,13, 8,19, 4,25,14,28, 9,34,20,40, |
| 166 5,17,26,38,15,46,29,48,10,31,35,54,21,50,41,57, |
| 167 63, 6,12,18,24,27,33,39,16,37,45,47,30,53,49,56, |
| 168 62,11,23,32,36,44,52,55,61,22,43,51,60,42,59,58 |
| 169 }; |
| 170 v|= v>>1; |
| 171 v|= v>>2; |
| 172 v|= v>>4; |
| 173 v|= v>>8; |
| 174 v|= v>>16; |
| 175 v|= v>>32; |
| 176 v= (v>>1)+1; |
| 177 return DEBRUIJN_IDX64[v*0x218A392CD3D5DBF>>58&0x3F]; |
| 178 } |
| 179 #endif |
| 180 } |
| 181 #endif |
| 182 |
39 unsigned FLAC__bitmath_silog2(int v); | 183 unsigned FLAC__bitmath_silog2(int v); |
40 unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v); | 184 unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v); |
41 | 185 |
42 #endif | 186 #endif |
OLD | NEW |