Skip to content

Commit

Permalink
Fix converting API for combinatorial functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jtlap authored Mar 13, 2024
1 parent edcc32c commit 0aaeacd
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 228 deletions.
30 changes: 14 additions & 16 deletions include/eve/module/combinatorial/regular/impl/nth_prime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace eve::detail
{
template<unsigned_value T, callable_options O>
constexpr EVE_FORCEINLINE T
nth_prime_(EVE_REQUIRES(cpu_), O const&, T nn)
nth_prime_(EVE_REQUIRES(cpu_), O const&, T nn) noexcept
{
// clang-format off
// This is basically three big tables which together
Expand Down Expand Up @@ -1225,20 +1225,18 @@ namespace eve::detail
else return apply_over(nth_prime, nn);
}

// TODO LATER
// template<unsigned_value T, unsigned_scalar_value D>
// EVE_FORCEINLINE constexpr auto
// nth_prime_(EVE_SUPPORTS(cpu_), converter_type<D> d, T n) noexcept
// {
// return nth_prime(d(n));
// }
template<unsigned_value T, unsigned_scalar_value U, callable_options O>
constexpr EVE_FORCEINLINE auto
nth_prime_(EVE_REQUIRES(cpu_), O const&, T n, as<U> const & target) noexcept
{
return nth_prime(convert(n, target));
}

// template<unsigned_value T, floating_scalar_value D>
// EVE_FORCEINLINE constexpr auto
// nth_prime_(EVE_SUPPORTS(cpu_), converter_type<D> d, T n) noexcept
// {
// auto nn = uint32(n);
// auto r = d(nth_prime(nn));
// return if_else(is_eqz(r), allbits, r);
// }
template<unsigned_value T, floating_scalar_value U, callable_options O>
constexpr EVE_FORCEINLINE auto
nth_prime_(EVE_REQUIRES(cpu_), O const&, T n, as<U> const & target) noexcept
{
auto r = convert(nth_prime(uint32(n)), target);
return if_else(is_eqz(r), allbits, r);
}
}
14 changes: 13 additions & 1 deletion include/eve/module/combinatorial/regular/nth_prime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@
namespace eve
{
template<typename Options>
struct nth_prime_t : elementwise_callable<nth_prime_t, Options/*, upgrade_converter_option*/>
struct nth_prime_t : elementwise_callable<nth_prime_t, Options>
{
template<eve::unsigned_value T>
constexpr EVE_FORCEINLINE
T operator()(T v) const noexcept { return EVE_DISPATCH_CALL(v); }

template<eve::integral_value T, floating_scalar_value U>
EVE_FORCEINLINE constexpr eve::as_wide_as_t<U, T> operator()(T v, eve::as<U> target ) const noexcept
{
return EVE_DISPATCH_CALL(v, target);
}

template<eve::integral_value T, unsigned_scalar_value U>
EVE_FORCEINLINE constexpr eve::as_wide_as_t<U, T> operator()(T v, eve::as<U> target ) const noexcept
{
return EVE_DISPATCH_CALL(v, target);
}

EVE_CALLABLE_OBJECT(nth_prime_t, nth_prime_);
};

Expand Down
64 changes: 61 additions & 3 deletions include/eve/module/combinatorial/regular/prime_ceil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <eve/arch.hpp>
#include <eve/traits/overload.hpp>
#include <eve/module/core/decorator/core.hpp>
#include <eve/module/combinatorial/regular/nth_prime.hpp>
#include <eve/module/core.hpp>

namespace eve
{
Expand All @@ -20,6 +22,18 @@ namespace eve
constexpr EVE_FORCEINLINE
T operator()(T v) const noexcept { return EVE_DISPATCH_CALL(v); }

template<eve::integral_value T, floating_scalar_value U>
EVE_FORCEINLINE constexpr eve::as_wide_as_t<U, T> operator()(T v, eve::as<U> target ) const noexcept
{
return EVE_DISPATCH_CALL(v, target);
}

template<eve::integral_value T, unsigned_scalar_value U>
EVE_FORCEINLINE constexpr eve::as_wide_as_t<U, T> operator()(T v, eve::as<U> target ) const noexcept
{
return EVE_DISPATCH_CALL(v, target);
}

EVE_CALLABLE_OBJECT(prime_ceil_t, prime_ceil_);
};

Expand Down Expand Up @@ -69,7 +83,51 @@ namespace eve
//! @godbolt{doc/combinatorial/conversion/prime_ceil.cpp}
//! @}
//================================================================================================
inline constexpr auto prime_ceil = functor<prime_ceil_t>;
}
inline constexpr auto prime_ceil = functor<prime_ceil_t>;

#include <eve/module/combinatorial/regular/impl/prime_ceil.hpp>
namespace detail
{
template<unsigned_value T, callable_options O>
constexpr EVE_FORCEINLINE T
prime_ceil_(EVE_REQUIRES(cpu_), O const&, T n)
{
using elt_t = element_type_t<T>;
n = if_else(is_eqz(n), T(2), n);
auto max_n = (sizeof(elt_t) == 1) ? T(53) : (sizeof(elt_t) == 2 ? T(6541) : T(9999));
if constexpr( has_native_abi_v<T> )
{
auto first = T(0);
auto last = max_n;
auto toobig = n > nth_prime(max_n);
n = if_else(toobig, zero, n);
while( eve::any(inc(first) < last) )
{
T mid = average(first, last);
auto pmid = nth_prime(mid);
auto test = pmid >= n;
last = if_else(test, mid, last);
first = if_else(test, first, mid);
}
auto tmp = nth_prime(first);
auto t = tmp >= n;
return if_else(toobig, zero, if_else(t, tmp, nth_prime(last)));
}
else return apply_over(prime_ceil, n);
}

template<unsigned_value T, unsigned_scalar_value U, callable_options O>
constexpr EVE_FORCEINLINE auto
prime_ceil_(EVE_REQUIRES(cpu_), O const&, T n, as<U> const & target) noexcept
{
return convert(prime_ceil(uint32(n)), target);
}

template<unsigned_value T, floating_scalar_value U, callable_options O>
constexpr EVE_FORCEINLINE auto
prime_ceil_(EVE_REQUIRES(cpu_), O const&, T n, as<U> const & target) noexcept
{
auto r = convert(prime_ceil(uint32(n)), target);
return if_else(is_eqz(r), allbits, r);
}
}
}
63 changes: 60 additions & 3 deletions include/eve/module/combinatorial/regular/prime_floor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ namespace eve
constexpr EVE_FORCEINLINE
T operator()(T v) const noexcept { return EVE_DISPATCH_CALL(v); }

template<eve::integral_value T, floating_scalar_value U>
EVE_FORCEINLINE constexpr eve::as_wide_as_t<U, T> operator()(T v, eve::as<U> target ) const noexcept
{
return EVE_DISPATCH_CALL(v, target);
}

template<eve::integral_value T, unsigned_scalar_value U>
EVE_FORCEINLINE constexpr eve::as_wide_as_t<U, T> operator()(T v, eve::as<U> target ) const noexcept
{
return EVE_DISPATCH_CALL(v, target);
}

EVE_CALLABLE_OBJECT(prime_floor_t, prime_floor_);
};

Expand Down Expand Up @@ -69,7 +81,52 @@ namespace eve
//! @godbolt{doc/combinatorial/conversion/prime_floor.cpp}
//! @}
//================================================================================================
inline constexpr auto prime_floor = functor<prime_floor_t>;
}
inline constexpr auto prime_floor = functor<prime_floor_t>;

#include <eve/module/combinatorial/regular/impl/prime_floor.hpp>
namespace detail
{
template<unsigned_value T, callable_options O>
constexpr EVE_FORCEINLINE T
prime_floor_(EVE_REQUIRES(cpu_), O const&, T n) noexcept
{
using elt_t = element_type_t<T>;
if constexpr( has_native_abi_v<T> )
{
auto constexpr maxi =
(sizeof(elt_t) == 1) ? (53u) : ((sizeof(elt_t) == 2) ? (6541u) : (10000u));
auto constexpr next =
(sizeof(elt_t) == 1) ? (255u) : ((sizeof(elt_t) == 2) ? (65535u) : (104742u));
auto first = T(0);
auto last = T(maxi);
while( eve::any(inc(first) < last) )
{
auto mid = average(first, last);
auto pmid = convert(nth_prime(mid), as<elt_t>());
auto test = pmid <= n;
first = if_else(test, mid, first);
last = if_else(test, last, mid);
}
auto z = nth_prime(first);
z = if_else(
(((last == T(maxi)) && (n < T(next))) || (first < T(maxi - 1))) && (n >= 2), z, zero);
return z;
}
else return apply_over(prime_floor, n);
}

template<unsigned_value T, unsigned_scalar_value U, callable_options O>
constexpr EVE_FORCEINLINE auto
prime_floor_(EVE_REQUIRES(cpu_), O const&, T n, as<U> const & target) noexcept
{
return convert(prime_floor(uint32(n)), target);
}

template<unsigned_value T, floating_scalar_value U, callable_options O>
constexpr EVE_FORCEINLINE auto
prime_floor_(EVE_REQUIRES(cpu_), O const&, T n, as<U> const & target) noexcept
{
auto r = convert(prime_floor(uint32(n)), target);
return if_else(is_eqz(r), allbits, r);
}
}
}
120 changes: 0 additions & 120 deletions include/eve/module/math/regular/impl/exp2.hpp

This file was deleted.

5 changes: 3 additions & 2 deletions test/doc/combinatorial/regular/nth_prime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ main()
std::cout << "---- simd" << '\n'
<< " <- pi16 = " << pi16 << '\n'
<< " -> nth_prime(pi16) = " << eve::nth_prime(pi16)
<< " // note the 0 outputs meaning overflow or out of range\n";

<< " // note the 0 outputs meaning overflow or out of range\n"
<< " -> nth_prime(pi16, as<float>()) " << eve::nth_prime(pi16, eve::as<float>());
::
std::uint16_t xi = 18;
std::cout << "---- scalar" << '\n'
<< " xi = " << xi << '\n'
Expand Down
7 changes: 4 additions & 3 deletions test/doc/combinatorial/regular/prime_ceil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ main()
w32_t pi32 = {0, 6, 6542, 15, 104729, 104730, 10000, 1000};

std::cout << "---- simd" << '\n'
<< " <- pi32 = " << pi32 << '\n'
<< " -> prime_ceil(pi32) = " << eve::prime_ceil(pi32)
<< " // note 0 meaning out of implemented range\n";
<< " <- pi32 = " << pi32 << '\n'
<< " -> prime_ceil(pi32) = " << eve::prime_ceil(pi32)
<< " // note 0 meaning out of implemented range\n"
<< " -> prime_ceil(pi16, as<float>()) = " << eve::prime_ceil(pi32, eve::as<float>())<< '\n';

std::uint32_t xi = 18;
std::cout << "---- scalar" << '\n'
Expand Down
8 changes: 5 additions & 3 deletions test/doc/combinatorial/regular/prime_floor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ main()
w32_t pi32 = {2, 6, 6542, 15, 1, 200000, 10000, 1000};

std::cout << "---- simd" << '\n'
<< " <- pi32 = " << pi32 << '\n'
<< " -> prime_floor(pi32) = " << eve::prime_floor(pi32)
<< " // note 0 meaning out of implemented range\n";
<< " <- pi32 = " << pi32 << '\n'
<< " -> prime_floor(pi32) = " << eve::prime_floor(pi32)
<< " // note 0 meaning out of implemented range\n"
<< " -> prime_floor(pi32, as<float>()) = " << eve::prime_floor(pi32, eve::as<float>())
<< " // note nan meaning out of implemented range\n" << '\n';

std::uint32_t xi = 18;
std::cout << "---- scalar" << '\n'
Expand Down
Loading

0 comments on commit 0aaeacd

Please sign in to comment.