PCB Environment 2

Loading...
Searching...
No Matches
Misc.hpp
1
2#ifndef GYM_PCB_MATH_MISC_H
3#define GYM_PCB_MATH_MISC_H
4
5#if defined(__x86_64__)
6#include <xmmintrin.h>
7#endif
8#include "Defs.hpp"
9#include <cmath>
10#include <vector>
11
12 namespace math {
13
14 constexpr inline Real Radians(Real D)
15{
16 return M_PI * (D / 180.0);
17}
18
19 template<typename T> inline T squared(T v) { return v * v; }
20
21 namespace approx {
22
23 inline float rsqrt(float x)
24{
25#if defined(__x86_64__)
26 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
27#else
28 return 1.0f / std::sqrtf(x);
29#endif
30}
31
32} // approx
33
34 template<typename T> T reduce_max(const std::vector<T> &V)
35{
36 if (V.empty())
37 throw std::runtime_error("maximum(empty vector)");
38 T mx = V[0];
39 for (size_t i = 1; i < V.size(); ++i)
40 mx = std::max(mx, V[i]);
41 return mx;
42}
43
44 template<typename T> void multiply(std::vector<T> &V, T s)
45{
46 if (s != T(1)) {
47 for (T &x : V)
48 x *= s;
49 }
50}
51
52} // math
53
54#endif // GYM_PCB_MATH_MISC_H