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 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
22ff6e8
take 1
SadiinsoSnowfall Sep 11, 2024
c165f5a
added backends
SadiinsoSnowfall Sep 11, 2024
011a5c3
typo
SadiinsoSnowfall Sep 11, 2024
54b73bd
allow booleans in common_logical
SadiinsoSnowfall Sep 19, 2024
40c67a6
fixed common_logical impl
SadiinsoSnowfall Sep 19, 2024
f2a1dc8
fix
SadiinsoSnowfall Sep 19, 2024
131b561
fixed missing case for bool x bool
SadiinsoSnowfall Sep 20, 2024
29abcbd
riscv fix
SadiinsoSnowfall Sep 20, 2024
7ea3c77
avx512 fix
SadiinsoSnowfall Sep 20, 2024
07fd843
fix resolution problem
SadiinsoSnowfall Sep 24, 2024
b309af1
fix
SadiinsoSnowfall Sep 26, 2024
3594603
made common_logical more commutative
SadiinsoSnowfall Sep 26, 2024
959832b
fix NO_SIMD
SadiinsoSnowfall Sep 30, 2024
374bbc0
fix
SadiinsoSnowfall Sep 30, 2024
c7bdc8a
changed common_logical behaviour
SadiinsoSnowfall Nov 29, 2024
6090b71
fix
SadiinsoSnowfall Nov 29, 2024
78b12f2
revert new behaviour
SadiinsoSnowfall Dec 2, 2024
758da64
added a new logical elementwise callable type
SadiinsoSnowfall Dec 2, 2024
43e77ee
new behaviour
SadiinsoSnowfall Dec 6, 2024
cb68b56
[ci skip] stash
SadiinsoSnowfall Dec 9, 2024
98a2a82
fix
SadiinsoSnowfall Dec 10, 2024
413396c
fixed NOSIMD + improved operator|| codegen + fixed shadowing
SadiinsoSnowfall Dec 11, 2024
d9c231e
fix
SadiinsoSnowfall Dec 11, 2024
75ec64f
fix typo
SadiinsoSnowfall Dec 12, 2024
c9b000c
fix impls
SadiinsoSnowfall Dec 12, 2024
d88cb08
SVE fix
SadiinsoSnowfall Dec 12, 2024
0e35da3
review fixes
SadiinsoSnowfall Dec 13, 2024
eba6e03
rename concept
SadiinsoSnowfall Dec 13, 2024
8f33961
fix missing includes in wide
SadiinsoSnowfall Dec 16, 2024
eaaa7d0
moved relaxed_logical_scalar_value
SadiinsoSnowfall Dec 16, 2024
25eb612
renamed logical_ops to logical_test
SadiinsoSnowfall Dec 16, 2024
1882295
revert useless changes to strict_elementwise_callable
SadiinsoSnowfall Dec 16, 2024
9903a9b
adjusted comment
SadiinsoSnowfall Dec 16, 2024
1cae9a1
refactor AVX512 code
SadiinsoSnowfall Dec 16, 2024
b446032
revert is_equal return types
SadiinsoSnowfall Dec 16, 2024
742d79d
more comments
SadiinsoSnowfall Dec 16, 2024
6a33114
preemptive fix for comparison operators
SadiinsoSnowfall Dec 17, 2024
1d51da6
Clarify logical_elementwise_callable behavior
jfalcou Dec 17, 2024
a87e245
review fix
SadiinsoSnowfall Dec 20, 2024
753bbeb
remove logical_elementwise_callable
SadiinsoSnowfall Dec 20, 2024
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
SadiinsoSnowfall marked this conversation as resolved.
Show resolved Hide resolved
{
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});
SadiinsoSnowfall marked this conversation as resolved.
Show resolved Hide resolved
}

//! 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>>{}));
DenisYaroshevskiy marked this conversation as resolved.
Show resolved Hide resolved
SadiinsoSnowfall marked this conversation as resolved.
Show resolved Hide resolved
}
}
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>)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a very weird requirement. Can't you just require relaxed_logical_value on T and U?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See the comment in the file.

Copy link
Collaborator

Choose a reason for hiding this comment

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

what comment.
Like - ok - this is an unacceptable requirement for an interface. We need something reasonable.

{
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>>{}));
}
SadiinsoSnowfall marked this conversation as resolved.
Show resolved Hide resolved
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Usually - this type of code would be in regular file (since you have the platform implementations.

Also - seems like conversions should be in behaviour, not here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unaddressed.

Copy link
Owner

Choose a reason for hiding this comment

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

We do that a lot for small impl in a lot of places. Save on including a file for 5 lines.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We only do that if we don't have the special file includes. Either one file or everyone gets a file.

I mean - that's how it is now. Let's not make this harder.

Copy link
Owner

Choose a reason for hiding this comment

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

OK this is true.
@SadiinsoSnowfall put it back in a common/ file


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