blob: ac19ddca83be4d390d5f9e35172ba54df9e82865 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#ifndef MATH_UTILS_HPP
#define MATH_UTILS_HPP
#include <array>
#include <utility>
#include <concepts>
#include "math/vector.hpp"
namespace engine::math {
struct Vector2;
struct Vector3;
struct Vector4;
}
namespace engine::math::utils {
template<size_t size> struct Vector;
template<> struct Vector<2> { using type = engine::math::Vector2; };
template<> struct Vector<3> { using type = engine::math::Vector3; };
template<> struct Vector<4> { using type = engine::math::Vector4; };
template<size_t vector_size>
constexpr Vector<vector_size>::type array_to_vec(const std::array<float, vector_size>& coords) {
return [&]<size_t... i>(std::index_sequence<i...>) constexpr -> Vector<vector_size>::type {
return { coords[i] ... };
}(std::make_index_sequence<vector_size>());
}
constexpr float lerp(float a, float b, float t) {
return a + t * (b - a);
}
constexpr float map(float x, float from1, float from2, float to1, float to2) {
return to1 + (x - from1) * (to2 - to1) / (from2 - from1);
}
template<typename UInt>
constexpr UInt log2_floored(UInt n) noexcept
requires std::same_as<UInt, unsigned> || std::same_as<UInt, unsigned long> || std::same_as<UInt, unsigned long long>
{
#ifdef __GNUG__
if constexpr (std::is_same_v<UInt, unsigned>) {
return static_cast<UInt>(sizeof(UInt) * 8 - 1) - __builtin_clz(n);
} else if constexpr (std::is_same_v<UInt, unsigned long>) {
return static_cast<UInt>(sizeof(UInt) * 8 - 1) - __builtin_clzl(n);
} else { // unsigned long long
return static_cast<UInt>(sizeof(UInt) * 8 - 1) - __builtin_clzll(n);
}
#else
UInt ret = 0;
while (n & 1) {
n >>= 1;
ret++;
}
return ret;
#endif
}
}
#endif // MATH_UTILS_HPP
|