diff mbox

[FFmpeg-devel,v2,14/15] avcodec/j2kenc: Simplify computations

Message ID 20190928022610.5903-14-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt Sept. 28, 2019, 2:26 a.m. UTC
1. (1 << (c - 1)) & ~((1 << c) - 1) is always zero. This together with
the fact that a square is always nonnegative (there's no overflow) allows
to remove a FFMAX.
2. The discriminant of the polynomial i * i - 2^(F+1) * i + 2^(F - 1) +
2^(2 * F) is negative; hence this polynomial has no real solutions, i.e.
its sign doesn't change and is always positive. This allows to remove a
FFMAX.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/j2kenc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index b5ac06b906..0a1421c844 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -522,13 +522,12 @@  static void init_luts(void)
 
     for (i = 0; i < (1 << NMSEDEC_BITS); i++){
         lut_nmsedec_sig[i]  = FFMAX((6 * i - (9 << (NMSEDEC_FRACBITS - 1))) * (1 << (12 - NMSEDEC_FRACBITS)), 0);
-        lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
+        lut_nmsedec_sig0[i] = 2 * i * i;
 
         a = (i >> (NMSEDEC_BITS-2)&2) + 1;
         lut_nmsedec_ref[i]  = FFMAX((-2 * i + (1 << NMSEDEC_FRACBITS) + a * i - (a * a << (NMSEDEC_FRACBITS - 2)))
                                     * (1 << (13 - NMSEDEC_FRACBITS)), 0);
-        lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << (NMSEDEC_FRACBITS + 1)) + (1 << (NMSEDEC_FRACBITS - 1)) + (1 << 2 * NMSEDEC_FRACBITS)) & mask)
-                                    << 1, 0);
+        lut_nmsedec_ref0[i] = ((i * i - (i << (NMSEDEC_FRACBITS + 1)) + (1 << (NMSEDEC_FRACBITS - 1)) + (1 << 2 * NMSEDEC_FRACBITS)) & mask) << 1;
     }
 }