remove reference to obsolete Makefile.vc
[platform/upstream/flac.git] / src / libFLAC / bitmath.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2001,2002,2003  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include "private/bitmath.h"
21 #include "FLAC/assert.h"
22
23 /* An example of what FLAC__bitmath_ilog2() computes:
24  *
25  * ilog2( 0) = assertion failure
26  * ilog2( 1) = 0
27  * ilog2( 2) = 1
28  * ilog2( 3) = 1
29  * ilog2( 4) = 2
30  * ilog2( 5) = 2
31  * ilog2( 6) = 2
32  * ilog2( 7) = 2
33  * ilog2( 8) = 3
34  * ilog2( 9) = 3
35  * ilog2(10) = 3
36  * ilog2(11) = 3
37  * ilog2(12) = 3
38  * ilog2(13) = 3
39  * ilog2(14) = 3
40  * ilog2(15) = 3
41  * ilog2(16) = 4
42  * ilog2(17) = 4
43  * ilog2(18) = 4
44  */
45 unsigned FLAC__bitmath_ilog2(unsigned v)
46 {
47         unsigned l = 0;
48         FLAC__ASSERT(v > 0);
49         while(v >>= 1)
50                 l++;
51         return l;
52 }
53
54 /* An example of what FLAC__bitmath_silog2() computes:
55  *
56  * silog2(-10) = 5
57  * silog2(- 9) = 5
58  * silog2(- 8) = 4
59  * silog2(- 7) = 4
60  * silog2(- 6) = 4
61  * silog2(- 5) = 4
62  * silog2(- 4) = 3
63  * silog2(- 3) = 3
64  * silog2(- 2) = 2
65  * silog2(- 1) = 2
66  * silog2(  0) = 0
67  * silog2(  1) = 2
68  * silog2(  2) = 3
69  * silog2(  3) = 3
70  * silog2(  4) = 4
71  * silog2(  5) = 4
72  * silog2(  6) = 4
73  * silog2(  7) = 4
74  * silog2(  8) = 5
75  * silog2(  9) = 5
76  * silog2( 10) = 5
77  */
78 unsigned FLAC__bitmath_silog2(int v)
79 {
80         while(1) {
81                 if(v == 0) {
82                         return 0;
83                 }
84                 else if(v > 0) {
85                         unsigned l = 0;
86                         while(v) {
87                                 l++;
88                                 v >>= 1;
89                         }
90                         return l+1;
91                 }
92                 else if(v == -1) {
93                         return 2;
94                 }
95                 else {
96                         v++;
97                         v = -v;
98                 }
99         }
100 }
101
102 unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v)
103 {
104         while(1) {
105                 if(v == 0) {
106                         return 0;
107                 }
108                 else if(v > 0) {
109                         unsigned l = 0;
110                         while(v) {
111                                 l++;
112                                 v >>= 1;
113                         }
114                         return l+1;
115                 }
116                 else if(v == -1) {
117                         return 2;
118                 }
119                 else {
120                         v++;
121                         v = -v;
122                 }
123         }
124 }