Skip to content

Commit

Permalink
Make if_else less nit-picky
Browse files Browse the repository at this point in the history
  • Loading branch information
jfalcou committed Aug 30, 2023
1 parent a2bb7d9 commit a1d0958
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
13 changes: 8 additions & 5 deletions include/kyosu/types/impl/arithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ namespace kyosu::_
{
if constexpr(concepts::cayley_dickson<T> && concepts::cayley_dickson<U>)
{
using type = as_cayley_dickson_t<T,U>;
auto parts = kumi::map([&](auto const& v, auto const& w) { return eve::if_else(m, v, w); }, t, f);

if constexpr(eve::simd_value<Mask>) return eve::as_wide_as_t<type,Mask>{parts};
else return type{parts};
using type = as_cayley_dickson_t<T,U>;
using ret_t = std::conditional_t<eve::simd_value<Mask>, eve::as_wide_as_t<type,Mask>, type>;
constexpr eve::as<eve::element_type_t<type>> tgt = {};

return ret_t{ kumi::map ( [&](auto const& v, auto const& w) { return eve::if_else(m, v, w); }
, kyosu::convert(t, tgt), kyosu::convert(f, tgt)
)
};
}
else if constexpr(concepts::cayley_dickson<T> && !concepts::cayley_dickson<U>)
{
Expand Down
64 changes: 64 additions & 0 deletions test/unit/function/if_else.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//======================================================================================================================
/*
Kyosu - Complex Without Complexes
Copyright : KYOSU Contributors & Maintainers
SPDX-License-Identifier: BSL-1.0
*/
//======================================================================================================================
#include <kyosu/kyosu.hpp>
#include <test.hpp>

TTS_CASE_WITH ( "Check behavior of if_else on scalar cayley-dickson"
, tts::bunch<kyosu::scalar_real_types>
, tts::generate(tts::randoms(-100,+100))
)
<typename T>(T const& a0 )
{
using e_t = typename T::value_type;
for(auto e : a0)
{
auto m = eve::is_odd(e);
TTS_EQUAL ( kyosu::if_else( m, kyosu::complex<e_t>(0,-e), kyosu::complex<e_t>(0, e))
, kyosu::complex<e_t>(0,m ? -e : e)
);

TTS_EQUAL ( kyosu::if_else(m, kyosu::quaternion<e_t>(0,-e,1,-2), kyosu::quaternion<e_t>(0,e,-1, 2))
, kyosu::quaternion<e_t>(0,m ? -e : e,m ? 1 : -1,m ? -2 : 2)
);

TTS_EQUAL ( kyosu::if_else( eve::is_odd(e)
, kyosu::complex<e_t>(0,-e)
, kyosu::quaternion<e_t>(0, e,-1, 2)
)
, kyosu::quaternion<e_t>(0,m ? -e : e,m ? 0 : -1,m ? 0 : 2)
);
TTS_EQUAL ( kyosu::if_else( eve::is_odd(e)
, kyosu::quaternion<e_t>(0,-e, 1, -2)
, kyosu::complex<e_t>(0, e)
)
, kyosu::quaternion<e_t>(0,m ? -e : e,m ? 1 : 0,m ? -2 : 0)
);
}
};

TTS_CASE_WITH ( "Check behavior of if_else on SIMD cayley-dickson"
, tts::bunch<kyosu::scalar_real_types>
, tts::generate(tts::randoms(-100,+100))
)
<typename T>(T const& a0 )
{
using e_t = typename T::value_type;
using c_t = kyosu::complex<e_t>;
using q_t = kyosu::quaternion<e_t>;
using wc_t = eve::wide<c_t>;
using wq_t = eve::wide<q_t>;

for(auto e : a0)
{
auto m = eve::is_odd(e);
TTS_EQUAL(kyosu::if_else(m, wc_t(c_t(0,-e)) , wc_t(c_t(0,e))) , wc_t(c_t(0,m ? -e : e)));
TTS_EQUAL(kyosu::if_else(m, wq_t(q_t(0,-e,1,-2)), wq_t(q_t(0,e,-1,2))), wq_t(q_t(0,m ? -e : e,m ? 1 : -1,m ? -2 : 2)));
TTS_EQUAL(kyosu::if_else(m, wc_t(c_t(0,-e)) , wq_t(q_t(0,e,-1,2))), wq_t(q_t(0,m ? -e : e,m ? 0 : -1,m ? 0 : 2)));
TTS_EQUAL(kyosu::if_else(m, wq_t(q_t(0,-e,1,-2)), wc_t(c_t(0, e))) , wq_t(q_t(0,m ? -e : e,m ? 1 : 0,m ? -2 : 0)));
}
};

0 comments on commit a1d0958

Please sign in to comment.