src/libFLAC/lpc.c : Include <inttypes.h> so compiling debug version works.
[platform/upstream/flac.git] / src / libFLAC / lpc.c
index 37dcdf8..f5eaf22 100644 (file)
@@ -1,5 +1,5 @@
 /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #endif
 
 #include <math.h>
+#include <inttypes.h>
 #include "FLAC/assert.h"
 #include "FLAC/format.h"
 #include "private/bitmath.h"
 #include "private/lpc.h"
+#include "private/macros.h"
 #if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
 #include <stdio.h>
 #endif
 
+/* OPT: #undef'ing this may improve the speed on some architectures */
+#define FLAC__LPC_UNROLLED_FILTER_LOOPS
+
 #ifndef FLAC__INTEGER_ONLY_LIBRARY
 
 #ifndef M_LN2
 #define M_LN2 0.69314718055994530942
 #endif
 
+#if !defined(HAVE_LROUND)
+#if defined(_MSC_VER)
+#include <float.h>
+#define copysign _copysign
+#elif defined(__GNUC__)
+#define copysign __builtin_copysign
+#endif
+static inline long int lround(double x) {
+    return (long)(x + copysign (0.5, x));
+}
+//If this fails, we are in the precence of a mid 90's compiler..move along...
+#endif
+
 void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len)
 {
        unsigned i;
@@ -108,7 +126,7 @@ void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_le
 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
 {
        unsigned i, j;
-       FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
+       FLAC__double r, err, lpc[FLAC__MAX_LPC_ORDER];
 
        FLAC__ASSERT(0 != max_order);
        FLAC__ASSERT(0 < *max_order);
@@ -122,7 +140,6 @@ void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_o
                r = -autoc[i+1];
                for(j = 0; j < i; j++)
                        r -= lpc[j] * autoc[i-j];
-               ref[i] = (r/=err);
 
                /* Update LPC coefficients and total error. */
                lpc[i]=r;
@@ -196,14 +213,8 @@ int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order,
                FLAC__int32 q;
                for(i = 0; i < order; i++) {
                        error += lp_coeff[i] * (1 << *shift);
-#if 1 /* unfortunately lround() is C99 */
-                       if(error >= 0.0)
-                               q = (FLAC__int32)(error + 0.5);
-                       else
-                               q = (FLAC__int32)(error - 0.5);
-#else
                        q = lround(error);
-#endif
+
 #ifdef FLAC__OVERFLOW_DETECT
                        if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
                                fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
@@ -231,14 +242,7 @@ int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order,
 #endif
                for(i = 0; i < order; i++) {
                        error += lp_coeff[i] / (1 << nshift);
-#if 1 /* unfortunately lround() is C99 */
-                       if(error >= 0.0)
-                               q = (FLAC__int32)(error + 0.5);
-                       else
-                               q = (FLAC__int32)(error - 0.5);
-#else
                        q = lround(error);
-#endif
 #ifdef FLAC__OVERFLOW_DETECT
                        if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
                                fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
@@ -259,7 +263,7 @@ int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order,
 }
 
 void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
-#ifdef FLAC__OVERFLOW_DETECT /* this ugly flavor is only for debugging */
+#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
 {
        FLAC__int64 sumo;
        unsigned i, j;
@@ -281,13 +285,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                for(j = 0; j < order; j++) {
                        sum += qlp_coeff[j] * (*(--history));
                        sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
-#if defined _MSC_VER
-                       if(sumo > 2147483647I64 || sumo < -2147483648I64)
-                               fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
-#else
-                       if(sumo > 2147483647ll || sumo < -2147483648ll)
-                               fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
-#endif
+                               fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo);
                }
                *(residual++) = *(data++) - (sum >> lp_quantization);
        }
@@ -303,7 +301,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
 }
 #else /* fully unrolled version for normal use */
 {
-       unsigned i;
+       int i;
        FLAC__int32 sum;
 
        FLAC__ASSERT(order > 0);
@@ -318,7 +316,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                if(order > 8) {
                        if(order > 10) {
                                if(order == 12) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[11] * data[i-12];
                                                sum += qlp_coeff[10] * data[i-11];
@@ -336,7 +334,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                                        }
                                }
                                else { /* order == 11 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[10] * data[i-11];
                                                sum += qlp_coeff[9] * data[i-10];
@@ -355,7 +353,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                        }
                        else {
                                if(order == 10) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[9] * data[i-10];
                                                sum += qlp_coeff[8] * data[i-9];
@@ -371,7 +369,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                                        }
                                }
                                else { /* order == 9 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[8] * data[i-9];
                                                sum += qlp_coeff[7] * data[i-8];
@@ -390,7 +388,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                else if(order > 4) {
                        if(order > 6) {
                                if(order == 8) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[7] * data[i-8];
                                                sum += qlp_coeff[6] * data[i-7];
@@ -404,7 +402,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                                        }
                                }
                                else { /* order == 7 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[6] * data[i-7];
                                                sum += qlp_coeff[5] * data[i-6];
@@ -419,7 +417,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                        }
                        else {
                                if(order == 6) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[5] * data[i-6];
                                                sum += qlp_coeff[4] * data[i-5];
@@ -431,7 +429,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                                        }
                                }
                                else { /* order == 5 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[4] * data[i-5];
                                                sum += qlp_coeff[3] * data[i-4];
@@ -446,7 +444,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                else {
                        if(order > 2) {
                                if(order == 4) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[3] * data[i-4];
                                                sum += qlp_coeff[2] * data[i-3];
@@ -456,7 +454,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                                        }
                                }
                                else { /* order == 3 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[2] * data[i-3];
                                                sum += qlp_coeff[1] * data[i-2];
@@ -467,7 +465,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                        }
                        else {
                                if(order == 2) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[1] * data[i-2];
                                                sum += qlp_coeff[0] * data[i-1];
@@ -475,14 +473,14 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
                                        }
                                }
                                else { /* order == 1 */
-                                       for(i = 0; i < data_len; i++)
+                                       for(i = 0; i < (int)data_len; i++)
                                                residual[i] = data[i] - ((qlp_coeff[0] * data[i-1]) >> lp_quantization);
                                }
                        }
                }
        }
        else { /* order > 12 */
-               for(i = 0; i < data_len; i++) {
+               for(i = 0; i < (int)data_len; i++) {
                        sum = 0;
                        switch(order) {
                                case 32: sum += qlp_coeff[31] * data[i-32];
@@ -525,7 +523,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, u
 #endif
 
 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
-#ifdef FLAC__OVERFLOW_DETECT /* this ugly flavor is only for debugging */
+#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
 {
        unsigned i, j;
        FLAC__int64 sum;
@@ -545,19 +543,11 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                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 defined _MSC_VER
-                       fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization);
-#else
-                       fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
-#endif
+                       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 defined _MSC_VER
-                       fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%I64d, residual=%I64d\n", i, *data, sum >> lp_quantization, (FLAC__int64)(*data) - (sum >> lp_quantization));
-#else
-                       fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*data) - (sum >> lp_quantization)));
-#endif
+                       fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%" PRId64 ", residual=%" PRId64 "\n", i, *data, (long long)(sum >> lp_quantization), ((FLAC__int64)(*data) - (sum >> lp_quantization)));
                        break;
                }
                *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
@@ -565,7 +555,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
 }
 #else /* fully unrolled version for normal use */
 {
-       unsigned i;
+       int i;
        FLAC__int64 sum;
 
        FLAC__ASSERT(order > 0);
@@ -580,7 +570,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                if(order > 8) {
                        if(order > 10) {
                                if(order == 12) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
                                                sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
@@ -598,7 +588,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                                        }
                                }
                                else { /* order == 11 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
                                                sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
@@ -617,7 +607,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                        }
                        else {
                                if(order == 10) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
                                                sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
@@ -633,7 +623,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                                        }
                                }
                                else { /* order == 9 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
                                                sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
@@ -652,7 +642,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                else if(order > 4) {
                        if(order > 6) {
                                if(order == 8) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
                                                sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
@@ -666,7 +656,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                                        }
                                }
                                else { /* order == 7 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
                                                sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
@@ -681,7 +671,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                        }
                        else {
                                if(order == 6) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
                                                sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
@@ -693,7 +683,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                                        }
                                }
                                else { /* order == 5 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
                                                sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
@@ -708,7 +698,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                else {
                        if(order > 2) {
                                if(order == 4) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
                                                sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
@@ -718,7 +708,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                                        }
                                }
                                else { /* order == 3 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
                                                sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
@@ -729,7 +719,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                        }
                        else {
                                if(order == 2) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
                                                sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
@@ -737,14 +727,14 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
                                        }
                                }
                                else { /* order == 1 */
-                                       for(i = 0; i < data_len; i++)
+                                       for(i = 0; i < (int)data_len; i++)
                                                residual[i] = data[i] - (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization);
                                }
                        }
                }
        }
        else { /* order > 12 */
-               for(i = 0; i < data_len; i++) {
+               for(i = 0; i < (int)data_len; i++) {
                        sum = 0;
                        switch(order) {
                                case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32];
@@ -789,7 +779,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *da
 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
 
 void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
-#ifdef FLAC__OVERFLOW_DETECT /* this ugly flavor is only for debugging */
+#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
 {
        FLAC__int64 sumo;
        unsigned i, j;
@@ -811,13 +801,8 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                for(j = 0; j < order; j++) {
                        sum += qlp_coeff[j] * (*(--history));
                        sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
-#if defined _MSC_VER
-                       if(sumo > 2147483647I64 || sumo < -2147483648I64)
-                               fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
-#else
                        if(sumo > 2147483647ll || sumo < -2147483648ll)
-                               fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
-#endif
+                               fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo);
                }
                *(data++) = *(r++) + (sum >> lp_quantization);
        }
@@ -833,7 +818,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
 }
 #else /* fully unrolled version for normal use */
 {
-       unsigned i;
+       int i;
        FLAC__int32 sum;
 
        FLAC__ASSERT(order > 0);
@@ -848,7 +833,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                if(order > 8) {
                        if(order > 10) {
                                if(order == 12) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[11] * data[i-12];
                                                sum += qlp_coeff[10] * data[i-11];
@@ -866,7 +851,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                                        }
                                }
                                else { /* order == 11 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[10] * data[i-11];
                                                sum += qlp_coeff[9] * data[i-10];
@@ -885,7 +870,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                        }
                        else {
                                if(order == 10) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[9] * data[i-10];
                                                sum += qlp_coeff[8] * data[i-9];
@@ -901,7 +886,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                                        }
                                }
                                else { /* order == 9 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[8] * data[i-9];
                                                sum += qlp_coeff[7] * data[i-8];
@@ -920,7 +905,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                else if(order > 4) {
                        if(order > 6) {
                                if(order == 8) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[7] * data[i-8];
                                                sum += qlp_coeff[6] * data[i-7];
@@ -934,7 +919,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                                        }
                                }
                                else { /* order == 7 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[6] * data[i-7];
                                                sum += qlp_coeff[5] * data[i-6];
@@ -949,7 +934,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                        }
                        else {
                                if(order == 6) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[5] * data[i-6];
                                                sum += qlp_coeff[4] * data[i-5];
@@ -961,7 +946,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                                        }
                                }
                                else { /* order == 5 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[4] * data[i-5];
                                                sum += qlp_coeff[3] * data[i-4];
@@ -976,7 +961,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                else {
                        if(order > 2) {
                                if(order == 4) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[3] * data[i-4];
                                                sum += qlp_coeff[2] * data[i-3];
@@ -986,7 +971,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                                        }
                                }
                                else { /* order == 3 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[2] * data[i-3];
                                                sum += qlp_coeff[1] * data[i-2];
@@ -997,7 +982,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                        }
                        else {
                                if(order == 2) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[1] * data[i-2];
                                                sum += qlp_coeff[0] * data[i-1];
@@ -1005,14 +990,14 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
                                        }
                                }
                                else { /* order == 1 */
-                                       for(i = 0; i < data_len; i++)
+                                       for(i = 0; i < (int)data_len; i++)
                                                data[i] = residual[i] + ((qlp_coeff[0] * data[i-1]) >> lp_quantization);
                                }
                        }
                }
        }
        else { /* order > 12 */
-               for(i = 0; i < data_len; i++) {
+               for(i = 0; i < (int)data_len; i++) {
                        sum = 0;
                        switch(order) {
                                case 32: sum += qlp_coeff[31] * data[i-32];
@@ -1055,7 +1040,7 @@ void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, c
 #endif
 
 void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
-#ifdef FLAC__OVERFLOW_DETECT /* this ugly flavor is only for debugging */
+#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
 {
        unsigned i, j;
        FLAC__int64 sum;
@@ -1075,19 +1060,11 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                for(j = 0; j < order; j++)
                        sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
                if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
-#ifdef _MSC_VER
-                       fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization);
-#else
-                       fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
-#endif
+                       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) {
-#ifdef _MSC_VER
-                       fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%I64d, data=%I64d\n", i, *r, sum >> lp_quantization, (FLAC__int64)(*r) + (sum >> lp_quantization));
-#else
-                       fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *r, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*r) + (sum >> lp_quantization)));
-#endif
+                       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;
                }
                *(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization);
@@ -1095,7 +1072,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
 }
 #else /* fully unrolled version for normal use */
 {
-       unsigned i;
+       int i;
        FLAC__int64 sum;
 
        FLAC__ASSERT(order > 0);
@@ -1110,7 +1087,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                if(order > 8) {
                        if(order > 10) {
                                if(order == 12) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
                                                sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
@@ -1128,7 +1105,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                                        }
                                }
                                else { /* order == 11 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
                                                sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
@@ -1147,7 +1124,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                        }
                        else {
                                if(order == 10) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
                                                sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
@@ -1163,7 +1140,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                                        }
                                }
                                else { /* order == 9 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
                                                sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
@@ -1182,7 +1159,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                else if(order > 4) {
                        if(order > 6) {
                                if(order == 8) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
                                                sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
@@ -1196,7 +1173,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                                        }
                                }
                                else { /* order == 7 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
                                                sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
@@ -1211,7 +1188,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                        }
                        else {
                                if(order == 6) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
                                                sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
@@ -1223,7 +1200,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                                        }
                                }
                                else { /* order == 5 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
                                                sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
@@ -1238,7 +1215,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                else {
                        if(order > 2) {
                                if(order == 4) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
                                                sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
@@ -1248,7 +1225,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                                        }
                                }
                                else { /* order == 3 */
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
                                                sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
@@ -1259,7 +1236,7 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                        }
                        else {
                                if(order == 2) {
-                                       for(i = 0; i < data_len; i++) {
+                                       for(i = 0; i < (int)data_len; i++) {
                                                sum = 0;
                                                sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
                                                sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
@@ -1267,14 +1244,14 @@ void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_l
                                        }
                                }
                                else { /* order == 1 */
-                                       for(i = 0; i < data_len; i++)
+                                       for(i = 0; i < (int)data_len; i++)
                                                data[i] = residual[i] + (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization);
                                }
                        }
                }
        }
        else { /* order > 12 */
-               for(i = 0; i < data_len; i++) {
+               for(i = 0; i < (int)data_len; i++) {
                        sum = 0;
                        switch(order) {
                                case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32];