Move helper methods inside Scalar.

This CL moves ValueOf and IsPositiveZero into the Scalar class which was
the only usage.

Bug: tint:1718
Change-Id: I2c99831ac30d4d3f0b3bfe9ad25a85186bba0f1c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/114123
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
dan sinclair 2022-12-14 16:20:09 +00:00 committed by Dawn LUCI CQ
parent 5225fcc689
commit 7efe4860c9
1 changed files with 22 additions and 22 deletions

View File

@ -168,24 +168,6 @@ auto ZeroTypeDispatch(const type::Type* type, F&& f) {
[&](const type::Bool*) { return f(static_cast<bool>(0)); });
}
/// @returns `value` if `T` is not a Number, otherwise ValueOf returns the inner value of the
/// Number.
template <typename T>
inline auto ValueOf(T value) {
if constexpr (std::is_same_v<UnwrapNumber<T>, T>) {
return value;
} else {
return value.value;
}
}
/// @returns true if `value` is a positive zero.
template <typename T>
inline bool IsPositiveZero(T value) {
using N = UnwrapNumber<T>;
return Number<N>(value) == Number<N>(0); // Considers sign bit
}
template <typename NumberT>
std::string OverflowErrorMessage(NumberT lhs, const char* op, NumberT rhs) {
std::stringstream ss;
@ -278,10 +260,28 @@ class Scalar : public Castable<Scalar<T>, constant::Constant> {
}
}
const constant::Constant* Index(size_t) const override { return nullptr; }
bool AllZero() const override { return IsPositiveZero(value); }
bool AnyZero() const override { return IsPositiveZero(value); }
bool AllZero() const override { return IsPositiveZero(); }
bool AnyZero() const override { return IsPositiveZero(); }
bool AllEqual() const override { return true; }
size_t Hash() const override { return utils::Hash(type, ValueOf(value)); }
size_t Hash() const override { return utils::Hash(type, ValueOf()); }
/// @returns `value` if `T` is not a Number, otherwise ValueOf returns the inner value of the
/// Number.
inline auto ValueOf() const {
if constexpr (std::is_same_v<UnwrapNumber<T>, T>) {
return value;
} else {
return value.value;
}
}
/// @returns true if `value` is a positive zero.
inline bool IsPositiveZero() const {
using N = UnwrapNumber<T>;
return Number<N>(value) == Number<N>(0); // Considers sign bit
}
type::Type const* const type;
const T value;
@ -364,7 +364,7 @@ ImplResult ScalarConvert(const Scalar<T>* scalar,
using FROM = T;
if constexpr (std::is_same_v<TO, bool>) {
// [x -> bool]
return builder.create<Scalar<TO>>(target_ty, !IsPositiveZero(scalar->value));
return builder.create<Scalar<TO>>(target_ty, !scalar->IsPositiveZero());
} else if constexpr (std::is_same_v<FROM, bool>) {
// [bool -> x]
return builder.create<Scalar<TO>>(target_ty, TO(scalar->value ? 1 : 0));