Implement const-eval for acos

This CL adds const-eval for the `acos` builtin.

Bug: tint:1581
Change-Id: I01c0f48e73eedf87cf9c912715487f8eea44f64e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/108341
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair
2022-11-03 14:49:23 +00:00
committed by Dawn LUCI CQ
parent b3518d8b4d
commit 9cbc7e1e54
133 changed files with 2482 additions and 227 deletions

View File

@@ -1595,6 +1595,23 @@ ConstEval::Result ConstEval::abs(const sem::Type* ty,
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::acos(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source) {
auto transform = [&](const sem::Constant* c0) {
auto create = [&](auto i) -> ImplResult {
using NumberT = decltype(i);
if (i < NumberT(-1.0) || i > NumberT(1.0)) {
AddError("acos must be called with a value in the range [-1, 1]", source);
return utils::Failure;
}
return CreateElement(builder, c0->Type(), NumberT(std::acos(i.value)));
};
return Dispatch_fa_f32_f16(create, c0);
};
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::all(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
@@ -1613,11 +1630,11 @@ ConstEval::Result ConstEval::asin(const sem::Type* ty,
auto transform = [&](const sem::Constant* c0) {
auto create = [&](auto i) -> ImplResult {
using NumberT = decltype(i);
if (i.value < NumberT(-1.0) || i.value > NumberT(1.0)) {
if (i < NumberT(-1.0) || i > NumberT(1.0)) {
AddError("asin must be called with a value in the range [-1, 1]", source);
return utils::Failure;
}
return CreateElement(builder, c0->Type(), decltype(i)(std::asin(i.value)));
return CreateElement(builder, c0->Type(), NumberT(std::asin(i.value)));
};
return Dispatch_fa_f32_f16(create, c0);
};
@@ -1659,11 +1676,11 @@ ConstEval::Result ConstEval::atanh(const sem::Type* ty,
auto transform = [&](const sem::Constant* c0) {
auto create = [&](auto i) -> ImplResult {
using NumberT = decltype(i);
if (i.value <= NumberT(-1.0) || i.value >= NumberT(1.0)) {
if (i <= NumberT(-1.0) || i >= NumberT(1.0)) {
AddError("atanh must be called with a value in the range (-1, 1)", source);
return utils::Failure;
}
return CreateElement(builder, c0->Type(), decltype(i)(std::atanh(i.value)));
return CreateElement(builder, c0->Type(), NumberT(std::atanh(i.value)));
};
return Dispatch_fa_f32_f16(create, c0);
};

View File

@@ -395,6 +395,15 @@ class ConstEval {
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// acos builtin
/// @param ty the expression type
/// @param args the input arguments
/// @param source the source location of the conversion
/// @return the result value, or null if the value cannot be calculated
Result acos(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// any builtin
/// @param ty the expression type
/// @param args the input arguments

View File

@@ -425,6 +425,70 @@ TEST_F(ResolverConstEvalBuiltinTest, Atanh_OutsideRange_Negative_INF) {
EXPECT_EQ(r()->error(), "12:24 error: atanh must be called with a value in the range (-1, 1)");
}
template <typename T, bool finite_only>
std::vector<Case> AcosCases() {
std::vector<Case> cases = {
// If i is +/-0, +/-0 is returned
C({T(0.87758256189)}, T(0.5)).FloatComp(),
C({T(1.0)}, T(0.0)),
C({-T(1.0)}, kPi<T>).FloatComp(),
// Vector tests
C({Vec(T(1.0), -T(1.0))}, Vec(T(0), kPi<T>)).FloatComp(),
};
ConcatIntoIf<!finite_only>( //
cases, std::vector<Case>{
// If i is NaN, NaN is returned
C({T::NaN()}, T::NaN()),
// Vector tests
C({Vec(T::NaN(), T::NaN())}, Vec(T::NaN(), T::NaN())),
});
return cases;
}
INSTANTIATE_TEST_SUITE_P( //
Acos,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kAcos),
testing::ValuesIn(Concat(AcosCases<AFloat, true>(), //
AcosCases<f32, false>(),
AcosCases<f16, false>()))));
TEST_F(ResolverConstEvalBuiltinTest, Acos_OutsideRange_Positive) {
auto* expr = Call(Source{{12, 24}}, "acos", Expr(1.1_a));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:24 error: acos must be called with a value in the range [-1, 1]");
}
TEST_F(ResolverConstEvalBuiltinTest, Acos_OutsideRange_Negative) {
auto* expr = Call(Source{{12, 24}}, "acos", Negation(1.1_a));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:24 error: acos must be called with a value in the range [-1, 1]");
}
TEST_F(ResolverConstEvalBuiltinTest, Acos_OutsideRange_Positive_INF) {
auto* expr = Call(Source{{12, 24}}, "acos", Expr(f32::Inf()));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:24 error: acos must be called with a value in the range [-1, 1]");
}
TEST_F(ResolverConstEvalBuiltinTest, Acos_OutsideRange_Negative_INF) {
auto* expr = Call(Source{{12, 24}}, "acos", Negation(f32::Inf()));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:24 error: acos must be called with a value in the range [-1, 1]");
}
template <typename T, bool finite_only>
std::vector<Case> AsinCases() {
std::vector<Case> cases = {

View File

@@ -13473,24 +13473,24 @@ constexpr OverloadInfo kOverloads[] = {
/* num parameters */ 1,
/* num template types */ 1,
/* num template numbers */ 0,
/* template types */ &kTemplateTypes[22],
/* template types */ &kTemplateTypes[23],
/* template numbers */ &kTemplateNumbers[10],
/* parameters */ &kParameters[934],
/* return matcher indices */ &kMatcherIndices[1],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::acos,
},
{
/* [429] */
/* num parameters */ 1,
/* num template types */ 1,
/* num template numbers */ 1,
/* template types */ &kTemplateTypes[22],
/* template types */ &kTemplateTypes[23],
/* template numbers */ &kTemplateNumbers[6],
/* parameters */ &kParameters[946],
/* return matcher indices */ &kMatcherIndices[30],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::acos,
},
{
/* [430] */
@@ -14020,8 +14020,8 @@ constexpr IntrinsicInfo kBuiltins[] = {
},
{
/* [1] */
/* fn acos<T : f32_f16>(T) -> T */
/* fn acos<N : num, T : f32_f16>(vec<N, T>) -> vec<N, T> */
/* fn acos<T : fa_f32_f16>(@test_value(0.87758256189) T) -> T */
/* fn acos<N : num, T : fa_f32_f16>(@test_value(0.87758256189) vec<N, T>) -> vec<N, T> */
/* num overloads */ 2,
/* overloads */ &kOverloads[428],
},