Add const-eval for asin and asinh.

This CL adds const-eval for the `asin` and `asinh` operators.

Bug: tint:1581
Change-Id: I18b2eeb4fb85b8979012b48551eefa773d1b980e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/106980
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair
2022-10-27 15:10:20 +00:00
committed by Dawn LUCI CQ
parent a92f4259d5
commit 59d9c89ff7
180 changed files with 9532 additions and 5064 deletions

View File

@@ -415,10 +415,10 @@ fn all<N: num>(vec<N, bool>) -> bool
fn any(bool) -> bool
fn any<N: num>(vec<N, bool>) -> bool
fn arrayLength<T, A: access>(ptr<storage, array<T>, A>) -> u32
fn asin<T: f32_f16>(T) -> T
fn asin<N: num, T: f32_f16>(vec<N, T>) -> vec<N, T>
fn asinh<T: f32_f16>(T) -> T
fn asinh<N: num, T: f32_f16>(vec<N, T>) -> vec<N, T>
@const fn asin<T: fa_f32_f16>(T) -> T
@const fn asin<N: num, T: fa_f32_f16>(vec<N, T>) -> vec<N, T>
@const fn asinh<T: fa_f32_f16>(T) -> T
@const fn asinh<N: num, T: fa_f32_f16>(vec<N, T>) -> vec<N, T>
@const fn atan<T: fa_f32_f16>(T) -> T
@const fn atan<N: num, T: fa_f32_f16>(vec<N, T>) -> vec<N, T>
@const fn atan2<T: fa_f32_f16>(T, T) -> T

View File

@@ -1526,6 +1526,40 @@ ConstEval::Result ConstEval::OpShiftLeft(const sem::Type* ty,
return r;
}
ConstEval::Result ConstEval::asin(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.value < NumberT(-1.0) || i.value > 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 Dispatch_fa_f32_f16(create, c0);
};
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::asinh(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
auto transform = [&](const sem::Constant* c0) {
auto create = [&](auto i) {
return CreateElement(builder, c0->Type(), decltype(i)(std::asinh(i.value)));
};
return Dispatch_fa_f32_f16(create, c0);
};
auto r = TransformElements(builder, ty, transform, args[0]);
if (builder.Diagnostics().contains_errors()) {
return utils::Failure;
}
return r;
}
ConstEval::Result ConstEval::atan(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {

View File

@@ -377,6 +377,24 @@ class ConstEval {
// Builtins
////////////////////////////////////////////////////////////////////////////
/// asin 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 asin(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// asinh 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 asinh(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// atan builtin
/// @param ty the expression type
/// @param args the input arguments

View File

@@ -256,6 +256,7 @@ INSTANTIATE_TEST_SUITE_P( //
testing::ValuesIn(Concat(AtanCases<AFloat, true>(), //
AtanCases<f32, false>(),
AtanCases<f16, false>()))));
template <typename T, bool finite_only>
std::vector<Case> AtanhCases() {
std::vector<Case> cases = {
@@ -296,7 +297,7 @@ TEST_F(ResolverConstEvalBuiltinTest, Atanh_OutsideRange_Positive) {
}
TEST_F(ResolverConstEvalBuiltinTest, Atanh_OutsideRange_Negative) {
auto* expr = Call(Source{{12, 24}}, "atanh", Expr(-1.0_a));
auto* expr = Call(Source{{12, 24}}, "atanh", Negation(1.0_a));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
@@ -312,13 +313,118 @@ TEST_F(ResolverConstEvalBuiltinTest, Atanh_OutsideRange_Positive_INF) {
}
TEST_F(ResolverConstEvalBuiltinTest, Atanh_OutsideRange_Negative_INF) {
auto* expr = Call(Source{{12, 24}}, "atanh", Expr(-f32::Inf()));
auto* expr = Call(Source{{12, 24}}, "atanh", Negation(f32::Inf()));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
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> AsinCases() {
std::vector<Case> cases = {
// If i is +/-0, +/-0 is returned
C({T(0.0)}, T(0.0)),
C({-T(0.0)}, -T(0.0)),
C({T(1.0)}, kPiOver2<T>).FloatComp(),
C({-T(1.0)}, -kPiOver2<T>).FloatComp(),
// Vector tests
C({Vec(T(0.0), T(1.0), -T(1.0))}, Vec(T(0.0), kPiOver2<T>, -kPiOver2<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())).FloatComp(),
});
return cases;
}
INSTANTIATE_TEST_SUITE_P( //
Asin,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kAsin),
testing::ValuesIn(Concat(AsinCases<AFloat, true>(), //
AsinCases<f32, false>(),
AsinCases<f16, false>()))));
TEST_F(ResolverConstEvalBuiltinTest, Asin_OutsideRange_Positive) {
auto* expr = Call(Source{{12, 24}}, "asin", Expr(1.1_a));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:24 error: asin must be called with a value in the range [-1, 1]");
}
TEST_F(ResolverConstEvalBuiltinTest, Asin_OutsideRange_Negative) {
auto* expr = Call(Source{{12, 24}}, "asin", Negation(1.1_a));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:24 error: asin must be called with a value in the range [-1, 1]");
}
TEST_F(ResolverConstEvalBuiltinTest, Asin_OutsideRange_Positive_INF) {
auto* expr = Call(Source{{12, 24}}, "asin", Expr(f32::Inf()));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:24 error: asin must be called with a value in the range [-1, 1]");
}
TEST_F(ResolverConstEvalBuiltinTest, Asin_OutsideRange_Negative_INF) {
auto* expr = Call(Source{{12, 24}}, "asin", Negation(f32::Inf()));
GlobalConst("C", expr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:24 error: asin must be called with a value in the range [-1, 1]");
}
template <typename T, bool finite_only>
std::vector<Case> AsinhCases() {
std::vector<Case> cases = {
// If i is +/-0, +/-0 is returned
C({T(0.0)}, T(0.0)),
C({-T(0.0)}, -T(0.0)),
C({T(0.9)}, T(0.80886693565278)).FloatComp(),
C({-T(2.0)}, -T(1.4436354751788)).FloatComp(),
// Vector tests
C({Vec(T(0.0), T(0.9), -T(2.0))}, //
Vec(T(0.0), T(0.8088669356278), -T(1.4436354751788)))
.FloatComp(),
};
ConcatIntoIf<!finite_only>( //
cases, std::vector<Case>{
// If i is +/- INF, +/-INF is returned
C({T::Inf()}, T::Inf()),
C({-T::Inf()}, -T::Inf()),
// If i is NaN, NaN is returned
C({T::NaN()}, T::NaN()),
// Vector tests
C({Vec(T::Inf(), T::NaN(), -T::Inf())}, //
Vec(T::Inf(), T::NaN(), -T::Inf())),
});
return cases;
}
INSTANTIATE_TEST_SUITE_P( //
Asinh,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kAsinh),
testing::ValuesIn(Concat(AsinhCases<AFloat, true>(), //
AsinhCases<f32, false>(),
AsinhCases<f16, false>()))));
template <typename T>
std::vector<Case> ClampCases() {
return {

File diff suppressed because it is too large Load Diff

View File

@@ -167,7 +167,7 @@ DataMap polyfillSinh() {
TEST_F(BuiltinPolyfillTest, ShouldRunAsinh) {
auto* src = R"(
fn f() {
asinh(1.0);
asinh(1.0f);
}
)";
@@ -178,7 +178,7 @@ fn f() {
TEST_F(BuiltinPolyfillTest, Asinh_f32) {
auto* src = R"(
fn f() {
let r : f32 = asinh(1234);
let r : f32 = asinh(1234f);
}
)";
@@ -188,7 +188,7 @@ fn tint_sinh(x : f32) -> f32 {
}
fn f() {
let r : f32 = tint_sinh(1234);
let r : f32 = tint_sinh(1234.0f);
}
)";
@@ -200,7 +200,7 @@ fn f() {
TEST_F(BuiltinPolyfillTest, Asinh_vec3_f32) {
auto* src = R"(
fn f() {
let r : vec3<f32> = asinh(vec3<f32>(1234));
let r : vec3<f32> = asinh(vec3<f32>(1234f));
}
)";
@@ -210,7 +210,7 @@ fn tint_sinh(x : vec3<f32>) -> vec3<f32> {
}
fn f() {
let r : vec3<f32> = tint_sinh(vec3<f32>(1234));
let r : vec3<f32> = tint_sinh(vec3<f32>(1234.0f));
}
)";

View File

@@ -92,14 +92,15 @@ TEST_P(GlslImportData_SingleVectorParamTest, FloatVector) {
auto param = GetParam();
auto* ident = Expr(param.name);
auto* expr = Call(ident, vec3<f32>(1_f, 2_f, 3_f));
auto* expr = Call(ident, vec3<f32>(0.1_f, 0.2_f, 0.3_f));
WrapInFunction(expr);
GeneratorImpl& gen = Build();
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.glsl_name) + "(vec3(1.0f, 2.0f, 3.0f))");
EXPECT_EQ(out.str(),
std::string(param.glsl_name) + "(vec3(0.100000001f, 0.200000003f, 0.300000012f))");
}
INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
GlslImportData_SingleVectorParamTest,

View File

@@ -92,14 +92,15 @@ TEST_P(HlslImportData_SingleVectorParamTest, FloatVector) {
auto param = GetParam();
auto* ident = Expr(param.name);
auto* expr = Call(ident, vec3<f32>(1_f, 2_f, 3_f));
auto* expr = Call(ident, vec3<f32>(0.1_f, 0.2_f, 0.3_f));
WrapInFunction(expr);
GeneratorImpl& gen = Build();
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.hlsl_name) + "(float3(1.0f, 2.0f, 3.0f))");
EXPECT_EQ(out.str(),
std::string(param.hlsl_name) + "(float3(0.100000001f, 0.200000003f, 0.300000012f))");
}
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
HlslImportData_SingleVectorParamTest,