mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-11 22:44:04 +00:00
tint: const eval of ldexp builtin
Bug: tint:1581 Change-Id: Ib21717065041b65a637f4d73ce0088544b1fce0d Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/114321 Reviewed-by: Ben Clayton <bclayton@google.com> Commit-Queue: Antonio Maiorano <amaiorano@google.com> Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
9a8ca18d6d
commit
bf8a230c81
@@ -226,6 +226,7 @@ match fia_fiu32: fa | ia | f32 | i32 | u32
|
||||
match fa_f32: fa | f32
|
||||
match fa_f32_f16: fa | f32 | f16
|
||||
match ia_iu32: ia | i32 | u32
|
||||
match ia_i32: ia | i32
|
||||
match fiu32_f16: f32 | i32 | u32 | f16
|
||||
match fiu32: f32 | i32 | u32
|
||||
match fi32_f16: f32 | i32 | f16
|
||||
@@ -527,8 +528,8 @@ fn dot4U8Packed(u32, u32) -> u32
|
||||
@const fn insertBits<N: num, T: iu32>(vec<N, T>, vec<N, T>, u32, u32) -> vec<N, T>
|
||||
@const fn inverseSqrt<T: fa_f32_f16>(T) -> T
|
||||
@const fn inverseSqrt<N: num, T: fa_f32_f16>(vec<N, T>) -> vec<N, T>
|
||||
fn ldexp<T: f32_f16>(T, i32) -> T
|
||||
fn ldexp<N: num, T: f32_f16>(vec<N, T>, vec<N, i32>) -> vec<N, T>
|
||||
@const fn ldexp<T: fa_f32_f16, U: ia_i32>(T, U) -> T
|
||||
@const fn ldexp<N: num, T: fa_f32_f16, U: ia_i32>(vec<N, T>, vec<N, U>) -> vec<N, T>
|
||||
@const fn length<T: fa_f32_f16>(@test_value(0.0) T) -> T
|
||||
@const fn length<N: num, T: fa_f32_f16>(@test_value(0.0) vec<N, T>) -> T
|
||||
@const fn log<T: fa_f32_f16>(T) -> T
|
||||
|
||||
@@ -2654,6 +2654,48 @@ ConstEval::Result ConstEval::inverseSqrt(const type::Type* ty,
|
||||
return TransformElements(builder, ty, transform, args[0]);
|
||||
}
|
||||
|
||||
ConstEval::Result ConstEval::ldexp(const type::Type* ty,
|
||||
utils::VectorRef<const constant::Value*> args,
|
||||
const Source& source) {
|
||||
auto transform = [&](const constant::Value* c1, size_t index) {
|
||||
auto create = [&](auto e1) -> ConstEval::Result {
|
||||
using E1Type = decltype(e1);
|
||||
// If e1 is AFloat, then e2 is AInt, otherwise it's i32
|
||||
using E2Type = std::conditional_t<std::is_same_v<E1Type, AFloat>, AInt, i32>;
|
||||
|
||||
E2Type e2;
|
||||
auto* c2 = args[1];
|
||||
if (c2->Type()->Is<type::Vector>()) {
|
||||
e2 = c2->Index(index)->ValueAs<E2Type>();
|
||||
} else {
|
||||
e2 = c2->ValueAs<E2Type>();
|
||||
}
|
||||
|
||||
E2Type bias;
|
||||
if constexpr (std::is_same_v<E1Type, f16>) {
|
||||
bias = 15;
|
||||
} else if constexpr (std::is_same_v<E1Type, f32>) {
|
||||
bias = 127;
|
||||
} else {
|
||||
bias = 1023;
|
||||
}
|
||||
|
||||
if (e2 > bias + 1) {
|
||||
AddError("e2 must be less than or equal to " + std::to_string(bias + 1), source);
|
||||
return utils::Failure;
|
||||
}
|
||||
|
||||
auto target_ty = type::Type::DeepestElementOf(ty);
|
||||
|
||||
auto r = std::ldexp(e1, static_cast<int>(e2));
|
||||
return CreateScalar(builder, source, target_ty, E1Type{r});
|
||||
};
|
||||
return Dispatch_fa_f32_f16(create, c1);
|
||||
};
|
||||
|
||||
return TransformElements(builder, ty, transform, args[0]);
|
||||
}
|
||||
|
||||
ConstEval::Result ConstEval::length(const type::Type* ty,
|
||||
utils::VectorRef<const constant::Value*> args,
|
||||
const Source& source) {
|
||||
|
||||
@@ -725,6 +725,15 @@ class ConstEval {
|
||||
utils::VectorRef<const constant::Value*> args,
|
||||
const Source& source);
|
||||
|
||||
/// ldexp builtin
|
||||
/// @param ty the expression type
|
||||
/// @param args the input arguments
|
||||
/// @param source the source location
|
||||
/// @return the result value, or null if the value cannot be calculated
|
||||
Result ldexp(const type::Type* ty,
|
||||
utils::VectorRef<const constant::Value*> args,
|
||||
const Source& source);
|
||||
|
||||
/// length builtin
|
||||
/// @param ty the expression type
|
||||
/// @param args the input arguments
|
||||
|
||||
@@ -1550,6 +1550,71 @@ INSTANTIATE_TEST_SUITE_P( //
|
||||
testing::ValuesIn(Concat(ExtractBitsCases<i32>(), //
|
||||
ExtractBitsCases<u32>()))));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> LdexpCases() {
|
||||
using T2 = std::conditional_t<std::is_same_v<T, AFloat>, AInt, i32>;
|
||||
T2 bias;
|
||||
if constexpr (std::is_same_v<T, f16>) {
|
||||
bias = 15;
|
||||
} else if constexpr (std::is_same_v<T, f32>) {
|
||||
bias = 127;
|
||||
} else {
|
||||
bias = 1023;
|
||||
}
|
||||
|
||||
auto compute = [](T e1, T2 e2) { return T{std::ldexp(e1.value, static_cast<int>(e2.value))}; };
|
||||
|
||||
auto r = std::vector<Case>{
|
||||
C({T(0), T2(0)}, T(0)), //
|
||||
C({T(7), T2(4)}, T(112)), //
|
||||
C({T(7), T2(5)}, T(224)), //
|
||||
C({T(7), T2(6)}, T(448)), //
|
||||
C({T(7), T2(-4)}, T(0.4375)), //
|
||||
C({T(7), T2(-5)}, T(0.21875)), //
|
||||
C({T(7), T2(-6)}, T(0.109375)), //
|
||||
// With bias exponent
|
||||
C({T(0), T2(bias)}, T(0)), //
|
||||
C({T(0), T2(bias + 1)}, T(0)), //
|
||||
C({T(1), T2(bias)}, compute(T(1), T2(bias))), //
|
||||
C({T(0.5), T2(bias)}, compute(T(0.5), T2(bias))), //
|
||||
C({T(0.25), T2(bias)}, compute(T(0.25), T2(bias))), //
|
||||
// The result may be zero if e2 + bias ≤ 0.
|
||||
C({T(0), T2(-bias)}, T(0)), //
|
||||
C({T(0), T2(-bias - 1)}, T(0)), //
|
||||
C({T(0), T2(-bias - 2)}, T(0)), //
|
||||
|
||||
// Vector tests
|
||||
C({Vec(T(0), T(7), T(7)), Vec(T2(0), T2(4), T2(-4))}, Vec(T(0), T(112), T(0.4375))),
|
||||
};
|
||||
|
||||
std::string e2_too_large_error_msg =
|
||||
"12:34 error: e2 must be less than or equal to " + std::to_string(bias + 1);
|
||||
auto val_overflow_error_msg = [](auto val) {
|
||||
return "12:34 error: " + OverflowErrorMessage(val, FriendlyName<T>());
|
||||
};
|
||||
ConcatInto(r, std::vector<Case>{
|
||||
// e2 is > bias + 1
|
||||
E({T(0), T2(bias + 2)}, e2_too_large_error_msg),
|
||||
E({T(0), T2(bias + 1000)}, e2_too_large_error_msg),
|
||||
E({T(0), T2::Highest()}, e2_too_large_error_msg),
|
||||
// Result is inf
|
||||
E({T(1), T2(bias + 1)}, val_overflow_error_msg(T::Inf())),
|
||||
E({T(2), T2(bias + 1)}, val_overflow_error_msg(T::Inf())),
|
||||
E({T::Highest(), T2(bias + 1)}, val_overflow_error_msg(T::Inf())),
|
||||
E({T(-1), T2(bias + 1)}, val_overflow_error_msg(-T::Inf())),
|
||||
E({T(-2), T2(bias + 1)}, val_overflow_error_msg(-T::Inf())),
|
||||
E({T::Lowest(), T2(bias + 1)}, val_overflow_error_msg(-T::Inf())),
|
||||
});
|
||||
return r;
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P( //
|
||||
Ldexp,
|
||||
ResolverConstEvalBuiltinTest,
|
||||
testing::Combine(testing::Values(sem::BuiltinType::kLdexp),
|
||||
testing::ValuesIn(Concat(LdexpCases<AFloat>(), //
|
||||
LdexpCases<f32>(),
|
||||
LdexpCases<f16>()))));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> LengthCases() {
|
||||
const auto kSqrtOfHighest = T(std::sqrt(T::Highest()));
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user