tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
fma.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_CMATH_FMA_HPP
5#define TETL_CMATH_FMA_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_type_traits/is_constant_evaluated.hpp>
10#include <etl/_type_traits/is_same.hpp>
11
12namespace etl {
13
14namespace detail {
15
16inline constexpr struct fma {
17 template <typename Float>
18 [[nodiscard]] constexpr auto operator()(Float x, Float y, Float z) const noexcept -> Float
19 {
21#if __has_builtin(__builtin_fmaf)
22 if constexpr (is_same_v<Float, float>) {
23 return __builtin_fmaf(x, y, z);
24 }
25#endif
26#if __has_builtin(__builtin_fma)
27 if constexpr (is_same_v<Float, double>) {
28 return __builtin_fma(x, y, z);
29 }
30#endif
31 }
32
33 return x * y + z;
34 }
35} fma;
36
37} // namespace detail
38
39/// \ingroup cmath
40/// @{
41
42/// Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type.
43/// \details https://en.cppreference.com/w/cpp/numeric/math/fma
44/// \ingroup cmath
45[[nodiscard]] constexpr auto fma(float x, float y, float z) noexcept -> float
46{
47 return etl::detail::fma(x, y, z);
48}
49[[nodiscard]] constexpr auto fmaf(float x, float y, float z) noexcept -> float
50{
51 return etl::detail::fma(x, y, z);
52}
53[[nodiscard]] constexpr auto fma(double x, double y, double z) noexcept -> double
54{
55 return etl::detail::fma(x, y, z);
56}
57[[nodiscard]] constexpr auto fma(long double x, long double y, long double z) noexcept -> long double
58{
59 return etl::detail::fma(x, y, z);
60}
61[[nodiscard]] constexpr auto fmal(long double x, long double y, long double z) noexcept -> long double
62{
63 return etl::detail::fma(x, y, z);
64}
65
66/// @}
67
68} // namespace etl
69
70#endif // TETL_CMATH_FMA_HPP
constexpr auto fma(float x, float y, float z) noexcept -> float
Definition fma.hpp:45
constexpr auto fmal(long double x, long double y, long double z) noexcept -> long double
Definition fma.hpp:61
constexpr auto fma(long double x, long double y, long double z) noexcept -> long double
Definition fma.hpp:57
constexpr auto fma(double x, double y, double z) noexcept -> double
Definition fma.hpp:53
constexpr auto fmaf(float x, float y, float z) noexcept -> float
Definition fma.hpp:49
Definition adjacent_find.hpp:9
constexpr auto is_constant_evaluated() noexcept -> bool
Detects whether the function call occurs within a constant-evaluated context. Returns true if the eva...
Definition is_constant_evaluated.hpp:17