mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-09 21:47:47 +00:00
Add const-eval for degrees and radians
This CL adds const-eval for `degrees` and `radians`. Bug: tint:1581 Change-Id: I7f00e2b1e5ab7c8e895680a6b75b9531dac31f5a Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/110601 Commit-Queue: Dan Sinclair <dsinclair@chromium.org> Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
d114055e4e
commit
efe9c49819
@@ -440,8 +440,8 @@ fn arrayLength<T, A: access>(ptr<storage, array<T>, A>) -> u32
|
||||
@const fn countTrailingZeros<T: iu32>(T) -> T
|
||||
@const fn countTrailingZeros<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
|
||||
@const fn cross<T: fa_f32_f16>(vec3<T>, vec3<T>) -> vec3<T>
|
||||
fn degrees<T: f32_f16>(T) -> T
|
||||
fn degrees<N: num, T: f32_f16>(vec<N, T>) -> vec<N, T>
|
||||
@const fn degrees<T: fa_f32_f16>(T) -> T
|
||||
@const fn degrees<N: num, T: fa_f32_f16>(vec<N, T>) -> vec<N, T>
|
||||
fn determinant<N: num, T: f32_f16>(mat<N, N, T>) -> T
|
||||
fn distance<T: f32_f16>(T, T) -> T
|
||||
fn distance<N: num, T: f32_f16>(vec<N, T>, vec<N, T>) -> T
|
||||
@@ -516,8 +516,8 @@ fn pow<T: f32_f16>(T, T) -> T
|
||||
fn pow<N: num, T: f32_f16>(vec<N, T>, vec<N, T>) -> vec<N, T>
|
||||
@const fn quantizeToF16(f32) -> f32
|
||||
@const fn quantizeToF16<N: num>(vec<N, f32>) -> vec<N, f32>
|
||||
fn radians<T: f32_f16>(T) -> T
|
||||
fn radians<N: num, T: f32_f16>(vec<N, T>) -> vec<N, T>
|
||||
@const fn radians<T: fa_f32_f16>(T) -> T
|
||||
@const fn radians<N: num, T: fa_f32_f16>(vec<N, T>) -> vec<N, T>
|
||||
fn reflect<N: num, T: f32_f16>(vec<N, T>, vec<N, T>) -> vec<N, T>
|
||||
fn refract<N: num, T: f32_f16>(vec<N, T>, vec<N, T>, T) -> vec<N, T>
|
||||
@const fn reverseBits<T: iu32>(T) -> T
|
||||
|
||||
@@ -274,6 +274,9 @@ using f32 = Number<float>;
|
||||
/// However since C++ don't have native binary16 type, the value is stored as float.
|
||||
using f16 = Number<detail::NumberKindF16>;
|
||||
|
||||
template <typename T, traits::EnableIf<IsFloatingPoint<T>>* = nullptr>
|
||||
inline const auto kPi = T(UnwrapNumber<T>(3.14159265358979323846));
|
||||
|
||||
/// True iff T is an abstract number type
|
||||
template <typename T>
|
||||
constexpr bool IsAbstract = std::is_same_v<T, AInt> || std::is_same_v<T, AFloat>;
|
||||
|
||||
@@ -1954,6 +1954,32 @@ ConstEval::Result ConstEval::cross(const sem::Type* ty,
|
||||
utils::Vector<const sem::Constant*, 3>{x.Get(), y.Get(), z.Get()});
|
||||
}
|
||||
|
||||
ConstEval::Result ConstEval::degrees(const sem::Type* ty,
|
||||
utils::VectorRef<const sem::Constant*> args,
|
||||
const Source& source) {
|
||||
auto transform = [&](const sem::Constant* c0) {
|
||||
auto create = [&](auto e) -> ImplResult {
|
||||
using NumberT = decltype(e);
|
||||
using T = UnwrapNumber<NumberT>;
|
||||
|
||||
auto pi = kPi<T>;
|
||||
auto scale = Div(source, NumberT(180), NumberT(pi));
|
||||
if (!scale) {
|
||||
AddNote("when calculating degrees", source);
|
||||
return utils::Failure;
|
||||
}
|
||||
auto result = Mul(source, e, scale.Get());
|
||||
if (!result) {
|
||||
AddNote("when calculating degrees", source);
|
||||
return utils::Failure;
|
||||
}
|
||||
return CreateElement(builder, source, c0->Type(), result.Get());
|
||||
};
|
||||
return Dispatch_fa_f32_f16(create, c0);
|
||||
};
|
||||
return TransformElements(builder, ty, transform, args[0]);
|
||||
}
|
||||
|
||||
ConstEval::Result ConstEval::extractBits(const sem::Type* ty,
|
||||
utils::VectorRef<const sem::Constant*> args,
|
||||
const Source& source) {
|
||||
@@ -2267,6 +2293,32 @@ ConstEval::Result ConstEval::pack4x8unorm(const sem::Type* ty,
|
||||
return CreateElement(builder, source, ty, ret);
|
||||
}
|
||||
|
||||
ConstEval::Result ConstEval::radians(const sem::Type* ty,
|
||||
utils::VectorRef<const sem::Constant*> args,
|
||||
const Source& source) {
|
||||
auto transform = [&](const sem::Constant* c0) {
|
||||
auto create = [&](auto e) -> ImplResult {
|
||||
using NumberT = decltype(e);
|
||||
using T = UnwrapNumber<NumberT>;
|
||||
|
||||
auto pi = kPi<T>;
|
||||
auto scale = Div(source, NumberT(pi), NumberT(180));
|
||||
if (!scale) {
|
||||
AddNote("when calculating radians", source);
|
||||
return utils::Failure;
|
||||
}
|
||||
auto result = Mul(source, e, scale.Get());
|
||||
if (!result) {
|
||||
AddNote("when calculating radians", source);
|
||||
return utils::Failure;
|
||||
}
|
||||
return CreateElement(builder, source, c0->Type(), result.Get());
|
||||
};
|
||||
return Dispatch_fa_f32_f16(create, c0);
|
||||
};
|
||||
return TransformElements(builder, ty, transform, args[0]);
|
||||
}
|
||||
|
||||
ConstEval::Result ConstEval::reverseBits(const sem::Type* ty,
|
||||
utils::VectorRef<const sem::Constant*> args,
|
||||
const Source& source) {
|
||||
|
||||
@@ -539,6 +539,15 @@ class ConstEval {
|
||||
utils::VectorRef<const sem::Constant*> args,
|
||||
const Source& source);
|
||||
|
||||
/// degrees 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 degrees(const sem::Type* ty,
|
||||
utils::VectorRef<const sem::Constant*> args,
|
||||
const Source& source);
|
||||
|
||||
/// extractBits builtin
|
||||
/// @param ty the expression type
|
||||
/// @param args the input arguments
|
||||
@@ -647,6 +656,15 @@ class ConstEval {
|
||||
utils::VectorRef<const sem::Constant*> args,
|
||||
const Source& source);
|
||||
|
||||
/// radians 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 radians(const sem::Type* ty,
|
||||
utils::VectorRef<const sem::Constant*> args,
|
||||
const Source& source);
|
||||
|
||||
/// reverseBits builtin
|
||||
/// @param ty the expression type
|
||||
/// @param args the input arguments
|
||||
|
||||
@@ -993,6 +993,57 @@ INSTANTIATE_TEST_SUITE_P( //
|
||||
testing::ValuesIn(Concat(InsertBitsCases<i32>(), //
|
||||
InsertBitsCases<u32>()))));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> DegreesAFloatCases() {
|
||||
return std::vector<Case>{
|
||||
C({T(0)}, T(0)), //
|
||||
C({-T(0)}, -T(0)), //
|
||||
C({T(0.698132)}, T(40)).FloatComp(), //
|
||||
C({-T(1.5708)}, -T(90.000214)).FloatComp(), //
|
||||
C({T(1.5708)}, T(90.000214)).FloatComp(), //
|
||||
C({T(6.28319)}, T(360.00027)).FloatComp(),
|
||||
};
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P( //
|
||||
DegreesAFloat,
|
||||
ResolverConstEvalBuiltinTest,
|
||||
testing::Combine(testing::Values(sem::BuiltinType::kDegrees),
|
||||
testing::ValuesIn(DegreesAFloatCases<AFloat>())));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> DegreesF32Cases() {
|
||||
return std::vector<Case>{
|
||||
C({T(0)}, T(0)), //
|
||||
C({-T(0)}, -T(0)), //
|
||||
C({T(0.698132)}, T(40)).FloatComp(), //
|
||||
C({-T(1.5708)}, -T(90.000206)).FloatComp(), //
|
||||
C({T(1.5708)}, T(90.000206)).FloatComp(), //
|
||||
C({T(6.28319)}, T(360.00024)).FloatComp(),
|
||||
};
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P( //
|
||||
DegreesF32,
|
||||
ResolverConstEvalBuiltinTest,
|
||||
testing::Combine(testing::Values(sem::BuiltinType::kDegrees),
|
||||
testing::ValuesIn(DegreesF32Cases<f32>())));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> DegreesF16Cases() {
|
||||
return std::vector<Case>{
|
||||
C({T(0)}, T(0)), //
|
||||
C({-T(0)}, -T(0)), //
|
||||
C({T(0.698132)}, T(39.96875)).FloatComp(), //
|
||||
C({-T(1.5708)}, -T(89.9375)).FloatComp(), //
|
||||
C({T(1.5708)}, T(89.9375)).FloatComp(), //
|
||||
C({T(6.28319)}, T(359.75)).FloatComp(),
|
||||
};
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P( //
|
||||
DegreesF16,
|
||||
ResolverConstEvalBuiltinTest,
|
||||
testing::Combine(testing::Values(sem::BuiltinType::kDegrees),
|
||||
testing::ValuesIn(DegreesF16Cases<f16>())));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> ExtractBitsCases() {
|
||||
using UT = Number<std::make_unsigned_t<UnwrapNumber<T>>>;
|
||||
@@ -1275,6 +1326,41 @@ INSTANTIATE_TEST_SUITE_P( //
|
||||
testing::ValuesIn(Concat(ReverseBitsCases<i32>(), //
|
||||
ReverseBitsCases<u32>()))));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> RadiansCases() {
|
||||
return std::vector<Case>{
|
||||
C({T(0)}, T(0)), //
|
||||
C({-T(0)}, -T(0)), //
|
||||
C({T(40)}, T(0.69813168)).FloatComp(), //
|
||||
C({-T(90)}, -T(1.5707964)).FloatComp(), //
|
||||
C({T(90)}, T(1.5707964)).FloatComp(), //
|
||||
C({T(360)}, T(6.2831855)).FloatComp(),
|
||||
};
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P( //
|
||||
Radians,
|
||||
ResolverConstEvalBuiltinTest,
|
||||
testing::Combine(testing::Values(sem::BuiltinType::kRadians),
|
||||
testing::ValuesIn(Concat(RadiansCases<AFloat>(), //
|
||||
RadiansCases<f32>()))));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> RadiansF16Cases() {
|
||||
return std::vector<Case>{
|
||||
C({T(0)}, T(0)), //
|
||||
C({-T(0)}, -T(0)), //
|
||||
C({T(40)}, T(0.69726562)).FloatComp(), //
|
||||
C({-T(90)}, -T(1.5693359)).FloatComp(), //
|
||||
C({T(90)}, T(1.5693359)).FloatComp(), //
|
||||
C({T(360)}, T(6.2773438)).FloatComp(),
|
||||
};
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P( //
|
||||
RadiansF16,
|
||||
ResolverConstEvalBuiltinTest,
|
||||
testing::Combine(testing::Values(sem::BuiltinType::kRadians),
|
||||
testing::ValuesIn(RadiansF16Cases<f16>())));
|
||||
|
||||
template <typename T>
|
||||
std::vector<Case> RoundCases() {
|
||||
std::vector<Case> cases = {
|
||||
|
||||
@@ -26,9 +26,6 @@
|
||||
|
||||
namespace tint::resolver {
|
||||
|
||||
template <typename T>
|
||||
inline const auto kPi = T(UnwrapNumber<T>(3.14159265358979323846));
|
||||
|
||||
template <typename T>
|
||||
inline const auto kPiOver2 = T(UnwrapNumber<T>(1.57079632679489661923));
|
||||
|
||||
|
||||
@@ -12174,24 +12174,24 @@ constexpr OverloadInfo kOverloads[] = {
|
||||
/* num parameters */ 1,
|
||||
/* num template types */ 1,
|
||||
/* num template numbers */ 0,
|
||||
/* template types */ &kTemplateTypes[26],
|
||||
/* template types */ &kTemplateTypes[23],
|
||||
/* template numbers */ &kTemplateNumbers[10],
|
||||
/* parameters */ &kParameters[835],
|
||||
/* return matcher indices */ &kMatcherIndices[3],
|
||||
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
|
||||
/* const eval */ nullptr,
|
||||
/* const eval */ &ConstEval::degrees,
|
||||
},
|
||||
{
|
||||
/* [322] */
|
||||
/* num parameters */ 1,
|
||||
/* num template types */ 1,
|
||||
/* num template numbers */ 1,
|
||||
/* template types */ &kTemplateTypes[26],
|
||||
/* template types */ &kTemplateTypes[23],
|
||||
/* template numbers */ &kTemplateNumbers[4],
|
||||
/* parameters */ &kParameters[836],
|
||||
/* return matcher indices */ &kMatcherIndices[30],
|
||||
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
|
||||
/* const eval */ nullptr,
|
||||
/* const eval */ &ConstEval::degrees,
|
||||
},
|
||||
{
|
||||
/* [323] */
|
||||
@@ -12918,24 +12918,24 @@ constexpr OverloadInfo kOverloads[] = {
|
||||
/* num parameters */ 1,
|
||||
/* num template types */ 1,
|
||||
/* num template numbers */ 0,
|
||||
/* template types */ &kTemplateTypes[26],
|
||||
/* template types */ &kTemplateTypes[23],
|
||||
/* template numbers */ &kTemplateNumbers[10],
|
||||
/* parameters */ &kParameters[888],
|
||||
/* return matcher indices */ &kMatcherIndices[3],
|
||||
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
|
||||
/* const eval */ nullptr,
|
||||
/* const eval */ &ConstEval::radians,
|
||||
},
|
||||
{
|
||||
/* [384] */
|
||||
/* num parameters */ 1,
|
||||
/* num template types */ 1,
|
||||
/* num template numbers */ 1,
|
||||
/* template types */ &kTemplateTypes[26],
|
||||
/* template types */ &kTemplateTypes[23],
|
||||
/* template numbers */ &kTemplateNumbers[4],
|
||||
/* parameters */ &kParameters[889],
|
||||
/* return matcher indices */ &kMatcherIndices[30],
|
||||
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
|
||||
/* const eval */ nullptr,
|
||||
/* const eval */ &ConstEval::radians,
|
||||
},
|
||||
{
|
||||
/* [385] */
|
||||
@@ -14117,8 +14117,8 @@ constexpr IntrinsicInfo kBuiltins[] = {
|
||||
},
|
||||
{
|
||||
/* [19] */
|
||||
/* fn degrees<T : f32_f16>(T) -> T */
|
||||
/* fn degrees<N : num, T : f32_f16>(vec<N, T>) -> vec<N, T> */
|
||||
/* fn degrees<T : fa_f32_f16>(T) -> T */
|
||||
/* fn degrees<N : num, T : fa_f32_f16>(vec<N, T>) -> vec<N, T> */
|
||||
/* num overloads */ 2,
|
||||
/* overloads */ &kOverloads[321],
|
||||
},
|
||||
@@ -14408,8 +14408,8 @@ constexpr IntrinsicInfo kBuiltins[] = {
|
||||
},
|
||||
{
|
||||
/* [62] */
|
||||
/* fn radians<T : f32_f16>(T) -> T */
|
||||
/* fn radians<N : num, T : f32_f16>(vec<N, T>) -> vec<N, T> */
|
||||
/* fn radians<T : fa_f32_f16>(T) -> T */
|
||||
/* fn radians<N : num, T : fa_f32_f16>(vec<N, T>) -> vec<N, T> */
|
||||
/* num overloads */ 2,
|
||||
/* overloads */ &kOverloads[383],
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user