Skip to content

Commit

Permalink
src/ALAC : Fix all undefined behaviour warnings.
Browse files Browse the repository at this point in the history
Found using GCC's undefined behaviour sanitizer.
  • Loading branch information
erikd committed Dec 2, 2014
1 parent a5e5b48 commit 9bbba9e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 33 deletions.
11 changes: 6 additions & 5 deletions src/ALAC/ag_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
Copyright: (c) 2001-2011 Apple, Inc.
*/

#include "aglib.h"
#include "ALACBitUtilities.h"
#include "ALACAudioTypes.h"

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "aglib.h"
#include "ALACBitUtilities.h"
#include "ALACAudioTypes.h"
#include "shift.h"

#define CODE_TO_LONG_MAXBITS 32
#define N_MAX_MEAN_CLAMP 0xffff
#define N_MEAN_CLAMP_VAL 0xffff
Expand Down Expand Up @@ -306,7 +307,7 @@ int32_t dyn_decomp (AGParamRecPtr params, BitBuffer * bitstream, int32_t * pc, i
// least significant bit is sign bit
{
uint32_t ndecode = n + zmode ;
int32_t multiplier = - (ndecode & 1) ;
int32_t multiplier = - (int) (ndecode & 1) ;

multiplier |= 1 ;
del = ((ndecode+1) >> 1) * (multiplier) ;
Expand Down
5 changes: 3 additions & 2 deletions src/ALAC/alac_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "dplib.h"
#include "aglib.h"
#include "matrixlib.h"
#include "shift.h"

#include "ALACBitUtilities.h"
#include "EndianPortable.h"
Expand Down Expand Up @@ -277,7 +278,7 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u
for (i = 0 ; i < numSamples ; i++)
{
val = (int32_t) BitBufferRead (bits, 16) ;
val = (val << 16) >> shift ;
val = arith_shift_left (val, 16) >> shift ;
p->mMixBufferU [i] = val | BitBufferRead (bits, (uint8_t) extraBits) ;
}
}
Expand All @@ -303,7 +304,7 @@ alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, u
case 16:
out32 = sampleBuffer + channelIndex ;
for (i = 0, j = 0 ; i < numSamples ; i++, j += numChannels)
out32 [j] = p->mMixBufferU [i] << 16 ;
out32 [j] = arith_shift_left (p->mMixBufferU [i], 16) ;
break ;
case 20:
out32 = sampleBuffer + channelIndex ;
Expand Down
10 changes: 6 additions & 4 deletions src/ALAC/dp_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
*/


#include "dplib.h"
#include <string.h>

#include "dplib.h"
#include "shift.h"

#if __GNUC__
#define ALWAYS_INLINE __attribute__ ((always_inline))
#else
Expand Down Expand Up @@ -91,7 +93,7 @@ void unpc_block (int32_t * pc1, int32_t * out, int32_t num, int16_t * coefs, int
for (j = 1 ; j <= numactive ; j++)
{
del = pc1 [j] + out [j-1] ;
out [j] = (del << chanshift) >> chanshift ;
out [j] = arith_shift_left (del, chanshift) >> chanshift ;
}

lim = numactive + 1 ;
Expand Down Expand Up @@ -126,7 +128,7 @@ void unpc_block (int32_t * pc1, int32_t * out, int32_t num, int16_t * coefs, int
sg = sign_of_int (del) ;
del += top + sum1 ;

out [j] = (del << chanshift) >> chanshift ;
out [j] = arith_shift_left (del, chanshift) >> chanshift ;

if (sg > 0)
{
Expand Down Expand Up @@ -220,7 +222,7 @@ void unpc_block (int32_t * pc1, int32_t * out, int32_t num, int16_t * coefs, int
sg = sign_of_int (del) ;
del += top + sum1 ;

out [j] = (del << chanshift) >> chanshift ;
out [j] = arith_shift_left (del, chanshift) >> chanshift ;

if (sg > 0)
{
Expand Down
10 changes: 6 additions & 4 deletions src/ALAC/dp_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
Copyright: (c) 2001-2011 Apple, Inc.
*/

#include "dplib.h"
#include <string.h>

#include "dplib.h"
#include "shift.h"

#if __GNUC__
#define ALWAYS_INLINE __attribute__ ((always_inline))
#else
Expand Down Expand Up @@ -100,7 +102,7 @@ void pc_block (int32_t * in, int32_t * pc1, int32_t num, int16_t * coefs, int32_
for (j = 1 ; j <= numactive ; j++)
{
del = in [j] - in [j-1] ;
pc1 [j] = (del << chanshift) >> chanshift ;
pc1 [j] = arith_shift_left (del, chanshift) >> chanshift ;
}

lim = numactive + 1 ;
Expand Down Expand Up @@ -128,7 +130,7 @@ void pc_block (int32_t * in, int32_t * pc1, int32_t num, int16_t * coefs, int32_
sum1 = (denhalf - a0 * b0 - a1 * b1 - a2 * b2 - a3 * b3) >> denshift ;

del = in [j] - top - sum1 ;
del = (del << chanshift) >> chanshift ;
del = arith_shift_left (del, chanshift) >> chanshift ;
pc1 [j] = del ;
del0 = del ;

Expand Down Expand Up @@ -221,7 +223,7 @@ void pc_block (int32_t * in, int32_t * pc1, int32_t num, int16_t * coefs, int32_
- a4 * b4 - a5 * b5 - a6 * b6 - a7 * b7) >> denshift ;

del = in [j] - top - sum1 ;
del = (del << chanshift) >> chanshift ;
del = arith_shift_left (del, chanshift) >> chanshift ;
pc1 [j] = del ;
del0 = del ;

Expand Down
35 changes: 18 additions & 17 deletions src/ALAC/matrix_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "matrixlib.h"
#include "ALACAudioTypes.h"
#include "shift.h"

// up to 24-bit "offset" macros for the individual bytes of a 20/24-bit word
#if TARGET_RT_BIG_ENDIAN
Expand Down Expand Up @@ -72,8 +73,8 @@ void unmix16 (int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t
l = u [j] + v [j] - ((mixres * v [j]) >> mixbits) ;
r = l - v [j] ;

out [0] = l << 16 ;
out [1] = r << 16 ;
out [0] = arith_shift_left (l, 16) ;
out [1] = arith_shift_left (r, 16) ;
out += stride ;
}
}
Expand Down Expand Up @@ -106,8 +107,8 @@ void unmix20 (int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t
l = u [j] + v [j] - ((mixres * v [j]) >> mixbits) ;
r = l - v [j] ;

out [0] = l << 12 ;
out [1] = r << 12 ;
out [0] = arith_shift_left (l, 12) ;
out [1] = arith_shift_left (r, 12) ;
out += stride ;
}
}
Expand All @@ -116,8 +117,8 @@ void unmix20 (int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t
/* Conventional separated stereo. */
for (j = 0 ; j < numSamples ; j++)
{
out [0] = u [j] << 12 ;
out [1] = v [j] << 12 ;
out [0] = arith_shift_left (u [j], 12) ;
out [1] = arith_shift_left (v [j], 12) ;
out += stride ;
}
}
Expand All @@ -143,11 +144,11 @@ void unmix24 (int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t
l = u [j] + v [j] - ((mixres * v [j]) >> mixbits) ;
r = l - v [j] ;

l = (l << shift) | (uint32_t) shiftUV [k + 0] ;
r = (r << shift) | (uint32_t) shiftUV [k + 1] ;
l = arith_shift_left (l, shift) | (uint32_t) shiftUV [k + 0] ;
r = arith_shift_left (r, shift) | (uint32_t) shiftUV [k + 1] ;

out [0] = l << 8 ;
out [1] = r << 8 ;
out [0] = arith_shift_left (l, 8) ;
out [1] = arith_shift_left (r, 8) ;
out += stride ;
}
}
Expand Down Expand Up @@ -221,8 +222,8 @@ void unmix32 (int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t
l = lt + rt - ((mixres * rt) >> mixbits) ;
r = l - rt ;

out [0] = (l << shift) | (uint32_t) shiftUV [k + 0] ;
out [1] = (r << shift) | (uint32_t) shiftUV [k + 1] ;
out [0] = arith_shift_left (l, shift) | (uint32_t) shiftUV [k + 0] ;
out [1] = arith_shift_left (r, shift) | (uint32_t) shiftUV [k + 1] ;
out += stride ;
}
}
Expand Down Expand Up @@ -275,8 +276,8 @@ void copyPredictorTo24Shift (int32_t * in, uint16_t * shift, int32_t * out, uint
{
int32_t val = in [j] ;

val = (val << shiftVal) | (uint32_t) shift [j] ;
out [0] = val << 8 ;
val = arith_shift_left (val, shiftVal) | (uint32_t) shift [j] ;
out [0] = arith_shift_left (val, 8) ;
out += stride ;
}
}
Expand All @@ -289,7 +290,7 @@ void copyPredictorTo20 (int32_t * in, int32_t * out, uint32_t stride, int32_t nu
// in the 24-bit output buffer
for (j = 0 ; j < numSamples ; j++)
{
out [0] = in [j] << 12 ;
out [0] = arith_shift_left (in [j], 12) ;
out += stride ;
}
}
Expand All @@ -300,7 +301,7 @@ void copyPredictorTo32 (int32_t * in, int32_t * out, uint32_t stride, int32_t nu

// this is only a subroutine to abstract the "iPod can only output 16-bit data" problem
for (i = 0, j = 0 ; i < numSamples ; i++, j += stride)
out [j] = in [i] << 8 ;
out [j] = arith_shift_left (in [i], 8) ;
}

void copyPredictorTo32Shift (int32_t * in, uint16_t * shift, int32_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted)
Expand All @@ -314,7 +315,7 @@ void copyPredictorTo32Shift (int32_t * in, uint16_t * shift, int32_t * out, uint
// this is only a subroutine to abstract the "iPod can only output 16-bit data" problem
for (j = 0 ; j < numSamples ; j++)
{
op [0] = (in [j] << shiftVal) | (uint32_t) shift [j] ;
op [0] = arith_shift_left (in [j], shiftVal) | (uint32_t) shift [j] ;
op += stride ;
}
}
29 changes: 29 additions & 0 deletions src/ALAC/shift.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
** Copyright (C) 2014 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#if __GNUC__
#define ALWAYS_INLINE __attribute__ ((always_inline))
#else
#define ALWAYS_INLINE
#endif


static inline int32_t ALWAYS_INLINE
arith_shift_left (int32_t x, int shift)
{ return (int32_t) (((uint32_t) x) << shift) ;
} /* arith_shift_left */
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ G72x_libg72x_la_SOURCES = G72x/g72x.h G72x/g72x_priv.h \

ALAC_libalac_la_SOURCES = ALAC/ALACAudioTypes.h ALAC/ALACBitUtilities.h \
ALAC/EndianPortable.h ALAC/aglib.h ALAC/dplib.h ALAC/matrixlib.h \
ALAC/alac_codec.h \
ALAC/alac_codec.h ALAC/shift.h \
ALAC/ALACBitUtilities.c ALAC/ag_dec.c \
ALAC/ag_enc.c ALAC/dp_dec.c ALAC/dp_enc.c ALAC/matrix_dec.c \
ALAC/matrix_enc.c ALAC/alac_decoder.c ALAC/alac_encoder.c
Expand Down

0 comments on commit 9bbba9e

Please sign in to comment.