Avoid overflow in WebRtcSpl_Sqrt

When the input to WebRtcSpl_Sqrt was the maximum negative value
(-2147483648), the calculations would overflow. This is now solved by
nudging this particular input value one step.

BUG=webrtc:5512

Review URL: https://codereview.webrtc.org/1685743003

Cr-Commit-Position: refs/heads/master@{#11631}
This commit is contained in:
henrik.lundin 2016-02-15 12:38:08 -08:00 committed by Commit bot
parent 092c951c65
commit 71e92dc5e8

View File

@ -139,8 +139,19 @@ int32_t WebRtcSpl_Sqrt(int32_t value)
A = value;
if (A == 0)
return (int32_t)0; // sqrt(0) = 0
// The convention in this function is to calculate sqrt(abs(A)). Negate the
// input if it is negative.
if (A < 0) {
if (A == WEBRTC_SPL_WORD32_MIN) {
// This number cannot be held in an int32_t after negating.
// Map it to the maximum positive value.
A = WEBRTC_SPL_WORD32_MAX;
} else {
A = -A;
}
} else if (A == 0) {
return 0; // sqrt(0) = 0
}
sh = WebRtcSpl_NormW32(A); // # shifts to normalize A
A = WEBRTC_SPL_LSHIFT_W32(A, sh); // Normalize A