Skip to content

Commit

Permalink
[riscv] Optimizations for RISC-V
Browse files Browse the repository at this point in the history
  • Loading branch information
ita-sc committed Aug 17, 2024
1 parent 979fa91 commit af4327d
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/eve/module/core/regular/abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ namespace eve
#if defined(EVE_INCLUDE_SVE_HEADER)
# include <eve/module/core/regular/impl/simd/arm/sve/abs.hpp>
#endif

#if defined(EVE_INCLUDE_RISCV_HEADER)
# include <eve/module/core/regular/impl/simd/riscv/abs.hpp>
#endif
4 changes: 4 additions & 0 deletions include/eve/module/core/regular/add.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ namespace eve
#if defined(EVE_INCLUDE_SVE_HEADER)
# include <eve/module/core/regular/impl/simd/arm/sve/add.hpp>
#endif

#if defined(EVE_INCLUDE_RISCV_HEADER)
# include <eve/module/core/regular/impl/simd/riscv/add.hpp>
#endif
4 changes: 4 additions & 0 deletions include/eve/module/core/regular/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ EVE_MAKE_CALLABLE(all_, all);
#if defined(EVE_INCLUDE_SVE_HEADER)
# include <eve/module/core/regular/impl/simd/arm/sve/all.hpp>
#endif

#if defined(EVE_INCLUDE_RISCV_HEADER)
# include <eve/module/core/regular/impl/simd/riscv/all.hpp>
#endif
4 changes: 4 additions & 0 deletions include/eve/module/core/regular/any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ EVE_MAKE_CALLABLE(any_, any);
#if defined(EVE_INCLUDE_SVE_HEADER)
# include <eve/module/core/regular/impl/simd/arm/sve/any.hpp>
#endif

#if defined(EVE_INCLUDE_RISCV_HEADER)
# include <eve/module/core/regular/impl/simd/riscv/any.hpp>
#endif
4 changes: 4 additions & 0 deletions include/eve/module/core/regular/bit_and.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@ namespace eve
#if defined(EVE_INCLUDE_X86_HEADER)
# include <eve/module/core/regular/impl/simd/x86/bit_and.hpp>
#endif

#if defined(EVE_INCLUDE_RISCV_HEADER)
# include <eve/module/core/regular/impl/simd/riscv/bit_and.hpp>
#endif
4 changes: 4 additions & 0 deletions include/eve/module/core/regular/div.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ namespace eve
#if defined(EVE_INCLUDE_SVE_HEADER)
# include <eve/module/core/regular/impl/simd/arm/sve/div.hpp>
#endif

#if defined(EVE_INCLUDE_RISCV_HEADER)
# include <eve/module/core/regular/impl/simd/riscv/div.hpp>
#endif
55 changes: 55 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/abs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//==================================================================================================
/*
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<scalar_value T, typename N>
EVE_FORCEINLINE auto
abs_(EVE_SUPPORTS(rvv_), wide<T, N> const& a) noexcept -> wide<T, N>
requires rvv_abi<abi_t<T, N>> && (match(categorize<wide<T, N>>(), category::float_))
{
return __riscv_vfabs(a, N::value);
}

template<scalar_value T, typename N>
EVE_FORCEINLINE auto
abs_(EVE_SUPPORTS(rvv_), wide<T, N> const& a) noexcept -> wide<T, N>
requires rvv_abi<abi_t<T, N>> && (match(categorize<wide<T, N>>(), category::int_))
{
wide<T, N> negative_values = __riscv_vneg(a, N::value);
logical<wide<T, N>> need_to_change_mask = self_less(a, static_cast<T>(0));
return if_else(need_to_change_mask, negative_values, a);
}

template<conditional_expr C, scalar_value T, typename N>
EVE_FORCEINLINE auto
abs_(EVE_SUPPORTS(rvv_), C const& cx, wide<T, N> const& v) noexcept -> wide<T, N>
requires rvv_abi<abi_t<T, N>>
{
constexpr auto c = categorize<wide<T, N>>();
if constexpr( C::is_complete ) return abs_(EVE_RETARGET(cpu_), cx, v);
else
{
auto mask = expand_mask(cx, as<wide<T, N>> {});
if constexpr( match(c, category::float_) ) { return __riscv_vfabs_tumu(mask, v, v, N::value); }
if constexpr( match(c, category::int_) )
{
wide<T, N> negative_values = __riscv_vneg(v, N::value);
logical<wide<T, N>> need_to_change_mask = self_less(v, static_cast<T>(0));
logical<wide<T, N>> mask_to_update = __riscv_vmand(mask, need_to_change_mask, N::value);
return if_else(mask_to_update, negative_values, v);
}
}
}
}
34 changes: 34 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/add.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

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

namespace eve::detail
{

template<conditional_expr C, typename T, typename N>
EVE_FORCEINLINE wide<T, N>
add_(EVE_SUPPORTS(rvv_), C const &cx, wide<T, N> v, wide<T, N> w) noexcept
requires rvv_abi<abi_t<T, N>>
{
if constexpr( C::is_complete ) { return add_(EVE_RETARGET(cpu_), cx, v, w); }
else
{
if constexpr( !C::has_alternative )
{
auto m = expand_mask(cx, as<wide<T, N>> {});
constexpr auto c = categorize<wide<T, N>>();
if constexpr( match(c, category::float_) ) return __riscv_vfadd_tumu(m, v, v, w, N::value);
else return __riscv_vadd_tumu(m, v, v, w, N::value);
}
else return add_(EVE_RETARGET(cpu_), cx, v, w);
}
}
}
28 changes: 28 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/all.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

#include <eve/detail/has_abi.hpp>
#include <eve/detail/implementation.hpp>
#include <eve/module/core/regular/count_true.hpp>

namespace eve::detail
{
template<scalar_value T, typename N, relative_conditional_expr C>
EVE_FORCEINLINE bool
all_(EVE_SUPPORTS(rvv_), C const& cond, logical<wide<T, N>> const& v) noexcept
requires rvv_abi<abi_t<T, N>>
{
if constexpr( C::is_complete )
{
if constexpr( C::is_inverted ) return __riscv_vcpop(v, N::value) == N::value;
else return true;
}
else return count_true(cond, v) == cond.count(as<wide<T, N>>());
}
}
27 changes: 27 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/any.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

#include <eve/detail/has_abi.hpp>
#include <eve/detail/implementation.hpp>

namespace eve::detail
{
template<scalar_value T, typename N, relative_conditional_expr C>
EVE_FORCEINLINE bool
any_(EVE_SUPPORTS(rvv_), C const& cond, logical<wide<T, N>> v) noexcept
requires rvv_abi<abi_t<T, N>>
{
if constexpr( C::is_complete && !C::is_inverted ) return false;
else
{
auto m = expand_mask(cond, as<wide<T, N>> {});
return __riscv_vcpop(m, v, N::value) > 0;
}
}
}
58 changes: 58 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/bit_and.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

#include <eve/concept/value.hpp>
#include <eve/detail/abi.hpp>
#include <eve/detail/overload.hpp>
#include <eve/forward.hpp>
namespace eve ::detail
{
// -----------------------------------------------------------------------------------------------
// Masked case
template<conditional_expr C, arithmetic_scalar_value T, typename N>
EVE_FORCEINLINE wide<T, N>
bit_and_(EVE_SUPPORTS(rvv_), C const& cx, wide<T, N> const& v0, wide<T, N> const& v1) noexcept
requires rvv_abi<abi_t<T, N>>
{
constexpr auto c = categorize<wide<T, N>>();

if constexpr( C::is_complete || abi_t<T, N>::is_wide_logical )
{
return bit_and_(EVE_RETARGET(cpu_), cx, v0, v1);
}
else
{
auto m = expand_mask(cx, as<wide<T, N>> {});
using sign = unsigned;
using out_part_scalar = as_integer_t<T, sign>;
using out_part_wide = wide<out_part_scalar, N>;
auto part_tgt = as<out_part_wide> {};
auto v0_int = bit_cast(v0, part_tgt);
auto v1_int = bit_cast(v1, part_tgt);
out_part_wide part_res = __riscv_vand_tumu(m, v0_int, v0_int, v1_int, N::value);
return bit_cast(part_res, as<wide<T, N>> {});
}
}
// -----------------------------------------------------------------------------------------------
// Masked case
template<arithmetic_scalar_value T, typename N>
EVE_FORCEINLINE wide<T, N>
bit_and_(EVE_SUPPORTS(rvv_), wide<T, N> const &v0, wide<T, N> const &v1) noexcept
requires rvv_abi<abi_t<T, N>>
{
using sign = unsigned;
using out_part_scalar = as_integer_t<T, sign>;
using out_part_wide = wide<out_part_scalar, N>;
auto part_tgt = as<out_part_wide> {};
auto v0_int = bit_cast(v0, part_tgt);
auto v1_int = bit_cast(v1, part_tgt);
out_part_wide part_res = __riscv_vand(v0_int, v1_int, N::value);
return bit_cast(part_res, as<wide<T, N>> {});
}
}
34 changes: 34 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/div.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

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

namespace eve::detail
{
template<conditional_expr C, arithmetic_scalar_value T, typename N>
EVE_FORCEINLINE wide<T, N>
div_(EVE_SUPPORTS(rvv_), C const& cx, wide<T, N> const& v, wide<T, N> const& w) noexcept
requires rvv_abi<abi_t<T, N>>
{
if constexpr( C::is_complete ) { return div_(EVE_RETARGET(cpu_), cx, v, w); }
else
{
if constexpr( !C::has_alternative )
{
auto m = expand_mask(cx, as<wide<T, N>> {});
constexpr auto c = categorize<wide<T, N>>();
if constexpr( match(c, category::int_) ) return __riscv_vdiv_tumu(m, v, v, w, N::value);
else if constexpr( match(c, category::uint_) ) return __riscv_vdivu_tumu(m, v, v, w, N::value);
else if constexpr( match(c, category::float_) ) return __riscv_vfdiv_tumu(m, v, v, w, N::value);
}
else return div_(EVE_RETARGET(cpu_), cx, v, w);
}
}
}
20 changes: 20 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/logical_xor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once
namespace eve::detail
{
template<arithmetic_scalar_value T, typename N, scalar_value U>
EVE_FORCEINLINE logical<wide<T, N>>
logical_xor_(EVE_SUPPORTS(rvv_),
logical<wide<T, N>> const &a,
logical<wide<U, N>> const &b) noexcept
requires rvv_abi<abi_t<T, N>>
{
return a != bit_cast(b, as<logical<wide<T, N>>> {});
}
}
35 changes: 35 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/max.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once

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

namespace eve::detail
{

template<conditional_expr C, typename T, typename N>
EVE_FORCEINLINE wide<T, N>
max_(EVE_SUPPORTS(rvv_), C const &cx, wide<T, N> v, wide<T, N> w) noexcept
requires rvv_abi<abi_t<T, N>>
{
if constexpr( C::is_complete ) { return max_(EVE_RETARGET(cpu_), cx, v, w); }
else
{
if constexpr( !C::has_alternative )
{
auto m = expand_mask(cx, as<wide<T, N>> {});
constexpr auto c = categorize<wide<T, N>>();
if constexpr( match(c, category::float_) ) return __riscv_vfmax_tumu(m, v, v, w, N::value);
else if constexpr( match(c, category::int_) ) return __riscv_vmax_tumu(m, v, v, w, N::value);
else if constexpr( match(c, category::uint_) ) return __riscv_vmaxu_tumu(m, v, v, w, N::value);
}
else return max_(EVE_RETARGET(cpu_), cx, v, w);
}
}
}
43 changes: 43 additions & 0 deletions include/eve/module/core/regular/impl/simd/riscv/min.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//==================================================================================================
/*
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<scalar_value T, typename N>
EVE_FORCEINLINE auto
min_(EVE_SUPPORTS(rvv_), wide<T, N> const& a, wide<T, N> const& b) noexcept -> wide<T, N>
requires rvv_abi<abi_t<T, N>>
{
constexpr auto c = categorize<wide<T, N>>();
if constexpr( match(c, category::float_) ) return __riscv_vfmin_tu(a, a, b, N::value);
else if constexpr( match(c, category::int_) )
{
wide<T, N> res = __riscv_vmin_tu(a, a, b, N::value);
return res;
}
else if constexpr( match(c, category::uint_) ) return __riscv_vminu_tu(a, a, b, N::value);
}

template<conditional_expr C, arithmetic_scalar_value T, typename N>
EVE_FORCEINLINE auto
min_(EVE_SUPPORTS(rvv_), C const& cx, wide<T, N> const& v, wide<T, N> const& w) noexcept
-> wide<T, N>
{
auto mask = expand_mask(cx, as<wide<T, N>> {});
constexpr auto c = categorize<wide<T, N>>();
if constexpr( match(c, category::float_) ) return __riscv_vfmin_tumu(mask, v, v, w, N::value);
else if constexpr( match(c, category::int_) ) return __riscv_vmin_tumu(mask, v, v, w, N::value);
else if constexpr( match(c, category::uint_) ) return __riscv_vminu_tumu(mask, v, v, w, N::value);
}

}
Loading

0 comments on commit af4327d

Please sign in to comment.