From 93c3a3897e287760092553fdd4ec6674a5537e8c Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Sun, 3 Jan 2016 19:13:52 +0100 Subject: [PATCH] Cleanup FLAC__bitmath_silog2() - 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 Closes: https://github.com/xiph/flac/pull/6 --- src/libFLAC/bitmath.c | 54 +++++---------------------- src/libFLAC/include/private/bitmath.h | 9 +---- src/libFLAC/lpc.c | 10 ++--- 3 files changed, 16 insertions(+), 57 deletions(-) diff --git a/src/libFLAC/bitmath.c b/src/libFLAC/bitmath.c index 5b58ca99..b3d797d3 100644 --- a/src/libFLAC/bitmath.c +++ b/src/libFLAC/bitmath.c @@ -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 @@ -60,50 +60,14 @@ * 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; } diff --git a/src/libFLAC/include/private/bitmath.h b/src/libFLAC/include/private/bitmath.h index bdd154a9..9c75f85b 100644 --- a/src/libFLAC/include/private/bitmath.h +++ b/src/libFLAC/include/private/bitmath.h @@ -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 diff --git a/src/libFLAC/lpc.c b/src/libFLAC/lpc.c index e5900ef0..dc946d47 100644 --- a/src/libFLAC/lpc.c +++ b/src/libFLAC/lpc.c @@ -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; }