shithub: opus

Download patch

ref: 558c50eb3d052182cabcbe4c6276a257fe49d8cc
parent: 57e004bf7460b2ad3861b7898c8fd75bf85efb50
author: Jean-Marc Valin <[email protected]>
date: Wed Apr 16 10:15:07 EDT 2008

optimisation: Making it clear to the compiler that many of the loops in cwrs
need to iterate at least once.

--- a/libcelt/cwrs.c
+++ b/libcelt/cwrs.c
@@ -54,11 +54,12 @@
 static inline void unext32(celt_uint32_t *_ui,int _len,celt_uint32_t _ui0){
   celt_uint32_t ui1;
   int           j;
-  for(j=1;j<_len;j++){
+  /* doing a do-while would overrun the array if we had less than 2 samples */
+  j=1; do {
     ui1=_ui[j]+_ui[j-1]+_ui0;
     _ui[j-1]=_ui0;
     _ui0=ui1;
-  }
+  } while (++j<_len);
   _ui[j-1]=_ui0;
 }
 
@@ -65,11 +66,12 @@
 static inline void unext64(celt_uint64_t *_ui,int _len,celt_uint64_t _ui0){
   celt_uint64_t ui1;
   int           j;
-  for(j=1;j<_len;j++){
+  /* doing a do-while would overrun the array if we had less than 2 samples */
+  j=1; do {
     ui1=_ui[j]+_ui[j-1]+_ui0;
     _ui[j-1]=_ui0;
     _ui0=ui1;
-  }
+  } while (++j<_len);
   _ui[j-1]=_ui0;
 }
 
@@ -79,11 +81,12 @@
 static inline void uprev32(celt_uint32_t *_ui,int _n,celt_uint32_t _ui0){
   celt_uint32_t ui1;
   int           j;
-  for(j=1;j<_n;j++){
+  /* doing a do-while would overrun the array if we had less than 2 samples */
+  j=1; do {
     ui1=_ui[j]-_ui[j-1]-_ui0;
     _ui[j-1]=_ui0;
     _ui0=ui1;
-  }
+  } while (++j<_n);
   _ui[j-1]=_ui0;
 }
 
@@ -90,11 +93,12 @@
 static inline void uprev64(celt_uint64_t *_ui,int _n,celt_uint64_t _ui0){
   celt_uint64_t ui1;
   int           j;
-  for(j=1;j<_n;j++){
+  /* doing a do-while would overrun the array if we had less than 2 samples */
+  j=1; do {
     ui1=_ui[j]-_ui[j-1]-_ui0;
     _ui[j-1]=_ui0;
     _ui0=ui1;
-  }
+  } while (++j<_n);
   _ui[j-1]=_ui0;
 }
 
@@ -108,12 +112,13 @@
   celt_uint32_t ui1;
   int           j;
   ret=ui0=2;
-  for(j=1;j<_n;j++){
+  celt_assert(_n>=2);
+  j=1; do {
     ui1=_ui[j]+_ui[j-1]+ui0;
     _ui[j-1]=ui0;
     ui0=ui1;
     ret+=ui0;
-  }
+  } while (++j<_n);
   _ui[j-1]=ui0;
   return ret;
 }
@@ -124,12 +129,13 @@
   celt_uint64_t ui1;
   int           j;
   ret=ui0=2;
-  for(j=1;j<_n;j++){
+  celt_assert(_n>=2);
+  j=1; do {
     ui1=_ui[j]+_ui[j-1]+ui0;
     _ui[j-1]=ui0;
     ui0=ui1;
     ret+=ui0;
-  }
+  } while (++j<_n);
   _ui[j-1]=ui0;
   return ret;
 }
--- a/libcelt/mathops.h
+++ b/libcelt/mathops.h
@@ -37,6 +37,7 @@
 
 #include "arch.h"
 #include "entcode.h"
+#include "os_support.h"
 
 #ifndef OVERRIDE_CELT_ILOG2
 /** Integer log in base2. Undefined for zero and negative numbers */
--- a/tests/cwrs32-test.c
+++ b/tests/cwrs32-test.c
@@ -10,7 +10,7 @@
 
 int main(int _argc,char **_argv){
   int n;
-  for(n=0;n<=NMAX;n++){
+  for(n=2;n<=NMAX;n++){
     int m;
     for(m=0;m<=MMAX;m++){
       celt_uint32_t uu[NMAX];
--- a/tests/cwrs64-test.c
+++ b/tests/cwrs64-test.c
@@ -11,7 +11,7 @@
 
 int main(int _argc,char **_argv){
   int n;
-  for(n=0;n<=NMAX;n+=3){
+  for(n=2;n<=NMAX;n+=3){
     int m;
     for(m=0;m<=MMAX;m++){
       celt_uint64_t uu[NMAX];