Skip to content

Commit

Permalink
New style for exponent, mantissa, frac and byte_reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
jtlap authored May 1, 2024
1 parent a2e2cf5 commit 18bcfaf
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 412 deletions.
76 changes: 72 additions & 4 deletions include/eve/module/core/regular/byte_reverse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@
//==================================================================================================
#pragma once

#include <eve/detail/overload.hpp>
#include <eve/arch.hpp>
#include <eve/traits/overload.hpp>
#include <eve/module/core/decorator/core.hpp>
#include <eve/module/core/regular/bit_swap_adjacent.hpp>
#include <eve/detail/builtin_detect.hpp>
#include <eve/forward.hpp>
#include <eve/module/core/regular/bit_cast.hpp>
#include <eve/module/core/regular/bit_shl.hpp>
#include <eve/module/core/regular/bit_shr.hpp>
#include <eve/module/core/regular/shuffle.hpp>

namespace eve
{

template<typename Options>
struct byte_reverse_t : elementwise_callable<byte_reverse_t, Options>
{
template<eve::integral_value T>
constexpr EVE_FORCEINLINE T operator()(T v) const
{ return EVE_DISPATCH_CALL(v); }

EVE_CALLABLE_OBJECT(byte_reverse_t, byte_reverse_);
};

//================================================================================================
//! @addtogroup core_bitops
//! @{
Expand Down Expand Up @@ -59,7 +79,55 @@ namespace eve
//!
//! @}
//================================================================================================
EVE_MAKE_CALLABLE(byte_reverse_, byte_reverse);
}
inline constexpr auto byte_reverse = functor<byte_reverse_t>;

namespace detail
{
template<unsigned_scalar_value T> T bswap(T x)
{
constexpr size_t S = sizeof(T);
auto bs=[](auto x){
auto b = std::bit_cast<std::array<std::uint8_t, S>>(x);
std::reverse(b.begin(), b.end());
return std::bit_cast<T>(b);
};
if constexpr(sizeof(T)==1) return x;
else if constexpr(sizeof(T)==2) return bit_shl(x, 8) | bit_shr(x, 8);
else if constexpr(sizeof(T)==4)
{
if constexpr(has_builtin_swap32()) return builtin_bswap32(x);
else return bs(x);
}
else if constexpr(sizeof(T)==8)
{
if constexpr(has_builtin_swap64()) return builtin_bswap64(x);
else return bs(x);
}
}

#include <eve/module/core/regular/impl/byte_reverse.hpp>
template<typename T, callable_options O>
EVE_FORCEINLINE constexpr T
byte_reverse_(EVE_REQUIRES(cpu_), O const&, T const& x) noexcept
{
if constexpr(scalar_value<T>)
{
using u_t = as_uinteger_t<T>;
return std::bit_cast<T>(bswap(std::bit_cast<u_t>(x)));
}
else
{
using e_t = element_type_t<T>;
constexpr auto S = sizeof(e_t);
if constexpr(S == 1) return x;
else
{
constexpr auto C = cardinal_v<T>;
using u8_t = wide<uint8_t, fixed<S*C>>;
auto p = [] (auto i, auto ) { auto S = sizeof(e_t); return (i/S+1)*S-1-i%S; };
auto y = eve::shuffle(bit_cast(x, as<u8_t>()), eve::as_pattern(p));
return bit_cast(y, as<T>());
}
}
}
}
}
34 changes: 30 additions & 4 deletions include/eve/module/core/regular/copysign.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
//==================================================================================================
#pragma once

#include <eve/detail/overload.hpp>
#include <eve/arch.hpp>
#include <eve/traits/overload.hpp>
#include <eve/module/core/decorator/core.hpp>
#include <eve/module/core/constant/signmask.hpp>
#include <eve/module/core/regular/bit_notand.hpp>
#include <eve/module/core/regular/bit_or.hpp>
#include <eve/module/core/regular/bitofsign.hpp>

namespace eve
{
template<typename Options>
struct copysign_t : elementwise_callable<copysign_t, Options>
{
template<value T, value U>
constexpr EVE_FORCEINLINE common_value_t<T, U> operator()(T a, U b) const
{ return EVE_DISPATCH_CALL(a, b); }

EVE_CALLABLE_OBJECT(copysign_t, copysign_);
};

//================================================================================================
//! @addtogroup core_arithmetic
//! @{
Expand Down Expand Up @@ -58,7 +74,17 @@ namespace eve
//!
//! @}
//================================================================================================
EVE_MAKE_CALLABLE(copysign_, copysign);
}
inline constexpr auto copysign = functor<copysign_t>;

#include <eve/module/core/regular/impl/copysign.hpp>
namespace detail
{
template<floating_value T, floating_value U, callable_options O>
EVE_FORCEINLINE constexpr auto copysign_(EVE_REQUIRES(cpu_), O const &, T aa, U bb) noexcept
{
using r_t = common_value_t<T, U>;
r_t a = r_t(aa);
r_t b = r_t(bb);
return bit_or(bitofsign(b), bit_notand(signmask(eve::as(a)), a));
}
}
}
49 changes: 45 additions & 4 deletions include/eve/module/core/regular/exponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,33 @@
#pragma once

#include <eve/arch.hpp>
#include <eve/detail/overload.hpp>
#include <eve/traits/overload.hpp>
#include <eve/module/core/decorator/core.hpp>

#include <eve/module/core/constant/mantissamask.hpp>
#include <eve/module/core/constant/one.hpp>
#include <eve/module/core/regular/bit_and.hpp>
#include <eve/module/core/regular/bit_or.hpp>
#include <eve/module/core/regular/if_else.hpp>
#include <eve/module/core/regular/is_eqz.hpp>
#include <eve/module/core/regular/is_not_finite.hpp>
#include <eve/module/core/regular/logical_not.hpp>
#include <eve/module/core/regular/logical_or.hpp>
#include <eve/arch/platform.hpp>

namespace eve
{

template<typename Options>
struct exponent_t : elementwise_callable<exponent_t, Options>
{
template<eve::value T>
constexpr EVE_FORCEINLINE as_integer_t<T> operator()(T v) const noexcept
{ return EVE_DISPATCH_CALL(v); }

EVE_CALLABLE_OBJECT(exponent_t, exponent_);
};

//================================================================================================
//! @addtogroup core_internal
//! @{
Expand Down Expand Up @@ -53,11 +76,29 @@ namespace eve
//! @godbolt{doc/core/exponent.cpp}
//! @}
//================================================================================================
EVE_MAKE_CALLABLE(exponent_, exponent);
inline constexpr auto exponent = functor<exponent_t>;

namespace detail
{
template<floating_value T, callable_options O>
constexpr as_integer_t<T> exponent_(EVE_REQUIRES(cpu_), O const&, T const& a) noexcept
{
auto z = bit_and(exponentmask(as<T>()), a);
if constexpr( scalar_value<T> )
{
if( is_not_finite(a) ) return as_integer_t<T>(0);
auto x = (z >> nbmantissabits(eve::as<T>()));
return sub[is_nez(a)](x, maxexponent(eve::as<T>()));
}
else
{
auto x = (z >> nbmantissabits(eve::as<T>()));
return if_else(is_not_finite(a), eve::zero, sub[is_nez(a)](x, maxexponent(eve::as<T>())));
}
}
}
}

#include <eve/module/core/regular/impl/exponent.hpp>

#if defined(EVE_INCLUDE_X86_HEADER)
# include <eve/module/core/regular/impl/simd/x86/exponent.hpp>
#endif
37 changes: 33 additions & 4 deletions include/eve/module/core/regular/frac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
//==================================================================================================
#pragma once

#include <eve/detail/overload.hpp>
#include <eve/arch.hpp>
#include <eve/traits/overload.hpp>
#include <eve/module/core/decorator/core.hpp>
#include <eve/module/core/regular/is_eqz.hpp>
#include <eve/module/core/regular/if_else.hpp>
#include <eve/module/core/regular/trunc.hpp>

namespace eve
{

template<typename Options>
struct frac_t : elementwise_callable<frac_t, Options>
{
template<eve::value T>
constexpr EVE_FORCEINLINE T operator()(T v) const
{ return EVE_DISPATCH_CALL(v); }

EVE_CALLABLE_OBJECT(frac_t, frac_);
};

//================================================================================================
//! @addtogroup core_arithmetic
//! @{
Expand Down Expand Up @@ -63,7 +79,20 @@ namespace eve
//!
//! @}
//================================================================================================
EVE_MAKE_CALLABLE(frac_, frac);
}
inline constexpr auto frac = functor<frac_t>;

#include <eve/module/core/regular/impl/frac.hpp>
namespace detail
{
template<typename T, callable_options O>
EVE_FORCEINLINE constexpr T
frac_(EVE_REQUIRES(cpu_), O const&, T const& a) noexcept
{
if constexpr( floating_value<T> )
{
if constexpr( scalar_value<T> ) return !a ? a : a - trunc(a);
else return if_else(is_eqz(a), a, a - trunc(a));
}
else return zero(eve::as(a));
}
}
}
89 changes: 0 additions & 89 deletions include/eve/module/core/regular/impl/byte_reverse.hpp

This file was deleted.

Loading

0 comments on commit 18bcfaf

Please sign in to comment.