From 8c6f90f1aa9e2580bdcc1613fbb59a7ecd2c67df Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Thu, 19 Jul 2001 17:07:13 +0000 Subject: [PATCH] switch to better method for getting floor(log2(double)) --- src/libFLAC/lpc.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/libFLAC/lpc.c b/src/libFLAC/lpc.c index 28e82bc..b90eae5 100644 --- a/src/libFLAC/lpc.c +++ b/src/libFLAC/lpc.c @@ -145,10 +145,11 @@ redo_it: return 2; } else { - const int log2cmax = (int)floor(log(cmax) / M_LN2); /* this is a good estimate but may not be precise enough, so we have to check for corner cases later when shifting */ - const int maxshift = (int)precision - log2cmax - 1; + int log2cmax; - *shift = maxshift; + (void)frexp(cmax, &log2cmax); + log2cmax--; + *shift = (int)precision - log2cmax - 1; if(*shift < min_shiftlimit || *shift > max_shiftlimit) { return 1; @@ -159,7 +160,7 @@ redo_it: for(i = 0; i < order; i++) { qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] * (double)(1 << *shift)); - /* check for corner cases mentioned in the comment for log2cmax above */ + /* double-check the result */ if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) { #ifdef FLAC__OVERFLOW_DETECT fprintf(stderr,"FLAC__lpc_quantize_coefficients: compensating for overflow, qlp_coeff[%u]=%d, lp_coeff[%u]=%f, cmax=%f, precision=%u, shift=%d, q=%f, f(q)=%f\n", i, qlp_coeff[i], i, lp_coeff[i], cmax, precision, *shift, (double)lp_coeff[i] * (double)(1 << *shift), floor((double)lp_coeff[i] * (double)(1 << *shift))); @@ -177,7 +178,7 @@ redo_it: for(i = 0; i < order; i++) { qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] / (double)(1 << nshift)); - /* check for corner cases mentioned in the comment for log2cmax above */ + /* double-check the result */ if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) { #ifdef FLAC__OVERFLOW_DETECT fprintf(stderr,"FLAC__lpc_quantize_coefficients: compensating for overflow, qlp_coeff[%u]=%d, lp_coeff[%u]=%f, cmax=%f, precision=%u, shift=%d, q=%f, f(q)=%f\n", i, qlp_coeff[i], i, lp_coeff[i], cmax, precision, *shift, (double)lp_coeff[i] / (double)(1 << nshift), floor((double)lp_coeff[i] / (double)(1 << nshift))); -- 2.7.4