tint: const eval of countOneBits

Bug: tint:1581
Change-Id: I156cbd162010d28ec25b410220638e810fc34c98
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/107701
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano
2022-10-31 18:44:15 +00:00
committed by Dawn LUCI CQ
parent 68ed8d92d3
commit 76c21c070b
46 changed files with 250 additions and 196 deletions

View File

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

View File

@@ -1657,6 +1657,30 @@ ConstEval::Result ConstEval::countLeadingZeros(const sem::Type* ty,
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::countOneBits(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 kRightMost = UT{1};
auto count = UT{0};
for (auto v = static_cast<UT>(e); v != UT{0}; v >>= 1) {
if ((v & kRightMost) == 1) {
++count;
}
}
return CreateElement(builder, c0->Type(), NumberT(count));
};
return Dispatch_iu32(create, c0);
};
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::countTrailingZeros(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {

View File

@@ -449,14 +449,23 @@ class ConstEval {
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// countOneBits 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 countOneBits(const sem::Type* ty,
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);
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// saturate builtin
/// @param ty the expression type

View File

@@ -581,6 +581,43 @@ INSTANTIATE_TEST_SUITE_P( //
testing::ValuesIn(Concat(CountTrailingZerosCases<i32>(), //
CountTrailingZerosCases<u32>()))));
template <typename T>
std::vector<Case> CountOneBitsCases() {
using B = BitValues<T>;
return {
C({T(0)}, T(0)), //
C({B::Lsh(1, 31)}, T(1)), //
C({B::Lsh(1, 30)}, T(1)), //
C({B::Lsh(1, 29)}, T(1)), //
C({B::Lsh(1, 28)}, T(1)),
//...
C({B::Lsh(1, 3)}, T(1)), //
C({B::Lsh(1, 2)}, T(1)), //
C({B::Lsh(1, 1)}, T(1)), //
C({B::Lsh(1, 0)}, T(1)),
C({T(0b1010'1010'1010'1010'1010'1010'1010'1010)}, T(16)),
C({T(0b0000'1111'0000'1111'0000'1111'0000'1111)}, T(16)),
C({T(0b0101'0000'0000'0000'0000'0000'0000'0101)}, T(4)),
// Vector tests
C({Vec(B::Lsh(1, 31), B::Lsh(1, 30), B::Lsh(1, 29))}, Vec(T(1), T(1), T(1))),
C({Vec(B::Lsh(1, 2), B::Lsh(1, 1), B::Lsh(1, 0))}, Vec(T(1), T(1), T(1))),
C({Vec(T(0b1010'1010'1010'1010'1010'1010'1010'1010),
T(0b0000'1111'0000'1111'0000'1111'0000'1111),
T(0b0101'0000'0000'0000'0000'0000'0000'0101))},
Vec(T(16), T(16), T(4))),
};
}
INSTANTIATE_TEST_SUITE_P( //
CountOneBits,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kCountOneBits),
testing::ValuesIn(Concat(CountOneBitsCases<i32>(), //
CountOneBitsCases<u32>()))));
template <typename T>
std::vector<Case> SaturateCases() {
return {

View File

@@ -12964,7 +12964,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[944],
/* return matcher indices */ &kMatcherIndices[1],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::countOneBits,
},
{
/* [387] */
@@ -12976,7 +12976,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[932],
/* return matcher indices */ &kMatcherIndices[30],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::countOneBits,
},
{
/* [388] */