shithub: opus

Download patch

ref: dc767f67f97c598ca463bdcd95788f1765265522
parent: 22823834348e0dc2c4953fd16f53c0a895245590
author: Jean-Marc Valin <[email protected]>
date: Sat Mar 22 18:23:58 EDT 2008

No longer trying to save bits when encoding integers near the upper limit
(and fix for celt_div with 16-bit numerator on a 16-bit CPU)

--- a/libcelt/entdec.c
+++ b/libcelt/entdec.c
@@ -111,7 +111,6 @@
 }
 
 ec_uint32 ec_dec_uint(ec_dec *_this,ec_uint32 _ft){
-  ec_uint32 mask;
   ec_uint32 t;
   unsigned  ft;
   unsigned  s;
@@ -119,25 +118,23 @@
   t=0;
   _ft--;
   ftb=EC_ILOG(_ft);
-  while(ftb>EC_UNIT_BITS){
+  if(ftb>EC_UNIT_BITS){
     ftb-=EC_UNIT_BITS;
     ft=(unsigned)(_ft>>ftb)+1;
     s=ec_decode(_this,ft);
     ec_dec_update(_this,s,s+1,ft);
     t=t<<EC_UNIT_BITS|s;
-    if(s<ft-1)return t<<ftb|ec_dec_bits(_this,ftb);
-    mask=((ec_uint32)1<<ftb)-1;
-    _ft=_ft&mask;
+    return t<<ftb|ec_dec_bits(_this,ftb);
+  } else {
+    _ft++;
+    s=ec_decode(_this,(unsigned)_ft);
+    ec_dec_update(_this,s,s+1,(unsigned)_ft);
+    t=t<<ftb|s;
+    return t;
   }
-  _ft++;
-  s=ec_decode(_this,(unsigned)_ft);
-  ec_dec_update(_this,s,s+1,(unsigned)_ft);
-  t=t<<ftb|s;
-  return t;
 }
 
 ec_uint64 ec_dec_uint64(ec_dec *_this,ec_uint64 _ft){
-  ec_uint64 mask;
   ec_uint64 t;
   unsigned  ft;
   unsigned  s;
@@ -145,19 +142,18 @@
   t=0;
   _ft--;
   ftb=EC_ILOG64(_ft);
-  while(ftb>EC_UNIT_BITS){
+  if(ftb>EC_UNIT_BITS){
     ftb-=EC_UNIT_BITS;
     ft=(unsigned)(_ft>>ftb)+1;
     s=ec_decode(_this,ft);
     ec_dec_update(_this,s,s+1,ft);
     t=t<<EC_UNIT_BITS|s;
-    if(s<ft-1)return t<<ftb|ec_dec_bits64(_this,ftb);
-    mask=((ec_uint64)1<<ftb)-1;
-    _ft=_ft&mask;
+    return t<<ftb|ec_dec_bits64(_this,ftb);
+  } else {
+    _ft++;
+    s=ec_decode(_this,(unsigned)_ft);
+    ec_dec_update(_this,s,s+1,(unsigned)_ft);
+    t=t<<ftb|s;
+    return t;
   }
-  _ft++;
-  s=ec_decode(_this,(unsigned)_ft);
-  ec_dec_update(_this,s,s+1,(unsigned)_ft);
-  t=t<<ftb|s;
-  return t;
 }
--- a/libcelt/entenc.c
+++ b/libcelt/entenc.c
@@ -87,47 +87,35 @@
 }
 
 void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){
-  ec_uint32 mask;
   unsigned  ft;
   unsigned  fl;
   int       ftb;
   _ft--;
   ftb=EC_ILOG(_ft)&-!!_ft;
-  while(ftb>EC_UNIT_BITS){
+  if(ftb>EC_UNIT_BITS){
     ftb-=EC_UNIT_BITS;
     ft=(_ft>>ftb)+1;
     fl=(unsigned)(_fl>>ftb);
     ec_encode(_this,fl,fl+1,ft);
-    if(fl<ft-1){
-      ec_enc_bits(_this,_fl,ftb);
-      return;
-    }
-    mask=((ec_uint32)1<<ftb)-1;
-    _fl=_fl&mask;
-    _ft=_ft&mask;
+    ec_enc_bits(_this,_fl,ftb);
+  } else {
+    ec_encode(_this,_fl,_fl+1,_ft+1);
   }
-  ec_encode(_this,_fl,_fl+1,_ft+1);
 }
 
 void ec_enc_uint64(ec_enc *_this,ec_uint64 _fl,ec_uint64 _ft){
-  ec_uint64 mask;
   unsigned  ft;
   unsigned  fl;
   int       ftb;
   _ft--;
   ftb=EC_ILOG64(_ft)&-!!_ft;
-  while(ftb>EC_UNIT_BITS){
+  if(ftb>EC_UNIT_BITS){
     ftb-=EC_UNIT_BITS;
     ft=(unsigned)(_ft>>ftb)+1;
     fl=(unsigned)(_fl>>ftb);
     ec_encode(_this,fl,fl+1,ft);
-    if(fl<ft-1){
-      ec_enc_bits64(_this,_fl,ftb);
-      return;
-    }
-    mask=((ec_uint64)1<<ftb)-1;
-    _fl=_fl&mask;
-    _ft=_ft&mask;
+    ec_enc_bits64(_this,_fl,ftb);
+  } else {
+    ec_encode(_this,_fl,_fl+1,_ft+1);
   }
-  ec_encode(_this,_fl,_fl+1,_ft+1);
 }
--- a/libcelt/mathops.h
+++ b/libcelt/mathops.h
@@ -203,7 +203,7 @@
    return VSHR32(EXTEND32(frac),i-16);
 }
 
-#define celt_div(a,b) MULT32_32_Q31(a,celt_rcp(b))
+#define celt_div(a,b) MULT32_32_Q31((celt_word32_t)(a),celt_rcp(b))
 
 #endif /* FIXED_POINT */