Browse code

avcodec/mpegaudio_tablegen: speed up dynamic table creation

This does some miscellaneous stuff mainly avoiding the usage of pow to
achieve significant speedups. This is not speed critical, but is
unnecessary latency and cycles wasted for a user.

All tables tested and are identical to the old ones
(bit-exact even in floating point case).

Sample benchmark (x86-64, Haswell, GNU/Linux):
old:
102329530 decicycles in mpegaudio_tableinit, 1 runs, 0 skips

new:
34111900 decicycles in mpegaudio_tableinit, 1 runs, 0 skips

Reviewed-by: Ronald S. Bultje <rsbultje@gmail.com>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com>

Ganesh Ajjanagadde authored on 2015/11/26 06:26:08
Showing 2 changed files
... ...
@@ -23,6 +23,7 @@
23 23
 #include <stdlib.h>
24 24
 #define CONFIG_HARDCODED_TABLES 0
25 25
 #include "mpegaudio_tablegen.h"
26
+#include "libavutil/tablegen.h"
26 27
 #include "tableprint.h"
27 28
 
28 29
 int main(void)
... ...
@@ -45,12 +45,21 @@ static float expval_table_float[512][16];
45 45
 static av_cold void mpegaudio_tableinit(void)
46 46
 {
47 47
     int i, value, exponent;
48
+    double exp2_lut[4] = {
49
+        1.00000000000000000000, /* 2 ^ (0 * 0.25) */
50
+        1.18920711500272106672, /* 2 ^ (1 * 0.25) */
51
+        M_SQRT2               , /* 2 ^ (2 * 0.25) */
52
+        1.68179283050742908606, /* 2 ^ (3 * 0.25) */
53
+    };
54
+    double cbrt_lut[16];
55
+    for (i = 0; i < 16; ++i)
56
+        cbrt_lut[i] = cbrt(i);
57
+
48 58
     for (i = 1; i < TABLE_4_3_SIZE; i++) {
49 59
         double value = i / 4;
50 60
         double f, fm;
51 61
         int e, m;
52
-        /* cbrtf() isn't available on all systems, so we use powf(). */
53
-        f  = value / IMDCT_SCALAR * pow(value, 1.0 / 3.0) * pow(2, (i & 3) * 0.25);
62
+        f  = value / IMDCT_SCALAR * cbrt(value) * exp2_lut[i & 3];
54 63
         fm = frexp(f, &e);
55 64
         m  = (uint32_t)(fm * (1LL << 31) + 0.5);
56 65
         e += FRAC_BITS - 31 + 5 - 100;
... ...
@@ -61,10 +70,8 @@ static av_cold void mpegaudio_tableinit(void)
61 61
     }
62 62
     for (exponent = 0; exponent < 512; exponent++) {
63 63
         for (value = 0; value < 16; value++) {
64
-            /* cbrtf() isn't available on all systems, so we use powf(). */
65
-            double f = (double)value * pow(value, 1.0 / 3.0) * pow(2, (exponent - 400) * 0.25 + FRAC_BITS + 5) / IMDCT_SCALAR;
66
-            /* llrint() isn't always available, so round and cast manually. */
67
-            expval_table_fixed[exponent][value] = (long long int) (f < 0xFFFFFFFF ? floor(f + 0.5) : 0xFFFFFFFF);
64
+            double f = value * cbrt_lut[value] * pow(2, (exponent - 400) * 0.25 + FRAC_BITS + 5) / IMDCT_SCALAR;
65
+            expval_table_fixed[exponent][value] = (f < 0xFFFFFFFF ? llrint(f) : 0xFFFFFFFF);
68 66
             expval_table_float[exponent][value] = f;
69 67
         }
70 68
         exp_table_fixed[exponent] = expval_table_fixed[exponent][1];