Replace positive/negative_inf() with infinity()
This matches the std::numeric_limits<> method name. Negative infinity
can be obtained using -infinity() just like in C++.
Also replace the use of C99 INFINITY with the C++ constant.
Bug: b/140302841
Change-Id: Ifd5e5d1fa4fa2d9ccce6d334af22903ea7597a69
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/40148
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Pipeline/SpirvShaderGroup.cpp b/src/Pipeline/SpirvShaderGroup.cpp
index 877020d..fde2138 100644
--- a/src/Pipeline/SpirvShaderGroup.cpp
+++ b/src/Pipeline/SpirvShaderGroup.cpp
@@ -410,7 +410,7 @@
using Type = SIMD::Float;
Impl::Group::BinaryOperation(
this, insn, state, dst,
- Type::positive_inf(),
+ Type::infinity(),
[](RValue<Type> a, RValue<Type> b) { return NMin(a, b); });
break;
}
@@ -438,9 +438,10 @@
case spv::OpGroupNonUniformFMax:
{
using Type = SIMD::Float;
+ SIMD::Float negative_inf = -SIMD::Float::infinity();
Impl::Group::BinaryOperation(
this, insn, state, dst,
- Type::negative_inf(),
+ negative_inf,
[](RValue<Type> a, RValue<Type> b) { return NMax(a, b); });
break;
}
diff --git a/src/Reactor/Reactor.cpp b/src/Reactor/Reactor.cpp
index fe8e103..7e5f72b 100644
--- a/src/Reactor/Reactor.cpp
+++ b/src/Reactor/Reactor.cpp
@@ -3880,14 +3880,28 @@
// being reinterpreted as float and then bitcast to integer again,
// which does not guarantee preserving the integer value.
//
- // Should infinity and NaN constants be required, methods like
- // infinity(), quiet_NaN(), and signaling_NaN() should be added
- // to the Float class.
+ // The inifinity() method can be used to obtain positive infinity.
+ // Should NaN constants be required, methods like quiet_NaN() and
+ // signaling_NaN() should be added (matching std::numeric_limits).
ASSERT(std::isfinite(x));
storeValue(Nucleus::createConstantFloat(x));
}
+// TODO(b/140302841): Negative infinity can be obtained by using '-infinity()'.
+// This comes at a minor run-time JIT cost, and the backend may or may not
+// perform constant folding. This can be optimized by having Reactor perform
+// the folding, which would still be cheaper than having a capable backend do it.
+Float Float::infinity()
+{
+ Float result;
+
+ constexpr double inf = std::numeric_limits<double>::infinity();
+ result.storeValue(Nucleus::createConstantFloat(inf));
+
+ return result;
+}
+
Float::Float(RValue<Float> rhs)
{
storeValue(rhs.value);
@@ -4111,25 +4125,15 @@
constant(x, y, z, w);
}
-Float4 Float4::positive_inf()
+Float4 Float4::infinity()
{
Float4 result;
- result.infinity_constant(false);
- return result;
-}
-Float4 Float4::negative_inf()
-{
- Float4 result;
- result.infinity_constant(true);
- return result;
-}
-
-void Float4::infinity_constant(bool negative)
-{
- double inf = negative ? -INFINITY : INFINITY;
+ constexpr double inf = std::numeric_limits<double>::infinity();
double constantVector[4] = { inf, inf, inf, inf };
- storeValue(Nucleus::createConstantVector(constantVector, getType()));
+ result.storeValue(Nucleus::createConstantVector(constantVector, getType()));
+
+ return result;
}
void Float4::constant(float x, float y, float z, float w)
diff --git a/src/Reactor/Reactor.hpp b/src/Reactor/Reactor.hpp
index 95f0700..3ddbec9 100644
--- a/src/Reactor/Reactor.hpp
+++ b/src/Reactor/Reactor.hpp
@@ -22,7 +22,7 @@
#include <cassert>
#include <cstddef>
#include <cstdio>
-
+#include <limits>
#include <tuple>
#include <unordered_set>
@@ -2081,6 +2081,8 @@
template<int T>
RValue<Float> operator=(const SwizzleMask1<Float4, T> &rhs);
+ static Float infinity();
+
static Type *getType();
};
@@ -2243,13 +2245,12 @@
template<int T>
RValue<Float4> operator=(const Swizzle4<Float4, T> &rhs);
+ static Float4 infinity();
+
static Type *getType();
- static Float4 negative_inf();
- static Float4 positive_inf();
private:
void constant(float x, float y, float z, float w);
- void infinity_constant(bool negative);
};
RValue<Float4> operator+(RValue<Float4> lhs, RValue<Float4> rhs);
diff --git a/src/Reactor/ReactorUnitTests.cpp b/src/Reactor/ReactorUnitTests.cpp
index ff69d2f..a72cb1e 100644
--- a/src/Reactor/ReactorUnitTests.cpp
+++ b/src/Reactor/ReactorUnitTests.cpp
@@ -460,7 +460,7 @@
}
}
-TEST(ReactorUnitTests, Blend)
+TEST(ReactorUnitTests, Shuffle)
{
{
// |select| is [0aaa:0bbb:0ccc:0ddd] where |aaa|, |bbb|, |ccc|