Lodestone.Common
Loading...
Searching...
No Matches
Math.h
Go to the documentation of this file.
1//
2// Created by DexrnZacAttack on 11/17/25 using zPc-i2.
3//
4#ifndef LODESTONE_MATH_H
5#define LODESTONE_MATH_H
8
9#include <cstring>
10#include <random>
11#include <numbers>
12
14 class Math {
15 private:
16 // https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
17 template <typename I>
18 requires std::is_integral_v<I>
19 static constexpr I _fnv1a(const char *s, const size_t l, const I hash, const I prime) {
20 // set init value to our hash so we can pass into loop directly and
21 // recurse
22 uint64_t h = hash;
23
24 for (int i = 0; i < l; i++) {
25 h = (h ^ s[i]) * prime;
26 }
27
28 return h;
29 }
30
31 public:
32 Math() = delete;
33
34 static std::random_device s_randomDevice;
35 static std::mt19937_64 s_random;
36
37 static constexpr size_t FNV1A_64_HASH = 0xcbf29ce484222325;
38 static constexpr size_t FNV1A_64_PRIME = 0x00000100000001b3;
39
40 static constexpr uint32_t FNV1A_32_HASH = 0x811c9dc5;
41 static constexpr uint32_t FNV1A_32_PRIME = 0x01000193;
42
43 static constexpr auto BASE36 = "0123456789abcdefghijklmnopqrstuvwxyz";
44
45 static constexpr int ceilDiv(const int x, const int y) {
46 return (x + y - 1) / y;
47 };
48
49 static constexpr uint64_t fnv1a64(const char *s, const size_t l) {
51 }
52
53 static constexpr uint32_t fnv1a32(const char *s, const size_t l) {
55 }
56
57 static constexpr uint64_t fnv1a64(const std::string_view &s) {
58 return fnv1a64(s.data(), s.length());
59 }
60
61 static constexpr uint64_t fnv1a64(const char *s) {
62 return fnv1a64(s, Util::strlenConstexpr(s));
63 }
64
65 static constexpr uint32_t fnv1a32(const std::string_view &s) {
66 return fnv1a32(s.data(), s.length());
67 }
68
69 static constexpr uint32_t fnv1a32(const char *s) {
70 return fnv1a32(s, Util::strlenConstexpr(s));
71 }
72
73 static consteval size_t constevalLog(size_t value, const size_t base) {
74 if (value <= 1) return 0;
75
76 size_t l = 0;
77 while (value >= base) {
78 value /= base;
79 l++;
80 }
81
82 return l;
83 }
84
89 static unsigned long random();
90
96 static std::string encodeBase36(signed_size_t value);
97
98 static signed_size_t decodeBase36(const std::string_view &input) {
99 return std::stoull(input.data(), nullptr, 36);
100 }
101
102 static constexpr float degreesToRadians(const float deg) {
103 return deg * (std::numbers::pi / 180.0);
104 }
105
106 static constexpr float radiansToDegrees(const float deg) {
107 return deg * (180.0 / std::numbers::pi);
108 }
109 };
110} // namespace lodestone::common::util
111
112constexpr uint64_t operator""_hash(const char *s, const size_t l) {
114}
115
116#endif // LODESTONE_MATH_H
long signed int signed_size_t
Definition Defines.h:39
static constexpr uint32_t fnv1a32(const char *s)
Definition Math.h:69
static constexpr size_t FNV1A_64_HASH
Definition Math.h:37
static constexpr I _fnv1a(const char *s, const size_t l, const I hash, const I prime)
Definition Math.h:19
static constexpr uint32_t FNV1A_32_HASH
Definition Math.h:40
static consteval size_t constevalLog(size_t value, const size_t base)
Definition Math.h:73
static constexpr float radiansToDegrees(const float deg)
Definition Math.h:106
static constexpr uint64_t fnv1a64(const char *s, const size_t l)
Definition Math.h:49
static constexpr uint64_t fnv1a64(const char *s)
Definition Math.h:61
static constexpr uint32_t fnv1a32(const char *s, const size_t l)
Definition Math.h:53
static std::random_device s_randomDevice
Definition Math.h:34
static std::mt19937_64 s_random
Definition Math.h:35
static signed_size_t decodeBase36(const std::string_view &input)
Definition Math.h:98
static constexpr auto BASE36
Definition Math.h:43
static constexpr int ceilDiv(const int x, const int y)
Definition Math.h:45
static constexpr float degreesToRadians(const float deg)
Definition Math.h:102
static constexpr size_t FNV1A_64_PRIME
Definition Math.h:38
static constexpr uint32_t fnv1a32(const std::string_view &s)
Definition Math.h:65
static unsigned long random()
Gets a random number.
Definition Math.cpp:13
static std::string encodeBase36(signed_size_t value)
Converts an integer to two's complement Base36.
Definition Math.cpp:15
static constexpr uint64_t fnv1a64(const std::string_view &s)
Definition Math.h:57
static constexpr uint32_t FNV1A_32_PRIME
Definition Math.h:41
static constexpr size_t strlenConstexpr(const char *s)
Definition Util.h:24
Definition Casts.cpp:10