Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 1 | /* (C) 2007 Timothy B. Terriberry */ |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 2 | /* |
| 3 | Redistribution and use in source and binary forms, with or without |
| 4 | modification, are permitted provided that the following conditions |
| 5 | are met: |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 6 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 7 | - Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 9 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 10 | - Redistributions in binary form must reproduce the above copyright |
| 11 | notice, this list of conditions and the following disclaimer in the |
| 12 | documentation and/or other materials provided with the distribution. |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 13 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 14 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 15 | contributors may be used to endorse or promote products derived from |
| 16 | this software without specific prior written permission. |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 17 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 22 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 30 | #include <stdlib.h> |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 31 | #include "cwrs.h" |
| 32 | |
| 33 | /*Returns the numer of ways of choosing _m elements from a set of size _n with |
| 34 | replacement when a sign bit is needed for each unique element.*/ |
| 35 | #if 0 |
Jean-Marc Valin | abe043f | 2008-01-31 14:26:29 +1100 | [diff] [blame^] | 36 | static celt_uint32_t ncwrs(int _n,int _m){ |
| 37 | static celt_uint32_t c[32][32]; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 38 | if(_n<0||_m<0)return 0; |
| 39 | if(!c[_n][_m]){ |
| 40 | if(_m<=0)c[_n][_m]=1; |
| 41 | else if(_n>0)c[_n][_m]=ncwrs(_n-1,_m)+ncwrs(_n,_m-1)+ncwrs(_n-1,_m-1); |
| 42 | } |
| 43 | return c[_n][_m]; |
| 44 | } |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 45 | #else |
Jean-Marc Valin | abe043f | 2008-01-31 14:26:29 +1100 | [diff] [blame^] | 46 | celt_uint32_t ncwrs(int _n,int _m){ |
| 47 | celt_uint32_t ret; |
| 48 | celt_uint32_t f; |
| 49 | celt_uint32_t d; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 50 | int i; |
| 51 | if(_n<0||_m<0)return 0; |
| 52 | if(_m==0)return 1; |
| 53 | if(_n==0)return 0; |
| 54 | ret=0; |
| 55 | f=_n; |
| 56 | d=1; |
| 57 | for(i=1;i<=_m;i++){ |
| 58 | ret+=f*d<<i; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 59 | f=(f*(_n-i))/(i+1); |
| 60 | d=(d*(_m-i))/i; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 61 | } |
| 62 | return ret; |
| 63 | } |
| 64 | #endif |
| 65 | |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 66 | celt_uint64_t ncwrs64(int _n,int _m){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 67 | celt_uint64_t ret; |
| 68 | celt_uint64_t f; |
| 69 | celt_uint64_t d; |
| 70 | int i; |
| 71 | if(_n<0||_m<0)return 0; |
| 72 | if(_m==0)return 1; |
| 73 | if(_n==0)return 0; |
| 74 | ret=0; |
| 75 | f=_n; |
| 76 | d=1; |
| 77 | for(i=1;i<=_m;i++){ |
| 78 | ret+=f*d<<i; |
| 79 | f=(f*(_n-i))/(i+1); |
| 80 | d=(d*(_m-i))/i; |
| 81 | } |
| 82 | return ret; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 83 | } |
| 84 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 85 | /*Returns the _i'th combination of _m elements chosen from a set of size _n |
| 86 | with associated sign bits. |
| 87 | _x: Returns the combination with elements sorted in ascending order. |
| 88 | _s: Returns the associated sign bits.*/ |
Jean-Marc Valin | abe043f | 2008-01-31 14:26:29 +1100 | [diff] [blame^] | 89 | void cwrsi(int _n,int _m,celt_uint32_t _i,int *_x,int *_s){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 90 | int j; |
| 91 | int k; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 92 | for(k=j=0;k<_m;k++){ |
Jean-Marc Valin | abe043f | 2008-01-31 14:26:29 +1100 | [diff] [blame^] | 93 | celt_uint32_t pn; |
| 94 | celt_uint32_t p; |
| 95 | celt_uint32_t t; |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 96 | p=ncwrs(_n-j,_m-k-1); |
| 97 | pn=ncwrs(_n-j-1,_m-k-1); |
| 98 | p+=pn; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 99 | if(k>0){ |
| 100 | t=p>>1; |
| 101 | if(t<=_i||_s[k-1])_i+=t; |
| 102 | } |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 103 | while(p<=_i){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 104 | _i-=p; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 105 | j++; |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 106 | p=pn; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 107 | pn=ncwrs(_n-j-1,_m-k-1); |
| 108 | p+=pn; |
| 109 | } |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 110 | t=p>>1; |
| 111 | _s[k]=_i>=t; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 112 | _x[k]=j; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 113 | if(_s[k])_i-=t; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /*Returns the index of the given combination of _m elements chosen from a set |
| 118 | of size _n with associated sign bits. |
| 119 | _x: The combination with elements sorted in ascending order. |
| 120 | _s: The associated sign bits.*/ |
Jean-Marc Valin | abe043f | 2008-01-31 14:26:29 +1100 | [diff] [blame^] | 121 | celt_uint32_t icwrs(int _n,int _m,const int *_x,const int *_s){ |
| 122 | celt_uint32_t i; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 123 | int j; |
| 124 | int k; |
| 125 | i=0; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 126 | for(k=j=0;k<_m;k++){ |
Jean-Marc Valin | abe043f | 2008-01-31 14:26:29 +1100 | [diff] [blame^] | 127 | celt_uint32_t pn; |
| 128 | celt_uint32_t p; |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 129 | p=ncwrs(_n-j,_m-k-1); |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 130 | pn=ncwrs(_n-j-1,_m-k-1); |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 131 | p+=pn; |
| 132 | if(k>0)p>>=1; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 133 | while(j<_x[k]){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 134 | i+=p; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 135 | j++; |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 136 | p=pn; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 137 | pn=ncwrs(_n-j-1,_m-k-1); |
| 138 | p+=pn; |
| 139 | } |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 140 | if((k==0||_x[k]!=_x[k-1])&&_s[k])i+=p>>1; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 141 | } |
| 142 | return i; |
| 143 | } |
| 144 | |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 145 | /*Returns the _i'th combination of _m elements chosen from a set of size _n |
| 146 | with associated sign bits. |
| 147 | _x: Returns the combination with elements sorted in ascending order. |
| 148 | _s: Returns the associated sign bits.*/ |
| 149 | void cwrsi64(int _n,int _m,celt_uint64_t _i,int *_x,int *_s){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 150 | int j; |
| 151 | int k; |
| 152 | for(k=j=0;k<_m;k++){ |
| 153 | celt_uint64_t pn; |
| 154 | celt_uint64_t p; |
| 155 | celt_uint64_t t; |
| 156 | p=ncwrs64(_n-j,_m-k-1); |
| 157 | pn=ncwrs64(_n-j-1,_m-k-1); |
| 158 | p+=pn; |
| 159 | if(k>0){ |
| 160 | t=p>>1; |
| 161 | if(t<=_i||_s[k-1])_i+=t; |
| 162 | } |
| 163 | while(p<=_i){ |
| 164 | _i-=p; |
| 165 | j++; |
| 166 | p=pn; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 167 | pn=ncwrs64(_n-j-1,_m-k-1); |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 168 | p+=pn; |
| 169 | } |
| 170 | t=p>>1; |
| 171 | _s[k]=_i>=t; |
| 172 | _x[k]=j; |
| 173 | if(_s[k])_i-=t; |
| 174 | } |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /*Returns the index of the given combination of _m elements chosen from a set |
| 178 | of size _n with associated sign bits. |
| 179 | _x: The combination with elements sorted in ascending order. |
| 180 | _s: The associated sign bits.*/ |
| 181 | celt_uint64_t icwrs64(int _n,int _m,const int *_x,const int *_s){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 182 | celt_uint64_t i; |
| 183 | int j; |
| 184 | int k; |
| 185 | i=0; |
| 186 | for(k=j=0;k<_m;k++){ |
| 187 | celt_uint64_t pn; |
| 188 | celt_uint64_t p; |
| 189 | p=ncwrs64(_n-j,_m-k-1); |
| 190 | pn=ncwrs64(_n-j-1,_m-k-1); |
| 191 | p+=pn; |
| 192 | if(k>0)p>>=1; |
| 193 | while(j<_x[k]){ |
| 194 | i+=p; |
| 195 | j++; |
| 196 | p=pn; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 197 | pn=ncwrs64(_n-j-1,_m-k-1); |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 198 | p+=pn; |
| 199 | } |
| 200 | if((k==0||_x[k]!=_x[k-1])&&_s[k])i+=p>>1; |
| 201 | } |
| 202 | return i; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 203 | } |
| 204 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 205 | /*Converts a combination _x of _m unit pulses with associated sign bits _s into |
| 206 | a pulse vector _y of length _n. |
| 207 | _y: Returns the vector of pulses. |
| 208 | _x: The combination with elements sorted in ascending order. |
| 209 | _s: The associated sign bits.*/ |
| 210 | void comb2pulse(int _n,int _m,int *_y,const int *_x,const int *_s){ |
| 211 | int j; |
| 212 | int k; |
| 213 | int n; |
| 214 | for(k=j=0;k<_m;k+=n){ |
| 215 | for(n=1;k+n<_m&&_x[k+n]==_x[k];n++); |
| 216 | while(j<_x[k])_y[j++]=0; |
| 217 | _y[j++]=_s[k]?-n:n; |
| 218 | } |
| 219 | while(j<_n)_y[j++]=0; |
| 220 | } |
| 221 | |
| 222 | /*Converts a pulse vector vector _y of length _n into a combination of _m unit |
| 223 | pulses with associated sign bits _s. |
| 224 | _x: Returns the combination with elements sorted in ascending order. |
| 225 | _s: Returns the associated sign bits. |
| 226 | _y: The vector of pulses, whose sum of absolute values must be _m.*/ |
| 227 | void pulse2comb(int _n,int _m,int *_x,int *_s,const int *_y){ |
| 228 | int j; |
| 229 | int k; |
| 230 | for(k=j=0;j<_n;j++){ |
| 231 | if(_y[j]){ |
| 232 | int n; |
| 233 | int s; |
| 234 | n=abs(_y[j]); |
| 235 | s=_y[j]<0; |
| 236 | for(;n-->0;k++){ |
| 237 | _x[k]=j; |
| 238 | _s[k]=s; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |