GCC Code Coverage Report


Directory: include/polyfloat/
File: include/polyfloat/module/core/div.hpp
Date: 2026-11-09 15:28:50
Exec Total Coverage
Lines: 9 9 100.0%
Functions: 102 102 100.0%
Branches: 0 0 -%

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/core/trunc.hpp>
15 #include <polyfloat/module/core/nearest.hpp>
16 #include <polyfloat/module/core/ceil.hpp>
17 #include <polyfloat/module/core/floor.hpp>
18
19 namespace plf
20 {
21
22 template<typename Options>
23 struct div_t : eve::strict_tuple_callable<div_t,
24 Options,
25 raw_option,
26 pedantic_option,
27 to_nearest_option,
28 toward_zero_option,
29 downward_option,
30 upward_option>
31 {
32 template<concepts::polyfloat_like Z1, concepts::polyfloat_like Z2>
33 134 POLYFLOAT_ABI constexpr as_polyfloat_like_t<Z1, Z2> operator()(Z1 z1, Z2 z2) const noexcept
34 {
35 134 return POLYFLOAT_CALL(z1, z2);
36 }
37
38 134 POLYFLOAT_CALLABLE_OBJECT(div_t, div_);
39 };
40 //======================================================================================================================
41 //! @addtogroup core
42 //! @{
43 //! @var div
44 //! @brief return the ratio of the values.
45 //!
46 //! @groupheader{Header file}
47 //!
48 //! @code
49 //! #include <polyfloat/core.hpp>
50 //! @endcode
51 //!
52 //! @groupheader{Callable Signatures}
53 //!
54 //! @code
55 //! namespace polyfloat
56 //! {
57 //! template<polyfloat::concepts::polyfloat_like T1, polyfloat_like Z2> constexpr auto div(T1 z1, T2 z2) noexcept; //1
58 //!
59 //! // Semantic exclusive options
60 //! constexpr auto div[upward](/*any of the above overloads*/) noexcept; // 2
61 //! constexpr auto div[downward](/*any of the above overloads*/) noexcept; // 2
62 //! constexpr auto div[toward_zero](/*any of the above overloads*/) noexcept; // 2
63 //! constexpr auto div[to_nearest](/*any of the above overloads*/) noexcept; // 2
64 //! }
65 //! @endcode
66 //!
67 //! **Parameters**
68 //!
69 //! * `z1`, `z2`: Values to process.
70 //!
71 //! **Return value**
72 //!
73 //! 1. Returns the ratio of the arguments.
74 //! 2. Pproduces:
75 //! * `eve::trunc(div(x, z))`, if `d` is `toward_zero`.
76 //! * `eve::floor(div(x, z))`, if `d` is `downward`.
77 //! * `eve::ceil(div(x, z))`, if `d` is `upward`.
78 //! * `eve::nearest(div(x, z))`, if `d` is `to_nearest`.
79 //!
80 //! @groupheader{Example}
81 //!
82 //! @godbolt{doc/core/div.cpp}
83 //======================================================================================================================
84
85 inline constexpr auto div = eve::functor<div_t>;
86 //======================================================================================================================
87 //! @}
88 //======================================================================================================================
89 }
90
91 namespace plf::_
92 {
93 template<typename T0, typename T1, eve::callable_options O>
94 134 constexpr auto div_(POLYFLOAT_DELAY(), O const&, T0 const& a, T1 const& b) noexcept
95 {
96 using t_t = as_polyfloat_t<T0, T1>;
97 using u_t = eve::element_type_t<t_t>;
98 134 auto cvt = [](auto a) { return plf::convert(a, as<u_t>()); };
99 134 auto d = cvt(a) / cvt(b);
100 4 if constexpr (O::contains(to_nearest)) return nearest(d);
101 64 else if constexpr (O::contains(toward_zero)) return trunc(d);
102 else if constexpr (O::contains(downward)) return floor(d);
103 else if constexpr (O::contains(upward)) return ceil(d);
104 66 else return d;
105 }
106 }
107