| 1 | // Adds common vector methods to an impl. |
| 2 | // The methods here should be supported for all types of $t and all sizes of vector. |
| 3 | macro_rules! impl_vecn_common_methods { |
| 4 | ($t:ty, $vecn:ident, $mask:ident, $inner:ident, $vectrait:ident) => { |
| 5 | /// Creates a vector with all elements set to `v`. |
| 6 | #[inline(always)] |
| 7 | pub fn splat(v: $t) -> Self { |
| 8 | Self($inner::splat(v)) |
| 9 | } |
| 10 | |
| 11 | /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use |
| 12 | /// for each element of `self`. |
| 13 | /// |
| 14 | /// A true element in the mask uses the corresponding element from `if_true`, and false |
| 15 | /// uses the element from `if_false`. |
| 16 | #[inline(always)] |
| 17 | pub fn select(mask: $mask, if_true: $vecn, if_false: $vecn) -> $vecn { |
| 18 | Self($inner::select(mask.0, if_true.0, if_false.0)) |
| 19 | } |
| 20 | |
| 21 | /// Computes the dot product of `self` and `other`. |
| 22 | #[inline(always)] |
| 23 | pub fn dot(self, other: Self) -> $t { |
| 24 | $vectrait::dot(self.0, other.0) |
| 25 | } |
| 26 | |
| 27 | /// Returns a vector containing the mininum values for each element of `self` and `other`. |
| 28 | /// |
| 29 | /// In other words this computes `[self.x.max(other.x), self.y.max(other.y), ..]`. |
| 30 | #[inline(always)] |
| 31 | pub fn min(self, other: Self) -> Self { |
| 32 | Self(self.0.min(other.0)) |
| 33 | } |
| 34 | |
| 35 | /// Returns a vector containing the maximum values for each element of `self` and `other`. |
| 36 | /// |
| 37 | /// In other words this computes `[self.x.max(other.x), self.y.max(other.y), ..]`. |
| 38 | #[inline(always)] |
| 39 | pub fn max(self, other: Self) -> Self { |
| 40 | Self(self.0.max(other.0)) |
| 41 | } |
| 42 | |
| 43 | /// Component-wise clamping of values, similar to [`f32::clamp`]. |
| 44 | /// |
| 45 | /// Each element in `min` must be less-or-equal to the corresponing element in `max`. |
| 46 | /// |
| 47 | /// # Panics |
| 48 | /// |
| 49 | /// Will panic if `min` is greater than `max` when `glam_assert` is enabled. |
| 50 | #[inline(always)] |
| 51 | pub fn clamp(self, min: Self, max: Self) -> Self { |
| 52 | Self($vectrait::clamp(self.0, min.0, max.0)) |
| 53 | } |
| 54 | |
| 55 | /// Returns the horizontal minimum of `self`. |
| 56 | /// |
| 57 | /// In other words this computes `min(x, y, ..)`. |
| 58 | #[inline(always)] |
| 59 | pub fn min_element(self) -> $t { |
| 60 | $vectrait::min_element(self.0) |
| 61 | } |
| 62 | |
| 63 | /// Returns the horizontal maximum of `self`. |
| 64 | /// |
| 65 | /// In other words this computes `max(x, y, ..)`. |
| 66 | #[inline(always)] |
| 67 | pub fn max_element(self) -> $t { |
| 68 | $vectrait::max_element(self.0) |
| 69 | } |
| 70 | |
| 71 | /// Returns a vector mask containing the result of a `==` comparison for each element of |
| 72 | /// `self` and `other`. |
| 73 | /// |
| 74 | /// In other words, this computes `[self.x == other.x, self.y == other.y, ..]` for all |
| 75 | /// elements. |
| 76 | #[inline(always)] |
| 77 | pub fn cmpeq(self, other: Self) -> $mask { |
| 78 | $mask(self.0.cmpeq(other.0)) |
| 79 | } |
| 80 | |
| 81 | /// Returns a vector mask containing the result of a `!=` comparison for each element of |
| 82 | /// `self` and `other`. |
| 83 | /// |
| 84 | /// In other words this computes `[self.x != other.x, self.y != other.y, ..]` for all |
| 85 | /// elements. |
| 86 | #[inline(always)] |
| 87 | pub fn cmpne(self, other: Self) -> $mask { |
| 88 | $mask(self.0.cmpne(other.0)) |
| 89 | } |
| 90 | |
| 91 | /// Returns a vector mask containing the result of a `>=` comparison for each element of |
| 92 | /// `self` and `other`. |
| 93 | /// |
| 94 | /// In other words this computes `[self.x >= other.x, self.y >= other.y, ..]` for all |
| 95 | /// elements. |
| 96 | #[inline(always)] |
| 97 | pub fn cmpge(self, other: Self) -> $mask { |
| 98 | $mask(self.0.cmpge(other.0)) |
| 99 | } |
| 100 | |
| 101 | /// Returns a vector mask containing the result of a `>` comparison for each element of |
| 102 | /// `self` and `other`. |
| 103 | /// |
| 104 | /// In other words this computes `[self.x > other.x, self.y > other.y, ..]` for all |
| 105 | /// elements. |
| 106 | #[inline(always)] |
| 107 | pub fn cmpgt(self, other: Self) -> $mask { |
| 108 | $mask(self.0.cmpgt(other.0)) |
| 109 | } |
| 110 | |
| 111 | /// Returns a vector mask containing the result of a `<=` comparison for each element of |
| 112 | /// `self` and `other`. |
| 113 | /// |
| 114 | /// In other words this computes `[self.x <= other.x, self.y <= other.y, ..]` for all |
| 115 | /// elements. |
| 116 | #[inline(always)] |
| 117 | pub fn cmple(self, other: Self) -> $mask { |
| 118 | $mask(self.0.cmple(other.0)) |
| 119 | } |
| 120 | |
| 121 | /// Returns a vector mask containing the result of a `<` comparison for each element of |
| 122 | /// `self` and `other`. |
| 123 | /// |
| 124 | /// In other words this computes `[self.x < other.x, self.y < other.y, ..]` for all |
| 125 | /// elements. |
| 126 | #[inline(always)] |
| 127 | pub fn cmplt(self, other: Self) -> $mask { |
| 128 | $mask(self.0.cmplt(other.0)) |
| 129 | } |
| 130 | |
| 131 | /// Creates a vector from the first N values in `slice`. |
| 132 | /// |
| 133 | /// # Panics |
| 134 | /// |
| 135 | /// Panics if `slice` is less than N elements long. |
| 136 | #[inline(always)] |
| 137 | pub fn from_slice(slice: &[$t]) -> Self { |
| 138 | Self($vectrait::from_slice_unaligned(slice)) |
| 139 | } |
| 140 | |
| 141 | /// Writes the elements of `self` to the first N elements in `slice`. |
| 142 | /// |
| 143 | /// # Panics |
| 144 | /// |
| 145 | /// Panics if `slice` is less than N elements long. |
| 146 | #[inline(always)] |
| 147 | pub fn write_to_slice(self, slice: &mut [$t]) { |
| 148 | $vectrait::write_to_slice_unaligned(self.0, slice) |
| 149 | } |
| 150 | |
| 151 | /// Per element multiplication/addition of the three inputs: b + (self * a) |
| 152 | #[inline(always)] |
| 153 | #[allow(dead_code)] |
| 154 | pub(crate) fn mul_add(self, a: Self, b: Self) -> Self { |
| 155 | Self(self.0.mul_add(a.0, b.0)) |
| 156 | } |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | // Adds signed type vector methods to an impl. |
| 161 | // The methods here should be supported for signed types of $t and all sizes of vector. |
| 162 | macro_rules! impl_vecn_signed_methods { |
| 163 | ($t:ty, $vecn:ident, $mask:ident, $inner:ident, $sgntrait:ident) => { |
| 164 | // impl_vecn_common_methods!($t, $vecn, $mask, $inner, $vectrait); |
| 165 | |
| 166 | /// Returns a vector containing the absolute value of each element of `self`. |
| 167 | #[inline(always)] |
| 168 | pub fn abs(self) -> Self { |
| 169 | Self($sgntrait::abs(self.0)) |
| 170 | } |
| 171 | |
| 172 | /// Returns a vector with elements representing the sign of `self`. |
| 173 | /// |
| 174 | /// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 175 | /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 176 | /// - `NAN` if the number is `NAN` |
| 177 | #[inline(always)] |
| 178 | pub fn signum(self) -> Self { |
| 179 | Self($sgntrait::signum(self.0)) |
| 180 | } |
| 181 | }; |
| 182 | } |
| 183 | |
| 184 | // Adds float type vector methods to an impl. |
| 185 | // The methods here should be supported for float types of $t and all sizes of vector. |
| 186 | macro_rules! impl_vecn_float_methods { |
| 187 | ($t:ty, $vecn:ident, $mask:ident, $inner:ident, $flttrait:ident) => { |
| 188 | // impl_vecn_signed_methods!($t, $vecn, $mask, $inner, $sgntrait, $vectrait); |
| 189 | |
| 190 | /// Returns `true` if, and only if, all elements are finite. If any element is either |
| 191 | /// `NaN`, positive or negative infinity, this will return `false`. |
| 192 | #[inline(always)] |
| 193 | pub fn is_finite(self) -> bool { |
| 194 | $flttrait::is_finite(self.0) |
| 195 | } |
| 196 | |
| 197 | /// Returns `true` if any elements are `NaN`. |
| 198 | #[inline(always)] |
| 199 | pub fn is_nan(self) -> bool { |
| 200 | $flttrait::is_nan(self.0) |
| 201 | } |
| 202 | |
| 203 | /// Performs `is_nan` on each element of self, returning a vector mask of the results. |
| 204 | /// |
| 205 | /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`. |
| 206 | #[inline(always)] |
| 207 | pub fn is_nan_mask(self) -> $mask { |
| 208 | $mask($flttrait::is_nan_mask(self.0)) |
| 209 | } |
| 210 | |
| 211 | /// Computes the length of `self`. |
| 212 | #[cfg_attr(docsrs, doc(alias = "magnitude"))] |
| 213 | #[inline(always)] |
| 214 | pub fn length(self) -> $t { |
| 215 | $flttrait::length(self.0) |
| 216 | } |
| 217 | |
| 218 | /// Computes the squared length of `self`. |
| 219 | /// |
| 220 | /// This is faster than `length()` as it avoids a square root operation. |
| 221 | #[cfg_attr(docsrs, doc(alias = "magnitude2"))] |
| 222 | #[inline(always)] |
| 223 | pub fn length_squared(self) -> $t { |
| 224 | $flttrait::length_squared(self.0) |
| 225 | } |
| 226 | |
| 227 | /// Computes `1.0 / length()`. |
| 228 | /// |
| 229 | /// For valid results, `self` must _not_ be of length zero. |
| 230 | #[inline(always)] |
| 231 | pub fn length_recip(self) -> $t { |
| 232 | $flttrait::length_recip(self.0) |
| 233 | } |
| 234 | |
| 235 | /// Computes the Euclidean distance between two points in space. |
| 236 | #[inline] |
| 237 | pub fn distance(self, other: Self) -> $t { |
| 238 | (self - other).length() |
| 239 | } |
| 240 | |
| 241 | /// Compute the squared euclidean distance between two points in space. |
| 242 | #[inline] |
| 243 | pub fn distance_squared(self, other: Self) -> $t { |
| 244 | (self - other).length_squared() |
| 245 | } |
| 246 | |
| 247 | /// Returns `self` normalized to length 1.0. |
| 248 | /// |
| 249 | /// For valid results, `self` must _not_ be of length zero, nor very close to zero. |
| 250 | /// |
| 251 | /// See also [`Self::try_normalize`] and [`Self::normalize_or_zero`]. |
| 252 | /// |
| 253 | /// Panics |
| 254 | /// |
| 255 | /// Will panic if `self` is zero length when `glam_assert` is enabled. |
| 256 | #[must_use] |
| 257 | #[inline(always)] |
| 258 | pub fn normalize(self) -> Self { |
| 259 | Self($flttrait::normalize(self.0)) |
| 260 | } |
| 261 | |
| 262 | /// Returns `self` normalized to length 1.0 if possible, else returns `None`. |
| 263 | /// |
| 264 | /// In particular, if the input is zero (or very close to zero), or non-finite, |
| 265 | /// the result of this operation will be `None`. |
| 266 | /// |
| 267 | /// See also [`Self::normalize_or_zero`]. |
| 268 | #[must_use] |
| 269 | #[inline] |
| 270 | pub fn try_normalize(self) -> Option<Self> { |
| 271 | let rcp = self.length_recip(); |
| 272 | if rcp.is_finite() && rcp > 0.0 { |
| 273 | Some(self * rcp) |
| 274 | } else { |
| 275 | None |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /// Returns `self` normalized to length 1.0 if possible, else returns zero. |
| 280 | /// |
| 281 | /// In particular, if the input is zero (or very close to zero), or non-finite, |
| 282 | /// the result of this operation will be zero. |
| 283 | /// |
| 284 | /// See also [`Self::try_normalize`]. |
| 285 | #[must_use] |
| 286 | #[inline] |
| 287 | pub fn normalize_or_zero(self) -> Self { |
| 288 | let rcp = self.length_recip(); |
| 289 | if rcp.is_finite() && rcp > 0.0 { |
| 290 | self * rcp |
| 291 | } else { |
| 292 | Self::ZERO |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /// Returns whether `self` is length `1.0` or not. |
| 297 | /// |
| 298 | /// Uses a precision threshold of `1e-6`. |
| 299 | #[inline(always)] |
| 300 | pub fn is_normalized(self) -> bool { |
| 301 | $flttrait::is_normalized(self.0) |
| 302 | } |
| 303 | |
| 304 | /// Returns the vector projection of `self` onto `other`. |
| 305 | /// |
| 306 | /// `other` must be of non-zero length. |
| 307 | /// |
| 308 | /// # Panics |
| 309 | /// |
| 310 | /// Will panic if `other` is zero length when `glam_assert` is enabled. |
| 311 | #[must_use] |
| 312 | #[inline] |
| 313 | pub fn project_onto(self, other: Self) -> Self { |
| 314 | let other_len_sq_rcp = other.dot(other).recip(); |
| 315 | glam_assert!(other_len_sq_rcp.is_finite()); |
| 316 | other * self.dot(other) * other_len_sq_rcp |
| 317 | } |
| 318 | |
| 319 | /// Returns the vector rejection of `self` from `other`. |
| 320 | /// |
| 321 | /// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 322 | /// `other`, in other words the result of `self - self.project_onto(other)`. |
| 323 | /// |
| 324 | /// `other` must be of non-zero length. |
| 325 | /// |
| 326 | /// # Panics |
| 327 | /// |
| 328 | /// Will panic if `other` has a length of zero when `glam_assert` is enabled. |
| 329 | #[must_use] |
| 330 | #[inline] |
| 331 | pub fn reject_from(self, other: Self) -> Self { |
| 332 | self - self.project_onto(other) |
| 333 | } |
| 334 | |
| 335 | /// Returns the vector projection of `self` onto `other`. |
| 336 | /// |
| 337 | /// `other` must be normalized. |
| 338 | /// |
| 339 | /// # Panics |
| 340 | /// |
| 341 | /// Will panic if `other` is not normalized when `glam_assert` is enabled. |
| 342 | #[must_use] |
| 343 | #[inline] |
| 344 | pub fn project_onto_normalized(self, other: Self) -> Self { |
| 345 | glam_assert!(other.is_normalized()); |
| 346 | other * self.dot(other) |
| 347 | } |
| 348 | |
| 349 | /// Returns the vector rejection of `self` from `other`. |
| 350 | /// |
| 351 | /// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 352 | /// `other`, in other words the result of `self - self.project_onto(other)`. |
| 353 | /// |
| 354 | /// `other` must be normalized. |
| 355 | /// |
| 356 | /// # Panics |
| 357 | /// |
| 358 | /// Will panic if `other` is not normalized when `glam_assert` is enabled. |
| 359 | #[must_use] |
| 360 | #[inline] |
| 361 | pub fn reject_from_normalized(self, other: Self) -> Self { |
| 362 | self - self.project_onto_normalized(other) |
| 363 | } |
| 364 | |
| 365 | /// Returns a vector containing the nearest integer to a number for each element of `self`. |
| 366 | /// Round half-way cases away from 0.0. |
| 367 | #[inline(always)] |
| 368 | pub fn round(self) -> Self { |
| 369 | Self($flttrait::round(self.0)) |
| 370 | } |
| 371 | |
| 372 | /// Returns a vector containing the largest integer less than or equal to a number for each |
| 373 | /// element of `self`. |
| 374 | #[inline(always)] |
| 375 | pub fn floor(self) -> Self { |
| 376 | Self($flttrait::floor(self.0)) |
| 377 | } |
| 378 | |
| 379 | /// Returns a vector containing the smallest integer greater than or equal to a number for |
| 380 | /// each element of `self`. |
| 381 | #[inline(always)] |
| 382 | pub fn ceil(self) -> Self { |
| 383 | Self($flttrait::ceil(self.0)) |
| 384 | } |
| 385 | |
| 386 | /// Returns a vector containing the fractional part of the vector, e.g. `self - |
| 387 | /// self.floor()`. |
| 388 | /// |
| 389 | /// Note that this is fast but not precise for large numbers. |
| 390 | #[inline(always)] |
| 391 | pub fn fract(self) -> Self { |
| 392 | self - self.floor() |
| 393 | } |
| 394 | |
| 395 | /// Returns a vector containing `e^self` (the exponential function) for each element of |
| 396 | /// `self`. |
| 397 | #[inline(always)] |
| 398 | pub fn exp(self) -> Self { |
| 399 | Self($flttrait::exp(self.0)) |
| 400 | } |
| 401 | |
| 402 | /// Returns a vector containing each element of `self` raised to the power of `n`. |
| 403 | #[inline(always)] |
| 404 | pub fn powf(self, n: $t) -> Self { |
| 405 | Self($flttrait::powf(self.0, n)) |
| 406 | } |
| 407 | |
| 408 | /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`. |
| 409 | #[inline(always)] |
| 410 | pub fn recip(self) -> Self { |
| 411 | Self($flttrait::recip(self.0)) |
| 412 | } |
| 413 | |
| 414 | /// Performs a linear interpolation between `self` and `other` based on the value `s`. |
| 415 | /// |
| 416 | /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result |
| 417 | /// will be equal to `other`. |
| 418 | #[cfg_attr(docsrs, doc(alias = "mix"))] |
| 419 | #[inline] |
| 420 | pub fn lerp(self, other: Self, s: $t) -> Self { |
| 421 | self + ((other - self) * s) |
| 422 | } |
| 423 | |
| 424 | /// Returns true if the absolute difference of all elements between `self` and `other` is |
| 425 | /// less than or equal to `max_abs_diff`. |
| 426 | /// |
| 427 | /// This can be used to compare if two vectors contain similar elements. It works best when |
| 428 | /// comparing with a known value. The `max_abs_diff` that should be used used depends on |
| 429 | /// the values being compared against. |
| 430 | /// |
| 431 | /// For more see |
| 432 | /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 433 | #[inline(always)] |
| 434 | pub fn abs_diff_eq(self, other: Self, max_abs_diff: $t) -> bool { |
| 435 | $flttrait::abs_diff_eq(self.0, other.0, max_abs_diff) |
| 436 | } |
| 437 | |
| 438 | /// Returns a vector with a length no less than `min` and no more than `max` |
| 439 | /// |
| 440 | /// # Panics |
| 441 | /// |
| 442 | /// Will panic if `min` is greater than `max` when `glam_assert` is enabled. |
| 443 | #[inline] |
| 444 | pub fn clamp_length(self, min: $t, max: $t) -> Self { |
| 445 | glam_assert!(min <= max); |
| 446 | let length_sq = self.length_squared(); |
| 447 | if length_sq < min * min { |
| 448 | self * (length_sq.sqrt().recip() * min) |
| 449 | } else if length_sq > max * max { |
| 450 | self * (length_sq.sqrt().recip() * max) |
| 451 | } else { |
| 452 | self |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /// Returns a vector with a length no more than `max` |
| 457 | pub fn clamp_length_max(self, max: $t) -> Self { |
| 458 | let length_sq = self.length_squared(); |
| 459 | if length_sq > max * max { |
| 460 | self * (length_sq.sqrt().recip() * max) |
| 461 | } else { |
| 462 | self |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /// Returns a vector with a length no less than `min` |
| 467 | pub fn clamp_length_min(self, min: $t) -> Self { |
| 468 | let length_sq = self.length_squared(); |
| 469 | if self.length_squared() < min * min { |
| 470 | self * (length_sq.sqrt().recip() * min) |
| 471 | } else { |
| 472 | self |
| 473 | } |
| 474 | } |
| 475 | }; |
| 476 | } |
| 477 | |
| 478 | // Adds common vector trait implementations. |
| 479 | // The traits here should be supported for all types of $t and all sizes of vector. |
| 480 | macro_rules! impl_vecn_common_traits { |
| 481 | ($t:ty, $size:literal, $vecn:ident, $inner:ident, $trait:ident) => { |
| 482 | impl Default for $vecn { |
| 483 | #[inline(always)] |
| 484 | fn default() -> Self { |
| 485 | Self($inner::ZERO) |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | impl PartialEq for $vecn { |
| 490 | #[inline(always)] |
| 491 | fn eq(&self, other: &Self) -> bool { |
| 492 | self.cmpeq(*other).all() |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | impl From<$vecn> for $inner { |
| 497 | #[inline(always)] |
| 498 | fn from(t: $vecn) -> Self { |
| 499 | t.0 |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | impl From<$inner> for $vecn { |
| 504 | #[inline(always)] |
| 505 | fn from(t: $inner) -> Self { |
| 506 | Self(t) |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | impl Div<$vecn> for $vecn { |
| 511 | type Output = Self; |
| 512 | #[inline(always)] |
| 513 | fn div(self, other: $vecn) -> Self { |
| 514 | Self(self.0.div(other.0)) |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | impl DivAssign<$vecn> for $vecn { |
| 519 | #[inline(always)] |
| 520 | fn div_assign(&mut self, other: $vecn) { |
| 521 | self.0 = self.0.div(other.0) |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | impl Div<$t> for $vecn { |
| 526 | type Output = Self; |
| 527 | #[inline(always)] |
| 528 | fn div(self, other: $t) -> Self { |
| 529 | Self(self.0.div_scalar(other)) |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | impl DivAssign<$t> for $vecn { |
| 534 | #[inline(always)] |
| 535 | fn div_assign(&mut self, other: $t) { |
| 536 | self.0 = self.0.div_scalar(other) |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | impl Div<$vecn> for $t { |
| 541 | type Output = $vecn; |
| 542 | #[inline(always)] |
| 543 | fn div(self, other: $vecn) -> $vecn { |
| 544 | $vecn($inner::splat(self).div(other.0)) |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | impl Mul<$vecn> for $vecn { |
| 549 | type Output = Self; |
| 550 | #[inline(always)] |
| 551 | fn mul(self, other: $vecn) -> Self { |
| 552 | Self(self.0.mul(other.0)) |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | impl MulAssign<$vecn> for $vecn { |
| 557 | #[inline(always)] |
| 558 | fn mul_assign(&mut self, other: $vecn) { |
| 559 | self.0 = self.0.mul(other.0) |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | impl Mul<$t> for $vecn { |
| 564 | type Output = Self; |
| 565 | #[inline(always)] |
| 566 | fn mul(self, other: $t) -> Self { |
| 567 | Self(self.0.mul_scalar(other)) |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | impl MulAssign<$t> for $vecn { |
| 572 | #[inline(always)] |
| 573 | fn mul_assign(&mut self, other: $t) { |
| 574 | self.0 = self.0.mul_scalar(other) |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | impl Mul<$vecn> for $t { |
| 579 | type Output = $vecn; |
| 580 | #[inline(always)] |
| 581 | fn mul(self, other: $vecn) -> $vecn { |
| 582 | $vecn($inner::splat(self).mul(other.0)) |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | impl Add<$vecn> for $vecn { |
| 587 | type Output = Self; |
| 588 | #[inline(always)] |
| 589 | fn add(self, other: $vecn) -> Self { |
| 590 | Self(self.0.add(other.0)) |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | impl AddAssign<$vecn> for $vecn { |
| 595 | #[inline(always)] |
| 596 | fn add_assign(&mut self, other: $vecn) { |
| 597 | self.0 = self.0.add(other.0) |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | impl Add<$t> for $vecn { |
| 602 | type Output = Self; |
| 603 | #[inline(always)] |
| 604 | fn add(self, other: $t) -> Self { |
| 605 | Self(self.0.add_scalar(other)) |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | impl AddAssign<$t> for $vecn { |
| 610 | #[inline(always)] |
| 611 | fn add_assign(&mut self, other: $t) { |
| 612 | self.0 = self.0.add_scalar(other) |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | impl Add<$vecn> for $t { |
| 617 | type Output = $vecn; |
| 618 | #[inline(always)] |
| 619 | fn add(self, other: $vecn) -> $vecn { |
| 620 | $vecn($inner::splat(self).add(other.0)) |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | impl Sub<$vecn> for $vecn { |
| 625 | type Output = Self; |
| 626 | #[inline(always)] |
| 627 | fn sub(self, other: $vecn) -> Self { |
| 628 | Self(self.0.sub(other.0)) |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | impl SubAssign<$vecn> for $vecn { |
| 633 | #[inline(always)] |
| 634 | fn sub_assign(&mut self, other: $vecn) { |
| 635 | self.0 = self.0.sub(other.0) |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | impl Sub<$t> for $vecn { |
| 640 | type Output = Self; |
| 641 | #[inline(always)] |
| 642 | fn sub(self, other: $t) -> Self { |
| 643 | Self(self.0.sub_scalar(other)) |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | impl SubAssign<$t> for $vecn { |
| 648 | #[inline(always)] |
| 649 | fn sub_assign(&mut self, other: $t) { |
| 650 | self.0 = self.0.sub_scalar(other) |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | impl Sub<$vecn> for $t { |
| 655 | type Output = $vecn; |
| 656 | #[inline(always)] |
| 657 | fn sub(self, other: $vecn) -> $vecn { |
| 658 | $vecn($inner::splat(self).sub(other.0)) |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | #[cfg(not(target_arch = "spriv"))] |
| 663 | impl AsRef<[$t; $size]> for $vecn { |
| 664 | #[inline(always)] |
| 665 | fn as_ref(&self) -> &[$t; $size] { |
| 666 | unsafe { &*(self as *const $vecn as *const [$t; $size]) } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | #[cfg(not(target_arch = "spriv"))] |
| 671 | impl AsMut<[$t; $size]> for $vecn { |
| 672 | #[inline(always)] |
| 673 | fn as_mut(&mut self) -> &mut [$t; $size] { |
| 674 | unsafe { &mut *(self as *mut $vecn as *mut [$t; $size]) } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | impl From<[$t; $size]> for $vecn { |
| 679 | #[inline(always)] |
| 680 | fn from(a: [$t; $size]) -> Self { |
| 681 | Self($trait::from_array(a)) |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | impl From<$vecn> for [$t; $size] { |
| 686 | #[inline(always)] |
| 687 | fn from(v: $vecn) -> Self { |
| 688 | v.into_array() |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | #[cfg(feature = "std")] |
| 693 | impl<'a> Sum<&'a Self> for $vecn { |
| 694 | #[inline] |
| 695 | fn sum<I>(iter: I) -> Self |
| 696 | where |
| 697 | I: Iterator<Item = &'a Self>, |
| 698 | { |
| 699 | iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | #[cfg(feature = "std")] |
| 704 | impl<'a> Product<&'a Self> for $vecn { |
| 705 | #[inline] |
| 706 | fn product<I>(iter: I) -> Self |
| 707 | where |
| 708 | I: Iterator<Item = &'a Self>, |
| 709 | { |
| 710 | iter.fold(Self::ONE, |a, &b| Self::mul(a, b)) |
| 711 | } |
| 712 | } |
| 713 | }; |
| 714 | } |
| 715 | |
| 716 | macro_rules! impl_vecn_eq_hash_traits { |
| 717 | ($t:ty, $size:literal, $vecn:ident) => { |
| 718 | impl Eq for $vecn {} |
| 719 | |
| 720 | #[cfg(not(target_arch = "spriv"))] |
| 721 | impl core::hash::Hash for $vecn { |
| 722 | fn hash<H: core::hash::Hasher>(&self, state: &mut H) { |
| 723 | let inner: &[$t; $size] = self.as_ref(); |
| 724 | inner.hash(state); |
| 725 | } |
| 726 | } |
| 727 | }; |
| 728 | } |
| 729 | |
| 730 | // Adds signed vector trait implementations. |
| 731 | // The traits here should be supported for signed types of $t and all sizes of vector. |
| 732 | macro_rules! impl_vecn_signed_traits { |
| 733 | ($t:ty, $size:literal, $vecn:ident, $inner:ident, $sgntrait:ident) => { |
| 734 | impl Neg for $vecn { |
| 735 | type Output = Self; |
| 736 | #[inline(always)] |
| 737 | fn neg(self) -> Self { |
| 738 | Self(self.0.neg()) |
| 739 | } |
| 740 | } |
| 741 | }; |
| 742 | } |
| 743 | |
| 744 | macro_rules! impl_vecn_as_f32 { |
| 745 | ($ret:ident, $($e:ident),+) => { |
| 746 | /// Casts all elements of `self` to `f32`. |
| 747 | #[inline(always)] |
| 748 | pub fn as_f32(&self) -> $ret { |
| 749 | $ret::new($(self.$e as f32),+) |
| 750 | } |
| 751 | }; |
| 752 | } |
| 753 | |
| 754 | macro_rules! impl_vecn_as_f64 { |
| 755 | ($ret:ident, $($e:ident),+) => { |
| 756 | /// Casts all elements of `self` to `f64`. |
| 757 | #[inline(always)] |
| 758 | pub fn as_f64(&self) -> $ret { |
| 759 | $ret::new($(self.$e as f64),+) |
| 760 | } |
| 761 | }; |
| 762 | } |
| 763 | |
| 764 | macro_rules! impl_vecn_as_i32 { |
| 765 | ($ret:ident, $($e:ident),+) => { |
| 766 | /// Casts all elements of `self` to `i32`. |
| 767 | #[inline(always)] |
| 768 | pub fn as_i32(&self) -> $ret { |
| 769 | $ret::new($(self.$e as i32),+) |
| 770 | } |
| 771 | }; |
| 772 | } |
| 773 | |
| 774 | macro_rules! impl_vecn_as_u32 { |
| 775 | ($ret:ident, $($e:ident),+) => { |
| 776 | /// Casts all elements of `self` to `u32`. |
| 777 | #[inline(always)] |
| 778 | pub fn as_u32(&self) -> $ret { |
| 779 | $ret::new($(self.$e as u32),+) |
| 780 | } |
| 781 | }; |
| 782 | } |