Index: mppenc/trunk/src/quant.c
===================================================================
--- mppenc/trunk/src/quant.c	(revision 97)
+++ mppenc/trunk/src/quant.c	(revision 109)
@@ -19,4 +19,5 @@
 
 #include "mppenc.h"
+#include "fastmath.h"
 
 /* V A R I A B L E S */
@@ -149,27 +150,21 @@
 ISNR_Schaetzer ( const float* input, const float SNRcomp, const int res )
 {
-    int    k;
-    float  fac    = A [res];
-    float  invfac = C [res];
-    float  Signal = 1.e-30f;
-    float  Fehler = 1.e-30f;
-    float  tmp ;
-    float  tmp2;
-    float  tmp3;
+    int k; float signal, error, sig, err;
+    const float fac       = A [res];
+    const float invfac    = C [res];
+    const float noiseComp = NoiseInjectionCompensation1D [res];
 
     // Summation of the absolute power and the quadratic error
-    for ( k = 0; k < 36; k++ ) {
-        tmp2    = input[k] * NoiseInjectionCompensation1D [res];
-        // q = ftol(in), correct rounding
-        tmp  = tmp2 * fac + 0xFF8000;
-        tmp3 = (*(int*) & tmp - 0x4B7F8000) * invfac;
-        tmp  = tmp3 - tmp2;
-
-        Fehler += tmp * tmp;
-        Signal += tmp2 * tmp2;
+    signal = error = 1.e-30f;
+    for ( k = 0; k < 36; k++ )
+    {
+        sig     = input[k] * noiseComp;
+        err     = mpc_nearbyintf(sig * fac) * invfac - sig;
+        signal += sig * sig;
+        error  += err * err;
     }
 
     // Utilization of SNRcomp only if SNR > 1 !!!
-    return Signal > Fehler  ?  Fehler / (SNRcomp * Signal)  :  Fehler / Signal;
+    return signal > error ? error / (signal * SNRcomp) : error / signal;
 }
 
@@ -178,60 +173,43 @@
 ISNR_Schaetzer_Trans ( const float* input, const float SNRcomp, const int res )
 {
-    int    k;
-    float  fac    = A [res];
-    float  invfac = C [res];
-    float  Signal;
-    float  Fehler;
-    float  ret ;
-    float  tmp ;
-    float  tmp2;
-    float  tmp3;
-
+    int k; float  signal, error, result, sig, err;
+    const float fac       = A [res];
+    const float invfac    = C [res];
+    const float noiseComp = NoiseInjectionCompensation1D [res];
+    
     // Summation of the absolute power and the quadratic error
-    k = 0;
-    Signal = Fehler = 1.e-30f;
-    for ( ; k < 12; k++ ) {
-        tmp2    = input[k] * NoiseInjectionCompensation1D [res];
-        // q = ftol(in), correct rounding
-        tmp  = tmp2 * fac + 0xFF8000;
-        tmp3 = (*(int*) & tmp - 0x4B7F8000) * invfac;
-        tmp  = tmp3 - tmp2;
-
-        Fehler += tmp * tmp;
-        Signal += tmp2 * tmp2;
-    }
-    tmp = Signal > Fehler  ?  Fehler / (SNRcomp * Signal)  :  Fehler / Signal;
-    ret = tmp;
-    Signal = Fehler = 1.e-30f;
-    for ( ; k < 24; k++ ) {
-        tmp2    = input[k] * NoiseInjectionCompensation1D [res];
-        // q = ftol(in), correct rounding
-        tmp  = tmp2 * fac + 0xFF8000;
-        tmp3 = (*(int*) & tmp - 0x4B7F8000) * invfac;
-        tmp  = tmp3 - tmp2;
-
-        Fehler += tmp * tmp;
-        Signal += tmp2 * tmp2;
-    }
-    tmp = Signal > Fehler  ?  Fehler / (SNRcomp * Signal)  :  Fehler / Signal;
-    if ( tmp > ret ) ret = tmp;
-    //ret += tmp;
-    Signal = Fehler = 1.e-30f;
-    for ( ; k < 36; k++ ) {
-        tmp2    = input[k] * NoiseInjectionCompensation1D [res];
-        // q = ftol(in), correct rounding
-        tmp  = tmp2 * fac + 0xFF8000;
-        tmp3 = (*(int*) & tmp - 0x4B7F8000) * invfac;
-        tmp  = tmp3 - tmp2;
-
-        Fehler += tmp * tmp;
-        Signal += tmp2 * tmp2;
-    }
-    tmp = Signal > Fehler  ?  Fehler / (SNRcomp * Signal)  :  Fehler / Signal;
-    if ( tmp > ret ) ret = tmp;
-    //ret += tmp;
-    //ret *= 0.33333333333f;
-
-    return ret;
+    signal = error = 1.e-30f;
+    for ( k = 0 ; k < 12; k++ )
+    {
+        sig     = input[k] * noiseComp;
+        err     = mpc_nearbyintf(sig * fac) * invfac - sig;
+        signal += sig * sig;
+        error  += err * err;
+    }
+    result = signal > error ? error / (signal * SNRcomp) : error / signal;
+
+    signal = error = 1.e-30f;
+    for ( ; k < 24; k++ )
+    {
+        sig     = input[k] * noiseComp;
+        err     = mpc_nearbyintf(sig * fac) * invfac - sig;
+        signal += sig * sig;
+        error  += err * err;
+    }
+    sig = signal > error ? error / (signal * SNRcomp) : error / signal;
+    if ( sig > result ) result = sig;
+
+    signal = error = 1.e-30f;
+    for ( ; k < 36; k++ )
+    {
+        sig     = input[k] * noiseComp;
+        err     = mpc_nearbyintf(sig * fac) * invfac - sig;
+        signal += sig * sig;
+        error  += err * err;
+    }
+    sig = signal > error ? error / (signal * SNRcomp) : error / signal;
+    if ( sig > result ) result = sig;
+
+    return result;
 }
 
@@ -241,40 +219,37 @@
 QuantizeSubband ( unsigned int* qu_output, const float* input, const int res, float* errors )
 {
-    int    n;
-    int    offset  = D [res];
-    float  mult    = A [res] * NoiseInjectionCompensation1D [res];
-    float  invmult = C [res];
-    float  tmp;
-    int    quant;
-    float  signal;
-
-    for ( n = 0; n < 36 - MAX_NS_ORDER; n++, input++, qu_output++ ) {
-        // q = ftol(in), correct rounding
-        tmp   = *input * mult + 0xFF8000;
-        quant = (unsigned int)(*(int*) & tmp - 0x4B7F8000 + offset);
-
+    int n, quant; float signal;
+    const int   offset    = D [res];
+    const float noiseComp = NoiseInjectionCompensation1D [res];
+    const float mult      = A [res] * noiseComp;
+    const float invmult   = C [res];
+
+    for ( n = 0; n < 36 - MAX_NS_ORDER; n++)
+    {
+        quant = (unsigned int) ( mpc_lrintf( input[n] * mult ) + offset );
         // limitation to 0...2D
-        if ((unsigned int)quant > (unsigned int)2*offset ) {
-            quant = mini ( quant, 2*offset );
-            quant = maxi ( quant,        0 );
+        if ( quant > offset * 2 )
+        {
+            quant = mini ( quant, offset * 2 );
+            quant = maxi ( quant, 0 );
         }
-        *qu_output  = quant;
-    }
-
-    for ( ; n < 36; n++, input++, qu_output++ ) {
-        // q = ftol(in), correct rounding
-        signal = *input * mult;
-        tmp   =  signal + 0xFF8000;
-        quant = (unsigned int)(*(int*) & tmp - 0x4B7F8000 + offset);
+        qu_output[n] = quant;
+    }
+
+    for ( ; n < 36; n++ )
+    {
+        signal = input[n] * mult;
+        quant  = (unsigned int) ( mpc_lrintf( signal ) + offset );
 
         // calculate the current error and save it for error refeeding
-        errors [n + 6] = invmult * (quant - offset) - signal * NoiseInjectionCompensation1D [res];
+        errors [n + 6] = invmult * (quant - offset) - signal * noiseComp;
 
         // limitation to 0...2D
-        if ((unsigned int)quant > (unsigned int)2*offset ) {
-            quant = mini ( quant, 2*offset );
-            quant = maxi ( quant,        0 );
+        if ( quant > offset * 2 )
+        {
+            quant = mini ( quant, offset * 2);
+            quant = maxi ( quant, 0 );
         }
-        *qu_output  = quant;
+        qu_output[n] = quant;
     }
 }
@@ -285,25 +260,21 @@
 QuantizeSubbandWithNoiseShaping ( unsigned int* qu_output, const float* input, const int res, float* errors, const float* FIR )
 {
-#define E(x) *((int*)errors+(x))
-
-    float  signal;
-    float  tmp;
-    float  mult    = A [res];
-    float  invmult = C [res];
-    int    offset  = D [res];
-    int    n;
-    int    quant;
-
-    E(0) = E(1) = E(2) = E(3) = E(4) = E(5) = 0;       // arghh, it produces pops on each frame boundary!
-
-    for ( n = 0; n < 36; n++, input++, qu_output++ ) {
-        signal = *input * NoiseInjectionCompensation1D [res] - (FIR[5]*errors[n+0] + FIR[4]*errors[n+1] + FIR[3]*errors[n+2] + FIR[2]*errors[n+3] + FIR[1]*errors[n+4] + FIR[0]*errors[n+5]);
-
-        // quant = ftol(signal), correct rounding
-        tmp   = signal * mult + 0xFF8000;
-        quant = *(int*) & tmp - 0x4B7F8000;
+    int n, quant; float signal;
+    const float mult      = A [res];
+    const float invmult   = C [res];
+    const int   offset    = D [res];
+    const float noiseComp = NoiseInjectionCompensation1D [res];
+
+    memset(errors, 0, 6 * sizeof *errors); // arghh, it produces pops on each frame boundary!
+
+    for ( n = 0; n < 36; n++ )
+    {
+        signal = input[n] * noiseComp -
+            (FIR[5]*errors[n+0] + FIR[4]*errors[n+1] + FIR[3]*errors[n+2] +
+             FIR[2]*errors[n+3] + FIR[1]*errors[n+4] + FIR[0]*errors[n+5]);
+        quant = mpc_lrintf(signal * mult);
 
         // calculate the current error and save it for error refeeding
-        errors [n + 6] = invmult * quant - signal * NoiseInjectionCompensation1D [res];
+        errors [n + 6] = invmult * quant - signal * noiseComp;
 
         // limitation to +/-D
@@ -311,5 +282,5 @@
         quant = maxf ( quant, -offset );
 
-        *qu_output = (unsigned int)(quant + offset);
+        qu_output[n] = (unsigned int) ( quant + offset );
     }
 }
