tint: const eval of countTrailingZeros

Bug: tint:1581
Change-Id: I2967ea1d6f3f297c03795bddd0e26abcccd57184
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/107700
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Antonio Maiorano
2022-10-29 14:02:38 +00:00
parent 2be5167e3e
commit 7494e10f91
45 changed files with 309 additions and 1415 deletions

View File

@@ -437,8 +437,8 @@ fn cosh<N: num, T: f32_f16>(vec<N, T>) -> vec<N, T>
@const fn countLeadingZeros<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
fn countOneBits<T: iu32>(T) -> T
fn countOneBits<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
fn countTrailingZeros<T: iu32>(T) -> T
fn countTrailingZeros<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
@const fn countTrailingZeros<T: iu32>(T) -> T
@const fn countTrailingZeros<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
fn cross<T: 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>

View File

@@ -1651,6 +1651,31 @@ ConstEval::Result ConstEval::countLeadingZeros(const sem::Type* ty,
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::countTrailingZeros(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
auto transform = [&](const sem::Constant* c0) {
auto create = [&](auto e) {
using NumberT = decltype(e);
using T = UnwrapNumber<NumberT>;
using UT = std::make_unsigned_t<T>;
constexpr UT kNumBits = sizeof(UT) * 8;
constexpr UT kRightMost = UT{1};
auto v = static_cast<UT>(e);
auto count = UT{0};
while ((count < kNumBits) && ((v & kRightMost) == 0)) {
++count;
v >>= 1;
}
return CreateElement(builder, c0->Type(), NumberT(count));
};
return Dispatch_iu32(create, c0);
};
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::saturate(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {

View File

@@ -440,6 +440,15 @@ class ConstEval {
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// countTrailingZeros 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 countTrailingZeros(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// saturate builtin
/// @param ty the expression type
/// @param args the input arguments

View File

@@ -506,6 +506,52 @@ INSTANTIATE_TEST_SUITE_P( //
testing::ValuesIn(Concat(CountLeadingZerosCases<i32>(), //
CountLeadingZerosCases<u32>()))));
template <typename T>
std::vector<Case> CountTrailingZerosCases() {
using B = BitValues<T>;
return {
C({B::Lsh(1, 31)}, T(31)), //
C({B::Lsh(1, 30)}, T(30)), //
C({B::Lsh(1, 29)}, T(29)), //
C({B::Lsh(1, 28)}, T(28)),
//...
C({B::Lsh(1, 3)}, T(3)), //
C({B::Lsh(1, 2)}, T(2)), //
C({B::Lsh(1, 1)}, T(1)), //
C({B::Lsh(1, 0)}, T(0)),
C({T(0b0000'1111'0000'1111'0000'1111'0000'1111)}, T(0)),
C({T(0b0001'1110'0001'1110'0001'1110'0001'1110)}, T(1)),
C({T(0b0011'1100'0011'1100'0011'1100'0011'1100)}, T(2)),
C({T(0b0111'1000'0111'1000'0111'1000'0111'1000)}, T(3)),
//...
C({T(0b1110'0000'0000'0000'0000'0000'0000'0000)}, T(29)),
C({T(0b1100'0000'0000'0000'0000'0000'0000'0000)}, T(30)),
C({T(0b1000'0000'0000'0000'0000'0000'0000'0000)}, T(31)),
C({T(0b0000'0000'0000'0000'0000'0000'0000'0000)}, T(32)),
//// Same as above, but remove trailing 0
C({T(0b0001'1110'0001'1110'0001'1110'0001'1111)}, T(0)),
C({T(0b0011'1100'0011'1100'0011'1100'0011'1101)}, T(0)),
C({T(0b0111'1000'0111'1000'0111'1000'0111'1001)}, T(0)),
//...
C({T(0b1110'0000'0000'0000'0000'0000'0000'0001)}, T(0)),
C({T(0b1100'0000'0000'0000'0000'0000'0000'0001)}, T(0)),
C({T(0b1000'0000'0000'0000'0000'0000'0000'0001)}, T(0)),
C({T(0b0000'0000'0000'0000'0000'0000'0000'0001)}, T(0)),
// Vector tests
C({Vec(B::Lsh(1, 31), B::Lsh(1, 30), B::Lsh(1, 29))}, Vec(T(31), T(30), T(29))),
C({Vec(B::Lsh(1, 2), B::Lsh(1, 1), B::Lsh(1, 0))}, Vec(T(2), T(1), T(0))),
};
}
INSTANTIATE_TEST_SUITE_P( //
CountTrailingZeros,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kCountTrailingZeros),
testing::ValuesIn(Concat(CountTrailingZerosCases<i32>(), //
CountTrailingZerosCases<u32>()))));
template <typename T>
std::vector<Case> SaturateCases() {
return {

View File

@@ -12940,7 +12940,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[931],
/* return matcher indices */ &kMatcherIndices[1],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::countTrailingZeros,
},
{
/* [385] */
@@ -12952,7 +12952,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[930],
/* return matcher indices */ &kMatcherIndices[30],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::countTrailingZeros,
},
{
/* [386] */