Cleanup FLAC__bitmath_silog2()
authorThomas Zander <thomas.e.zander@googlemail.com>
Sun, 3 Jan 2016 18:13:52 +0000 (19:13 +0100)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Mon, 4 Jan 2016 02:22:34 +0000 (13:22 +1100)
- Retire 32bit variant of silog2(), since only the _wide variant is used
- Rename FLAC__bitmath_silog2_wide() to FLAC__bitmath_silog2()
- Replace existing implementation by shorter, clearer implementation
  using optimised routines from bitmath.h
- Update Copyright string to 2016 in changed files

Signed-off-by: Erik de Castro Lopo <erikd@mega-nerd.com>
Closes: https://github.com/xiph/flac/pull/6

src/libFLAC/bitmath.c
src/libFLAC/include/private/bitmath.h
src/libFLAC/lpc.c

index 5b58ca9..b3d797d 100644 (file)
@@ -1,6 +1,6 @@
 /* libFLAC - Free Lossless Audio Codec library
  * Copyright (C) 2001-2009  Josh Coalson
- * Copyright (C) 2011-2014  Xiph.Org Foundation
+ * Copyright (C) 2011-2016  Xiph.Org Foundation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * silog2(  9) = 5
  * silog2( 10) = 5
  */
-unsigned FLAC__bitmath_silog2(int v)
+unsigned FLAC__bitmath_silog2(FLAC__int64 v)
 {
-       while(1) {
-               if(v == 0) {
-                       return 0;
-               }
-               else if(v > 0) {
-                       unsigned l = 0;
-                       while(v) {
-                               l++;
-                               v >>= 1;
-                       }
-                       return l+1;
-               }
-               else if(v == -1) {
-                       return 2;
-               }
-               else {
-                       v++;
-                       v = -v;
-               }
-       }
-}
+       if(v == 0)
+               return 0;
 
-unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v)
-{
-       while(1) {
-               if(v == 0) {
-                       return 0;
-               }
-               else if(v > 0) {
-                       unsigned l = 0;
-                       while(v) {
-                               l++;
-                               v >>= 1;
-                       }
-                       return l+1;
-               }
-               else if(v == -1) {
-                       return 2;
-               }
-               else {
-                       v++;
-                       v = -v;
-               }
-       }
+       if(v == -1)
+               return 2;
+
+       v = (v < 0) ? (-(v+1)) : v;
+       return FLAC__bitmath_ilog2_wide(v)+2;
 }
index bdd154a..9c75f85 100644 (file)
@@ -1,6 +1,6 @@
 /* libFLAC - Free Lossless Audio Codec library
  * Copyright (C) 2001-2009  Josh Coalson
- * Copyright (C) 2011-2014  Xiph.Org Foundation
+ * Copyright (C) 2011-2016  Xiph.Org Foundation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -169,9 +169,6 @@ static inline unsigned FLAC__bitmath_ilog2(FLAC__uint32 v)
 #endif
 }
 
-
-#ifdef FLAC__INTEGER_ONLY_LIBRARY /* Unused otherwise */
-
 static inline unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v)
 {
        FLAC__ASSERT(v > 0);
@@ -207,9 +204,7 @@ static inline unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v)
        }
 #endif
 }
-#endif
 
-unsigned FLAC__bitmath_silog2(int v);
-unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v);
+unsigned FLAC__bitmath_silog2(FLAC__int64 v);
 
 #endif
index e5900ef..dc946d4 100644 (file)
@@ -1,6 +1,6 @@
 /* libFLAC - Free Lossless Audio Codec library
  * Copyright (C) 2000-2009  Josh Coalson
- * Copyright (C) 2011-2014  Xiph.Org Foundation
+ * Copyright (C) 2011-2016  Xiph.Org Foundation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -545,11 +545,11 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 * f
                history = data;
                for(j = 0; j < order; j++)
                        sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
-               if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
+               if(FLAC__bitmath_silog2(sum >> lp_quantization) > 32) {
                        fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%" PRId64 "\n", i, (sum >> lp_quantization));
                        break;
                }
-               if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
+               if(FLAC__bitmath_silog2((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
                        fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%" PRId64 ", residual=%" PRId64 "\n", i, *data, (int64_t)(sum >> lp_quantization), ((FLAC__int64)(*data) - (sum >> lp_quantization)));
                        break;
                }
@@ -1062,11 +1062,11 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 * flac_restrict residual, u
                history = data;
                for(j = 0; j < order; j++)
                        sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
-               if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
+               if(FLAC__bitmath_silog2(sum >> lp_quantization) > 32) {
                        fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%" PRId64 "\n", i, (sum >> lp_quantization));
                        break;
                }
-               if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
+               if(FLAC__bitmath_silog2((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
                        fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization)));
                        break;
                }