ico_math 0.1.6

An opinionated SIMD Math Library for games and graphics in Rust.
Documentation
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright 2019 Icosahedra, LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//

use crate::int_vector::IntVector;
use crate::raw::RawVector_i32;
use crate::sse_extensions::*;
use crate::structure::SIMDVector2;
use crate::vector2::Vector2;
use crate::vector2_bool::Vector2Bool;
use crate::vector3_int::Vector3Int;
use crate::vector4_int::Vector4Int;
use core::arch::x86_64::*;
use core::hash::Hash;
use core::hash::Hasher;

#[derive(Copy, Clone, Debug)]
#[repr(C, align(16))]
pub struct Vector2Int {
    pub data: __m128i,
}

impl Vector2Int {
    /// Returns a new Vector2
    #[inline(always)]
    pub fn new(x: i32, y: i32) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_set_epi32(0, 0, y, x),
            }
        }
    }
    #[inline(always)]
    pub fn set<T: Into<IntVector>>(value: T) -> Vector2Int {
        return Vector2Int {
            data: value.into().data,
        };
    }
    #[inline(always)]
    pub fn zero() -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_setzero_si128(),
            }
        }
    }
    /// Load a value from aligned memory.
    #[inline(always)]
    pub fn load(raw: &RawVector_i32) -> Vector2Int {
        unsafe {
            // Use the sledgehammer cast here.  It's fine because RawVector is aligned and c-like.
            return Vector2Int {
                data: _mm_load_si128(core::mem::transmute(raw)),
            };
        }
    }

    /// Store a value to aligned memory.
    #[inline(always)]
    pub fn store(self, dst: &mut RawVector_i32) {
        unsafe {
            // Use the sledgehammer cast here.  It's fine because RawVector is aligned and c-like.
            _mm_store_si128(core::mem::transmute(dst), self.data);
        }
    }
    #[inline(always)]
    pub fn x(self) -> IntVector {
        return IntVector {
            data: self.xxxx().data,
        };
    }

    #[inline(always)]
    pub fn y(self) -> IntVector {
        return IntVector {
            data: self.yyyy().data,
        };
    }

    #[inline(always)]
    pub fn set_x<T: Into<i32>>(&mut self, value: T) {
        unsafe {
            self.data = _mm_insert_epi32(self.data, value.into(), 0);
        }
    }

    #[inline(always)]
    pub fn set_y<T: Into<i32>>(&mut self, value: T) {
        unsafe {
            self.data = _mm_insert_epi32(self.data, value.into(), 1);
        }
    }

    #[inline(always)]
    pub fn max(self, v2: Vector2Int) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_max_epi32(self.data, v2.data),
            }
        }
    }

    #[inline(always)]
    pub fn min(self, v2: Vector2Int) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_min_epi32(self.data, v2.data),
            }
        }
    }
    /// Choose component wise between A and B based on the mask.  False = A, True = B.
    #[inline(always)]
    pub fn select(self, v2: Vector2Int, mask: Vector2Bool) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _ico_select_si128(self.data, v2.data, mask.data),
            }
        }
    }

    #[inline(always)]
    pub fn add(self, v2: Vector2Int) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_add_epi32(self.data, v2.data),
            }
        }
    }

    #[inline(always)]
    pub fn sub(self, v2: Vector2Int) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_sub_epi32(self.data, v2.data),
            }
        }
    }
    #[inline(always)]
    pub fn mul(self, v2: Vector2Int) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_mullo_epi32(self.data, v2.data),
            }
        }
    }
    #[inline(always)]
    pub fn abs(self) -> Vector2Int {
        unsafe {
            return Vector2Int {
                data: _mm_abs_epi32(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn sign(self, v2: Vector2Int) -> Vector2Int {
        unsafe {
            return Vector2Int {
                data: _mm_sign_epi32(self.data, v2.data),
            };
        }
    }

    #[inline(always)]
    pub fn and<T: SIMDVector2>(self, v2: T) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_and_si128(self.data, v2.data_i()),
            }
        }
    }

    #[inline(always)]
    pub fn or<T: SIMDVector2>(self, v2: T) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_or_si128(self.data, v2.data_i()),
            }
        }
    }

    #[inline(always)]
    pub fn andnot<T: SIMDVector2>(self, v2: T) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_andnot_si128(self.data, v2.data_i()),
            }
        }
    }

    #[inline(always)]
    pub fn xor<T: SIMDVector2>(self, v2: T) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_xor_si128(self.data, v2.data_i()),
            }
        }
    }

    #[inline(always)]
    pub fn equal(self, v2: Vector2Int) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_cmpeq_epi32(self.data, v2.data),
            }
        }
    }
    #[inline(always)]
    pub fn not_equal(self, v2: Vector2Int) -> Vector2Bool {
        return Vector2Bool::xor(self.equal(self), self.equal(v2));
    }

    #[inline(always)]
    pub fn greater_equal(self, v2: Vector2Int) -> Vector2Bool {
        return Vector2Bool::or(self.equal(v2), self.greater(v2));
    }
    #[inline(always)]
    pub fn greater(self, v2: Vector2Int) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_cmpgt_epi32(self.data, v2.data),
            }
        }
    }
    #[inline(always)]
    pub fn less_equal(self, v2: Vector2Int) -> Vector2Bool {
        return Vector2Bool::or(self.equal(v2), self.less(v2));
    }

    #[inline(always)]
    pub fn less(self, v2: Vector2Int) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_cmplt_epi32(self.data, v2.data),
            }
        }
    }

    #[inline(always)]
    pub fn xxxx(self) -> Vector4Int {
        unsafe {
            return Vector4Int {
                data: _xxxx_i(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn yyyy(self) -> Vector4Int {
        unsafe {
            return Vector4Int {
                data: _yyyy_i(self.data),
            };
        }
    }

    #[inline(always)]
    pub fn xx(self) -> Vector2Int {
        unsafe {
            return Vector2Int {
                data: _xxzw_i(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn xy(self) -> Vector2Int {
        unsafe {
            return Vector2Int {
                data: _xyzw_i(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn yx(self) -> Vector2Int {
        unsafe {
            return Vector2Int {
                data: _yxzw_i(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn yy(self) -> Vector2Int {
        unsafe {
            return Vector2Int {
                data: _yyzw_i(self.data),
            };
        }
    }
}

impl From<i32> for Vector2Int {
    #[inline(always)]
    fn from(v: i32) -> Vector2Int {
        unsafe {
            return Vector2Int {
                data: _mm_set1_epi32(v),
            };
        }
    }
}
impl From<IntVector> for Vector2Int {
    #[inline(always)]
    fn from(val: IntVector) -> Vector2Int {
        return Vector2Int { data: val.data };
    }
}
impl From<Vector3Int> for Vector2Int {
    #[inline(always)]
    fn from(v: Vector3Int) -> Vector2Int {
        Vector2Int { data: v.data }
    }
}
impl From<Vector4Int> for Vector2Int {
    #[inline(always)]
    fn from(v: Vector4Int) -> Vector2Int {
        Vector2Int { data: v.data }
    }
}
impl From<Vector2> for Vector2Int {
    #[inline(always)]
    fn from(v: Vector2) -> Vector2Int {
        unsafe {
            return Vector2Int {
                data: _mm_cvttps_epi32(v.data),
            };
        }
    }
}

impl core::ops::Add for Vector2Int {
    type Output = Vector2Int;
    #[inline(always)]
    fn add(self, _rhs: Vector2Int) -> Vector2Int {
        return Vector2Int::add(self, _rhs);
    }
}
impl core::ops::AddAssign for Vector2Int {
    #[inline(always)]
    fn add_assign(&mut self, other: Vector2Int) {
        *self = Vector2Int::add(*self, other);
    }
}
impl core::ops::Sub for Vector2Int {
    type Output = Vector2Int;
    #[inline(always)]
    fn sub(self, _rhs: Vector2Int) -> Vector2Int {
        return Vector2Int::sub(self, _rhs);
    }
}
impl core::ops::SubAssign for Vector2Int {
    #[inline(always)]
    fn sub_assign(&mut self, other: Vector2Int) {
        *self = Vector2Int::sub(*self, other);
    }
}
impl core::ops::Neg for Vector2Int {
    type Output = Vector2Int;
    #[inline(always)]
    fn neg(self) -> Self::Output {
        unsafe {
            return Vector2Int {
                data: _mm_sub_epi32(_mm_set1_epi32(0), self.data),
            };
        }
    }
}

impl<T: Into<IntVector>> core::ops::Mul<T> for Vector2Int {
    type Output = Vector2Int;
    #[inline(always)]
    fn mul(self, _rhs: T) -> Vector2Int {
        return Vector2Int::mul(self, Vector2Int::from(_rhs.into()));
    }
}
impl<T: Into<IntVector>> core::ops::MulAssign<T> for Vector2Int {
    #[inline(always)]
    fn mul_assign(&mut self, _rhs: T) {
        *self = Vector2Int::mul(*self, Vector2Int::from(_rhs.into()));
    }
}
impl core::ops::Mul<Vector2Int> for IntVector {
    type Output = Vector2Int;
    #[inline(always)]
    fn mul(self: IntVector, _rhs: Vector2Int) -> Vector2Int {
        return Vector2Int::mul(_rhs, Vector2Int::from(self));
    }
}

impl PartialEq for Vector2Int {
    #[inline(always)]
    fn eq(&self, other: &Vector2Int) -> bool {
        return Vector2Int::equal(*self, *other).all();
    }
}

impl Hash for Vector2Int {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let mut dst = RawVector_i32 { data: [0; 4] };
        unsafe {
            let x: *mut __m128i = &mut (dst.data[0]) as *mut i32 as *mut __m128i;
            _mm_store_si128(x, self.data);
        }
        dst.data[2] = 0;
        dst.data[3] = 0;
        dst.data.hash(state);
    }
}
impl SIMDVector2 for Vector2Int {
    #[inline(always)]
    fn data(self) -> __m128 {
        return unsafe { _mm_castsi128_ps(self.data) };
    }
    #[inline(always)]
    fn data_i(self) -> __m128i {
        return self.data;
    }
}