shithub: opus

Download patch

ref: 8c4b88cfab81ae085f23935508ecd65e8065df40
parent: e35441f2cce59cb0a8dc1dbed5755ed1ce43f8ca
author: Jean-Marc Valin <jmvalin@amazon.com>
date: Wed Jun 30 14:14:12 EDT 2021

Using a bisection search for sampling

--- a/dnn/nnet.c
+++ b/dnn/nnet.c
@@ -399,9 +399,22 @@
     }
     /* Do the sampling (from the cdf). */
     r = tmp[N-1] * ((rand()+.5f)/(RAND_MAX+1.f));
+#if 1 /* Bisection search in the CDF (faster than the equivalent linear one below). */
+    {
+        int start=-1;
+        int end = N-1;
+        while (end > start+1) {
+            int mid = (start+end)>>1;
+            if (r <= tmp[mid]) end = mid;
+            else start = mid;
+        }
+        return end;
+    }
+#else
     for (i=0;i<N-1;i++)
     {
         if (r <= tmp[i]) return i;
     }
     return N-1;
+#endif
 }
--