From f34f31dac0032887887b5bbcb0944de055b757d0 Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Sun, 21 Jul 2013 20:38:33 +1000 Subject: [PATCH] stream_encoder.c : Improve fix for arithmetic overflow. The previous fix (patch 6f7ec60c) had the undesireable effect of slowing down encoding speed on 16 bit files where the arithmetic overflow was less likely to happen. This fix forces the use of a FLAC__uint64 accumulator for 24 bit files and restores the use of a FLAC_uint32 accumulator for 16 (and less) bit files. Unfortunately, I have not been able to prove to myself that this overflow *cannot* happen with 16 bit files. --- src/libFLAC/stream_encoder.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libFLAC/stream_encoder.c b/src/libFLAC/stream_encoder.c index 4045189f..5cf0bc7a 100644 --- a/src/libFLAC/stream_encoder.c +++ b/src/libFLAC/stream_encoder.c @@ -3772,7 +3772,7 @@ void precompute_partition_info_sums_( #if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM /* slightly pessimistic but still catches all common cases */ /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */ - if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) { + if(bps < 24 && FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) { precompute_partition_info_sums_32bit_asm_ia32_(residual, abs_residual_partition_sums, residual_samples + predictor_order, predictor_order, min_partition_order, max_partition_order); return; } @@ -3783,8 +3783,8 @@ void precompute_partition_info_sums_( unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order); /* slightly pessimistic but still catches all common cases */ /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */ - if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) { - FLAC__uint64 abs_residual_partition_sum; + if(bps < 24 && FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) { + FLAC__uint32 abs_residual_partition_sum; for(partition = residual_sample = 0; partition < partitions; partition++) { end += default_partition_samples;