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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use crate::core::{
storage::{Columns2, XY},
traits::matrix::{FloatMatrix2x2, Matrix2x2, MatrixConst},
};
use crate::{DMat3, DVec2, Mat3, Vec2};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::{Add, AddAssign, Deref, DerefMut, Mul, MulAssign, Sub, SubAssign};
#[cfg(all(
target_arch = "x86",
target_feature = "sse2",
not(feature = "scalar-math")
))]
use core::arch::x86::*;
#[cfg(all(
target_arch = "x86_64",
target_feature = "sse2",
not(feature = "scalar-math")
))]
use core::arch::x86_64::*;
#[cfg(feature = "std")]
use std::iter::{Product, Sum};
macro_rules! impl_mat2_methods {
($t:ty, $vec2:ident, $mat3:ident, $inner:ident) => {
pub const ZERO: Self = Self($inner::ZERO);
pub const IDENTITY: Self = Self($inner::IDENTITY);
#[inline(always)]
pub fn from_cols(x_axis: $vec2, y_axis: $vec2) -> Self {
Self($inner::from_cols(x_axis.0, y_axis.0))
}
#[inline(always)]
pub fn from_cols_array(m: &[$t; 4]) -> Self {
Self($inner::from_cols_array(m))
}
#[inline(always)]
pub fn to_cols_array(&self) -> [$t; 4] {
self.0.to_cols_array()
}
#[inline(always)]
pub fn from_cols_array_2d(m: &[[$t; 2]; 2]) -> Self {
Self($inner::from_cols_array_2d(m))
}
#[inline(always)]
pub fn to_cols_array_2d(&self) -> [[$t; 2]; 2] {
self.0.to_cols_array_2d()
}
#[cfg_attr(docsrs, doc(alias = "scale"))]
#[inline(always)]
pub fn from_diagonal(diagonal: $vec2) -> Self {
Self($inner::from_diagonal(diagonal.0))
}
#[inline(always)]
pub fn from_scale_angle(scale: $vec2, angle: $t) -> Self {
Self($inner::from_scale_angle(scale.0, angle))
}
#[inline(always)]
pub fn from_angle(angle: $t) -> Self {
Self($inner::from_angle(angle))
}
#[inline(always)]
pub fn from_mat3(m: $mat3) -> Self {
Self::from_cols(m.x_axis.into(), m.y_axis.into())
}
#[inline(always)]
pub fn from_cols_slice(slice: &[$t]) -> Self {
Self(Matrix2x2::from_cols_slice(slice))
}
#[inline(always)]
pub fn write_cols_to_slice(self, slice: &mut [$t]) {
Matrix2x2::write_cols_to_slice(&self.0, slice)
}
#[inline]
pub fn col(&self, index: usize) -> $vec2 {
match index {
0 => self.x_axis,
1 => self.y_axis,
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn col_mut(&mut self, index: usize) -> &mut $vec2 {
match index {
0 => &mut self.x_axis,
1 => &mut self.y_axis,
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn row(&self, index: usize) -> $vec2 {
match index {
0 => $vec2::new(self.x_axis.x, self.y_axis.x),
1 => $vec2::new(self.x_axis.y, self.y_axis.y),
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn is_finite(&self) -> bool {
self.x_axis.is_finite() && self.y_axis.is_finite()
}
#[inline]
pub fn is_nan(&self) -> bool {
self.x_axis.is_nan() || self.y_axis.is_nan()
}
#[must_use]
#[inline(always)]
pub fn transpose(&self) -> Self {
Self(self.0.transpose())
}
#[inline(always)]
pub fn determinant(&self) -> $t {
self.0.determinant()
}
#[must_use]
#[inline(always)]
pub fn inverse(&self) -> Self {
Self(self.0.inverse())
}
#[inline(always)]
pub fn mul_vec2(&self, other: $vec2) -> $vec2 {
$vec2(self.0.mul_vector(other.0))
}
#[inline(always)]
pub fn mul_mat2(&self, other: &Self) -> Self {
Self(self.0.mul_matrix(&other.0))
}
#[inline(always)]
pub fn add_mat2(&self, other: &Self) -> Self {
Self(self.0.add_matrix(&other.0))
}
#[inline(always)]
pub fn sub_mat2(&self, other: &Self) -> Self {
Self(self.0.sub_matrix(&other.0))
}
#[inline(always)]
pub fn mul_scalar(&self, other: $t) -> Self {
Self(self.0.mul_scalar(other))
}
#[inline(always)]
pub fn abs_diff_eq(&self, other: &Self, max_abs_diff: $t) -> bool {
self.0.abs_diff_eq(&other.0, max_abs_diff)
}
};
}
macro_rules! impl_mat2_traits {
($t:ty, $new:ident, $mat2:ident, $vec2:ident) => {
#[inline(always)]
pub fn $new(x_axis: $vec2, y_axis: $vec2) -> $mat2 {
$mat2::from_cols(x_axis, y_axis)
}
impl_matn_common_traits!($t, $mat2, $vec2);
impl PartialEq for $mat2 {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.x_axis.eq(&other.x_axis) && self.y_axis.eq(&other.y_axis)
}
}
#[cfg(not(target_arch = "spriv"))]
impl AsRef<[$t; 4]> for $mat2 {
#[inline(always)]
fn as_ref(&self) -> &[$t; 4] {
unsafe { &*(self as *const Self as *const [$t; 4]) }
}
}
#[cfg(not(target_arch = "spriv"))]
impl AsMut<[$t; 4]> for $mat2 {
#[inline(always)]
fn as_mut(&mut self) -> &mut [$t; 4] {
unsafe { &mut *(self as *mut Self as *mut [$t; 4]) }
}
}
impl Deref for $mat2 {
type Target = Columns2<$vec2>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self as *const Self::Target) }
}
}
impl DerefMut for $mat2 {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self as *mut Self::Target) }
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for $mat2 {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct(stringify!($mat2))
.field("x_axis", &self.x_axis)
.field("y_axis", &self.y_axis)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for $mat2 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
}
}
};
}
#[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
type InnerF32 = __m128;
#[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
type InnerF32 = crate::core::storage::Columns2<XY<f32>>;
#[derive(Clone, Copy)]
#[cfg_attr(
not(any(feature = "scalar-math", target_arch = "spriv")),
repr(align(16))
)]
#[cfg_attr(any(feature = "scalar-math", target_arch = "spriv"), repr(transparent))]
pub struct Mat2(pub(crate) InnerF32);
impl Mat2 {
impl_mat2_methods!(f32, Vec2, Mat3, InnerF32);
#[inline(always)]
pub fn as_f64(&self) -> DMat2 {
DMat2::from_cols(self.x_axis.as_f64(), self.y_axis.as_f64())
}
}
impl_mat2_traits!(f32, mat2, Mat2, Vec2);
type InnerF64 = crate::core::storage::Columns2<XY<f64>>;
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct DMat2(pub(crate) InnerF64);
impl DMat2 {
impl_mat2_methods!(f64, DVec2, DMat3, InnerF64);
#[inline(always)]
pub fn as_f32(&self) -> Mat2 {
Mat2::from_cols(self.x_axis.as_f32(), self.y_axis.as_f32())
}
}
impl_mat2_traits!(f64, dmat2, DMat2, DVec2);
#[cfg(any(feature = "scalar-math", target_arch = "spriv"))]
mod const_test_mat2 {
const_assert_eq!(
core::mem::align_of::<f32>(),
core::mem::align_of::<super::Mat2>()
);
const_assert_eq!(16, core::mem::size_of::<super::Mat2>());
}
#[cfg(not(any(feature = "scalar-math", target_arch = "spriv")))]
mod const_test_mat2 {
const_assert_eq!(16, core::mem::align_of::<super::Mat2>());
const_assert_eq!(16, core::mem::size_of::<super::Mat2>());
}
mod const_test_dmat2 {
const_assert_eq!(
core::mem::align_of::<f64>(),
core::mem::align_of::<super::DMat2>()
);
const_assert_eq!(32, core::mem::size_of::<super::DMat2>());
}