~ubuntu-branches/ubuntu/saucy/flac/saucy

« back to all changes in this revision

Viewing changes to src/libFLAC/bitmath.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2001-12-10 03:09:22 UTC
  • Revision ID: james.westby@ubuntu.com-20011210030922-0vdtpz6a7mfwefo5
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* libFLAC - Free Lossless Audio Codec library
 
2
 * Copyright (C) 2001  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 = -(++v);
 
97
                }
 
98
        }
 
99
}