| 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 <eve/module/core/regular/if_else.hpp> | ||
| 15 | |||
| 16 | namespace plf | ||
| 17 | { | ||
| 18 | |||
| 19 | template<typename Options> struct clamp_t : eve::strict_tuple_callable<clamp_t, Options, raw_option, pedantic_option> | ||
| 20 | { | ||
| 21 | template<typename... Ts> struct result : as_polyfloat_like<Ts...> | ||
| 22 | { | ||
| 23 | }; | ||
| 24 | |||
| 25 | template<concepts::polyfloat_like Z0, concepts::polyfloat_like Z1, concepts::polyfloat_like Z2> | ||
| 26 | 16 | POLYFLOAT_ABI typename result<Z0, Z1, Z2>::type constexpr operator()(Z0 z0, Z1 z1, Z2 z2) const noexcept | |
| 27 | { | ||
| 28 | 16 | return POLYFLOAT_CALL(z0, z1, z2); | |
| 29 | } | ||
| 30 | |||
| 31 | 14 | POLYFLOAT_CALLABLE_OBJECT(clamp_t, clamp_); | |
| 32 | }; | ||
| 33 | //====================================================================================================================== | ||
| 34 | //! @addtogroup core | ||
| 35 | //! @{ | ||
| 36 | //! @var clamp | ||
| 37 | //! @brief clamp the value between two bounds. | ||
| 38 | //! | ||
| 39 | //! @groupheader{Header file} | ||
| 40 | //! | ||
| 41 | //! @code | ||
| 42 | //! #include <polyfloat/core.hpp> | ||
| 43 | //! @endcode | ||
| 44 | //! | ||
| 45 | //! @groupheader{Callable Signatures} | ||
| 46 | //! | ||
| 47 | //! @code | ||
| 48 | //! namespace polyfloat | ||
| 49 | //! { | ||
| 50 | //! constexpr auto clamp(value auto x, value auto lo, value auto hi) noexcept; // 1 | ||
| 51 | //! } | ||
| 52 | //! @endcode | ||
| 53 | //! | ||
| 54 | //! **Parameters** | ||
| 55 | //! | ||
| 56 | //! * `x`: value to clamp. | ||
| 57 | //! * `lo`, `hi`: [the boundary values](@ref eve::value) of the interval. | ||
| 58 | //! | ||
| 59 | //! **Return value** | ||
| 60 | //! 1. Each [element](@ref glossary_elementwise) of the result contains: | ||
| 61 | //! * `lo`, if `x` is less than `lo`. | ||
| 62 | //! * `hi`, if `hi` is less than `x`. | ||
| 63 | //! * otherwise `x`. | ||
| 64 | //! | ||
| 65 | //! @groupheader{Example} | ||
| 66 | //! | ||
| 67 | //! @godbolt{doc/core/clamp.cpp} | ||
| 68 | //====================================================================================================================== | ||
| 69 | |||
| 70 | inline constexpr auto clamp = eve::functor<clamp_t>; | ||
| 71 | //====================================================================================================================== | ||
| 72 | //! @} | ||
| 73 | //====================================================================================================================== | ||
| 74 | } | ||
| 75 | |||
| 76 | namespace plf::_ | ||
| 77 | { | ||
| 78 | template<typename T0, typename T1, typename T2, eve::callable_options O> | ||
| 79 | 14 | constexpr auto clamp_(POLYFLOAT_DELAY(), O const&, T0 a, T1 l, T2 h) noexcept | |
| 80 | { | ||
| 81 | 14 | return plf::min(plf::max(a, l), h); | |
| 82 | } | ||
| 83 | } | ||
| 84 |