HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2019-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8#pragma once
9
10#include "utility.hpp"
11#include "cast.hpp"
12#include "type_traits.hpp"
13#include "assert.hpp"
14#include <complex>
15#include <cmath>
16#include <limits>
17#include <span>
18#include <tuple>
19#include <numeric>
20#include <iterator>
21#include <bit>
22#include <concepts>
23#include <algorithm>
24#include <numbers>
25
26#if HI_COMPILER == HI_CC_MSVC
27#include <intrin.h>
28#endif
29#if HI_PROCESSOR == HI_CPU_X64
30#include <immintrin.h>
31#endif
32
33namespace hi::inline v1 {
34
35constexpr long long pow10_table[20]{
36 1LL,
37 10LL,
38 100LL,
39 1'000LL,
40 10'000LL,
41 100'000LL,
42 1'000'000LL,
43 10'000'000LL,
44 100'000'000LL,
45 1'000'000'000LL,
46 10'000'000'000LL,
47 100'000'000'000LL,
48 1'000'000'000'000LL,
49 10'000'000'000'000LL,
50 100'000'000'000'000LL,
51 1'000'000'000'000'000LL,
52 10'000'000'000'000'000LL,
53 100'000'000'000'000'000LL,
54 1'000'000'000'000'000'000LL,
55};
56
57[[nodiscard]] constexpr long long pow10ll(int x) noexcept
58{
59 hi_axiom(x >= 0 && x <= 18);
60 return pow10_table[x];
61}
62
63template<typename Iterator>
64[[nodiscard]] auto mean(Iterator first, Iterator last)
65{
66 hilet init = static_cast<typename std::iterator_traits<Iterator>::value_type>(0);
67
68 hilet sum = std::reduce(first, last, init);
69 hilet count = static_cast<decltype(sum)>(std::distance(first, last));
70
71 return count > 0.0 ? sum / count : sum;
72}
73
74template<typename Iterator, typename T>
75[[nodiscard]] auto stddev(Iterator first, Iterator last, T mean)
76{
77 hilet init = static_cast<typename std::iterator_traits<Iterator>::value_type>(0);
78
79 hilet sum = std::accumulate(first, last, init, [=](hilet& acc, hilet& value) {
80 hilet tmp = value - mean;
81 return acc + tmp * tmp;
82 });
83
84 hilet count = static_cast<decltype(sum)>(std::distance(first, last));
85 return count > 0.0 ? sum / count : sum;
86}
87
88template<typename T>
89constexpr bool inplace_max(T& a, T const& b) noexcept
90{
91 using std::max;
92 a = max(a, b);
93 return a == b;
94}
95
96template<typename T>
97constexpr bool inplace_min(T& a, T const& b) noexcept
98{
99 using std::min;
100 a = min(a, b);
101 return a == b;
102}
103
104template<typename T>
105constexpr void inplace_clamp(T& a, T const& lo, T const& hi) noexcept
106{
107 hi_axiom(lo <= hi);
108
109 using std::clamp;
110 a = clamp(a, lo, hi);
111}
112
113
121template<std::signed_integral T>
122[[nodiscard]] constexpr friend std::make_unsigned_t<T> abs_unsigned(T rhs) noexcept
123{
124 auto rhs_u = static_cast<std::make_unsigned_t<T>>(rhs);
125 auto res_u = -rhs_u;
126
127 // All positive numbers and int_min, would result in a negative number.
128 if (static_cast<T>(res_u) < 0) {
129 // int_min casted to unsigned in int_max + 1.
130 res_u = rhs_u;
131 }
132 return rhs_u;
133}
134
135template<std::floating_point T>
136[[nodiscard]] constexpr bool almost_equal(T a, T b) noexcept
137{
138 auto e = (a + b) * std::numeric_limits<T>::epsilon();
139 return std::abs(a - b) <= e;
140}
141
147template<std::floating_point T>
148[[nodiscard]] constexpr T to_radian(T degree) noexcept
149{
150 return degree * (std::numbers::pi_v<T> / T{180.0});
151}
152
158template<std::unsigned_integral T>
159[[nodiscard]] constexpr T floor(T value, T alignment) noexcept
160{
161 return (value / alignment) * alignment;
162}
163
169template<std::unsigned_integral T>
170[[nodiscard]] constexpr T ceil(T value, T alignment) noexcept
171{
172 return floor(value + (alignment - 1), alignment);
173}
174
175template<std::floating_point T>
176[[nodiscard]] constexpr bool isnan(T value) noexcept
177{
178 return not (value == value);
179}
180
181} // namespace hi::inline v1
Utilities to assert and bound check.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr friend std::make_unsigned_t< T > abs_unsigned(T rhs) noexcept
Absolute value of a signed number converted to an unsigned number.
Definition math.hpp:122
constexpr T to_radian(T degree) noexcept
Convert degree to radian.
Definition math.hpp:148
geometry/margins.hpp
Definition cache.hpp:11
Horizontal/Vertical alignment combination.
Definition alignment.hpp:231
T accumulate(T... args)
T count(T... args)
T distance(T... args)
T max(T... args)
T min(T... args)