linphone-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Linphone-users] possible coding defect - left-shift of negative numbers


From: Robert Crovella
Subject: [Linphone-users] possible coding defect - left-shift of negative numbers
Date: Sun, 4 Sep 2016 02:02:28 +0000 (UTC)

I believe left-shift of negative numbers is UB.   I believe C99 says this:

C99 Section 6.5.7 Paragraph 4
The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 * 2**E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 * 2**E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

Therefore I think it could be considered a coding defect to perform such operations (in C or C++).  

One such example is in computeLP.c

If you add the following line:

                for (j=1; j<i; j++) {
                        sum = MAC32_32_Q31(sum, LPCoefficients[j], autoCorrelationCoefficient[i-j]);/* LPCoefficients in Q27, autoCorrelation in Q31 -> result in Q27 -> sum in Q27 */
                }
                sum = ADD32(SHL(sum, 4), autoCorrelationCoefficient[i]); /* set sum in Q31 and add r[0] */

                /* a[i] = -sum/E                                                           */
                LPCoefficients[i] = -DIV32_32_Q31(sum,E); /* LPCoefficient of current iteration is in Q31 for now, it will be set to Q27 at the end of this iteration */

                printf("sum: %d, E: %d, DIV: %d\n ", sum, E, LPCoefficients[i]); /** ADD THIS LINE  **/


                /* iterations j = 1..i-1                                                   */

And then run the encoder test using the supplied sample data:

./encoderTest data/test.encoder.in

You will see printout indicating that some values of sum are negative.  (sum is a word32_t; word32_t and word64_T are defined to be signed numbers in typedef.h)

The DIV32_32_Q31 macro is defined as:


fixedPointMacros.h:  #define DIV32_32_Q31(a,b) (((word64_t)(a)<<31)/((word32_t)(b)))

Therefore it seems evident to me that in some cases, left-shift of negative numbers is being requested.

Did I miss something?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]