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
// 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::raw::RawVector_i32;
use crate::structure::SIMDVector2;
use crate::vector3_bool::Vector3Bool;
use crate::vector4_bool::Vector4Bool;
use core::arch::x86_64::*;
use core::hash::Hash;
use core::hash::Hasher;
#[derive(Copy, Clone, Debug)]
#[repr(C, align(16))]
pub struct Vector2Bool {
    pub data: __m128i,
}

impl Vector2Bool {
    #[inline(always)]
    pub fn new(x: bool, y: bool) -> Vector2Bool {
        let x_val: u32 = if x { 0xFFFFFFFF } else { 0 };
        let y_val: u32 = if y { 0xFFFFFFFF } else { 0 };

        unsafe {
            return Vector2Bool {
                data: _mm_set_epi32(
                    0,
                    0,
                    core::mem::transmute::<u32, i32>(y_val),
                    core::mem::transmute::<u32, i32>(x_val),
                ),
            };
        }
    }
    /// Set all values of the vector to the same f32 value.
    #[inline(always)]
    pub fn set(value: bool) -> Vector2Bool {
        let val: u32 = if value { 0xFFFFFFFF } else { 0 };
        unsafe {
            return Vector2Bool {
                data: _mm_set1_epi32(core::mem::transmute::<u32, i32>(val)),
            };
        }
    }

    #[inline(always)]
    pub fn x(self) -> bool {
        unsafe {
            //1 2 4 8
            return (_mm_movemask_epi8(self.data) & 15) == 15;
        }
    }
    #[inline(always)]
    pub fn y(self) -> bool {
        unsafe {
            //16 32 64 128
            return (_mm_movemask_epi8(self.data) & 240) == 240;
        }
    }

    #[inline(always)]
    pub fn set_x(&mut self, value: bool) {
        let val: u32 = if value { 0xFFFFFFFF } else { 0 };
        unsafe {
            self.data = _mm_insert_epi32(self.data, core::mem::transmute::<u32, i32>(val), 0);
        }
    }
    #[inline(always)]
    pub fn set_y(&mut self, value: bool) {
        let val: u32 = if value { 0xFFFFFFFF } else { 0 };
        unsafe {
            self.data = _mm_insert_epi32(self.data, core::mem::transmute::<u32, i32>(val), 1);
        }
    }

    #[inline(always)]
    pub fn and(self, v2: Vector2Bool) -> Vector2Bool {
        unsafe {
            return Vector2Bool {
                data: _mm_and_si128(self.data, v2.data),
            };
        }
    }

    #[inline(always)]
    pub fn or(self, v2: Vector2Bool) -> Vector2Bool {
        unsafe {
            return Vector2Bool {
                data: _mm_or_si128(self.data, v2.data),
            };
        }
    }

    #[inline(always)]
    pub fn andnot(self, v2: Vector2Bool) -> Vector2Bool {
        unsafe {
            return Vector2Bool {
                data: _mm_andnot_si128(self.data, v2.data),
            };
        }
    }

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

    #[inline(always)]
    pub fn all(self: Vector2Bool) -> bool {
        unsafe {
            return (_mm_movemask_epi8(self.data) & 255) == 255;
        }
    }
    #[inline(always)]
    pub fn any(self: Vector2Bool) -> bool {
        unsafe {
            return (_mm_movemask_epi8(self.data) & 255) != 0;
        }
    }
}
impl From<bool> for Vector2Bool {
    #[inline(always)]
    fn from(v: bool) -> Vector2Bool {
        return Vector2Bool::set(v);
    }
}
impl From<Vector3Bool> for Vector2Bool {
    #[inline(always)]
    fn from(v: Vector3Bool) -> Vector2Bool {
        Vector2Bool { data: v.data }
    }
}
impl From<Vector4Bool> for Vector2Bool {
    #[inline(always)]
    fn from(v: Vector4Bool) -> Vector2Bool {
        Vector2Bool { data: v.data }
    }
}
impl PartialEq for Vector2Bool {
    #[inline(always)]
    fn eq(&self, other: &Vector2Bool) -> bool {
        return Vector2Bool::equal(*self, *other).all();
    }
}
impl Hash for Vector2Bool {
    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 Vector2Bool {
    #[inline(always)]
    fn data(self) -> __m128 {
        return unsafe { _mm_castsi128_ps(self.data) };
    }
    #[inline(always)]
    fn data_i(self) -> __m128i {
        return self.data;
    }
}