| 1 | use crate::core::traits::vector::{ |
| 2 | MaskVector, MaskVector2, MaskVector3, MaskVector4, MaskVectorConst, |
| 3 | }; |
| 4 | #[cfg(not(target_arch = "spirv"))] |
| 5 | use core::fmt; |
| 6 | use core::{hash, ops::*}; |
| 7 | |
| 8 | #[cfg(all( |
| 9 | target_arch = "x86", |
| 10 | target_feature = "sse2", |
| 11 | not(feature = "scalar-math") |
| 12 | ))] |
| 13 | use core::arch::x86::*; |
| 14 | #[cfg(all( |
| 15 | target_arch = "x86_64", |
| 16 | target_feature = "sse2", |
| 17 | not(feature = "scalar-math") |
| 18 | ))] |
| 19 | use core::arch::x86_64::*; |
| 20 | |
| 21 | #[macro_use] |
| 22 | macro_rules! impl_vecnmask_methods { |
| 23 | ($vecnmask:ident, $trait:ident) => { |
| 24 | /// Returns a bitmask with the lowest two bits set from the elements of `self`. |
| 25 | /// |
| 26 | /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes |
| 27 | /// into the first lowest bit, element `y` into the second, etc. |
| 28 | #[inline] |
| 29 | pub fn bitmask(self) -> u32 { |
| 30 | $trait::bitmask(self.0) |
| 31 | } |
| 32 | |
| 33 | /// Returns true if any of the elements are true, false otherwise. |
| 34 | #[inline] |
| 35 | pub fn any(self) -> bool { |
| 36 | $trait::any(self.0) |
| 37 | } |
| 38 | |
| 39 | /// Returns true if all the elements are true, false otherwise. |
| 40 | #[inline] |
| 41 | pub fn all(self) -> bool { |
| 42 | $trait::all(self.0) |
| 43 | } |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | #[macro_use] |
| 48 | macro_rules! impl_vecnmask_traits { |
| 49 | ($vecnmask:ident, $inner:ident) => { |
| 50 | impl Default for $vecnmask { |
| 51 | #[inline] |
| 52 | fn default() -> Self { |
| 53 | Self($inner::FALSE) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl PartialEq for $vecnmask { |
| 58 | #[inline] |
| 59 | fn eq(&self, other: &Self) -> bool { |
| 60 | self.as_ref().eq(other.as_ref()) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl Eq for $vecnmask {} |
| 65 | |
| 66 | impl hash::Hash for $vecnmask { |
| 67 | #[inline] |
| 68 | fn hash<H: hash::Hasher>(&self, state: &mut H) { |
| 69 | self.as_ref().hash(state); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl BitAnd for $vecnmask { |
| 74 | type Output = Self; |
| 75 | #[inline] |
| 76 | fn bitand(self, other: Self) -> Self { |
| 77 | Self(self.0.bitand(other.0)) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl BitAndAssign for $vecnmask { |
| 82 | #[inline] |
| 83 | fn bitand_assign(&mut self, other: Self) { |
| 84 | self.0 = self.0.bitand(other.0); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | impl BitOr for $vecnmask { |
| 89 | type Output = Self; |
| 90 | #[inline] |
| 91 | fn bitor(self, other: Self) -> Self { |
| 92 | Self(self.0.bitor(other.0)) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl BitOrAssign for $vecnmask { |
| 97 | #[inline] |
| 98 | fn bitor_assign(&mut self, other: Self) { |
| 99 | self.0 = self.0.bitor(other.0); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl Not for $vecnmask { |
| 104 | type Output = Self; |
| 105 | #[inline] |
| 106 | fn not(self) -> Self { |
| 107 | Self(self.0.not()) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl From<$vecnmask> for $inner { |
| 112 | #[inline] |
| 113 | fn from(t: $vecnmask) -> Self { |
| 114 | t.0 |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | #[macro_use] |
| 121 | macro_rules! impl_vec2mask { |
| 122 | ($vec2mask:ident, $t:ty, $inner:ident) => { |
| 123 | impl $vec2mask { |
| 124 | /// Creates a new vector mask. |
| 125 | #[inline] |
| 126 | pub fn new(x: bool, y: bool) -> Self { |
| 127 | Self(MaskVector2::new(x, y)) |
| 128 | } |
| 129 | |
| 130 | impl_vecnmask_methods!($vec2mask, MaskVector2); |
| 131 | } |
| 132 | |
| 133 | impl_vecnmask_traits!($vec2mask, $inner); |
| 134 | |
| 135 | #[cfg(not(target_arch = "spirv"))] |
| 136 | impl fmt::Debug for $vec2mask { |
| 137 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 138 | let arr = self.0.into_u32_array(); |
| 139 | write!(f, "{}({:#x}, {:#x})", stringify!($vec2mask), arr[0], arr[1]) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #[cfg(not(target_arch = "spirv"))] |
| 144 | impl fmt::Display for $vec2mask { |
| 145 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 146 | let arr = self.0.into_bool_array(); |
| 147 | write!(f, "[{}, {}]", arr[0], arr[1]) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | impl From<$vec2mask> for [bool; 2] { |
| 152 | #[inline] |
| 153 | fn from(mask: $vec2mask) -> Self { |
| 154 | mask.0.into_bool_array() |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | impl From<$vec2mask> for [u32; 2] { |
| 159 | #[inline] |
| 160 | fn from(mask: $vec2mask) -> Self { |
| 161 | mask.0.into_u32_array() |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | impl AsRef<[$t; 2]> for $vec2mask { |
| 166 | #[inline] |
| 167 | fn as_ref(&self) -> &[$t; 2] { |
| 168 | unsafe { &*(self as *const Self as *const [$t; 2]) } |
| 169 | } |
| 170 | } |
| 171 | }; |
| 172 | } |
| 173 | |
| 174 | #[macro_use] |
| 175 | macro_rules! impl_vec3mask { |
| 176 | ($vec3mask:ident, $t:ty, $inner:ident) => { |
| 177 | impl $vec3mask { |
| 178 | /// Creates a new vector mask. |
| 179 | #[inline] |
| 180 | pub fn new(x: bool, y: bool, z: bool) -> Self { |
| 181 | Self(MaskVector3::new(x, y, z)) |
| 182 | } |
| 183 | |
| 184 | impl_vecnmask_methods!($vec3mask, MaskVector3); |
| 185 | } |
| 186 | |
| 187 | impl_vecnmask_traits!($vec3mask, $inner); |
| 188 | |
| 189 | #[cfg(not(target_arch = "spirv"))] |
| 190 | impl fmt::Debug for $vec3mask { |
| 191 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 192 | let arr = MaskVector3::into_u32_array(self.0); |
| 193 | write!( |
| 194 | f, |
| 195 | "{}({:#x}, {:#x}, {:#x})", |
| 196 | stringify!($vec3mask), |
| 197 | arr[0], |
| 198 | arr[1], |
| 199 | arr[2] |
| 200 | ) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | #[cfg(not(target_arch = "spirv"))] |
| 205 | impl fmt::Display for $vec3mask { |
| 206 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 207 | let arr = MaskVector3::into_bool_array(self.0); |
| 208 | write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2]) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | impl From<$vec3mask> for [bool; 3] { |
| 213 | #[inline] |
| 214 | fn from(mask: $vec3mask) -> Self { |
| 215 | MaskVector3::into_bool_array(mask.0) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | impl From<$vec3mask> for [u32; 3] { |
| 220 | #[inline] |
| 221 | fn from(mask: $vec3mask) -> Self { |
| 222 | MaskVector3::into_u32_array(mask.0) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | impl AsRef<[$t; 3]> for $vec3mask { |
| 227 | #[inline] |
| 228 | fn as_ref(&self) -> &[$t; 3] { |
| 229 | unsafe { &*(self as *const Self as *const [$t; 3]) } |
| 230 | } |
| 231 | } |
| 232 | }; |
| 233 | } |
| 234 | |
| 235 | #[macro_use] |
| 236 | macro_rules! impl_vec4mask { |
| 237 | ($vec4mask:ident, $t:ty, $inner:ident) => { |
| 238 | impl $vec4mask { |
| 239 | /// Creates a new vector mask. |
| 240 | #[inline] |
| 241 | pub fn new(x: bool, y: bool, z: bool, w: bool) -> Self { |
| 242 | Self(MaskVector4::new(x, y, z, w)) |
| 243 | } |
| 244 | |
| 245 | impl_vecnmask_methods!($vec4mask, MaskVector4); |
| 246 | } |
| 247 | |
| 248 | impl_vecnmask_traits!($vec4mask, $inner); |
| 249 | |
| 250 | #[cfg(not(target_arch = "spirv"))] |
| 251 | impl fmt::Debug for $vec4mask { |
| 252 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 253 | let arr = MaskVector4::into_u32_array(self.0); |
| 254 | write!( |
| 255 | f, |
| 256 | "{}({:#x}, {:#x}, {:#x}, {:#x})", |
| 257 | stringify!($vec4mask), |
| 258 | arr[0], |
| 259 | arr[1], |
| 260 | arr[2], |
| 261 | arr[3] |
| 262 | ) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | #[cfg(not(target_arch = "spirv"))] |
| 267 | impl fmt::Display for $vec4mask { |
| 268 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 269 | let arr = MaskVector4::into_bool_array(self.0); |
| 270 | write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3]) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | impl From<$vec4mask> for [bool; 4] { |
| 275 | #[inline] |
| 276 | fn from(mask: $vec4mask) -> Self { |
| 277 | MaskVector4::into_bool_array(mask.0) |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | impl From<$vec4mask> for [u32; 4] { |
| 282 | #[inline] |
| 283 | fn from(mask: $vec4mask) -> Self { |
| 284 | MaskVector4::into_u32_array(mask.0) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | impl AsRef<[$t; 4]> for $vec4mask { |
| 289 | #[inline] |
| 290 | fn as_ref(&self) -> &[$t; 4] { |
| 291 | unsafe { &*(self as *const Self as *const [$t; 4]) } |
| 292 | } |
| 293 | } |
| 294 | }; |
| 295 | } |
| 296 | |
| 297 | // BVec3A ///////////////////////////////////////////////////////////////////////////////////////// |
| 298 | |
| 299 | /// A 3-dimensional SIMD vector mask. |
| 300 | /// |
| 301 | /// This type is 16 byte aligned and is backed by a SIMD vector. If SIMD is not available `BVec3A` |
| 302 | /// will be a type alias for `BVec3`. |
| 303 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 304 | #[derive(Clone, Copy)] |
| 305 | #[repr(transparent)] |
| 306 | pub struct BVec3A(pub(crate) __m128); |
| 307 | |
| 308 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 309 | impl_vec3mask!(BVec3A, u32, __m128); |
| 310 | |
| 311 | #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))] |
| 312 | pub type BVec3A = BVec3; |
| 313 | |
| 314 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 315 | impl From<BVec3> for BVec3A { |
| 316 | #[inline] |
| 317 | fn from(b: BVec3) -> Self { |
| 318 | Self::new(b.0.x, b.0.y, b.0.z) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 323 | impl From<BVec3A> for BVec3 { |
| 324 | #[inline] |
| 325 | fn from(b: BVec3A) -> Self { |
| 326 | let b: [bool; 3] = b.into(); |
| 327 | Self::new(b[0], b[1], b[2]) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // BVec4A //////////////////////////////////////////////////////////////////////////////////////// |
| 332 | |
| 333 | /// A 4-dimensional SIMD vector mask. |
| 334 | /// |
| 335 | /// This type is 16 byte aligned and is backed by a SIMD vector. If SIMD is not available `BVec4A` |
| 336 | /// will be a type alias for `BVec4`. |
| 337 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 338 | #[derive(Clone, Copy)] |
| 339 | #[repr(transparent)] |
| 340 | pub struct BVec4A(pub(crate) __m128); |
| 341 | |
| 342 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 343 | impl_vec4mask!(BVec4A, u32, __m128); |
| 344 | |
| 345 | #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))] |
| 346 | pub type BVec4A = BVec4; |
| 347 | |
| 348 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 349 | impl From<BVec4> for BVec4A { |
| 350 | #[inline] |
| 351 | fn from(b: BVec4) -> Self { |
| 352 | Self::new(b.0.x, b.0.y, b.0.z, b.0.w) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 357 | impl From<BVec4A> for BVec4 { |
| 358 | #[inline] |
| 359 | fn from(b: BVec4A) -> Self { |
| 360 | let b: [bool; 4] = b.into(); |
| 361 | Self::new(b[0], b[1], b[2], b[3]) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // boolean vectors //////////////////////////////////////////////////////////////////////////////// |
| 366 | type XYBool = crate::XY<bool>; |
| 367 | |
| 368 | /// A 2-dimensional boolean vector. |
| 369 | #[derive(Copy, Clone)] |
| 370 | #[repr(transparent)] |
| 371 | pub struct BVec2(pub(crate) XYBool); |
| 372 | impl_vec2mask!(BVec2, bool, XYBool); |
| 373 | |
| 374 | type XYZBool = crate::XYZ<bool>; |
| 375 | |
| 376 | /// A 3-dimensional boolean vector. |
| 377 | #[derive(Copy, Clone)] |
| 378 | #[repr(transparent)] |
| 379 | pub struct BVec3(pub(crate) XYZBool); |
| 380 | impl_vec3mask!(BVec3, bool, XYZBool); |
| 381 | |
| 382 | type XYZWBool = crate::XYZW<bool>; |
| 383 | |
| 384 | /// A 4-dimensional boolean vector. |
| 385 | #[derive(Copy, Clone)] |
| 386 | #[repr(transparent)] |
| 387 | pub struct BVec4(pub(crate) XYZWBool); |
| 388 | impl_vec4mask!(BVec4, bool, XYZWBool); |
| 389 | |
| 390 | mod const_test_bvec2 { |
| 391 | const_assert_eq!(1, core::mem::align_of::<super::BVec2>()); |
| 392 | const_assert_eq!(2, core::mem::size_of::<super::BVec2>()); |
| 393 | } |
| 394 | |
| 395 | mod const_test_bvec3 { |
| 396 | const_assert_eq!(1, core::mem::align_of::<super::BVec3>()); |
| 397 | const_assert_eq!(3, core::mem::size_of::<super::BVec3>()); |
| 398 | } |
| 399 | |
| 400 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 401 | mod const_test_bvec3a { |
| 402 | const_assert_eq!(16, core::mem::align_of::<super::BVec3A>()); |
| 403 | const_assert_eq!(16, core::mem::size_of::<super::BVec3A>()); |
| 404 | } |
| 405 | |
| 406 | mod const_test_bvec4 { |
| 407 | const_assert_eq!(1, core::mem::align_of::<super::BVec4>()); |
| 408 | const_assert_eq!(4, core::mem::size_of::<super::BVec4>()); |
| 409 | } |
| 410 | |
| 411 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 412 | mod const_test_bvec4a { |
| 413 | const_assert_eq!(16, core::mem::align_of::<super::BVec4A>()); |
| 414 | const_assert_eq!(16, core::mem::size_of::<super::BVec4A>()); |
| 415 | } |