Commit 17dfe1b6 by Nicolas Capens Committed by Nicolas Capens

Fix signed integer overflow.

Signed integer overflow is undefined behavior in C++. Change-Id: I12b7507a9624312a615826fd0a1d9cb30b8f8b58 Reviewed-on: https://swiftshader-review.googlesource.com/c/21768Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent d55d9973
......@@ -198,22 +198,22 @@ namespace sw
// Bit-cast of a floating-point value into a two's complement integer representation.
// This makes floating-point values comparable as integers.
inline int32_t float_as_twos_complement(float f)
{
// IEEE-754 floating-point numbers are sorted by magnitude in the same way as integers,
// except negative values are like one's complement integers. Convert them to two's complement.
int32_t i = bit_cast<int32_t>(f);
return (i < 0) ? (0x7FFFFFFF - i) : i;
}
// 'Safe' clamping operation which always returns a value between min and max (inclusive).
inline float clamp_s(float x, float min, float max)
{
// NaN values can't be compared directly
if(float_as_twos_complement(x) < float_as_twos_complement(min)) x = min;
if(float_as_twos_complement(x) > float_as_twos_complement(max)) x = max;
return x;
inline int32_t float_as_twos_complement(float f)
{
// IEEE-754 floating-point numbers are sorted by magnitude in the same way as integers,
// except negative values are like one's complement integers. Convert them to two's complement.
int32_t i = bit_cast<int32_t>(f);
return (i < 0) ? (0x7FFFFFFFu - i) : i;
}
// 'Safe' clamping operation which always returns a value between min and max (inclusive).
inline float clamp_s(float x, float min, float max)
{
// NaN values can't be compared directly
if(float_as_twos_complement(x) < float_as_twos_complement(min)) x = min;
if(float_as_twos_complement(x) > float_as_twos_complement(max)) x = max;
return x;
}
inline int ceilPow2(int x)
......
......@@ -198,22 +198,22 @@ namespace sw
// Bit-cast of a floating-point value into a two's complement integer representation.
// This makes floating-point values comparable as integers.
inline int32_t float_as_twos_complement(float f)
{
// IEEE-754 floating-point numbers are sorted by magnitude in the same way as integers,
// except negative values are like one's complement integers. Convert them to two's complement.
int32_t i = bit_cast<int32_t>(f);
return (i < 0) ? (0x7FFFFFFF - i) : i;
}
// 'Safe' clamping operation which always returns a value between min and max (inclusive).
inline float clamp_s(float x, float min, float max)
{
// NaN values can't be compared directly
if(float_as_twos_complement(x) < float_as_twos_complement(min)) x = min;
if(float_as_twos_complement(x) > float_as_twos_complement(max)) x = max;
return x;
inline int32_t float_as_twos_complement(float f)
{
// IEEE-754 floating-point numbers are sorted by magnitude in the same way as integers,
// except negative values are like one's complement integers. Convert them to two's complement.
int32_t i = bit_cast<int32_t>(f);
return (i < 0) ? (0x7FFFFFFFu - i) : i;
}
// 'Safe' clamping operation which always returns a value between min and max (inclusive).
inline float clamp_s(float x, float min, float max)
{
// NaN values can't be compared directly
if(float_as_twos_complement(x) < float_as_twos_complement(min)) x = min;
if(float_as_twos_complement(x) > float_as_twos_complement(max)) x = max;
return x;
}
inline int ceilPow2(int x)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment