GCC Code Coverage Report


Directory: include/polyfloat/
File: include/polyfloat/module/core/average.hpp
Date: 2026-11-09 15:28:50
Exec Total Coverage
Lines: 14 14 100.0%
Functions: 136 136 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/abs.hpp>
15 #include <polyfloat/module/core/parts.hpp>
16 #include <polyfloat/module/core/fma.hpp>
17 #include <polyfloat/module/core/rec.hpp>
18
19 namespace plf
20 {
21
22 template<typename Options>
23 struct average_t : eve::strict_tuple_callable<average_t, Options, raw_option, pedantic_option>
24 {
25 template<typename... Ts> struct result : as_polyfloat_like<Ts...>
26 {
27 };
28
29 template<concepts::polyfloat_like Z1, concepts::polyfloat_like... Zs>
30 926 POLYFLOAT_ABI constexpr as_polyfloat_like_t<Z1, Zs...> operator()(Z1 z1, Zs... zs) const noexcept
31 {
32 926 return POLYFLOAT_CALL(z1, zs...);
33 }
34
35 template<eve::non_empty_product_type Tup>
36 24 POLYFLOAT_ABI constexpr kumi::apply_traits_t<result, Tup> operator()(Tup tup) const noexcept
37 requires(eve::same_lanes_or_scalar_tuple<Tup> && kumi::size_v<Tup> >= 1)
38 {
39 24 return POLYFLOAT_CALL(tup);
40 }
41
42 948 POLYFLOAT_CALLABLE_OBJECT(average_t, average_);
43 };
44 //======================================================================================================================
45 //! @addtogroup core
46 //! @{
47 //! @var average
48 //! @brief return the mean value.
49 //!
50 //! @groupheader{Header file}
51 //!
52 //! @code
53 //! #include <polyfloat/core.hpp>
54 //! @endcode
55 //!
56 //! @groupheader{Callable Signatures}
57 //!
58 //! @code
59 //! namespace polyfloat
60 //! {
61 //! template<polyfloat::concepts::polyfloat_like T1, polyfloat_like Z2> constexpr auto average(T1 z1, T2 z2) noexcept;
62 //! }
63 //! @endcode
64 //!
65 //! **Parameters**
66 //!
67 //! * `z, zs...`: Values to process.
68 //!
69 //! **Return value**
70 //!
71 //! Returns the average of the parameters.
72 //!
73 //! @groupheader{Example}
74 //!
75 //! @godbolt_todo{doc/core/average.cpp}
76 //======================================================================================================================
77
78 inline constexpr auto average = eve::functor<average_t>;
79 //======================================================================================================================
80 //! @}
81 //======================================================================================================================
82 }
83
84 namespace plf::_
85 {
86 template<typename T1, typename... Ts, eve::callable_options O>
87 948 constexpr auto average_(POLYFLOAT_DELAY(), O const& o, T1 a0, Ts... args) noexcept
88 {
89 using r_t = as_polyfloat_like_t<T1, Ts...>;
90 using u_t = eve::element_type_t<r_t>;
91 if constexpr (sizeof...(Ts) == 0) return a0;
92 else
93 {
94 1026 auto cvt = [](auto a) { return plf::convert(a, eve::as_element<r_t>{}); };
95 948 constexpr auto N = sizeof...(Ts) + 1;
96 948 u_t const invn = plf::rec(u_t(N));
97 if constexpr (O::contains(raw))
98 {
99 return eve::mul[o](add[o.drop(raw)](cvt(a0), cvt(args)...), invn);
100 }
101 else
102 {
103 948 auto that(cvt(a0) * invn);
104 1500 auto next = [invn](auto avg, auto x) {
105 552 return plf::fma(x, invn, avg);
106 }; //return x * invn + avg; }; //plf::fma(x, invn, avg); }; //fma ?
107 948 ((that = next(that, cvt(args))), ...);
108 948 return that;
109 }
110 }
111 }
112 }
113