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

eve::abs as a demo for elementwise handling + decorator wrappings #1704

Merged
merged 12 commits into from
Dec 17, 2023
4 changes: 2 additions & 2 deletions benchmarks/module/core/abs/saturated/abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ int main()
auto arg0 = eve::bench::random_<EVE_VALUE>(lmin,lmax);

eve::bench::experiment xp;
run<EVE_VALUE> (EVE_NAME(saturated(eve::abs)) , xp, eve::saturated(eve::abs), arg0);
run<EVE_TYPE> (EVE_NAME(saturated(eve::abs)) , xp, eve::saturated(eve::abs), arg0);
run<EVE_VALUE> (EVE_NAME(eve::abs[eve::saturated]) , xp, eve::abs[eve::saturated], arg0);
run<EVE_TYPE> (EVE_NAME(eve::abs[eve::saturated]) , xp, eve::abs[eve::saturated], arg0);
}
27 changes: 11 additions & 16 deletions examples/oop/complex_numbers__declaring_an_object_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
//
// Writing custom cmplx numbers.
//
// NOTE: we will definetly have a proper `eve::complex` but we don't have one yet.
// NOTE: SIMD compatible complex numbers and beyond are available in https://github.com/jfalcou/kyosu
//
// This example will demonstrate the basics of how you can write code that looks like
// it uses objects but actally utilizes parallel arrays and simd under the hood.
// it uses objects but actually utilizes parallel arrays and simd under the hood.
//
// Related termins are: SOA (structure of arrays) and ECS - entity component system.
// Related terms are: SOA (structure of arrays) and ECS - entity component system.
//
// NOTE:
// You might also want to have a look at:
Expand Down Expand Up @@ -92,24 +92,19 @@ struct cmplx : eve::struct_support<cmplx, float, float>
return self;
}

// The ostream operator you don't need to customise for wide, it will do the right thing.
friend std::ostream& operator<<(std::ostream& out, cmplx self)
friend EVE_FORCEINLINE auto abs(eve::like<cmplx> auto self)
{
return out << '(' << re(self) << " + " << im(self) << "i)";
return eve::hypot(re(self), im(self));
}

// Custoizing eve functions ----

// All eve callables are customiseable through tagged dispatch.
// We find it useful but you do it at your own risk, we are quite likely to break you.
// Alternatively - use your own functions.

EVE_FORCEINLINE friend auto tagged_dispatch( eve::tag::abs_, eve::like<cmplx> auto self)
// The ostream operator you don't need to customise for wide, it will do the right thing.
friend std::ostream& operator<<(std::ostream& out, cmplx self)
{
return eve::hypot(re(self), im(self));
return out << '(' << re(self) << " + " << im(self) << "i)";
}
};


// ------------------
// Using

Expand Down Expand Up @@ -179,7 +174,7 @@ TTS_CASE("wide works")
{
eve::wide<cmplx> x{cmplx{3.0, 4.0}};
eve::wide<float> expected{5.0};
eve::wide<float> actual = eve::abs(x);
eve::wide<float> actual = abs(x);
TTS_RELATIVE_EQUAL(expected, actual, 0.00001);
}
};
Expand Down Expand Up @@ -219,7 +214,7 @@ TTS_CASE("scalar works")
{
cmplx x{3.0, 4.0};
float expected{5.0};
float actual = eve::abs(x);
float actual = abs(x);
TTS_RELATIVE_EQUAL(expected, actual, 0.00001);
}
};
Expand Down
52 changes: 0 additions & 52 deletions include/eve/module/core/decorator/compensated.hpp

This file was deleted.

87 changes: 84 additions & 3 deletions include/eve/module/core/decorator/core.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//==================================================================================================
//======================================================================================================================
/**
EVE - Expressive Vector Engine
Copyright : EVE Project Contributors
SPDX-License-Identifier: BSL-1.0
**/
//==================================================================================================
//======================================================================================================================
#pragma once

#include <eve/module/core/decorator/compensated.hpp>
#include <eve/module/core/decorator/cyl.hpp>
#include <eve/module/core/decorator/fuzzy.hpp>
#include <eve/module/core/decorator/kind.hpp>
Expand All @@ -22,3 +21,85 @@
#include <eve/module/core/decorator/saturated.hpp>
#include <eve/module/core/decorator/sph.hpp>
#include <eve/module/core/decorator/successor.hpp>

//======================================================================================================================
// New option style - TODO rename later without the '2'
//======================================================================================================================
#include <eve/detail/raberu.hpp>

namespace eve
{
struct almost_mode {};
struct definitely_mode {};
struct kind_1_mode {};
struct kind_2_mode {};
struct musl_mode {};
struct numeric_mode {};
struct p_kind_mode {};
struct pedantic_mode {};
struct plain_mode {};
struct promote_mode {};
struct q_kind_mode {};
struct raw_mode {};
struct regular_mode {};
struct saturated_mode {};
struct spherical_mode {};
struct successor_mode {};
struct tolerant_mode {};

struct rounding_key_t : rbr::as_keyword<rounding_key_t>
{
template<typename Value> constexpr auto operator=(Value const&) const noexcept
{
return rbr::option<rounding_key_t,Value>{};
}
};
inline constexpr rounding_key_t rounding_key = {};

template<int N>
inline constexpr auto rounding = (rounding_key = eve::index<N>);

[[maybe_unused]] inline constexpr auto const almost2 = ::rbr::flag( almost_mode{} );
[[maybe_unused]] inline constexpr auto const definitely2 = ::rbr::flag( definitely_mode{});
[[maybe_unused]] inline constexpr auto const downward2 = rounding<(0x01 | 0x08)>;
[[maybe_unused]] inline constexpr auto const kind_12 = ::rbr::flag( kind_1_mode{} );
[[maybe_unused]] inline constexpr auto const kind_22 = ::rbr::flag( kind_2_mode{} );
[[maybe_unused]] inline constexpr auto const musl2 = ::rbr::flag( musl_mode{} );
[[maybe_unused]] inline constexpr auto const numeric2 = ::rbr::flag( numeric_mode{} );
[[maybe_unused]] inline constexpr auto const p_kind2 = ::rbr::flag( p_kind_mode{} );
[[maybe_unused]] inline constexpr auto const pedantic2 = ::rbr::flag( pedantic_mode{} );
[[maybe_unused]] inline constexpr auto const plain2 = ::rbr::flag( plain_mode{} );
[[maybe_unused]] inline constexpr auto const promote2 = ::rbr::flag( promote_mode{} );
[[maybe_unused]] inline constexpr auto const q_kind2 = ::rbr::flag( q_kind_mode{} );
[[maybe_unused]] inline constexpr auto const raw2 = ::rbr::flag( raw_mode{} );
[[maybe_unused]] inline constexpr auto const regular2 = ::rbr::flag( regular_mode{} );
[[maybe_unused]] inline constexpr auto const saturated2 = ::rbr::flag( saturated_mode{} );
[[maybe_unused]] inline constexpr auto const spherical = ::rbr::flag( spherical_mode{} );
[[maybe_unused]] inline constexpr auto const successor2 = ::rbr::flag( successor_mode{} );
[[maybe_unused]] inline constexpr auto const to_nearest2 = rounding<(0x00 | 0x08)>;
[[maybe_unused]] inline constexpr auto const tolerant2 = ::rbr::flag( tolerant_mode{} );
[[maybe_unused]] inline constexpr auto const toward_zero2 = rounding<(0x03 | 0x08)>;
[[maybe_unused]] inline constexpr auto const upward2 = rounding<(0x02 | 0x08)>;

inline constexpr auto as_option(almost_type const&) { return almost2; }
inline constexpr auto as_option(definitely_type const&) { return definitely2; }
inline constexpr auto as_option(downward_type const&) { return downward2; }
inline constexpr auto as_option(kind_1_type const&) { return kind_12; }
inline constexpr auto as_option(kind_2_type const&) { return kind_22; }
inline constexpr auto as_option(musl_type const&) { return musl2; }
inline constexpr auto as_option(numeric_type const&) { return numeric; }
inline constexpr auto as_option(p_kind_type const&) { return p_kind2; }
inline constexpr auto as_option(pedantic_type const&) { return pedantic2; }
inline constexpr auto as_option(plain_type const&) { return plain2; }
inline constexpr auto as_option(promote_type const&) { return promote2; }
inline constexpr auto as_option(q_kind_type const&) { return q_kind2; }
inline constexpr auto as_option(raw_type const&) { return raw2; }
inline constexpr auto as_option(regular_type const&) { return regular2; }
inline constexpr auto as_option(saturated_type const&) { return saturated2; }
inline constexpr auto as_option(sph_type const&) { return spherical; }
inline constexpr auto as_option(successor_type const&) { return successor2; }
inline constexpr auto as_option(to_nearest_type const&) { return to_nearest2; }
inline constexpr auto as_option(tolerant_type const&) { return tolerant2; }
inline constexpr auto as_option(toward_zero_type const&) { return toward_zero2; }
inline constexpr auto as_option(upward_type const&) { return upward2; }
}
20 changes: 4 additions & 16 deletions include/eve/module/core/decorator/musl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,15 @@
//==================================================================================================
#pragma once

#include <eve/detail/abi.hpp>
#include <eve/detail/overload.hpp>

namespace eve
{
//================================================================================================
// Function decorators mark-up used in function overloads
struct musl_type : decorator_
struct musl_t
{
template<typename Function> constexpr EVE_FORCEINLINE auto operator()(Function f) const noexcept
{
return [f](auto&&...args) { return f(musl_type {}, EVE_FWD(args)...); };
}
template<typename D> static constexpr auto combine(D const&) noexcept = delete;
};

//================================================================================================
// Function decorator - musl mode
template<typename Function>
constexpr EVE_FORCEINLINE auto
musl_(Function f) noexcept
{
return musl_type {}(f);
}
using musl_type = decorated<musl_t()>;
DenisYaroshevskiy marked this conversation as resolved.
Show resolved Hide resolved
inline constexpr musl_type const musl_ = {};
}
20 changes: 4 additions & 16 deletions include/eve/module/core/decorator/plain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,15 @@
//==================================================================================================
#pragma once

#include <eve/detail/abi.hpp>
#include <eve/detail/overload.hpp>

namespace eve
{
//================================================================================================
// Function decorators mark-up used in function overloads
struct plain_type : decorator_
struct plain_
{
template<typename Function> constexpr EVE_FORCEINLINE auto operator()(Function f) const noexcept
{
return [f](auto&&...args) { return f(plain_type {}, EVE_FWD(args)...); };
}
template<typename D> static constexpr auto combine(D const&) noexcept = delete;
};

//================================================================================================
// Function decorator - plain mode
template<typename Function>
constexpr EVE_FORCEINLINE auto
plain(Function f) noexcept
{
return plain_type {}(f);
}
using plain_type = decorated<plain_()>;
inline constexpr plain_type const plain = {};
}
1 change: 0 additions & 1 deletion include/eve/module/core/pedantic/impl/absmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <eve/module/core/regular/all.hpp>
#include <eve/module/core/regular/is_nan.hpp>
#include <eve/module/core/regular/is_not_greater_equal.hpp>
#include <eve/module/core/saturated/abs.hpp>
#include <eve/arch/platform.hpp>

#include <type_traits>
Expand Down
1 change: 0 additions & 1 deletion include/eve/module/core/pedantic/impl/absmin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <eve/module/core/regular/all.hpp>
#include <eve/module/core/regular/is_nan.hpp>
#include <eve/module/core/regular/is_not_greater_equal.hpp>
#include <eve/module/core/saturated/abs.hpp>
#include <eve/arch/platform.hpp>

#include <type_traits>
Expand Down
6 changes: 3 additions & 3 deletions include/eve/module/core/pedantic/impl/maxmag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <eve/module/core/regular/if_else.hpp>
#include <eve/module/core/regular/is_nan.hpp>
#include <eve/module/core/regular/is_not_greater_equal.hpp>
#include <eve/module/core/saturated/abs.hpp>
#include <eve/module/core/regular/abs.hpp>
#include <eve/arch/platform.hpp>

#include <type_traits>
Expand All @@ -39,8 +39,8 @@ template<ordered_value T>
EVE_FORCEINLINE auto
maxmag_(EVE_SUPPORTS(cpu_), pedantic_type const&, T const& a, T const& b) noexcept
{
auto aa = saturated(eve::abs)(a);
auto bb = saturated(eve::abs)(b);
auto aa = eve::abs[saturated](a);
auto bb = eve::abs[saturated](b);
if constexpr( simd_value<T> )
{
auto tmp = if_else(is_not_greater_equal(aa, bb), b, pedantic(eve::max)(a, b));
Expand Down
6 changes: 3 additions & 3 deletions include/eve/module/core/pedantic/impl/minmag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <eve/module/core/regular/if_else.hpp>
#include <eve/module/core/regular/is_nan.hpp>
#include <eve/module/core/regular/is_not_greater_equal.hpp>
#include <eve/module/core/saturated/abs.hpp>
#include <eve/module/core/regular/abs.hpp>
#include <eve/arch/platform.hpp>

#include <type_traits>
Expand All @@ -39,8 +39,8 @@ template<ordered_value T>
EVE_FORCEINLINE auto
minmag_(EVE_SUPPORTS(cpu_), pedantic_type const&, T const& a, T const& b) noexcept
{
auto aa = saturated(eve::abs)(a);
auto bb = saturated(eve::abs)(b);
auto aa = eve::abs[saturated](a);
auto bb = eve::abs[saturated](b);
if constexpr( simd_value<T> )
{
auto tmp = if_else(is_not_greater_equal(bb, aa), b, pedantic(eve::min)(a, b));
Expand Down
2 changes: 1 addition & 1 deletion include/eve/module/core/pedantic/impl/negabsmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <eve/module/core/decorator/pedantic.hpp>
#include <eve/module/core/pedantic/absmax.hpp>
#include <eve/module/core/regular/all.hpp>
#include <eve/module/core/saturated/abs.hpp>
#include <eve/module/core/regular/abs.hpp>
#include <eve/arch/platform.hpp>

#include <type_traits>
Expand Down
2 changes: 1 addition & 1 deletion include/eve/module/core/pedantic/impl/negabsmin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <eve/module/core/decorator/pedantic.hpp>
#include <eve/module/core/pedantic/absmin.hpp>
#include <eve/module/core/regular/all.hpp>
#include <eve/module/core/saturated/abs.hpp>
#include <eve/module/core/regular/abs.hpp>
#include <eve/arch/platform.hpp>

#include <type_traits>
Expand Down
2 changes: 1 addition & 1 deletion include/eve/module/core/pedantic/impl/negmaxabs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <eve/module/core/decorator/pedantic.hpp>
#include <eve/module/core/pedantic/maxabs.hpp>
#include <eve/module/core/regular/all.hpp>
#include <eve/module/core/saturated/abs.hpp>
#include <eve/module/core/regular/abs.hpp>
#include <eve/arch/platform.hpp>

#include <type_traits>
Expand Down
2 changes: 1 addition & 1 deletion include/eve/module/core/pedantic/impl/negminabs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <eve/module/core/decorator/pedantic.hpp>
#include <eve/module/core/pedantic/minabs.hpp>
#include <eve/module/core/regular/all.hpp>
#include <eve/module/core/saturated/abs.hpp>
#include <eve/module/core/regular/abs.hpp>
#include <eve/arch/platform.hpp>

#include <type_traits>
Expand Down
Loading