tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
math_utils.h
Go to the documentation of this file.
1#ifndef MATH_UTILS_H
2#define MATH_UTILS_H
3
4#include "config.h" // For PRECISION_TOLERANCE and MyErrorHandler
5#include "simple_type_traits.h" // For is_floating_point_v and is_same_v
6
15namespace math
16{
17
28 template <typename T>
29 constexpr T sqrt(T x) noexcept
30 {
31 static_assert(is_floating_point_v<T>, "sqrt requires a floating-point type");
32 if (x < T(0))
33 MyErrorHandler::error("sqrt of negative value");
34 return is_same_v<T, float> ? __builtin_sqrtf(x) : __builtin_sqrt(x);
35 }
36
47 template <typename T>
48 constexpr T abs(T x) noexcept
49 {
50 if constexpr (is_same_v<T, float>)
51 return __builtin_fabsf(x);
52 else if constexpr (is_same_v<T, double>)
53 return __builtin_fabs(x);
54 else if constexpr (is_same_v<T, int>)
55 return __builtin_abs(x);
56 else if constexpr (is_same_v<T, long>)
57 return __builtin_labs(x);
58 else
59 return x < T(0) ? -x : x;
60 }
61
69 template <typename T>
70 constexpr T sin(T x) noexcept
71 {
72 static_assert(is_floating_point_v<T>, "sin requires a floating-point type");
73 return is_same_v<T, float> ? __builtin_sinf(x) : __builtin_sin(x);
74 }
75
83 template <typename T>
84 constexpr T cos(T x) noexcept
85 {
86 static_assert(is_floating_point_v<T>, "cos requires a floating-point type");
87 return is_same_v<T, float> ? __builtin_cosf(x) : __builtin_cos(x);
88 }
89
97 template <typename T>
98 constexpr T acos(T x) noexcept
99 {
100 static_assert(is_floating_point_v<T>, "acos requires a floating-point type");
101 return is_same_v<T, float> ? __builtin_acosf(x) : __builtin_acos(x);
102 }
103
104} // namespace math
105
106#endif // MATH_UTILS_H
static void error(const T &msg)
Definition error_handler.h:30
Global configuration for the tesseract tensor library.
Definition atan.h:5
T cos(T x)
Definition cos.h:7
constexpr T abs(T x) noexcept
Compute the absolute value of a numeric value.
Definition math_utils.h:48
constexpr T acos(T x) noexcept
Compute the arc cosine of a floating-point value.
Definition math_utils.h:98
constexpr T sin(T x) noexcept
Compute the sine of a floating-point value (radians).
Definition math_utils.h:70
constexpr T sqrt(T x) noexcept
Compute the square root of a floating-point value.
Definition math_utils.h:29