| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | /* | ||
| 3 | POLYFLOAT - Extended precision floating points | ||
| 4 | Copyright : POLYFLOAT Contributors & Maintainers | ||
| 5 | SPDX-License-Identifier: BSL-1.0 | ||
| 6 | */ | ||
| 7 | //====================================================================================================================== | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <polyfloat/details/callable.hpp> | ||
| 11 | #include <polyfloat/types/concepts.hpp> | ||
| 12 | #include <polyfloat/types/traits.hpp> | ||
| 13 | #include <type_traits> | ||
| 14 | #include <polyfloat/module/math/constants/log_2.hpp> | ||
| 15 | |||
| 16 | namespace plf | ||
| 17 | { | ||
| 18 | |||
| 19 | template<typename Options> | ||
| 20 | struct exp2_t : plf::elementwise_callable<exp2_t, eve::exp2_t, Options, raw_option, pedantic_option> | ||
| 21 | { | ||
| 22 | 128 | template<concepts::polyfloat_like Z> POLYFLOAT_ABI constexpr Z operator()(Z z) const noexcept | |
| 23 | { | ||
| 24 | 128 | return POLYFLOAT_CALL(z); | |
| 25 | } | ||
| 26 | |||
| 27 | 118 | POLYFLOAT_CALLABLE_OBJECT(exp2_t, exp2_); | |
| 28 | }; | ||
| 29 | //====================================================================================================================== | ||
| 30 | //! @addtogroup math | ||
| 31 | //! @{ | ||
| 32 | //! @var exp2 | ||
| 33 | //! @brief return \f$2^x\f$. | ||
| 34 | //! | ||
| 35 | //! @groupheader{Header file} | ||
| 36 | //! | ||
| 37 | //! @code | ||
| 38 | //! #include <polyfloat/core.hpp> | ||
| 39 | //! @endcode | ||
| 40 | //! | ||
| 41 | //! @groupheader{Callable Signatures} | ||
| 42 | //! | ||
| 43 | //! @code | ||
| 44 | //! namespace polyfloat | ||
| 45 | //! { | ||
| 46 | //! template<polyfloat::concepts::polyfloat_like T> constexp2r auto exp2(T x) noexcept; | ||
| 47 | //! } | ||
| 48 | //! @endcode | ||
| 49 | //! | ||
| 50 | //! **Parameters** | ||
| 51 | //! | ||
| 52 | //! * `z`: Value to process. | ||
| 53 | //! | ||
| 54 | //! **Return value** | ||
| 55 | //! | ||
| 56 | //! Returns \f$2^z\f$. If x is greater than plf::maxlog2 the result is inf. | ||
| 57 | //! | ||
| 58 | //! @groupheader{Example} | ||
| 59 | //! | ||
| 60 | //! @godbolt{doc/math/exp2.cpp} | ||
| 61 | //====================================================================================================================== | ||
| 62 | |||
| 63 | inline constexpr auto exp2 = eve::functor<exp2_t>; | ||
| 64 | //====================================================================================================================== | ||
| 65 | //! @} | ||
| 66 | //====================================================================================================================== | ||
| 67 | } | ||
| 68 | |||
| 69 | namespace plf::_ | ||
| 70 | { | ||
| 71 | |||
| 72 | 118 | template<typename T, eve::callable_options O> constexpr auto exp2_(POLYFLOAT_DELAY(), O const&, T xx) noexcept | |
| 73 | { | ||
| 74 | using u_t = eve::underlying_type_t<T>; | ||
| 75 | 118 | auto negative = plf::is_ltz(xx); | |
| 76 | 118 | auto nan = plf::is_nan(xx); | |
| 77 | 118 | auto out_of_range = plf::is_greater(plf::abs(xx), plf::maxlog2(eve::as<u_t>())); | |
| 78 | 118 | xx = plf::if_else(out_of_range, inf, xx); | |
| 79 | 118 | auto x = plf::if_else(out_of_range || nan, zero, xx); | |
| 80 | 118 | auto n = plf::hi(nearest(xx)); | |
| 81 | 118 | auto xf = (x - n) * plf::log_2(eve::as(xx)); | |
| 82 | 118 | auto r = plf::ldexp(plf::exp(xf), n); | |
| 83 | 118 | r = if_else(out_of_range, plf::if_else(negative, eve::zero, plf::inf(eve::as<T>())), r); | |
| 84 | 118 | r = if_else(nan, xx, r); | |
| 85 | 118 | return r; | |
| 86 | } | ||
| 87 | } | ||
| 88 |