Home | History | Annotate | Download | only in celt
      1 /* Copyright (c) 2001-2011 Timothy B. Terriberry
      2 */
      3 /*
      4    Redistribution and use in source and binary forms, with or without
      5    modification, are permitted provided that the following conditions
      6    are met:
      7 
      8    - Redistributions of source code must retain the above copyright
      9    notice, this list of conditions and the following disclaimer.
     10 
     11    - Redistributions in binary form must reproduce the above copyright
     12    notice, this list of conditions and the following disclaimer in the
     13    documentation and/or other materials provided with the distribution.
     14 
     15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 */
     27 
     28 #ifdef HAVE_CONFIG_H
     29 #include "config.h"
     30 #endif
     31 
     32 #include "entcode.h"
     33 #include "arch.h"
     34 
     35 #if !defined(EC_CLZ)
     36 /*This is a fallback for systems where we don't know how to access
     37    a BSR or CLZ instruction (see ecintrin.h).
     38   If you are optimizing Opus on a new platform and it has a native CLZ or
     39    BZR (e.g. cell, MIPS, x86, etc) then making it available to Opus will be
     40    an easy performance win.*/
     41 int ec_ilog(opus_uint32 _v){
     42   /*On a Pentium M, this branchless version tested as the fastest on
     43      1,000,000,000 random 32-bit integers, edging out a similar version with
     44      branches, and a 256-entry LUT version.*/
     45   int ret;
     46   int m;
     47   ret=!!_v;
     48   m=!!(_v&0xFFFF0000)<<4;
     49   _v>>=m;
     50   ret|=m;
     51   m=!!(_v&0xFF00)<<3;
     52   _v>>=m;
     53   ret|=m;
     54   m=!!(_v&0xF0)<<2;
     55   _v>>=m;
     56   ret|=m;
     57   m=!!(_v&0xC)<<1;
     58   _v>>=m;
     59   ret|=m;
     60   ret+=!!(_v&0x2);
     61   return ret;
     62 }
     63 #endif
     64 
     65 opus_uint32 ec_tell_frac(ec_ctx *_this){
     66   opus_uint32 nbits;
     67   opus_uint32 r;
     68   int         l;
     69   int         i;
     70   /*To handle the non-integral number of bits still left in the encoder/decoder
     71      state, we compute the worst-case number of bits of val that must be
     72      encoded to ensure that the value is inside the range for any possible
     73      subsequent bits.
     74     The computation here is independent of val itself (the decoder does not
     75      even track that value), even though the real number of bits used after
     76      ec_enc_done() may be 1 smaller if rng is a power of two and the
     77      corresponding trailing bits of val are all zeros.
     78     If we did try to track that special case, then coding a value with a
     79      probability of 1/(1<<n) might sometimes appear to use more than n bits.
     80     This may help explain the surprising result that a newly initialized
     81      encoder or decoder claims to have used 1 bit.*/
     82   nbits=_this->nbits_total<<BITRES;
     83   l=EC_ILOG(_this->rng);
     84   r=_this->rng>>(l-16);
     85   for(i=BITRES;i-->0;){
     86     int b;
     87     r=r*r>>15;
     88     b=(int)(r>>16);
     89     l=l<<1|b;
     90     r>>=b;
     91   }
     92   return nbits-l;
     93 }
     94