NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
fixed_type.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#if !((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
9#error "C++17 support was not detected."
10#endif
11
12#include <climits>
13
14static_assert(CHAR_BIT == 8, "CHAR_BIT must be 8");
15
16#include <limits>
17
18static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 not supported");
19
20#include <cassert>
21#include <cstdint>
22#include <cstring>
23#include <type_traits>
24
32
40#ifdef __GNUC__
41#define NNL_PACK(...) __VA_ARGS__ __attribute__((__packed__))
42#endif
43
44#ifdef _MSC_VER
45#define NNL_PACK(...) __pragma(pack(push, 1)) __VA_ARGS__ __pragma(pack(pop))
46#endif
47
49
50namespace nnl {
56
57using u64 = std::uint64_t;
58using i64 = std::int64_t;
59using f32 = float;
60using u32 = std::uint32_t;
61using i32 = std::int32_t;
62using u16 = std::uint16_t;
63using i16 = std::int16_t;
64using u8 = std::uint8_t;
65using i8 = std::int8_t;
66
74NNL_PACK(template <typename T> struct Vec4 {
75 static_assert(std::is_trivially_copyable_v<T>);
76 T x, y, z, w;
77
78 static constexpr std::size_t length() noexcept { return 4; }
79
80 using value_type = T;
81
82 constexpr Vec4() noexcept = default;
83
84 constexpr Vec4(T x, T y, T z, T w) noexcept : x(x), y(y), z(z), w(w) {}
85
86 constexpr Vec4(T val) noexcept : x(val), y(val), z(val), w(val) {}
87
88 template <typename A>
89 constexpr explicit Vec4(const Vec4<A>& vec) noexcept {
90 x = static_cast<T>(vec.x);
91 y = static_cast<T>(vec.y);
92 z = static_cast<T>(vec.z);
93 w = static_cast<T>(vec.w);
94 }
95
96 constexpr bool operator==(const Vec4& rhs) const noexcept {
97 return (x == rhs.x) && (y == rhs.y) && (z == rhs.z) && (w == rhs.w);
98 }
99
100 constexpr bool operator!=(const Vec4& rhs) const noexcept { return !(this->operator==(rhs)); }
101
102 constexpr const T& operator[](std::size_t i) const {
103 assert(i < length());
104 return this->*kMembers[i];
105 }
106
107 constexpr T& operator[](std::size_t i) {
108 assert(i < length());
109 return this->*kMembers[i];
110 }
111
112 private:
113 using Members = T Vec4::*const[length()];
114
115 static inline constexpr Members kMembers{&Vec4::x, &Vec4::y, &Vec4::z, &Vec4::w};
116});
124NNL_PACK(template <typename T> struct Vec3 {
125 static_assert(std::is_trivially_copyable_v<T>);
126 T x, y, z;
127
128 static constexpr std::size_t length() noexcept { return 3; }
129
130 using value_type = T;
131
132 constexpr Vec3() noexcept = default;
133
134 constexpr Vec3(T x, T y, T z) noexcept : x(x), y(y), z(z) {}
135
136 constexpr Vec3(T val) noexcept : x(val), y(val), z(val) {}
137
138 template <typename A>
139 constexpr explicit Vec3(const Vec3<A>& vec) noexcept {
140 x = static_cast<T>(vec.x);
141 y = static_cast<T>(vec.y);
142 z = static_cast<T>(vec.z);
143 }
144
145 constexpr bool operator==(const Vec3& rhs) const noexcept { return (x == rhs.x) && (y == rhs.y) && (z == rhs.z); }
146
147 constexpr bool operator!=(const Vec3& rhs) const noexcept { return !(this->operator==(rhs)); }
148
149 constexpr const T& operator[](std::size_t i) const {
150 assert(i < length());
151 return this->*kMembers[i];
152 }
153
154 constexpr T& operator[](std::size_t i) {
155 assert(i < length());
156 return this->*kMembers[i];
157 }
158
159 private:
160 using Members = T Vec3::*const[length()];
161
162 static inline constexpr Members kMembers{&Vec3::x, &Vec3::y, &Vec3::z};
163});
171NNL_PACK(template <typename T> struct Vec2 {
172 static_assert(std::is_trivially_copyable_v<T>);
173 T x, y;
174
175 static constexpr std::size_t length() noexcept { return 2; }
176
177 using value_type = T;
178
179 constexpr Vec2() noexcept = default;
180
181 constexpr Vec2(T x, T y) noexcept : x(x), y(y) {}
182
183 constexpr Vec2(T val) noexcept : x(val), y(val) {}
184
185 template <typename A>
186 constexpr explicit Vec2(const Vec2<A>& vec) noexcept {
187 x = static_cast<T>(vec.x);
188 y = static_cast<T>(vec.y);
189 }
190
191 constexpr bool operator==(const Vec2& rhs) const noexcept { return (x == rhs.x) && (y == rhs.y); }
192
193 constexpr bool operator!=(const Vec2& rhs) const noexcept { return !(this->operator==(rhs)); }
194
195 constexpr const T& operator[](std::size_t i) const {
196 assert(i < length());
197 return this->*kMembers[i];
198 }
199
200 constexpr T& operator[](std::size_t i) {
201 assert(i < length());
202 return this->*kMembers[i];
203 }
204
205 private:
206 using Members = T Vec2::*const[length()];
207
208 static inline constexpr Members kMembers{&Vec2::x, &Vec2::y};
209});
210
211static_assert(sizeof(Vec4<f32>) == 0x10);
212
213static_assert(sizeof(Vec4<u16>) == 0x8);
214
215static_assert(sizeof(Vec4<i16>) == 0x8);
216
217static_assert(sizeof(Vec4<u8>) == 0x4);
218
219static_assert(sizeof(Vec4<i8>) == 0x4);
220
221static_assert(sizeof(Vec3<f32>) == 0xC);
222
223static_assert(sizeof(Vec3<u32>) == 0xC);
224
225static_assert(sizeof(Vec3<u16>) == 0x6);
226
227static_assert(sizeof(Vec3<i16>) == 0x6);
228
229static_assert(sizeof(Vec3<i8>) == 0x3);
230
231static_assert(sizeof(Vec3<u8>) == 0x3);
232
233static_assert(sizeof(Vec2<f32>) == 0x8);
234
235static_assert(sizeof(Vec2<i8>) == 0x2);
236
237static_assert(sizeof(Vec2<u8>) == 0x2);
238
239static_assert(sizeof(Vec2<i16>) == 0x4);
240
241static_assert(sizeof(Vec2<u16>) == 0x4);
242
249NNL_PACK(template <typename T> struct Mat4 {
250 static_assert(std::is_trivially_copyable_v<T>);
251
252 Vec4<T> r0, r1, r2, r3;
253
254 static constexpr std::size_t length() noexcept { return 4; }
255
256 using value_type = Vec4<T>;
257
258 constexpr Mat4() noexcept = default;
259
260 constexpr Mat4(Vec4<T> r0, Vec4<T> r1, Vec4<T> r2, Vec4<T> r3) noexcept : r0(r0), r1(r1), r2(r2), r3(r3) {}
261
262 constexpr bool operator==(const Mat4& rhs) const noexcept {
263 return (r0 == rhs.r0) && (r1 == rhs.r1) && (r2 == rhs.r2) && (r3 == rhs.r3);
264 }
265
266 constexpr bool operator!=(const Mat4& rhs) const noexcept { return !(*this == rhs); }
267
268 constexpr const Vec4<T>& operator[](std::size_t i) const {
269 assert(i < length());
270 return this->*kMembers[i];
271 }
272
273 constexpr Vec4<T>& operator[](std::size_t i) {
274 assert(i < length());
275 return this->*kMembers[i];
276 }
277
278 private:
279 using Members = Vec4<T> Mat4::*const[length()];
280
281 static inline constexpr Members kMembers{&Mat4::r0, &Mat4::r1, &Mat4::r2, &Mat4::r3};
282});
283
284static_assert(sizeof(Mat4<f32>) == 0x40);
285
287} // namespace nnl
4D vector template with packed storage
Definition fixed_type.hpp:116
4x4 matrix template with packed storage
Definition fixed_type.hpp:282
2D vector template with packed storage
Definition fixed_type.hpp:209
3D vector template with packed storage
Definition fixed_type.hpp:163
std::uint16_t u16
16-bit unsigned integer
Definition fixed_type.hpp:62
std::int32_t i32
32-bit signed integer
Definition fixed_type.hpp:61
std::int16_t i16
16-bit signed integer
Definition fixed_type.hpp:63
std::int8_t i8
8-bit signed integer
Definition fixed_type.hpp:65
std::uint64_t u64
64-bit unsigned integer
Definition fixed_type.hpp:57
float f32
32-bit floating point
Definition fixed_type.hpp:59
std::uint32_t u32
32-bit unsigned integer
Definition fixed_type.hpp:60
std::uint8_t u8
8-bit unsigned integer
Definition fixed_type.hpp:64
std::int64_t i64
64-bit signed integer
Definition fixed_type.hpp:58
#define NNL_PACK(...)
A structure packing directive.
Definition fixed_type.hpp:41
Definition exception.hpp:56