From a2e2cf539e36e9a3326800194ad5206a8ef3f5b7 Mon Sep 17 00:00:00 2001 From: jtlap Date: Tue, 30 Apr 2024 09:36:15 +0200 Subject: [PATCH] New style for more bit operations --- include/eve/module/core/regular/bit_not.hpp | 33 +++++++++++++-- .../module/core/regular/bit_swap_pairs.hpp | 41 +++++++++++++++++-- include/eve/module/core/regular/bit_width.hpp | 30 ++++++++++++-- include/eve/module/core/regular/bitofsign.hpp | 31 ++++++++++++-- .../core/regular/byte_swap_adjacent.hpp | 32 +++++++++++++-- .../eve/module/core/regular/impl/bit_not.hpp | 35 ---------------- .../core/regular/impl/bit_swap_pairs.hpp | 39 ------------------ include/eve/module/core/regular/reldist.hpp | 31 ++++++++++++-- test/unit/module/core/bit_not.cpp | 3 +- test/unit/module/core/bitofsign.cpp | 1 - 10 files changed, 175 insertions(+), 101 deletions(-) delete mode 100644 include/eve/module/core/regular/impl/bit_not.hpp delete mode 100644 include/eve/module/core/regular/impl/bit_swap_pairs.hpp diff --git a/include/eve/module/core/regular/bit_not.hpp b/include/eve/module/core/regular/bit_not.hpp index e2a72c334e..2016061681 100644 --- a/include/eve/module/core/regular/bit_not.hpp +++ b/include/eve/module/core/regular/bit_not.hpp @@ -7,10 +7,25 @@ //================================================================================================== #pragma once -#include +#include +#include +#include +#include +#include namespace eve { + + template + struct bit_not_t : elementwise_callable + { + template + constexpr EVE_FORCEINLINE T operator()(T v) const noexcept + { return EVE_DISPATCH_CALL(v); } + + EVE_CALLABLE_OBJECT(bit_not_t, bit_not_); + }; + //================================================================================================ //! @addtogroup core_bitops //! @{ @@ -55,7 +70,17 @@ namespace eve //! //! @} //================================================================================================ -EVE_MAKE_CALLABLE(bit_not_, bit_not); + inline constexpr auto bit_not = functor; + + namespace detail + { + template + constexpr T bit_not_(EVE_REQUIRES(cpu_), O const&, T const& v) noexcept + { + if constexpr( floating_scalar_value ) + return bit_cast(~bit_cast(v, as> {}), as(v)); + else + return T(~v); + } + } } - -#include diff --git a/include/eve/module/core/regular/bit_swap_pairs.hpp b/include/eve/module/core/regular/bit_swap_pairs.hpp index 220ca5baa7..639c284cdf 100644 --- a/include/eve/module/core/regular/bit_swap_pairs.hpp +++ b/include/eve/module/core/regular/bit_swap_pairs.hpp @@ -7,10 +7,30 @@ //================================================================================================== #pragma once -#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace eve { + + template + struct bit_swap_pairs_t : strict_elementwise_callable + { + template + constexpr EVE_FORCEINLINE T operator()(T v, I0 i0, I1 i1) const noexcept + { return EVE_DISPATCH_CALL(v, i0, i1); } + + EVE_CALLABLE_OBJECT(bit_swap_pairs_t, bit_swap_pairs_); + }; + //================================================================================================ //! @addtogroup core_bitops //! @{ @@ -58,7 +78,20 @@ namespace eve //! //! @} //================================================================================================ -EVE_MAKE_CALLABLE(bit_swap_pairs_, bit_swap_pairs); -} +inline constexpr auto bit_swap_pairs = functor; + + namespace detail + { -#include + template + constexpr T bit_swap_pairs_(EVE_REQUIRES(cpu_), O const&, T a, I0 i0, I1 i1) noexcept + { + [[maybe_unused]] constexpr std::ptrdiff_t S8 = sizeof(element_type_t)*8; + EVE_ASSERT(eve::all(i0 < S8 && i1 < S8), "some indexes are out or range"); + auto x = bit_and(bit_xor(bit_shr(a, i0), bit_shr(a, i1)), one(as(a))); + a ^= bit_shl(x, i1); + a ^= bit_shl(x, i0); + return a; + } + } +} diff --git a/include/eve/module/core/regular/bit_width.hpp b/include/eve/module/core/regular/bit_width.hpp index d3c2df1adb..2c5dc92447 100644 --- a/include/eve/module/core/regular/bit_width.hpp +++ b/include/eve/module/core/regular/bit_width.hpp @@ -7,10 +7,24 @@ //================================================================================================== #pragma once -#include +#include +#include +#include +#include +#include namespace eve { + template + struct bit_width_t : elementwise_callable + { + template + constexpr EVE_FORCEINLINE T operator()(T v) const noexcept + { return EVE_DISPATCH_CALL(v); } + + EVE_CALLABLE_OBJECT(bit_width_t, bit_width_); + }; + //================================================================================================ //! @addtogroup core_bitops //! @{ @@ -48,7 +62,15 @@ namespace eve //! @godbolt{doc/core/bit_width.cpp} //! @} //================================================================================================ -EVE_MAKE_CALLABLE(bit_width_, bit_width); -} + inline constexpr auto bit_width = functor; -#include + namespace detail + { + template + constexpr T bit_width_(EVE_REQUIRES(cpu_), O const&, T const& v) noexcept + { + using elt_t = element_type_t; + return sizeof(elt_t) * 8 - countl_zero(v); + } + } +} diff --git a/include/eve/module/core/regular/bitofsign.hpp b/include/eve/module/core/regular/bitofsign.hpp index 0e1500eadd..8cc4290da4 100644 --- a/include/eve/module/core/regular/bitofsign.hpp +++ b/include/eve/module/core/regular/bitofsign.hpp @@ -7,10 +7,26 @@ //================================================================================================== #pragma once -#include +#include +#include +#include +#include +#include +#include namespace eve { + + template + struct bitofsign_t : elementwise_callable + { + template + constexpr EVE_FORCEINLINE T operator()(T v) const noexcept + { return EVE_DISPATCH_CALL(v); } + + EVE_CALLABLE_OBJECT(bitofsign_t, bitofsign_); + }; + //================================================================================================ //! @addtogroup core_internal //! @{ @@ -58,7 +74,14 @@ namespace eve //! @godbolt{doc/core/bitofsign.cpp} //! @} //================================================================================================ -EVE_MAKE_CALLABLE(bitofsign_, bitofsign); -} + inline constexpr auto bitofsign = functor; -#include + namespace detail + { + template + constexpr T bitofsign_(EVE_REQUIRES(cpu_), O const&, T const& a) noexcept + { + return bit_and(a, signmask(eve::as(a))); + } + } +} diff --git a/include/eve/module/core/regular/byte_swap_adjacent.hpp b/include/eve/module/core/regular/byte_swap_adjacent.hpp index 2906215e62..10833f6180 100644 --- a/include/eve/module/core/regular/byte_swap_adjacent.hpp +++ b/include/eve/module/core/regular/byte_swap_adjacent.hpp @@ -7,10 +7,24 @@ //================================================================================================== #pragma once -#include +#include +#include +#include +#include namespace eve { + + template + struct byte_swap_adjacent_t : strict_elementwise_callable + { + template + constexpr EVE_FORCEINLINE T operator()(T v, I i) const + { return EVE_DISPATCH_CALL(v, i); } + + EVE_CALLABLE_OBJECT(byte_swap_adjacent_t, byte_swap_adjacent_); + }; + //================================================================================================ //! @addtogroup core_bitops //! @{ @@ -64,7 +78,17 @@ namespace eve //! //! @} //================================================================================================ -EVE_MAKE_CALLABLE(byte_swap_adjacent_, byte_swap_adjacent); -} + inline constexpr auto byte_swap_adjacent = functor; -#include + namespace detail + { + template + EVE_FORCEINLINE constexpr T + byte_swap_adjacent_(EVE_REQUIRES(cpu_), O const&, T const& x, N const & n) noexcept + { + using v_t = element_type_t; + if constexpr(sizeof(v_t) == 0) return x; + else return bit_swap_adjacent(x, n*8); + } + } +} diff --git a/include/eve/module/core/regular/impl/bit_not.hpp b/include/eve/module/core/regular/impl/bit_not.hpp deleted file mode 100644 index 3014ec6965..0000000000 --- a/include/eve/module/core/regular/impl/bit_not.hpp +++ /dev/null @@ -1,35 +0,0 @@ -//================================================================================================== -/* - EVE - Expressive Vector Engine - Copyright : EVE Project Contributors - SPDX-License-Identifier: BSL-1.0 -*/ -//================================================================================================== -#pragma once - -#include -#include -#include -#include -#include - -namespace eve::detail -{ -template -EVE_FORCEINLINE auto -bit_not_(EVE_SUPPORTS(cpu_), T const& v) noexcept -{ - if constexpr( floating_scalar_value ) - return bit_cast(~bit_cast(v, as> {}), as(v)); - else return T(~v); -} - -// Masked case -template -EVE_FORCEINLINE auto -bit_not_(EVE_SUPPORTS(cpu_), C const& cond, U const& t) noexcept -{ - return mask_op(cond, eve::bit_not, t); -} - -} diff --git a/include/eve/module/core/regular/impl/bit_swap_pairs.hpp b/include/eve/module/core/regular/impl/bit_swap_pairs.hpp deleted file mode 100644 index f8c08b82ab..0000000000 --- a/include/eve/module/core/regular/impl/bit_swap_pairs.hpp +++ /dev/null @@ -1,39 +0,0 @@ -//================================================================================================== -/* - EVE - Expressive Vector Engine - Copyright : EVE Project Contributors - SPDX-License-Identifier: BSL-1.0 -*/ -//================================================================================================== -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -namespace eve::detail -{ - template - EVE_FORCEINLINE T - bit_swap_pairs_(EVE_SUPPORTS(cpu_), T a, I0 i0, I1 i1) noexcept - { - [[maybe_unused]] constexpr std::ptrdiff_t S8 = sizeof(element_type_t)*8; - EVE_ASSERT(eve::all(i0 < S8 && i1 < S8), "some indexes are out or range"); - auto x = bit_and(bit_xor(bit_shr(a, i0), bit_shr(a, i1)), one(as(a))); - a ^= bit_shl(x, i1); - a ^= bit_shl(x, i0); - return a; - } - - // Masked case - template - EVE_FORCEINLINE auto - bit_swap_pairs_(EVE_SUPPORTS(cpu_), C const& cond, T const& t, I0 const& i0, I1 const& i1 ) noexcept - { - return mask_op(cond, eve::bit_swap_pairs, t, i0, i1); - } -} diff --git a/include/eve/module/core/regular/reldist.hpp b/include/eve/module/core/regular/reldist.hpp index 47ef930716..614474bd5b 100644 --- a/include/eve/module/core/regular/reldist.hpp +++ b/include/eve/module/core/regular/reldist.hpp @@ -7,10 +7,26 @@ //================================================================================================== #pragma once -#include +#include +#include +#include +#include +#include +#include +#include namespace eve { + template + struct reldist_t : elementwise_callable + { + template + EVE_FORCEINLINE constexpr common_value_t operator()(T a, U b) const noexcept + { return EVE_DISPATCH_CALL(a, b); } + + EVE_CALLABLE_OBJECT(reldist_t, reldist_); + }; + //================================================================================================ //! @addtogroup core_arithmetic //! @{ @@ -47,7 +63,14 @@ namespace eve //! //! @} //================================================================================================ -EVE_MAKE_CALLABLE(reldist_, reldist); -} + inline constexpr auto reldist = functor; -#include + namespace detail + { + template + constexpr T reldist_(EVE_REQUIRES(cpu_), O const&, T a, T b) + { + return dist(a, b)/max(abs(a), abs(b), one(as(a))); + } + } +} diff --git a/test/unit/module/core/bit_not.cpp b/test/unit/module/core/bit_not.cpp index 01652a7cff..e95855cec1 100644 --- a/test/unit/module/core/bit_not.cpp +++ b/test/unit/module/core/bit_not.cpp @@ -23,7 +23,6 @@ TTS_CASE_TPL("Check return types of bit_not", eve::test::simd::all_types) // conditional TTS_EXPR_IS(eve::bit_not[logical()](T()), T); TTS_EXPR_IS(eve::bit_not[logical()](T()), T); - TTS_EXPR_IS(eve::bit_not[logical()](v_t()), T); }; //================================================================================================== @@ -65,7 +64,7 @@ TTS_CASE_WITH("Check behavior of eve::masked(eve::bit_not)(eve::wide)", eve::test::simd::ieee_reals, tts::generate(tts::randoms(eve::valmin, eve::valmax), tts::logicals(0, 3))) -(T const& a0, +(T const& a0, M const& mask) { TTS_IEEE_EQUAL(eve::bit_not[mask](a0), diff --git a/test/unit/module/core/bitofsign.cpp b/test/unit/module/core/bitofsign.cpp index 362e207d66..dae43bbde0 100644 --- a/test/unit/module/core/bitofsign.cpp +++ b/test/unit/module/core/bitofsign.cpp @@ -22,7 +22,6 @@ TTS_CASE_TPL("Check return types of bitofsign", eve::test::simd::all_types) TTS_EXPR_IS(eve::bitofsign(T()), T); TTS_EXPR_IS(eve::bitofsign(v_t()), v_t); TTS_EXPR_IS(eve::bitofsign[eve::logical()](T()), T); - TTS_EXPR_IS(eve::bitofsign[eve::logical()](v_t()), T); TTS_EXPR_IS(eve::bitofsign[eve::logical()](T()), T); TTS_EXPR_IS(eve::bitofsign[eve::logical()](v_t()), v_t); TTS_EXPR_IS(eve::bitofsign[bool()](T()), T);