Josh Coalson | 33abe8c | 2001-03-30 00:44:16 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 1152f9f | 2002-01-26 18:05:12 +0000 | [diff] [blame] | 2 | * Copyright (C) 2001,2002 Josh Coalson |
Josh Coalson | 33abe8c | 2001-03-30 00:44:16 +0000 | [diff] [blame] | 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 | |
Josh Coalson | 33abe8c | 2001-03-30 00:44:16 +0000 | [diff] [blame] | 20 | #include "private/bitmath.h" |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 21 | #include "FLAC/assert.h" |
Josh Coalson | 33abe8c | 2001-03-30 00:44:16 +0000 | [diff] [blame] | 22 | |
Josh Coalson | a4e95fc | 2001-07-12 21:28:12 +0000 | [diff] [blame] | 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 | */ |
Josh Coalson | 33abe8c | 2001-03-30 00:44:16 +0000 | [diff] [blame] | 45 | unsigned FLAC__bitmath_ilog2(unsigned v) |
| 46 | { |
| 47 | unsigned l = 0; |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 48 | FLAC__ASSERT(v > 0); |
Josh Coalson | 33abe8c | 2001-03-30 00:44:16 +0000 | [diff] [blame] | 49 | while(v >>= 1) |
| 50 | l++; |
| 51 | return l; |
| 52 | } |
| 53 | |
Josh Coalson | a4e95fc | 2001-07-12 21:28:12 +0000 | [diff] [blame] | 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 | */ |
Josh Coalson | 33abe8c | 2001-03-30 00:44:16 +0000 | [diff] [blame] | 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 { |
Josh Coalson | 28b1857 | 2002-03-12 16:18:11 +0000 | [diff] [blame] | 96 | v++; |
| 97 | v = -v; |
Josh Coalson | 33abe8c | 2001-03-30 00:44:16 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
Josh Coalson | bf26db9 | 2002-10-04 05:24:10 +0000 | [diff] [blame] | 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 | } |