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
use crate::Vec2;
use std::arch::x86_64::*;

/// 2D vector with double precision
///
/// The components are laid out in this order: `[x, y]`. This struct is aligned to 16 bytes.
///
/// ## Examples
///
/// ```
/// use mafs::{Vec2, Dvec2};
///
/// // Construction
/// let a = Dvec2::new(2.0, 3.0);
/// let b = Dvec2::new(6.0, 9.0);
/// let c = Dvec2::splat(0.0); // Set all four components to the same value
///
/// // Arithmetics
/// assert_eq!(a + b, Dvec2::new(8.0, 12.0));
/// assert_eq!(a - b, Dvec2::new(-4.0, -6.0));
/// assert_eq!(a * b, Dvec2::new(12.0, 27.0));
/// assert_eq!(b / a, Dvec2::new(3.0, 3.0));
///
/// // Euclidian norm
/// assert_eq!(a.norm(), 13.0f64.sqrt());
/// assert_eq!(a.normalize().norm(), 1.0);
///
/// // Specialized operations
/// assert_eq!(a.dot(b), 39.0);
/// assert_eq!(b.dot(a), a.dot(b));
/// assert_eq!(Dvec2::new(-0.5, 0.5).floor(), Dvec2::new(-1.0, 0.0));
///
/// // Comparisons
/// assert_eq!(a.min_componentwise(b), Dvec2::new(2.0, 3.0));
/// assert_eq!(a.max_componentwise(b), Dvec2::new(6.0, 9.0));
///
/// // Reduction
/// assert_eq!(a.min_reduce(), 2.0);
/// assert_eq!(b.max_reduce(), 9.0);
/// ```
#[repr(C)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Zeroable, bytemuck::Pod))]
pub struct Dvec2 {
pub(crate) inner: __m128d,
}

impl std::fmt::Debug for Dvec2 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.as_array().fmt(f)
}
}

impl Vec2<f64> for Dvec2 {
#[inline]
fn new(x: f64, y: f64) -> Dvec2 {
unsafe {
// The order is reversed!
Dvec2 {
inner: _mm_set_pd(y, x),
}
}
}

#[inline]
fn as_array(&self) -> &[f64; 2] {
unsafe { &*(self as *const Dvec2 as *const [f64; 2]) }
}

#[inline]
fn as_mut_array(&mut self) -> &mut [f64; 2] {
unsafe { &mut *(self as *mut Dvec2 as *mut [f64; 2]) }
}

#[inline]
fn add_componentwise(&self, rhs: Dvec2) -> Dvec2 {
unsafe {
Dvec2 {
}
}
}

#[inline]
fn sub_componentwise(&self, rhs: Dvec2) -> Dvec2 {
unsafe {
Dvec2 {
inner: _mm_sub_pd(self.inner, rhs.inner),
}
}
}

#[inline]
fn mul_componentwise(&self, rhs: Dvec2) -> Dvec2 {
unsafe {
Dvec2 {
inner: _mm_mul_pd(self.inner, rhs.inner),
}
}
}

#[inline]
fn div_componentwise(&self, rhs: Dvec2) -> Dvec2 {
unsafe {
Dvec2 {
inner: _mm_div_pd(self.inner, rhs.inner),
}
}
}

#[inline]
fn min_componentwise(&self, rhs: Dvec2) -> Dvec2 {
unsafe {
Dvec2 {
inner: _mm_min_pd(self.inner, rhs.inner),
}
}
}

#[inline]
fn max_componentwise(&self, rhs: Dvec2) -> Dvec2 {
unsafe {
Dvec2 {
inner: _mm_max_pd(self.inner, rhs.inner),
}
}
}

#[inline]
fn floor(&self) -> Dvec2 {
unsafe {
Dvec2 {
inner: _mm_floor_pd(self.inner),
}
}
}

#[inline]
fn min_reduce(&self) -> f64 {
unsafe {
let perm = _mm_permute_pd::<1>(self.inner);
let reduce = _mm_min_pd(self.inner, perm);
_mm_cvtsd_f64(reduce)
}
}

#[inline]
fn max_reduce(&self) -> f64 {
unsafe {
let perm = _mm_permute_pd::<1>(self.inner);
let reduce = _mm_max_pd(self.inner, perm);
_mm_cvtsd_f64(reduce)
}
}

#[inline]
fn eq_reduce(&self, rhs: Dvec2) -> bool {
unsafe {
reduce == 0xffff
}
}

#[inline]
fn dot(&self, rhs: Dvec2) -> f64 {
unsafe {
let prod = _mm_mul_pd(self.inner, rhs.inner);
_mm_cvtsd_f64(reduce64)
}
}
}

implement_scalarops!(Dvec2, f64);
implement_vecops!(Dvec2, f64);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn eq_works() {
let a = Dvec2::new(1.0, 2.0);
let b = Dvec2::new(1.0, 2.0);
let c = Dvec2::new(5.0, 6.0);
assert_eq!(a == b, true);
assert_eq!(b == a, true);
assert_eq!(a == a, true);
assert_eq!(a == c, false);

let d = Dvec2::new(0.0, -0.0);
let e = Dvec2::new(0.0, 0.0);
assert_eq!(d == e, true);

let f = Dvec2::new(f64::NAN, f64::NAN);
assert_eq!(f == f, false);
}
}