GCC Code Coverage Report


Directory: include/polyfloat/
File: include/polyfloat/module/math/cos.hpp
Date: 2026-11-09 15:28:50
Exec Total Coverage
Lines: 16 19 84.2%
Functions: 137 149 91.9%
Branches: 1 4 25.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/math/details/sincos_coefs.hpp>
15 #include <polyfloat/module/math/details/trig_finalize.hpp>
16 #include <polyfloat/module/math/details/rempio2_limits.hpp>
17 #include <polyfloat/module/math/details/pio2_reduce.hpp>
18 #include <polyfloat/module/math/details/rem2.hpp>
19 #include <polyfloat/module/math/details/rem180.hpp>
20 #include <polyfloat/module/math/div_180.hpp>
21
22 namespace plf
23 {
24
25 template<typename Options>
26 struct cos_t : plf::elementwise_callable<cos_t,
27 eve::cos_t,
28 Options,
29 raw_option,
30 pedantic_option,
31 eve::quarter_circle_option,
32 eve::half_circle_option,
33 eve::full_circle_option,
34 eve::medium_option,
35 eve::big_option,
36 eve::radpi_option,
37 eve::deg_option>
38 {
39 412 template<concepts::polyfloat_like Z> POLYFLOAT_ABI constexpr Z operator()(Z z) const noexcept
40 {
41 412 return POLYFLOAT_CALL(z);
42 }
43
44 388 POLYFLOAT_CALLABLE_OBJECT(cos_t, cos_);
45 };
46 //======================================================================================================================
47 //! @addtogroup math
48 //! @{
49 //! @var cos
50 //! @brief return the cosine value.
51 //!
52 //! @groupheader{Header file}
53 //!
54 //! @code
55 //! #include <polyfloat/core.hpp>
56 //! @endcode
57 //!
58 //! @groupheader{Callable Signatures}
59 //!
60 //! @code
61 //! namespace polyfloat
62 //! {
63 //! template<polyfloat::concepts::polyfloat_like T> constexpr auto cos(T z) noexcept;
64 //! }
65 //! @endcode
66 //!
67 //! **Parameters**
68 //!
69 //! * `z`: Value to process.
70 //!
71 //! **Return value**
72 //!
73 //! Returns the cosine of z.
74 //!
75 //! @groupheader{Example}
76 //!
77 //! @godbolt{doc/math/cos.cpp}
78 //======================================================================================================================
79
80 inline constexpr auto cos = eve::functor<cos_t>;
81 //======================================================================================================================
82 //! @}
83 //======================================================================================================================
84 }
85
86 namespace plf::_
87 {
88 388 template<typename T, eve::callable_options O> constexpr auto cos_(POLYFLOAT_DELAY(), O const&, T a0) noexcept
89 {
90 // using e_t = eve::element_type_t<T>;
91 if constexpr (O::contains(eve::deg))
92 {
93 60 return plf::cos[eve::radpi](plf::div_180(a0));
94 }
95 else if constexpr (O::contains(eve::radpi))
96 {
97 if constexpr (O::contains(eve::quarter_circle))
98 {
99 return plf::cos[eve::quarter_circle](a0 * plf::pi(eve::as<T>()));
100 }
101 else
102 {
103 112 auto x = plf::abs(a0);
104 112 x = plf::if_else(plf::is_not_finite(x), plf::nan(eve::as(x)), x); // nan or Inf input
105 112 x = plf::if_else(plf::is_greater(x, plf::maxflint(eve::as(x))), eve::zero, x);
106 336 auto [fn, xr, dxr] = plf::rem2(x);
107 112 return cos_finalize(fn, xr, dxr);
108 }
109 }
110 else
111 {
112 if constexpr (O::contains(eve::quarter_circle))
113 {
114 36 return cos_eval(a0);
115 }
116 else if constexpr (O::contains(eve::half_circle) || O::contains(eve::full_circle) || O::contains(eve::medium))
117 {
118 144 auto x = plf::abs(a0);
119 432 auto [fn, xr, dxr] = pio_2_reduce(x);
120 144 return cos_finalize(fn, xr, dxr);
121 }
122 else
123 {
124 36 auto x = abs(a0);
125
1/2
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
36 if (eve::all(x <= Rempio2_limit[quarter_circle](as(a0)))) return cos[quarter_circle](a0);
126 ✗ else if (eve::all(x <= Rempio2_limit[medium](as(a0)))) return cos[medium](a0);
127 else
128 ✗ return cos[medium](
129 ✗ plf::if_else(is_not_less_equal(x, Rempio2_limit[medium](as(a0))), nan(eve::as(a0)), cos[medium](a0)));
130 //else return cos[big](a0); TO DO REPLACE previous line
131 }
132 }
133 }
134 }
135
136 namespace plf
137 {
138 constexpr auto cosd = plf::cos[eve::deg];
139 constexpr auto cospi = plf::cos[eve::radpi];
140 }
141