Skip to content

Commit

Permalink
Fix non-representable left shifts.
Browse files Browse the repository at this point in the history
Convert signed integers to unsigned in places where result of left shift
operation is not representable in signed integer type. References #31.
  • Loading branch information
foo86 committed Jun 2, 2015
1 parent 8499504 commit 121ab1b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
18 changes: 9 additions & 9 deletions libdcadec/core_decoder.c
Expand Up @@ -235,8 +235,8 @@ static int parse_coding_header(struct core_decoder *core, enum HeaderType header
assert(core->nchannels <= MAX_CHANNELS);

// Loudspeaker layout mask
core->xxch_spkr_mask =
bits_get(&core->bits, core->xxch_mask_nbits - SPEAKER_Cs) << SPEAKER_Cs;
unsigned int mask = bits_get(&core->bits, core->xxch_mask_nbits - SPEAKER_Cs);
core->xxch_spkr_mask = mask << SPEAKER_Cs;

enforce(dca_popcount(core->xxch_spkr_mask) == n,
"Invalid XXCH speaker layout mask");
Expand Down Expand Up @@ -273,7 +273,7 @@ static int parse_coding_header(struct core_decoder *core, enum HeaderType header
int *coeff_ptr = core->dmix_coeff;
for (ch = xch_base; ch < core->nchannels; ch++) {
for (n = 0; n < core->xxch_mask_nbits; n++) {
if (core->dmix_mask[ch] & (1 << n)) {
if (core->dmix_mask[ch] & (1U << n)) {
int code = bits_get(&core->bits, 7);
int sign = (code >> 6) - 1; code &= 63;
if (code) {
Expand Down Expand Up @@ -853,7 +853,7 @@ static int map_prm_ch_to_spkr(struct core_decoder *core, int ch)
if (ch < pos) {
int spkr = prm_ch_to_spkr_map[core->audio_mode][ch];
if (core->ext_audio_mask & (CSS_XXCH | EXSS_XXCH)) {
if (core->xxch_core_mask & (1 << spkr))
if (core->xxch_core_mask & (1U << spkr))
return spkr;
if (spkr == SPEAKER_Ls && (core->xxch_core_mask & SPEAKER_MASK_Lss))
return SPEAKER_Lss;
Expand All @@ -871,7 +871,7 @@ static int map_prm_ch_to_spkr(struct core_decoder *core, int ch)
// Then XXCH
if (core->ext_audio_mask & (CSS_XXCH | EXSS_XXCH))
for (int spkr = SPEAKER_Cs; spkr < core->xxch_mask_nbits; spkr++)
if (core->xxch_spkr_mask & (1 << spkr))
if (core->xxch_spkr_mask & (1U << spkr))
if (pos++ == ch)
return spkr;

Expand Down Expand Up @@ -907,7 +907,7 @@ int core_filter(struct core_decoder *core, int flags)

int *ptr = core->output_buffer;
for (int spkr = 0; spkr < SPEAKER_COUNT; spkr++) {
if (core->ch_mask & (1 << spkr)) {
if (core->ch_mask & (1U << spkr)) {
core->output_samples[spkr] = ptr;
ptr += core->npcmsamples;
} else {
Expand Down Expand Up @@ -1023,7 +1023,7 @@ int core_filter(struct core_decoder *core, int flags)
int scale_inv = core->dmix_scale_inv;
if (scale_inv != (1 << 16)) {
for (int spkr = 0; spkr < core->xxch_mask_nbits; spkr++) {
if (core->xxch_core_mask & (1 << spkr)) {
if (core->xxch_core_mask & (1U << spkr)) {
int *samples = core->output_samples[spkr];
for (int n = 0; n < nsamples; n++)
samples[n] = mul16(samples[n], scale_inv);
Expand All @@ -1038,7 +1038,7 @@ int core_filter(struct core_decoder *core, int flags)
if (spkr1 < 0)
return -DCADEC_EINVAL;
for (int spkr2 = 0; spkr2 < core->xxch_mask_nbits; spkr2++) {
if (core->dmix_mask[ch] & (1 << spkr2)) {
if (core->dmix_mask[ch] & (1U << spkr2)) {
int coeff = mul16(*coeff_ptr++, scale_inv);
if (coeff) {
int *src = core->output_samples[spkr1];
Expand All @@ -1052,7 +1052,7 @@ int core_filter(struct core_decoder *core, int flags)

// Clip core channels
for (int spkr = 0; spkr < core->xxch_mask_nbits; spkr++) {
if (core->xxch_core_mask & (1 << spkr)) {
if (core->xxch_core_mask & (1U << spkr)) {
int *samples = core->output_samples[spkr];
for (int n = 0; n < nsamples; n++)
samples[n] = clip23(samples[n]);
Expand Down
6 changes: 3 additions & 3 deletions libdcadec/dca_context.c
Expand Up @@ -82,7 +82,7 @@ static int reorder_samples(struct dcadec_context *dca, int **dca_samples, int dc

if (dca->flags & DCADEC_FLAG_NATIVE_LAYOUT) {
for (int dca_ch = 0; dca_ch < SPEAKER_COUNT; dca_ch++) {
if (dca_mask & (1 << dca_ch)) {
if (dca_mask & (1U << dca_ch)) {
if (!dca_samples[dca_ch])
return -DCADEC_EINVAL;
dca->samples[nchannels++] = dca_samples[dca_ch];
Expand Down Expand Up @@ -165,7 +165,7 @@ static int down_mix_prim_chset(struct dcadec_context *dca, int **samples,

// Perform downmix
for (int spkr = 0, pos = 0; spkr < SPEAKER_COUNT; spkr++) {
if (!(*ch_mask & (1 << spkr)))
if (!(*ch_mask & (1U << spkr)))
continue;

for (int ch = 0; ch < 2; ch++) {
Expand Down Expand Up @@ -264,7 +264,7 @@ static int filter_core_frame(struct dcadec_context *dca)

static int map_spkr_to_core_spkr(struct core_decoder *core, int spkr)
{
if (core->ch_mask & (1 << spkr))
if (core->ch_mask & (1U << spkr))
return spkr;
if (spkr == SPEAKER_Lss && (core->ch_mask & SPEAKER_MASK_Ls))
return SPEAKER_Ls;
Expand Down
2 changes: 1 addition & 1 deletion libdcadec/dca_waveout.c
Expand Up @@ -225,7 +225,7 @@ DCADEC_API int dcadec_waveout_write(struct dcadec_waveout *wave, int **samples,
if (wave->flags & DCADEC_WAVEOUT_FLAG_MONO) {
wave->block_align = wave->bytes_per_sample;
for (int i = 0, j = 0; i < SPEAKER_COUNT; i++) {
if (!(wave->channel_mask & (1 << i)))
if (!(wave->channel_mask & (1U << i)))
continue;

if (!wave->fp[j]) {
Expand Down
2 changes: 1 addition & 1 deletion libdcadec/xll_decoder.c
Expand Up @@ -744,7 +744,7 @@ int xll_map_ch_to_spkr(struct xll_chset *chs, int ch)

if (chs->ch_mask_enabled) {
for (int spkr = 0, pos = 0; spkr < xll->ch_mask_nbits; spkr++)
if (chs->ch_mask & (1 << spkr))
if (chs->ch_mask & (1U << spkr))
if (pos++ == ch)
return spkr;
return -1; // Invalid
Expand Down
6 changes: 4 additions & 2 deletions test/stddev.c
Expand Up @@ -75,8 +75,10 @@ int main(int argc, char **argv)
int64_t d;

if (bps == 3) {
int32_t v1 = ((p1[0] << 8) | (p1[1] << 16) | (p1[2] << 24)) >> 8;
int32_t v2 = ((p2[0] << 8) | (p2[1] << 16) | (p2[2] << 24)) >> 8;
uint32_t u1 = (p1[0] << 8) | (p1[1] << 16) | ((uint32_t)p1[2] << 24);
uint32_t u2 = (p2[0] << 8) | (p2[1] << 16) | ((uint32_t)p2[2] << 24);
int32_t v1 = (int32_t)u1 >> 8;
int32_t v2 = (int32_t)u2 >> 8;
d = v1 - v2;
} else {
int16_t v1 = p1[0] | (p1[1] << 8);
Expand Down

0 comments on commit 121ab1b

Please sign in to comment.