Fix UBSan errors (left shift of negative value)
I've settled on replacing x << n with x * (1 << n); this gets rid of the "left shift of negative value" warning, but will still trigger undefined behavior if the multiplication overflows. It also still looks like a left shift, which is good for the readability of the fixed-point code. (The compiler is smart enough to recognize that the multiplication+shift is just a shift, for both variable and constant shift amounts, so the generated code should not change.) BUG=chromium:603491 Review-Url: https://codereview.webrtc.org/1989803002 Cr-Commit-Position: refs/heads/master@{#12845}
This commit is contained in:
@ -182,7 +182,7 @@ int WebRtcIsacfix_DecodeImpl(int16_t* signal_out16,
|
||||
|
||||
for (k = 0; k < FRAMESAMPLES/2; k++)
|
||||
{
|
||||
Vector_Word32_1[k] = (Vector_Word16_2[k] * gainQ13) << 3; // Q25
|
||||
Vector_Word32_1[k] = (Vector_Word16_2[k] * gainQ13) * (1 << 3); // Q25
|
||||
}
|
||||
|
||||
|
||||
@ -192,7 +192,7 @@ int WebRtcIsacfix_DecodeImpl(int16_t* signal_out16,
|
||||
|
||||
/* --- Store Highpass Residual --- */
|
||||
for (k = 0; k < FRAMESAMPLES/2; k++)
|
||||
Vector_Word32_1[k] = Vector_Word32_2[k] << 9; // Q16 -> Q25
|
||||
Vector_Word32_1[k] = Vector_Word32_2[k] * (1 << 9); // Q16 -> Q25
|
||||
|
||||
for( k = 0; k < PITCH_MAX_LAG + 10; k++ )
|
||||
(ISACdec_obj->plcstr_obj).prevHP[k] = Vector_Word32_1[FRAMESAMPLES/2 - (PITCH_MAX_LAG + 10) + k];
|
||||
|
||||
@ -255,7 +255,7 @@ static void CalcInvArSpec(const int16_t *ARCoefQ12,
|
||||
}
|
||||
|
||||
for (k=0; k<FRAMESAMPLES/8; k++) {
|
||||
int32_t diff_q16 = diffQ16[k] << shftVal;
|
||||
int32_t diff_q16 = diffQ16[k] * (1 << shftVal);
|
||||
CurveQ16[FRAMESAMPLES / 4 - 1 - k] = CurveQ16[k] - diff_q16;
|
||||
CurveQ16[k] += diff_q16;
|
||||
}
|
||||
@ -864,8 +864,8 @@ void WebRtcIsacfix_MatrixProduct1C(const int16_t matrix0[],
|
||||
matrix0_index = matrix0_index_factor1 * (*matrix0_index_factor2);
|
||||
matrix1_index = matrix1_index_factor1 * (*matrix1_index_factor2);
|
||||
for (n = 0; n < inner_loop_count; n++) {
|
||||
sum32 += (WEBRTC_SPL_MUL_16_32_RSFT16(matrix0[matrix0_index],
|
||||
matrix1[matrix1_index] << shift));
|
||||
sum32 += WEBRTC_SPL_MUL_16_32_RSFT16(
|
||||
matrix0[matrix0_index], matrix1[matrix1_index] * (1 << shift));
|
||||
matrix0_index += matrix0_index_step;
|
||||
matrix1_index += matrix1_index_step;
|
||||
}
|
||||
@ -1042,7 +1042,8 @@ int WebRtcIsacfix_DecodeLpcCoef(Bitstr_dec *streamdata,
|
||||
/* hi band LAR coeffs */
|
||||
for (n=0; n<ORDERHI; n++, pos++, poss++) {
|
||||
// ((Q13*Q17)>>16)<<3 = Q17, with 1/0.45 = 2.222222222222 ~= 18204 in Q13
|
||||
tmp32 = WEBRTC_SPL_MUL_16_32_RSFT16(18204, tmpcoeffs_sQ17[poss]) << 3;
|
||||
tmp32 =
|
||||
WEBRTC_SPL_MUL_16_32_RSFT16(18204, tmpcoeffs_sQ17[poss]) * (1 << 3);
|
||||
tmp32 = tmp32 + WebRtcIsacfix_kMeansShapeQ17[model][poss]; // Q17+Q17 = Q17
|
||||
LPCCoefQ17[pos] = tmp32;
|
||||
}
|
||||
|
||||
@ -50,33 +50,37 @@ void WebRtcIsacfix_AllpassFilter2FixDec16C(
|
||||
// Process channel 1:
|
||||
in_out = data_ch1[n];
|
||||
a = factor_ch1[0] * in_out; // Q15 * Q0 = Q15
|
||||
a <<= 1; // Q15 -> Q16
|
||||
a *= 1 << 1; // Q15 -> Q16
|
||||
b = WebRtcSpl_AddSatW32(a, state0_ch1);
|
||||
a = -factor_ch1[0] * (int16_t)(b >> 16); // Q15
|
||||
state0_ch1 = WebRtcSpl_AddSatW32(a << 1, (uint32_t)in_out << 16); // Q16
|
||||
state0_ch1 =
|
||||
WebRtcSpl_AddSatW32(a * (1 << 1), (int32_t)in_out * (1 << 16)); // Q16
|
||||
in_out = (int16_t) (b >> 16); // Save as Q0
|
||||
|
||||
a = factor_ch1[1] * in_out; // Q15 * Q0 = Q15
|
||||
a <<= 1; // Q15 -> Q16
|
||||
a *= 1 << 1; // Q15 -> Q16
|
||||
b = WebRtcSpl_AddSatW32(a, state1_ch1); // Q16
|
||||
a = -factor_ch1[1] * (int16_t)(b >> 16); // Q15
|
||||
state1_ch1 = WebRtcSpl_AddSatW32(a << 1, (uint32_t)in_out << 16); // Q16
|
||||
state1_ch1 =
|
||||
WebRtcSpl_AddSatW32(a * (1 << 1), (int32_t)in_out * (1 << 16)); // Q16
|
||||
data_ch1[n] = (int16_t) (b >> 16); // Save as Q0
|
||||
|
||||
// Process channel 2:
|
||||
in_out = data_ch2[n];
|
||||
a = factor_ch2[0] * in_out; // Q15 * Q0 = Q15
|
||||
a <<= 1; // Q15 -> Q16
|
||||
a *= 1 << 1; // Q15 -> Q16
|
||||
b = WebRtcSpl_AddSatW32(a, state0_ch2); // Q16
|
||||
a = -factor_ch2[0] * (int16_t)(b >> 16); // Q15
|
||||
state0_ch2 = WebRtcSpl_AddSatW32(a << 1, (uint32_t)in_out << 16); // Q16
|
||||
state0_ch2 =
|
||||
WebRtcSpl_AddSatW32(a * (1 << 1), (int32_t)in_out * (1 << 16)); // Q16
|
||||
in_out = (int16_t) (b >> 16); // Save as Q0
|
||||
|
||||
a = factor_ch2[1] * in_out; // Q15 * Q0 = Q15
|
||||
a <<= 1; // Q15 -> Q16
|
||||
a *= (1 << 1); // Q15 -> Q16
|
||||
b = WebRtcSpl_AddSatW32(a, state1_ch2); // Q16
|
||||
a = -factor_ch2[1] * (int16_t)(b >> 16); // Q15
|
||||
state1_ch2 = WebRtcSpl_AddSatW32(a << 1, (uint32_t)in_out << 16); // Q16
|
||||
state1_ch2 =
|
||||
WebRtcSpl_AddSatW32(a * (1 << 1), (int32_t)in_out * (1 << 16)); // Q16
|
||||
data_ch2[n] = (int16_t) (b >> 16); // Save as Q0
|
||||
}
|
||||
|
||||
@ -144,11 +148,11 @@ void WebRtcIsacfix_HighpassFilterFixDec32C(int16_t *io,
|
||||
c = in + ((a1 + b1) >> 7); // Q0.
|
||||
io[k] = (int16_t)WebRtcSpl_SatW32ToW16(c); // Write output as Q0.
|
||||
|
||||
c = (in << 2) - a2 - b2; // In Q2.
|
||||
c = in * (1 << 2) - a2 - b2; // In Q2.
|
||||
c = (int32_t)WEBRTC_SPL_SAT(536870911, c, -536870912);
|
||||
|
||||
state1 = state0;
|
||||
state0 = c << 2; // Write state as Q4
|
||||
state0 = c * (1 << 2); // Write state as Q4
|
||||
}
|
||||
state[0] = state0;
|
||||
state[1] = state1;
|
||||
|
||||
@ -274,7 +274,7 @@ void WebRtcIsacfix_NormLatticeFilterAr(size_t orderCoef,
|
||||
for (i=0;i<HALF_SUBFRAMELEN;i++)
|
||||
{
|
||||
|
||||
tmp32 = lat_inQ25[i + temp1] << 1; // Q25->Q26
|
||||
tmp32 = lat_inQ25[i + temp1] * (1 << 1); // Q25->Q26
|
||||
tmp32 = WEBRTC_SPL_MUL_16_32_RSFT16(inv_gain16, tmp32); //lat_in[]*inv_gain in (Q(18-sh)*Q26)>>16 = Q(28-sh)
|
||||
tmp32 = WEBRTC_SPL_SHIFT_W32(tmp32, -(28-sh)); // lat_in[]*inv_gain in Q0
|
||||
|
||||
|
||||
@ -132,10 +132,10 @@ void WebRtcIsacfix_Spec2TimeC(int16_t *inreQ7, int16_t *inimQ7, int32_t *outre1Q
|
||||
tmp1rQ14 = -WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4 - 1 - k];
|
||||
tmp1iQ14 = WebRtcIsacfix_kSinTab2[k];
|
||||
|
||||
tmpInRe = inreQ7[k] << 9; // Q7 -> Q16
|
||||
tmpInIm = inimQ7[k] << 9; // Q7 -> Q16
|
||||
tmpInRe2 = inreQ7[FRAMESAMPLES / 2 - 1 - k] << 9; // Q7 -> Q16
|
||||
tmpInIm2 = inimQ7[FRAMESAMPLES / 2 - 1 - k] << 9; // Q7 -> Q16
|
||||
tmpInRe = inreQ7[k] * (1 << 9); // Q7 -> Q16
|
||||
tmpInIm = inimQ7[k] * (1 << 9); // Q7 -> Q16
|
||||
tmpInRe2 = inreQ7[FRAMESAMPLES / 2 - 1 - k] * (1 << 9); // Q7 -> Q16
|
||||
tmpInIm2 = inimQ7[FRAMESAMPLES / 2 - 1 - k] * (1 << 9); // Q7 -> Q16
|
||||
|
||||
xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInRe) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInIm);
|
||||
xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInIm) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInRe);
|
||||
@ -184,8 +184,8 @@ void WebRtcIsacfix_Spec2TimeC(int16_t *inreQ7, int16_t *inimQ7, int32_t *outre1Q
|
||||
}
|
||||
} else {
|
||||
for (k=0; k<240; k++) {
|
||||
outre1Q16[k] = inreQ7[k] << -sh; // Q(16+sh) -> Q16
|
||||
outre2Q16[k] = inimQ7[k] << -sh; // Q(16+sh) -> Q16
|
||||
outre1Q16[k] = inreQ7[k] * (1 << -sh); // Q(16+sh) -> Q16
|
||||
outre2Q16[k] = inimQ7[k] * (1 << -sh); // Q(16+sh) -> Q16
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user