| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #pragma once |
|
|
| #include <cute/config.hpp> |
| #include <cute/container/tuple.hpp> |
| #include <cute/numeric/integral_constant.hpp> |
| #include <cute/numeric/math.hpp> |
| #include <cute/algorithm/tuple_algorithms.hpp> |
|
|
| namespace cute |
| { |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <int BBits, int MBase, int SShift = BBits> |
| struct Swizzle |
| { |
| static constexpr int num_bits = BBits; |
| static constexpr int num_base = MBase; |
| static constexpr int num_shft = SShift; |
|
|
| static_assert(num_base >= 0, "MBase must be positive."); |
| static_assert(num_bits >= 0, "BBits must be positive."); |
| static_assert(abs(num_shft) >= num_bits, "abs(SShift) must be more than BBits."); |
|
|
| |
| using bit_msk = cute::constant<int, (1 << num_bits) - 1>; |
| using yyy_msk = cute::constant<int, bit_msk{} << (num_base + max(0,num_shft))>; |
| using zzz_msk = cute::constant<int, bit_msk{} << (num_base - min(0,num_shft))>; |
| using msk_sft = cute::constant<int, num_shft>; |
|
|
| static constexpr uint32_t swizzle_code = uint32_t(yyy_msk::value | zzz_msk::value); |
|
|
| template <class Offset> |
| CUTE_HOST_DEVICE constexpr static |
| auto |
| apply(Offset const& offset) |
| { |
| return offset ^ shiftr(offset & yyy_msk{}, msk_sft{}); |
| } |
|
|
| template <class Offset> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator()(Offset const& offset) const |
| { |
| return apply(offset); |
| } |
|
|
| template <int B, int M, int S> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator==(Swizzle<B,M,S> const&) const |
| { |
| return B == BBits && M == MBase && S == SShift; |
| } |
| }; |
|
|
| |
| |
| |
| |
|
|
| template <uint32_t Y, uint32_t Z> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| make_swizzle() |
| { |
| constexpr uint32_t BZ = popcount(Y); |
| constexpr uint32_t BY = popcount(Z); |
| static_assert(BZ == BY, "Number of bits in Y and Z don't match"); |
| constexpr uint32_t TZ_Y = countr_zero(Y); |
| constexpr uint32_t TZ_Z = countr_zero(Z); |
| constexpr uint32_t M = cute::min(TZ_Y, TZ_Z) % 32; |
| constexpr int32_t S = int32_t(TZ_Y) - int32_t(TZ_Z); |
| static_assert((Y | Z) == Swizzle<BZ,M,S>::swizzle_code, "Something went wrong."); |
| return Swizzle<BZ,M,S>{}; |
| } |
|
|
| template <int B0, int M0, int S0, |
| int B1, int M1, int S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| composition(Swizzle<B0,M0,S0>, Swizzle<B1,M1,S1>) |
| { |
| static_assert(S0 == S1, "Can only merge swizzles of the same shift."); |
| constexpr uint32_t Y = Swizzle<B0,M0,S0>::yyy_msk::value ^ Swizzle<B1,M1,S1>::yyy_msk::value; |
| constexpr uint32_t Z = Swizzle<B0,M0,S0>::zzz_msk::value ^ Swizzle<B1,M1,S1>::zzz_msk::value; |
| return make_swizzle<Y,Z>(); |
|
|
| |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
|
|
| |
| template <uint32_t StaticInt, |
| uint32_t StaticFlags> |
| struct MixedBits |
| { |
| |
| static_assert(StaticFlags != 0, "Should be at least one dynamic bit in MixedBits."); |
| static_assert((StaticInt & StaticFlags) == 0, "No static/dynamic overlap allowed in MixedBits."); |
|
|
| uint32_t dynamic_int_; |
| |
|
|
| CUTE_HOST_DEVICE constexpr operator uint32_t() const noexcept { return StaticInt | dynamic_int_; } |
| }; |
|
|
| |
| |
| template <auto s, class DynamicType, auto f> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| make_mixed_bits(C<s>, DynamicType const& d, C<f>) |
| { |
| static_assert(is_integral<DynamicType>::value); |
| constexpr uint32_t new_f = uint32_t(f) & ~uint32_t(s); |
| if constexpr (new_f == 0 || is_static<DynamicType>::value) { |
| return C<s>{} | (d & C<new_f>{}); |
| } else { |
| return MixedBits<s, new_f>{uint32_t(d) & new_f}; |
| } |
|
|
| CUTE_GCC_UNREACHABLE; |
| } |
|
|
| |
| |
| |
|
|
| |
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator==(MixedBits<S0,F0> const& m, C<S1>) |
| { |
| return (S0 == (uint32_t(S1) & ~F0)) && (m.dynamic_int_ == (uint32_t(S1) & F0)); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator==(C<S1> s, MixedBits<S0,F0> const& m) |
| { |
| return m == s; |
| } |
|
|
| |
| template <uint32_t S0, uint32_t F0, |
| uint32_t S1, uint32_t F1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator&(MixedBits<S0,F0> const& m0, MixedBits<S1,F1> const& m1) |
| { |
| |
| |
| |
| |
| |
| |
| |
|
|
| return make_mixed_bits(C<S0 & S1>{}, |
| |
| ((S1 & F0) & m0.dynamic_int_) | ((S0 & F1) & m1.dynamic_int_) | (m0.dynamic_int_ & m1.dynamic_int_), |
| C<(S1 & F0) | (S0 & F1) | (F0 & F1)>{}); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator&(MixedBits<S0,F0> const& m, C<S1>) |
| { |
| return make_mixed_bits(C<S0 & uint32_t(S1)>{}, |
| m.dynamic_int_, |
| C<F0 & uint32_t(S1)>{}); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator&(C<S1> s, MixedBits<S0,F0> const& m) |
| { |
| return m & s; |
| } |
|
|
| |
| template <uint32_t S0, uint32_t F0, |
| uint32_t S1, uint32_t F1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator|(MixedBits<S0,F0> const& m0, MixedBits<S1,F1> const& m1) |
| { |
| |
| |
| |
| |
| |
| |
| |
|
|
| return make_mixed_bits(C<S0 | S1>{}, |
| ((~S1 & F0) & m0.dynamic_int_) | ((~S0 & F1) & m1.dynamic_int_), |
| C<(~S0 & F1) | (~S1 & F0)>{}); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator|(MixedBits<S0,F0> const& m, C<S1>) |
| { |
| return make_mixed_bits(C<S0 | uint32_t(S1)>{}, |
| m.dynamic_int_, |
| C<F0 & ~uint32_t(S1)>{}); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator|(C<S1> s, MixedBits<S0,F0> const& m) |
| { |
| return m | s; |
| } |
|
|
| |
| template <uint32_t S0, uint32_t F0, |
| uint32_t S1, uint32_t F1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator^(MixedBits<S0,F0> const& m0, MixedBits<S1,F1> const& m1) |
| { |
| |
| |
| |
| |
| |
| |
| |
|
|
| return make_mixed_bits(C<(~S0 & S1 & ~F0) | (S0 & ~S1 & ~F1)>{}, |
| (S0 | m0.dynamic_int_) ^ (S1 | m1.dynamic_int_), |
| C<F0 | F1>{}); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator^(MixedBits<S0,F0> const& m, C<S1>) |
| { |
| return make_mixed_bits(C<(~S0 & uint32_t(S1) & ~F0) | (S0 & ~uint32_t(S1))>{}, |
| (S0 | m.dynamic_int_) ^ uint32_t(S1), |
| C<F0>{}); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator^(C<S1> s, MixedBits<S0,F0> const& m) |
| { |
| return m ^ s; |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator<<(MixedBits<S0,F0> const& m, C<S1>) |
| { |
| return make_mixed_bits(C<(S0 << S1)>{}, |
| m.dynamic_int_ << S1, |
| C<(F0 << S1)>{}); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| operator>>(MixedBits<S0,F0> const& m, C<S1>) |
| { |
| return make_mixed_bits(C<(S0 >> S1)>{}, |
| m.dynamic_int_ >> S1, |
| C<(F0 >> S1)>{}); |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| shiftl(MixedBits<S0,F0> const& m, C<S1> s) |
| { |
| if constexpr (S1 >= 0) { |
| return m << s; |
| } else { |
| return m >> -s; |
| } |
| } |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| shiftr(MixedBits<S0,F0> const& m, C<S1> s) |
| { |
| if constexpr (S1 >= 0) { |
| return m >> s; |
| } else { |
| return m << -s; |
| } |
| } |
|
|
| |
| |
| |
|
|
| template <uint32_t S0, uint32_t F0, auto S1> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| safe_div(MixedBits<S0,F0> const& m, C<S1> s) |
| { |
| static_assert(has_single_bit(uint32_t(S1)), "Only divide MixedBits by powers of two."); |
| return make_mixed_bits(safe_div(C<S0>{}, s), |
| safe_div(m.dynamic_int_, s), |
| safe_div(C<F0>{}, s)); |
| } |
|
|
| template <uint32_t N, uint32_t S0, uint32_t F0> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| upcast(MixedBits<S0,F0> const& m) |
| { |
| static_assert(has_single_bit(N), "Only divide MixedBits by powers of two."); |
| return safe_div(m, C<N>{}); |
| } |
|
|
| template <uint32_t N, class T, __CUTE_REQUIRES(cute::is_integral<T>::value)> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| upcast(T const& m) |
| { |
| return safe_div(m, C<N>{}); |
| } |
|
|
| template <uint32_t N, uint32_t S0, uint32_t F0> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| downcast(MixedBits<S0,F0> const& m) |
| { |
| static_assert(has_single_bit(N), "Only scale MixedBits by powers of two."); |
| return make_mixed_bits(C<S0 * N>{}, |
| m.dynamic_int_ * N, |
| C<F0 * N>{}); |
| } |
|
|
| template <uint32_t N, class T, __CUTE_REQUIRES(cute::is_integral<T>::value)> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| downcast(T const& m) |
| { |
| return m * C<N>{}; |
| } |
|
|
| template <uint32_t S0, uint32_t F0> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| max_alignment(MixedBits<S0,F0> const&) |
| { |
| return C<uint32_t(1) << countr_zero(S0 | F0)>{}; |
| } |
|
|
| template <auto v> |
| CUTE_HOST_DEVICE constexpr |
| C<v> |
| max_alignment(C<v> const& c) |
| { |
| return c; |
| } |
|
|
| |
| |
| |
|
|
| template <class Shape, class Stride, class Coord> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| to_mixed_bits(Shape const& shape, Stride const& stride, Coord const& coord) |
| { |
| if constexpr (is_tuple<Shape>::value && is_tuple<Stride>::value && is_tuple<Coord>::value) { |
| static_assert(tuple_size<Shape>::value == tuple_size<Stride>::value, "Mismatched ranks"); |
| static_assert(tuple_size<Shape>::value == tuple_size<Coord >::value, "Mismatched ranks"); |
| return transform_apply(shape, stride, coord, [](auto const& s, auto const& d, auto const& c) { return to_mixed_bits(s,d,c); }, |
| [](auto const&... a) { return (a ^ ...); }); |
| } else if constexpr (is_integral<Shape>::value && is_integral<Stride>::value && is_integral<Coord>::value) { |
| static_assert(decltype(shape*stride)::value == 0 || has_single_bit(decltype(shape*stride)::value), "Requires pow2 shape*stride."); |
| return make_mixed_bits(Int<0>{}, coord * stride, (shape - Int<1>{}) * stride); |
| } else { |
| static_assert(is_integral<Shape>::value && is_integral<Stride>::value && is_integral<Coord>::value, "Either Shape, Stride, and Coord must be all tuples, or they must be all integral (in the sense of cute::is_integral)."); |
| } |
|
|
| CUTE_GCC_UNREACHABLE; |
| } |
|
|
| template <class Layout, class Coord> |
| CUTE_HOST_DEVICE constexpr |
| auto |
| to_mixed_bits(Layout const& layout, Coord const& coord) |
| { |
| return to_mixed_bits(layout.shape(), layout.stride(), idx2crd(coord, layout.shape())); |
| } |
|
|
| |
| |
| |
|
|
| template <int B, int M, int S> |
| CUTE_HOST_DEVICE void print(Swizzle<B,M,S> const&) |
| { |
| printf("Sw<%d,%d,%d>", B, M, S); |
| } |
|
|
| template <uint32_t S, uint32_t F> |
| CUTE_HOST_DEVICE void print(MixedBits<S,F> const& m) |
| { |
| printf("M_%u|(%u&%u)=%u", S, m.dynamic_int_, F, uint32_t(m)); |
| } |
|
|
| #if !defined(__CUDACC_RTC__) |
| template <int B, int M, int S> |
| CUTE_HOST std::ostream& operator<<(std::ostream& os, Swizzle<B,M,S> const&) |
| { |
| return os << "Sw<" << B << "," << M << "," << S << ">"; |
| } |
|
|
| template <uint32_t S, class D, uint32_t F> |
| CUTE_HOST std::ostream& operator<<(std::ostream& os, MixedBits<S,F> const& m) |
| { |
| return os << "M_" << S << "|(" << m.dynamic_int_ << "&" << F << ")=" << uint32_t(m); |
| } |
| #endif |
|
|
| |
| |
| |
| template <class T, class = void> |
| struct get_swizzle { using type = Swizzle<0,4,3>; }; |
|
|
| template <class T> |
| using get_swizzle_t = typename get_swizzle<T>::type; |
|
|
| } |
|
|