Fix signed integer overflow.
Signed integer overflow is undefined behavior in C++.
Change-Id: I12b7507a9624312a615826fd0a1d9cb30b8f8b58
Reviewed-on: https://swiftshader-review.googlesource.com/c/21768
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Common/Math.hpp b/src/Common/Math.hpp
index 7343cc9..79c8e74 100644
--- a/src/Common/Math.hpp
+++ b/src/Common/Math.hpp
@@ -198,22 +198,22 @@
// 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)
diff --git a/src/System/Math.hpp b/src/System/Math.hpp
index 7343cc9..79c8e74 100644
--- a/src/System/Math.hpp
+++ b/src/System/Math.hpp
@@ -198,22 +198,22 @@
// 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)