Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move self_logand to the logical_and callable #1959

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions include/eve/arch/cpu/logical_wide.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <eve/detail/function/make.hpp>
#include <eve/detail/function/slice.hpp>
#include <eve/detail/function/subscript.hpp>
#include <eve/module/core/regular/logical_and.hpp>

#include <cstring>
#include <concepts>
Expand Down Expand Up @@ -274,23 +275,23 @@ namespace eve
//==============================================================================================
//! Perform a logical and operation between two eve::logical
template<typename U>
friend EVE_FORCEINLINE auto operator&&(logical const& v, logical<wide<U, Cardinal>> const& w) noexcept
friend EVE_FORCEINLINE auto operator&&(logical const& a, logical<wide<U, Cardinal>> const& b) noexcept
{
return detail::self_logand(eve::current_api,v,w);
return logical_and(a, b);
}

//! Perform a logical and operation between a eve::logical and a scalar
template<scalar_value S>
friend EVE_FORCEINLINE auto operator&&(logical const& v, S w) noexcept
friend EVE_FORCEINLINE auto operator&&(logical const& w, S s) noexcept
{
return v && logical{w};
return logical_and(w, logical{s});
}

//! Perform a logical and operation between a scalar and a eve::logical
template<scalar_value S>
friend EVE_FORCEINLINE auto operator&&(S v, logical const& w) noexcept
friend EVE_FORCEINLINE auto operator&&(S s, logical const& w) noexcept
{
return w && v;
return logical_and(s, w);
}

//! Perform a logical or operation between two eve::logical
Expand Down
20 changes: 0 additions & 20 deletions include/eve/detail/function/simd/arm/sve/friends.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,6 @@ EVE_FORCEINLINE auto
self_geq(wide<T, N> v, wide<T, N> w) noexcept -> as_logical_t<wide<T, N>>
requires sve_abi<abi_t<T, N>> { return svcmpge(sve_true<T>(), v, w); }


// ---------------------------------------------------------

template<typename T, typename U, typename N>
EVE_FORCEINLINE auto
self_logand(sve_ const&, logical<wide<T,N>> v, logical<wide<U,N>> w) noexcept -> logical<wide<T, N>>
requires(sve_abi<abi_t<T, N>> || sve_abi<abi_t<U, N>>)
{
if constexpr(!is_aggregated_v<abi_t<T, N>>)
{
return svmov_z(v, convert(w, as<logical<T>>{}));
}
else
{
auto[lv,hv] = v.slice();
auto[lw,hw] = w.slice();
return logical<wide<T, N>>{ lv && lw, hv && hw};
}
}

template<typename T, typename U, typename N>
EVE_FORCEINLINE auto
self_logor(sve_ const&, logical<wide<T,N>> v, logical<wide<U,N>> w) noexcept -> logical<wide<T, N>>
Expand Down
29 changes: 0 additions & 29 deletions include/eve/detail/function/simd/common/friends.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,6 @@ namespace eve::detail
}
}

//================================================================================================
template<typename T, typename U, typename N>
EVE_FORCEINLINE auto self_logand(cpu_ const&, logical<wide<T,N>> v, logical<wide<U,N>> w) noexcept
{
using abi_t = typename logical<wide<T,N>>::abi_type;
using abi_u = typename logical<wide<U,N>>::abi_type;

// Both arguments are aggregated, we can safely slice
if constexpr( is_aggregated_v<abi_t> && is_aggregated_v<abi_u> )
{
auto [vl, vh] = v.slice();
auto [wl, wh] = w.slice();
return logical<wide<T,N>> { self_logand(eve::current_api,vl, wl)
, self_logand(eve::current_api,vh, wh)
};
}
else
{
if constexpr( !is_aggregated_v<abi_t> && !is_aggregated_v<abi_u> && (sizeof(T) == sizeof(U)) )
{
return bit_cast ( v.bits() & w.bits(), as(v) );
}
else
{
return self_logand(cpu_{}, v, convert(w, as<logical<T>>()));
}
}
}

//================================================================================================
template<typename T, typename U, typename N>
EVE_FORCEINLINE auto self_logor(cpu_ const&, logical<wide<T,N>> v, logical<wide<U,N>> w) noexcept
Expand Down
21 changes: 0 additions & 21 deletions include/eve/detail/function/simd/riscv/friends.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,6 @@ requires rvv_abi<abi_t<T, N>>
return __riscv_vmxor(lhs, rhs, N::value);
}

template<typename T, typename U, typename N>
EVE_FORCEINLINE auto
self_logand(rvv_ const&, logical<wide<T, N>> v, logical<wide<U, N>> w) noexcept
-> logical<wide<T, N>>
requires(rvv_abi<abi_t<T, N>> || rvv_abi<abi_t<U, N>>)
{
if constexpr( !is_aggregated_v<abi_t<T, N>> && !is_aggregated_v<abi_t<U, N>> )
{
auto casted_w = bit_cast(w, as<logical<wide<T, N>>> {});
logical<wide<T, N>> to_ret = __riscv_vmand(v, casted_w, N::value);
return to_ret;
}
else
{
auto [lv, hv] = v.slice();
auto [lw, hw] = w.slice();
auto res = logical<wide<T, N>> {lv && lw, hv && hw};
return res;
}
}

template<typename T, typename U, typename N>
EVE_FORCEINLINE auto
self_logor(rvv_ const&, logical<wide<T, N>> v, logical<wide<U, N>> w) noexcept
Expand Down
45 changes: 0 additions & 45 deletions include/eve/detail/function/simd/x86/friends.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,6 @@

namespace eve::detail
{
//================================================================================================
template<typename T, typename U, typename N>
EVE_FORCEINLINE auto
self_logand(sse2_ const&, logical<wide<T, N>> v, logical<wide<U, N>> w) noexcept
requires(x86_abi<abi_t<T, N>> || x86_abi<abi_t<U, N>>)
{
if constexpr( !use_is_wide_logical<abi_t<T, N>>::value )
{
using abi_t = typename logical<wide<T,N>>::abi_type;
using abi_u = typename logical<wide<U,N>>::abi_type;
using storage_t = typename logical<wide<T, N>>::storage_type;
using m_t = std::conditional_t< is_aggregated_v<abi_t>
, typename logical<wide<U, N>>::storage_type
, storage_t
>;
using u_t = typename m_t::type;

// We need to know which side is not aggregated to safely bit_cast its contents
auto cvt = [](auto a, auto b)
{
storage_t dst;
if constexpr( is_aggregated_v<abi_t> )
{
u_t them = bit_cast(a.storage(),as<u_t>()) & b.storage().value;
dst = bit_cast(them,as<storage_t>());
}
else if constexpr( is_aggregated_v<abi_u> )
{
dst.value = a.storage().value & bit_cast(b.storage(),as<u_t>());
}
else
{
dst.value = a.storage().value & b.storage().value;
}
return dst;
};

return logical<wide<T, N>>(cvt(v,w));
}
else
{
return self_logand(cpu_ {}, v, w);
}
}

//================================================================================================
template<typename T, typename U, typename N>
EVE_FORCEINLINE auto
Expand Down
3 changes: 3 additions & 0 deletions include/eve/forward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ EVE_FORCEINLINE auto mask_op(C const& c,
template <typename From, typename To>
To call_simd_cast(From, as<To>);

// This is an inderect wrapper of eve::convert to avoid cycling dependencies
template <typename From, typename To>
auto call_convert(From, as<To>);
}
10 changes: 10 additions & 0 deletions include/eve/module/core/regular/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ namespace eve
//================================================================================================
//! @}
//================================================================================================

namespace detail
{
// This function is forward declared wrapper around convert, so that internally we can call it anywhere.
template<typename T, typename Target>
EVE_FORCEINLINE auto call_convert(T x, as<Target> tgt)
{
return eve::convert(x, tgt);
}
}
}

#include <eve/module/core/regular/impl/convert.hpp>
Expand Down
5 changes: 5 additions & 0 deletions include/eve/module/core/regular/if_else.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ namespace eve
return EVE_DISPATCH_CALL(logical<std::uint8_t>(mask),v0,v1);
}

EVE_FORCEINLINE constexpr bool operator()(bool mask, bool v0, bool v1) const noexcept
{
return mask ? v0 : v1;
}

EVE_CALLABLE_OBJECT(if_else_t, if_else_);
};

Expand Down
22 changes: 22 additions & 0 deletions include/eve/module/core/regular/impl/simd/arm/sve/logical_and.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

#include <eve/concept/value.hpp>
#include <eve/detail/category.hpp>
#include <eve/detail/implementation.hpp>

namespace eve::detail
{
template<callable_options O, typename T, typename U, typename N>
EVE_FORCEINLINE logical<wide<T, N>> logical_and_(EVE_REQUIRES(sve_), O const&, logical<wide<T, N>> v, logical<wide<U, N>> w) noexcept
requires(sve_abi<abi_t<T, N>> || sve_abi<abi_t<U, N>>)
{
return svmov_z(v, convert(w, as<logical<T>>{}));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem right anymore. I appreciate that you might not had time to fix it and this is a draft.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type shouldn't change based on what logical is first

}
}
22 changes: 22 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/logical_and.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

#include <eve/concept/compatible.hpp>
#include <eve/concept/value.hpp>
#include <eve/detail/implementation.hpp>

namespace eve::detail
{
template<callable_options O, typename T, typename U, typename N>
EVE_FORCEINLINE logical<wide<T, N>> logical_and_(EVE_REQUIRES(rvv_), O const&, logical<wide<T, N>> a, logical<wide<U, N>> b) noexcept
requires(rvv_abi<abi_t<T, N>> || rvv_abi<abi_t<U, N>>)
{
return __riscv_vmand(a, bit_cast(b, as<logical<wide<T, N>>>{}), N::value);
}
}
31 changes: 31 additions & 0 deletions include/eve/module/core/regular/impl/simd/x86/logical_and.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

#include <eve/concept/compatible.hpp>
#include <eve/concept/value.hpp>
#include <eve/detail/implementation.hpp>

namespace eve::detail
{
template<callable_options O, typename T, typename U, typename N>
EVE_FORCEINLINE logical<wide<T, N>> logical_and_(EVE_REQUIRES(sse2_), O const& opts, logical<wide<T, N>> a, logical<wide<U, N>> b) noexcept
requires (x86_abi<abi_t<T, N>> || x86_abi<abi_t<U, N>>)
{
if constexpr (use_is_wide_logical<abi_t<T, N>>::value)
{
return logical_and.behavior(cpu_{}, opts, a, b);
}
else
{
typename logical<wide<T, N>>::storage_type dst;
dst.value = a.storage().value & b.storage().value;
return dst;
}
}
}
71 changes: 31 additions & 40 deletions include/eve/module/core/regular/logical_and.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,14 @@
namespace eve
{
template<typename Options>
struct logical_and_t : strict_elementwise_callable<logical_and_t, Options>
struct logical_and_t : logical_elementwise_callable<logical_and_t, Options>
{
template<logical_value T, logical_value U>
constexpr EVE_FORCEINLINE auto operator()(T a, U b) const noexcept -> decltype(a && b)
requires(eve::same_lanes_or_scalar<T, U>)
{ return EVE_DISPATCH_CALL(a, b); }

template<logical_value U>
constexpr EVE_FORCEINLINE U operator()(bool a, U b) const noexcept
{ return EVE_DISPATCH_CALL(a, b); }

template<logical_value T>
constexpr EVE_FORCEINLINE T operator()(T a, bool b) const noexcept
{ return EVE_DISPATCH_CALL(a, b); }

constexpr EVE_FORCEINLINE bool operator()(bool a, bool b) const noexcept
{ return EVE_DISPATCH_CALL(a, b); }
template<typename T, typename U>
constexpr EVE_FORCEINLINE common_logical_t<T, U> operator()(T a, U b) const noexcept
requires (same_lanes_or_scalar<T, U> && !arithmetic_simd_value<T> && !arithmetic_simd_value<U>)
{
return EVE_DISPATCH_CALL(a, b);
}

EVE_CALLABLE_OBJECT(logical_and_t, logical_and_);
};
Expand Down Expand Up @@ -83,32 +74,32 @@ namespace eve

namespace detail
{
template<typename T, typename U, callable_options O>
EVE_FORCEINLINE constexpr auto
logical_and_(EVE_REQUIRES(cpu_), O const &, T a, U b) noexcept
template<callable_options O, typename T, typename U>
EVE_FORCEINLINE constexpr common_logical_t<T, U> logical_and_(EVE_REQUIRES(cpu_), O const&, T a, U b) noexcept
{
using r_t = as_logical_t<decltype(a && b)>;
if constexpr( scalar_value<T> && scalar_value<U> ) return r_t(a && b);
else return a && b;
if constexpr (std::same_as<T, bool> && std::same_as<U, bool>) return a && b;
else if constexpr (std::same_as<T, bool>) return logical_and(U{a}, b);
else if constexpr (std::same_as<U, bool>) return logical_and(a, T{b});
else if constexpr (logical_simd_value<T> && scalar_value<U>) return logical_and(a, T{b});
else if constexpr (scalar_value<T> && logical_simd_value<U>) return logical_and(b, U{a});
else if constexpr (std::same_as<typename T::bits_type, typename U::bits_type>)
{
if constexpr (scalar_value<T> && scalar_value<U>) return T{a && b};
else return bit_cast(a.bits() & b.bits(), as<as_logical_t<T>>{});
}
else return logical_and(a, convert(b, as<as_logical_t<typename T::value_type>>{}));
}
}
}

template<typename T, callable_options O>
EVE_FORCEINLINE constexpr
auto logical_and_(EVE_REQUIRES(cpu_), O const &, T a, bool b) noexcept
{
return b ? T {a} : false_(as<T>());
}
#if defined(EVE_INCLUDE_X86_HEADER)
# include <eve/module/core/regular/impl/simd/x86/logical_and.hpp>
#endif

template<typename U, callable_options O>
EVE_FORCEINLINE constexpr
auto logical_and_(EVE_REQUIRES(cpu_), O const &, bool a, U b) noexcept
{
return a ? U {b} : false_(as<U>());
}
#if defined(EVE_INCLUDE_ARM_SVE_HEADER)
# include <eve/module/core/regular/impl/simd/arm/sve/logical_and.hpp>
#endif

template<callable_options O>
EVE_FORCEINLINE constexpr
auto logical_and_(EVE_REQUIRES(cpu_), O const &, bool a, bool b) noexcept
{ return a && b; }
}
}
#if defined(EVE_INCLUDE_RISCV_HEADER)
# include <eve/module/core/regular/impl/simd/riscv/logical_and.hpp>
#endif
Loading
Loading