tint: const eval of reverseBits

Bug: tint:1581
Change-Id: I3dd1ea2c774f9fc0dff87b71f25375a469c3d05e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/108300
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Antonio Maiorano
2022-11-04 13:48:26 +00:00
committed by Dawn LUCI CQ
parent 23a35c8a26
commit 6fcc4f3a54
45 changed files with 274 additions and 200 deletions

View File

@@ -520,8 +520,8 @@ fn radians<T: f32_f16>(T) -> T
fn radians<N: num, T: 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>
fn reverseBits<T: iu32>(T) -> T
fn reverseBits<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
@const fn reverseBits<T: iu32>(T) -> T
@const fn reverseBits<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
fn round<T: f32_f16>(T) -> T
fn round<N: num, T: f32_f16>(vec<N, T>) -> vec<N, T>
@const fn saturate<T: fa_f32_f16>(@test_value(2) T) -> T

View File

@@ -1822,7 +1822,7 @@ ConstEval::Result ConstEval::extractBits(const sem::Type* ty,
// Only need to set other bits if bit at c - 1 of result is 1
if ((r & (UT{1} << (c - UT{1}))) != UT{0}) {
UT dst_mask = src_mask >> o;
r = r | (~UT{0} & ~dst_mask);
r |= (~UT{0} & ~dst_mask);
}
}
@@ -1956,9 +1956,9 @@ ConstEval::Result ConstEval::insertBits(const sem::Type* ty,
// newbits. Other bits of the result are copied from e.
UT from = newbits << o;
UT mask = ((UT{1} << c) - UT{1}) << UT{o};
auto r = e; // Start with 'e' as the result
r = r & ~mask; // Zero the bits in 'e' we're overwriting
r = r | (from & mask); // Overwrite from 'newbits' (shifted into position)
auto r = e; // Start with 'e' as the result
r &= ~mask; // Zero the bits in 'e' we're overwriting
r |= (from & mask); // Overwrite from 'newbits' (shifted into position)
result = NumberT{r};
}
@@ -1969,6 +1969,33 @@ ConstEval::Result ConstEval::insertBits(const sem::Type* ty,
return TransformElements(builder, ty, transform, args[0], args[1]);
}
ConstEval::Result ConstEval::reverseBits(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
auto transform = [&](const sem::Constant* c0) {
auto create = [&](auto in_e) -> ImplResult {
using NumberT = decltype(in_e);
using T = UnwrapNumber<NumberT>;
using UT = std::make_unsigned_t<T>;
constexpr UT kNumBits = sizeof(UT) * 8;
UT e = static_cast<UT>(in_e);
UT r = UT{0};
for (size_t s = 0; s < kNumBits; ++s) {
// Write source 's' bit to destination 'd' bit if 1
if (e & (UT{1} << s)) {
size_t d = kNumBits - s - 1;
r |= (UT{1} << d);
}
}
return CreateElement(builder, c0->Type(), NumberT{r});
};
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

@@ -509,8 +509,8 @@ class ConstEval {
/// @param source the source location of the conversion
/// @return the result value, or null if the value cannot be calculated
Result extractBits(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// firstLeadingBit builtin
/// @param ty the expression type
@@ -548,6 +548,15 @@ class ConstEval {
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// reverseBits 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 reverseBits(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

@@ -1106,6 +1106,52 @@ INSTANTIATE_TEST_SUITE_P(ExtractBits,
std::make_tuple(1000, 1000), //
std::make_tuple(u32::Highest(), u32::Highest())));
template <typename T>
std::vector<Case> ReverseBitsCases() {
using B = BitValues<T>;
return {
C({T(0)}, T(0)),
C({B::Lsh(1, 0)}, B::Lsh(1, 31)), //
C({B::Lsh(1, 1)}, B::Lsh(1, 30)), //
C({B::Lsh(1, 2)}, B::Lsh(1, 29)), //
C({B::Lsh(1, 3)}, B::Lsh(1, 28)), //
C({B::Lsh(1, 4)}, B::Lsh(1, 27)), //
//...
C({B::Lsh(1, 27)}, B::Lsh(1, 4)), //
C({B::Lsh(1, 28)}, B::Lsh(1, 3)), //
C({B::Lsh(1, 29)}, B::Lsh(1, 2)), //
C({B::Lsh(1, 30)}, B::Lsh(1, 1)), //
C({B::Lsh(1, 31)}, B::Lsh(1, 0)), //
C({/**/ T(0b00010001000100010000000000000000)},
/* */ T(0b00000000000000001000100010001000)),
C({/**/ T(0b00011000000110000000000000000000)},
/* */ T(0b00000000000000000001100000011000)),
C({/**/ T(0b00000100000000001111111111111111)},
/* */ T(0b11111111111111110000000000100000)),
C({/**/ T(0b10010101111000110000011111101010)},
/* */ T(0b01010111111000001100011110101001)),
// Vector tests
C({/**/ Vec(T(0b00010001000100010000000000000000), //
T(0b00011000000110000000000000000000), //
T(0b00000000000000001111111111111111))},
/* */ Vec(T(0b00000000000000001000100010001000), //
T(0b00000000000000000001100000011000), //
T(0b11111111111111110000000000000000))),
};
}
INSTANTIATE_TEST_SUITE_P( //
ReverseBits,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kReverseBits),
testing::ValuesIn(Concat(ReverseBitsCases<i32>(), //
ReverseBitsCases<u32>()))));
template <typename T>
std::vector<Case> SaturateCases() {
return {

View File

@@ -12398,7 +12398,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[869],
/* return matcher indices */ &kMatcherIndices[1],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::reverseBits,
},
{
/* [339] */
@@ -12410,7 +12410,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[868],
/* return matcher indices */ &kMatcherIndices[30],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::reverseBits,
},
{
/* [340] */