1c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* libFLAC - Free Lossless Audio Codec library
2c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Copyright (C) 2004,2005,2006,2007  Josh Coalson
3c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
4c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Redistribution and use in source and binary forms, with or without
5c74663799493f2b1e6123c18def94295d0afab7Kenny Root * modification, are permitted provided that the following conditions
6c74663799493f2b1e6123c18def94295d0afab7Kenny Root * are met:
7c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
8c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Redistributions of source code must retain the above copyright
9c74663799493f2b1e6123c18def94295d0afab7Kenny Root * notice, this list of conditions and the following disclaimer.
10c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
11c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Redistributions in binary form must reproduce the above copyright
12c74663799493f2b1e6123c18def94295d0afab7Kenny Root * notice, this list of conditions and the following disclaimer in the
13c74663799493f2b1e6123c18def94295d0afab7Kenny Root * documentation and/or other materials provided with the distribution.
14c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
15c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Neither the name of the Xiph.org Foundation nor the names of its
16c74663799493f2b1e6123c18def94295d0afab7Kenny Root * contributors may be used to endorse or promote products derived from
17c74663799493f2b1e6123c18def94295d0afab7Kenny Root * this software without specific prior written permission.
18c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
19c74663799493f2b1e6123c18def94295d0afab7Kenny Root * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20c74663799493f2b1e6123c18def94295d0afab7Kenny Root * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21c74663799493f2b1e6123c18def94295d0afab7Kenny Root * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22c74663799493f2b1e6123c18def94295d0afab7Kenny Root * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23c74663799493f2b1e6123c18def94295d0afab7Kenny Root * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24c74663799493f2b1e6123c18def94295d0afab7Kenny Root * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25c74663799493f2b1e6123c18def94295d0afab7Kenny Root * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26c74663799493f2b1e6123c18def94295d0afab7Kenny Root * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27c74663799493f2b1e6123c18def94295d0afab7Kenny Root * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28c74663799493f2b1e6123c18def94295d0afab7Kenny Root * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29c74663799493f2b1e6123c18def94295d0afab7Kenny Root * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30c74663799493f2b1e6123c18def94295d0afab7Kenny Root */
31c74663799493f2b1e6123c18def94295d0afab7Kenny Root
32c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if HAVE_CONFIG_H
33c74663799493f2b1e6123c18def94295d0afab7Kenny Root#  include <config.h>
34c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
35c74663799493f2b1e6123c18def94295d0afab7Kenny Root
36c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "FLAC/assert.h"
37c74663799493f2b1e6123c18def94295d0afab7Kenny Root
38c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "private/float.h"
39c74663799493f2b1e6123c18def94295d0afab7Kenny Root
40c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef FLAC__INTEGER_ONLY_LIBRARY
41c74663799493f2b1e6123c18def94295d0afab7Kenny Root
42c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
43c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
44c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__U64L(x) x
45c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
46c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__U64L(x) x##LLU
47c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
48c74663799493f2b1e6123c18def94295d0afab7Kenny Root
49c74663799493f2b1e6123c18def94295d0afab7Kenny Rootconst FLAC__fixedpoint FLAC__FP_ZERO = 0;
50c74663799493f2b1e6123c18def94295d0afab7Kenny Rootconst FLAC__fixedpoint FLAC__FP_ONE_HALF = 0x00008000;
51c74663799493f2b1e6123c18def94295d0afab7Kenny Rootconst FLAC__fixedpoint FLAC__FP_ONE = 0x00010000;
52c74663799493f2b1e6123c18def94295d0afab7Kenny Rootconst FLAC__fixedpoint FLAC__FP_LN2 = 45426;
53c74663799493f2b1e6123c18def94295d0afab7Kenny Rootconst FLAC__fixedpoint FLAC__FP_E = 178145;
54c74663799493f2b1e6123c18def94295d0afab7Kenny Root
55c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* Lookup tables for Knuth's logarithm algorithm */
56c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define LOG2_LOOKUP_PRECISION 16
57c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic const FLAC__uint32 log2_lookup[][LOG2_LOOKUP_PRECISION] = {
58c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
59c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
60c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 0 fraction bits
61c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
62c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
63c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ 0x00000001,
64c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ 0x00000000,
65c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ 0x00000000,
66c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ 0x00000000,
67c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ 0x00000000,
68c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ 0x00000000,
69c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ 0x00000000,
70c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ 0x00000000,
71c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ 0x00000000,
72c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ 0x00000000,
73c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ 0x00000000,
74c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ 0x00000000,
75c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ 0x00000000,
76c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ 0x00000000,
77c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ 0x00000000
78c74663799493f2b1e6123c18def94295d0afab7Kenny Root	},
79c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
80c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
81c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 4 fraction bits
82c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
83c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
84c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ 0x00000010,
85c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ 0x00000007,
86c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ 0x00000003,
87c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ 0x00000001,
88c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ 0x00000001,
89c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ 0x00000000,
90c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ 0x00000000,
91c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ 0x00000000,
92c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ 0x00000000,
93c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ 0x00000000,
94c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ 0x00000000,
95c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ 0x00000000,
96c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ 0x00000000,
97c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ 0x00000000,
98c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ 0x00000000
99c74663799493f2b1e6123c18def94295d0afab7Kenny Root	},
100c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
101c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
102c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 8 fraction bits
103c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
104c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
105c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ 0x00000100,
106c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ 0x0000006a,
107c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ 0x00000031,
108c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ 0x00000018,
109c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ 0x0000000c,
110c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ 0x00000006,
111c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ 0x00000003,
112c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ 0x00000001,
113c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ 0x00000001,
114c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ 0x00000000,
115c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ 0x00000000,
116c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ 0x00000000,
117c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ 0x00000000,
118c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ 0x00000000,
119c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ 0x00000000
120c74663799493f2b1e6123c18def94295d0afab7Kenny Root	},
121c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
122c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
123c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 12 fraction bits
124c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
125c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
126c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ 0x00001000,
127c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ 0x000006a4,
128c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ 0x00000315,
129c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ 0x0000017d,
130c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ 0x000000bc,
131c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ 0x0000005d,
132c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ 0x0000002e,
133c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ 0x00000017,
134c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ 0x0000000c,
135c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ 0x00000006,
136c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ 0x00000003,
137c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ 0x00000001,
138c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ 0x00000001,
139c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ 0x00000000,
140c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ 0x00000000
141c74663799493f2b1e6123c18def94295d0afab7Kenny Root	},
142c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
143c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
144c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 16 fraction bits
145c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
146c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
147c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ 0x00010000,
148c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ 0x00006a40,
149c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ 0x00003151,
150c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ 0x000017d6,
151c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ 0x00000bba,
152c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ 0x000005d1,
153c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ 0x000002e6,
154c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ 0x00000172,
155c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ 0x000000b9,
156c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ 0x0000005c,
157c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ 0x0000002e,
158c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ 0x00000017,
159c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ 0x0000000c,
160c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ 0x00000006,
161c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ 0x00000003
162c74663799493f2b1e6123c18def94295d0afab7Kenny Root	},
163c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
164c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
165c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 20 fraction bits
166c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
167c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
168c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ 0x00100000,
169c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ 0x0006a3fe,
170c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ 0x00031513,
171c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ 0x00017d60,
172c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ 0x0000bb9d,
173c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ 0x00005d10,
174c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ 0x00002e59,
175c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ 0x00001721,
176c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ 0x00000b8e,
177c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ 0x000005c6,
178c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ 0x000002e3,
179c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ 0x00000171,
180c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ 0x000000b9,
181c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ 0x0000005c,
182c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ 0x0000002e
183c74663799493f2b1e6123c18def94295d0afab7Kenny Root	},
184c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
185c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
186c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 24 fraction bits
187c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
188c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
189c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ 0x01000000,
190c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ 0x006a3fe6,
191c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ 0x00315130,
192c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ 0x0017d605,
193c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ 0x000bb9ca,
194c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ 0x0005d0fc,
195c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ 0x0002e58f,
196c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ 0x0001720e,
197c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ 0x0000b8d8,
198c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ 0x00005c61,
199c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ 0x00002e2d,
200c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ 0x00001716,
201c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ 0x00000b8b,
202c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ 0x000005c5,
203c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ 0x000002e3
204c74663799493f2b1e6123c18def94295d0afab7Kenny Root	},
205c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
206c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
207c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 28 fraction bits
208c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
209c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
210c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ 0x10000000,
211c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ 0x06a3fe5c,
212c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ 0x03151301,
213c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ 0x017d6049,
214c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ 0x00bb9ca6,
215c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ 0x005d0fba,
216c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ 0x002e58f7,
217c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ 0x001720da,
218c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ 0x000b8d87,
219c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ 0x0005c60b,
220c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ 0x0002e2d7,
221c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ 0x00017160,
222c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ 0x0000b8ad,
223c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ 0x00005c56,
224c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ 0x00002e2b
225c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
226c74663799493f2b1e6123c18def94295d0afab7Kenny Root};
227c74663799493f2b1e6123c18def94295d0afab7Kenny Root
228c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 0
229c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic const FLAC__uint64 log2_lookup_wide[] = {
230c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
231c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
232c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 32 fraction bits
233c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
234c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
235c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ FLAC__U64L(0x100000000),
236c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ FLAC__U64L(0x6a3fe5c6),
237c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ FLAC__U64L(0x31513015),
238c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ FLAC__U64L(0x17d60497),
239c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ FLAC__U64L(0x0bb9ca65),
240c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ FLAC__U64L(0x05d0fba2),
241c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ FLAC__U64L(0x02e58f74),
242c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ FLAC__U64L(0x01720d9c),
243c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ FLAC__U64L(0x00b8d875),
244c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ FLAC__U64L(0x005c60aa),
245c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ FLAC__U64L(0x002e2d72),
246c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ FLAC__U64L(0x00171600),
247c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ FLAC__U64L(0x000b8ad2),
248c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ FLAC__U64L(0x0005c55d),
249c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ FLAC__U64L(0x0002e2ac)
250c74663799493f2b1e6123c18def94295d0afab7Kenny Root	},
251c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
252c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/*
253c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * 48 fraction bits
254c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
255c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* undefined */ 0x00000000,
256c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2/1) = */ FLAC__U64L(0x1000000000000),
257c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4/3) = */ FLAC__U64L(0x6a3fe5c60429),
258c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8/7) = */ FLAC__U64L(0x315130157f7a),
259c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16/15) = */ FLAC__U64L(0x17d60496cfbb),
260c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32/31) = */ FLAC__U64L(0xbb9ca64ecac),
261c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(64/63) = */ FLAC__U64L(0x5d0fba187cd),
262c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(128/127) = */ FLAC__U64L(0x2e58f7441ee),
263c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(256/255) = */ FLAC__U64L(0x1720d9c06a8),
264c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(512/511) = */ FLAC__U64L(0xb8d8752173),
265c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(1024/1023) = */ FLAC__U64L(0x5c60aa252e),
266c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(2048/2047) = */ FLAC__U64L(0x2e2d71b0d8),
267c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(4096/4095) = */ FLAC__U64L(0x1716001719),
268c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(8192/8191) = */ FLAC__U64L(0xb8ad1de1b),
269c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(16384/16383) = */ FLAC__U64L(0x5c55d640d),
270c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* lg(32768/32767) = */ FLAC__U64L(0x2e2abcf52)
271c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
272c74663799493f2b1e6123c18def94295d0afab7Kenny Root};
273c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
274c74663799493f2b1e6123c18def94295d0afab7Kenny Root
275c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__uint32 FLAC__fixedpoint_log2(FLAC__uint32 x, unsigned fracbits, unsigned precision)
276c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
277c74663799493f2b1e6123c18def94295d0afab7Kenny Root	const FLAC__uint32 ONE = (1u << fracbits);
278c74663799493f2b1e6123c18def94295d0afab7Kenny Root	const FLAC__uint32 *table = log2_lookup[fracbits >> 2];
279c74663799493f2b1e6123c18def94295d0afab7Kenny Root
280c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(fracbits < 32);
281c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT((fracbits & 0x3) == 0);
282c74663799493f2b1e6123c18def94295d0afab7Kenny Root
283c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(x < ONE)
284c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return 0;
285c74663799493f2b1e6123c18def94295d0afab7Kenny Root
286c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(precision > LOG2_LOOKUP_PRECISION)
287c74663799493f2b1e6123c18def94295d0afab7Kenny Root		precision = LOG2_LOOKUP_PRECISION;
288c74663799493f2b1e6123c18def94295d0afab7Kenny Root
289c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* Knuth's algorithm for computing logarithms, optimized for base-2 with lookup tables */
290c74663799493f2b1e6123c18def94295d0afab7Kenny Root	{
291c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__uint32 y = 0;
292c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__uint32 z = x >> 1, k = 1;
293c74663799493f2b1e6123c18def94295d0afab7Kenny Root		while (x > ONE && k < precision) {
294c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if (x - z >= ONE) {
295c74663799493f2b1e6123c18def94295d0afab7Kenny Root				x -= z;
296c74663799493f2b1e6123c18def94295d0afab7Kenny Root				z = x >> k;
297c74663799493f2b1e6123c18def94295d0afab7Kenny Root				y += table[k];
298c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
299c74663799493f2b1e6123c18def94295d0afab7Kenny Root			else {
300c74663799493f2b1e6123c18def94295d0afab7Kenny Root				z >>= 1;
301c74663799493f2b1e6123c18def94295d0afab7Kenny Root				k++;
302c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
303c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
304c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return y;
305c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
306c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
307c74663799493f2b1e6123c18def94295d0afab7Kenny Root
308c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif /* defined FLAC__INTEGER_ONLY_LIBRARY */
309