| 1 |
{% import "macros.rs.tera" as macros %} |
| 2 |
|
| 3 |
// Generated from {{template_path}} template. Edit the template, not the generated file. |
| 4 |
|
| 5 |
{% if is_scalar and not is_align %} |
| 6 |
{% set mask_t = "BVec" ~ dim %} |
| 7 |
{% else %} |
| 8 |
{% set is_simd = true %} |
| 9 |
{% if is_sse2 %} |
| 10 |
{% set simd_t = "__m128" %} |
| 11 |
{% elif is_wasm32 %} |
| 12 |
{% set simd_t = "v128" %} |
| 13 |
{% elif is_neon %} |
| 14 |
{% set simd_t = "float32x4_t" %} |
| 15 |
{% elif is_coresimd %} |
| 16 |
{% set simd_t = "f32x4" %} |
| 17 |
{% else %} |
| 18 |
{% set is_simd = false %} |
| 19 |
{% endif %} |
| 20 |
{% set mask_t = "BVec" ~ dim ~ "A" %} |
| 21 |
{% endif %} |
| 22 |
|
| 23 |
{% if scalar_t == "f32" or scalar_t == "f64" %} |
| 24 |
{% set is_signed = true %} |
| 25 |
{% set is_float = true %} |
| 26 |
{% if scalar_t == "f32" %} |
| 27 |
{% if dim == 3 and is_align %} |
| 28 |
{% set self_t = "Vec3A" %} |
| 29 |
{% set mask_t = "BVec3A" %} |
| 30 |
{% else %} |
| 31 |
{% set self_t = "Vec" ~ dim %} |
| 32 |
{% endif %} |
| 33 |
{% set vec2_t = "Vec2" %} |
| 34 |
{% set vec3_t = "Vec3" %} |
| 35 |
{% set vec3a_t = "Vec3A" %} |
| 36 |
{% set vec4_t = "Vec4" %} |
| 37 |
{% set quat_t = "Quat" %} |
| 38 |
{% elif scalar_t == "f64" %} |
| 39 |
{% set self_t = "DVec" ~ dim %} |
| 40 |
{% set vec2_t = "DVec2" %} |
| 41 |
{% set vec3_t = "DVec3" %} |
| 42 |
{% set vec4_t = "DVec4" %} |
| 43 |
{% set from_types = ["Vec" ~ dim, "IVec" ~ dim, "UVec" ~ dim] %} |
| 44 |
{% set quat_t = "DQuat" %} |
| 45 |
{% endif %} |
| 46 |
{% elif scalar_t == "i8" %} |
| 47 |
{% set is_signed = true %} |
| 48 |
{% set is_float = false %} |
| 49 |
{% set unsigned_scalar_t = "u8" %} |
| 50 |
{% set self_t = "I8Vec" ~ dim %} |
| 51 |
{% set opposite_signedness_t = "U8Vec" ~ dim %} |
| 52 |
{% set vec2_t = "I8Vec2" %} |
| 53 |
{% set vec3_t = "I8Vec3" %} |
| 54 |
{% set vec4_t = "I8Vec4" %} |
| 55 |
{% set try_from_types = ["U8Vec" ~ dim, "I16Vec" ~ dim, "U16Vec" ~ dim, "IVec" ~ dim, "UVec" ~ dim, "I64Vec" ~ dim, "U64Vec" ~ dim, "USizeVec" ~ dim] %} |
| 56 |
{% elif scalar_t == "u8" %} |
| 57 |
{% set is_signed = false %} |
| 58 |
{% set is_float = false %} |
| 59 |
{% set unsigned_scalar_t = "u8" %} |
| 60 |
{% set self_t = "U8Vec" ~ dim %} |
| 61 |
{% set opposite_signedness_t = "I8Vec" ~ dim %} |
| 62 |
{% set vec2_t = "U8Vec2" %} |
| 63 |
{% set vec3_t = "U8Vec3" %} |
| 64 |
{% set vec4_t = "U8Vec4" %} |
| 65 |
{% set try_from_types = ["I8Vec" ~ dim, "I16Vec" ~ dim, "U16Vec" ~ dim, "IVec" ~ dim, "UVec" ~ dim, "I64Vec" ~ dim, "U64Vec" ~ dim, "USizeVec" ~ dim] %} |
| 66 |
{% elif scalar_t == "i16" %} |
| 67 |
{% set is_signed = true %} |
| 68 |
{% set is_float = false %} |
| 69 |
{% set unsigned_scalar_t = "u16" %} |
| 70 |
{% set self_t = "I16Vec" ~ dim %} |
| 71 |
{% set opposite_signedness_t = "U16Vec" ~ dim %} |
| 72 |
{% set vec2_t = "I16Vec2" %} |
| 73 |
{% set vec3_t = "I16Vec3" %} |
| 74 |
{% set vec4_t = "I16Vec4" %} |
| 75 |
{% set from_types = ["I8Vec" ~ dim, "U8Vec" ~ dim] %} |
| 76 |
{% set try_from_types = ["U16Vec" ~ dim, "IVec" ~ dim, "UVec" ~ dim, "I64Vec" ~ dim, "U64Vec" ~ dim, "USizeVec" ~ dim] %} |
| 77 |
{% elif scalar_t == "u16" %} |
| 78 |
{% set is_signed = false %} |
| 79 |
{% set is_float = false %} |
| 80 |
{% set unsigned_scalar_t = "u16" %} |
| 81 |
{% set self_t = "U16Vec" ~ dim %} |
| 82 |
{% set opposite_signedness_t = "I16Vec" ~ dim %} |
| 83 |
{% set vec2_t = "U16Vec2" %} |
| 84 |
{% set vec3_t = "U16Vec3" %} |
| 85 |
{% set vec4_t = "U16Vec4" %} |
| 86 |
{% set from_types = ["U8Vec" ~ dim] %} |
| 87 |
{% set try_from_types = ["I8Vec" ~ dim, "I16Vec" ~ dim, "IVec" ~ dim, "UVec" ~ dim, "I64Vec" ~ dim, "U64Vec" ~ dim, "USizeVec" ~ dim] %} |
| 88 |
{% elif scalar_t == "i32" %} |
| 89 |
{% set is_signed = true %} |
| 90 |
{% set is_float = false %} |
| 91 |
{% set unsigned_scalar_t = "u32" %} |
| 92 |
{% set self_t = "IVec" ~ dim %} |
| 93 |
{% set opposite_signedness_t = "UVec" ~ dim %} |
| 94 |
{% set vec2_t = "IVec2" %} |
| 95 |
{% set vec3_t = "IVec3" %} |
| 96 |
{% set vec4_t = "IVec4" %} |
| 97 |
{% set from_types = ["I8Vec" ~ dim, "U8Vec" ~ dim, "I16Vec" ~ dim, "U16Vec" ~ dim] %} |
| 98 |
{% set try_from_types = ["UVec" ~ dim, "I64Vec" ~ dim, "U64Vec" ~ dim, "USizeVec" ~ dim] %} |
| 99 |
{% elif scalar_t == "u32" %} |
| 100 |
{% set is_signed = false %} |
| 101 |
{% set is_float = false %} |
| 102 |
{% set unsigned_scalar_t = "u32" %} |
| 103 |
{% set self_t = "UVec" ~ dim %} |
| 104 |
{% set opposite_signedness_t = "IVec" ~ dim %} |
| 105 |
{% set vec2_t = "UVec2" %} |
| 106 |
{% set vec3_t = "UVec3" %} |
| 107 |
{% set vec4_t = "UVec4" %} |
| 108 |
{% set from_types = ["U8Vec" ~ dim, "U16Vec" ~ dim] %} |
| 109 |
{% set try_from_types = ["I8Vec" ~ dim, "I16Vec" ~ dim, "IVec" ~ dim, "I64Vec" ~ dim, "U64Vec" ~ dim, "USizeVec" ~ dim] %} |
| 110 |
{% elif scalar_t == "i64" %} |
| 111 |
{% set is_signed = true %} |
| 112 |
{% set is_float = false %} |
| 113 |
{% set unsigned_scalar_t = "u64" %} |
| 114 |
{% set self_t = "I64Vec" ~ dim %} |
| 115 |
{% set opposite_signedness_t = "U64Vec" ~ dim %} |
| 116 |
{% set vec2_t = "I64Vec2" %} |
| 117 |
{% set vec3_t = "I64Vec3" %} |
| 118 |
{% set vec4_t = "I64Vec4" %} |
| 119 |
{% set from_types = ["I8Vec" ~ dim, "U8Vec" ~ dim, "I16Vec" ~ dim, "U16Vec" ~ dim, "IVec" ~ dim, "UVec" ~ dim] %} |
| 120 |
{% set try_from_types = ["U64Vec" ~ dim, "USizeVec" ~ dim] %} |
| 121 |
{% elif scalar_t == "u64" %} |
| 122 |
{% set is_signed = false %} |
| 123 |
{% set is_float = false %} |
| 124 |
{% set unsigned_scalar_t = "u64" %} |
| 125 |
{% set self_t = "U64Vec" ~ dim %} |
| 126 |
{% set opposite_signedness_t = "I64Vec" ~ dim %} |
| 127 |
{% set vec2_t = "U64Vec2" %} |
| 128 |
{% set vec3_t = "U64Vec3" %} |
| 129 |
{% set vec4_t = "U64Vec4" %} |
| 130 |
{% set from_types = ["U8Vec" ~ dim, "U16Vec" ~ dim, "UVec" ~ dim] %} |
| 131 |
{% set try_from_types = ["I8Vec" ~ dim, "I16Vec" ~ dim, "IVec" ~ dim, "I64Vec" ~ dim, "USizeVec" ~ dim] %} |
| 132 |
{% elif scalar_t == "usize" %} |
| 133 |
{% set is_signed = false %} |
| 134 |
{% set is_float = false %} |
| 135 |
{% set unsigned_scalar_t = "usize" %} |
| 136 |
{% set self_t = "USizeVec" ~ dim %} |
| 137 |
{% set vec2_t = "USizeVec2" %} |
| 138 |
{% set vec3_t = "USizeVec3" %} |
| 139 |
{% set vec4_t = "USizeVec4" %} |
| 140 |
{% set from_types = ["U8Vec" ~ dim, "U16Vec" ~ dim] %} |
| 141 |
{% set try_from_types = ["I8Vec" ~ dim, "I16Vec" ~ dim, "IVec" ~ dim, "I64Vec" ~ dim, "UVec" ~ dim, "U64Vec" ~ dim] %} |
| 142 |
{% endif %} |
| 143 |
{% set bvec_from_type = "BVec" ~ dim %} |
| 144 |
{% if dim > 2 %} |
| 145 |
{% set bveca_from_type = "BVec" ~ dim ~ "A" %} |
| 146 |
{% endif %} |
| 147 |
|
| 148 |
{% if dim == 2 %} |
| 149 |
{% if scalar_t == "i8" or scalar_t == "u8" %} |
| 150 |
{% set cuda_align = 2 %} |
| 151 |
{% elif scalar_t == "i16" or scalar_t == "u16" %} |
| 152 |
{% set cuda_align = 4 %} |
| 153 |
{% elif scalar_t == "f32" or scalar_t == "i32" or scalar_t == "u32" %} |
| 154 |
{% set cuda_align = 8 %} |
| 155 |
{% elif scalar_t == "f64" or scalar_t == "i64" or scalar_t == "u64" %} |
| 156 |
{% set cuda_align = 16 %} |
| 157 |
{% elif scalar_t == "usize" %} |
| 158 |
{# assuming 64 bit here #} |
| 159 |
{% set cuda_align = 16 %} |
| 160 |
{% endif %} |
| 161 |
{% elif dim == 4 %} |
| 162 |
{% if scalar_t == "i8" or scalar_t == "u8" %} |
| 163 |
{% set cuda_align = 4 %} |
| 164 |
{% elif scalar_t == "i16" or scalar_t == "u16" %} |
| 165 |
{% set cuda_align = 8 %} |
| 166 |
{% elif scalar_t == "f32" or scalar_t == "i32" or scalar_t == "u32" %} |
| 167 |
{% set cuda_align = 16 %} |
| 168 |
{% elif scalar_t == "f64" or scalar_t == "i64" or scalar_t == "u64" %} |
| 169 |
{% set cuda_align = 16 %} |
| 170 |
{% elif scalar_t == "usize" %} |
| 171 |
{# assuming 64 bit here #} |
| 172 |
{% set cuda_align = 16 %} |
| 173 |
{% endif %} |
| 174 |
{% endif %} |
| 175 |
|
| 176 |
{% set components = ["x", "y", "z", "w"] | slice(end = dim) %} |
| 177 |
{% if is_float %} |
| 178 |
{% set one = "1.0" %} |
| 179 |
{% set neg_one = "-1.0" %} |
| 180 |
{% set zero = "0.0" %} |
| 181 |
{% else %} |
| 182 |
{% set one = "1" %} |
| 183 |
{% set neg_one = "-1" %} |
| 184 |
{% set zero = "0" %} |
| 185 |
{% endif %} |
| 186 |
|
| 187 |
{% if bveca_from_type and bveca_from_type == "BVec4A" and is_scalar %} |
| 188 |
{% if scalar_t == "f32" %} |
| 189 |
#[cfg(feature = "scalar-math")] |
| 190 |
use crate::BVec4 as BVec4A; |
| 191 |
{% endif %} |
| 192 |
#[cfg(not(feature = "scalar-math"))] |
| 193 |
use crate::BVec4A; |
| 194 |
use crate::{ |
| 195 |
{% if bveca_from_type and bveca_from_type != mask_t %} |
| 196 |
{{ mask_t }}, |
| 197 |
{% endif %} |
| 198 |
{% else %} |
| 199 |
use crate::{ |
| 200 |
{{ mask_t }}, |
| 201 |
{% if bveca_from_type and bveca_from_type != mask_t %} |
| 202 |
{{ bveca_from_type }}, |
| 203 |
{% endif %} |
| 204 |
{% endif %} |
| 205 |
{% if self_t != vec2_t %} |
| 206 |
{{ vec2_t }}, |
| 207 |
{% endif %} |
| 208 |
{% if self_t != vec3_t %} |
| 209 |
{{ vec3_t }}, |
| 210 |
{% endif %} |
| 211 |
{% if self_t == "Vec3" or self_t == "Vec4" %} |
| 212 |
{{ vec3a_t }}, |
| 213 |
{% endif %} |
| 214 |
{% if dim > 2 and self_t != vec4_t %} |
| 215 |
{{ vec4_t }}, |
| 216 |
{% endif %} |
| 217 |
{% if is_sse2 %} |
| 218 |
sse2::*, |
| 219 |
{% elif is_wasm32 %} |
| 220 |
wasm32::*, |
| 221 |
{% elif is_neon %} |
| 222 |
neon::*, |
| 223 |
{% elif is_coresimd %} |
| 224 |
coresimd::*, |
| 225 |
{% endif %} |
| 226 |
{% if is_float %} |
| 227 |
{{ scalar_t }}::math, |
| 228 |
{% if dim == 3 %} |
| 229 |
FloatExt, |
| 230 |
{{ quat_t }}, |
| 231 |
{% endif %} |
| 232 |
{% endif %} |
| 233 |
{% if from_types %} |
| 234 |
{% for ty in from_types %} |
| 235 |
{{ ty }}, |
| 236 |
{% endfor %} |
| 237 |
{% endif %} |
| 238 |
{% if try_from_types %} |
| 239 |
{% for ty in try_from_types %} |
| 240 |
{{ ty }}, |
| 241 |
{% endfor %} |
| 242 |
{% endif %} |
| 243 |
{% if bvec_from_type != mask_t %} |
| 244 |
{{ bvec_from_type }}, |
| 245 |
{% endif %} |
| 246 |
}; |
| 247 |
|
| 248 |
use core::fmt; |
| 249 |
use core::iter::{Product, Sum}; |
| 250 |
use core::{f32, ops::*}; |
| 251 |
|
| 252 |
{% if is_sse2 %} |
| 253 |
#[cfg(target_arch = "x86")] |
| 254 |
use core::arch::x86::*; |
| 255 |
#[cfg(target_arch = "x86_64")] |
| 256 |
use core::arch::x86_64::*; |
| 257 |
{% elif is_wasm32 %} |
| 258 |
use core::arch::wasm32::*; |
| 259 |
{% elif is_coresimd %} |
| 260 |
use core::simd::{cmp::SimdPartialEq, cmp::SimdPartialOrd, num::SimdFloat, *}; |
| 261 |
use std::simd::StdFloat; |
| 262 |
{% elif is_neon %} |
| 263 |
use core::arch::aarch64::*; |
| 264 |
{% endif %} |
| 265 |
|
| 266 |
{% if not is_coresimd %} |
| 267 |
#[cfg(feature = "zerocopy")] |
| 268 |
use zerocopy_derive::*; |
| 269 |
{% endif %} |
| 270 |
|
| 271 |
{% if is_sse2 or is_neon %} |
| 272 |
#[repr(C)] |
| 273 |
union UnionCast { |
| 274 |
a: [f32; 4], |
| 275 |
v: {{ self_t }} |
| 276 |
} |
| 277 |
{% endif %} |
| 278 |
|
| 279 |
/// Creates a {{ dim }}-dimensional vector. |
| 280 |
#[inline(always)] |
| 281 |
#[must_use] |
| 282 |
pub const fn {{ self_t | lower }}( |
| 283 |
{% for c in components %} |
| 284 |
{{ c }}: {{ scalar_t }}, |
| 285 |
{% endfor %} |
| 286 |
) -> {{ self_t }} { |
| 287 |
{{ self_t }}::new({{ components | join(sep=",") }}) |
| 288 |
} |
| 289 |
|
| 290 |
/// A {{ dim }}-dimensional vector. |
| 291 |
{%- if self_t == "Vec3A" %} |
| 292 |
/// |
| 293 |
/// SIMD vector types are used for storage on supported platforms for better |
| 294 |
/// performance than the [`Vec3`] type. |
| 295 |
/// |
| 296 |
/// It is possible to convert between [`Vec3`] and [`Vec3A`] types using [`From`] |
| 297 |
/// or [`Into`] trait implementations. |
| 298 |
/// |
| 299 |
/// This type is 16 byte aligned. |
| 300 |
{%- elif self_t == "Vec4" and is_simd %} |
| 301 |
/// |
| 302 |
/// SIMD vector types are used for storage on supported platforms. |
| 303 |
/// |
| 304 |
/// This type is 16 byte aligned. |
| 305 |
{%- endif %} |
| 306 |
#[derive( |
| 307 |
Clone, |
| 308 |
Copy, |
| 309 |
{% if is_scalar %} |
| 310 |
PartialEq, |
| 311 |
{% if not is_float %} |
| 312 |
Eq, |
| 313 |
Hash, |
| 314 |
{% endif %} |
| 315 |
{% endif %} |
| 316 |
)] |
| 317 |
{%- if self_t == "Vec3A" and is_scalar %} |
| 318 |
#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))] |
| 319 |
{%- if not is_coresimd %} |
| 320 |
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, KnownLayout))] |
| 321 |
{%- endif %} |
| 322 |
{%- else %} |
| 323 |
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))] |
| 324 |
{%- if not is_coresimd %} |
| 325 |
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes, KnownLayout))] |
| 326 |
{%- endif %} |
| 327 |
{%- endif %} |
| 328 |
{%- if self_t == "Vec3A" and is_scalar %} |
| 329 |
#[repr(align(16))] |
| 330 |
{%- elif self_t == "Vec4" and is_scalar %} |
| 331 |
#[cfg_attr(any(not(feature = "scalar-math"), feature = "cuda"), repr(align(16)))] |
| 332 |
{%- elif dim != 3 and is_scalar %} |
| 333 |
#[cfg_attr(feature = "cuda", repr(align({{ cuda_align }})))] |
| 334 |
{%- endif %} |
| 335 |
{%- if is_scalar %} |
| 336 |
#[repr(C)] |
| 337 |
#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)] |
| 338 |
pub struct {{ self_t }} |
| 339 |
{ |
| 340 |
{% for c in components %} |
| 341 |
pub {{ c }}: {{ scalar_t }}, |
| 342 |
{%- endfor %} |
| 343 |
} |
| 344 |
{% else %} |
| 345 |
#[repr(transparent)] |
| 346 |
pub struct {{ self_t }}(pub(crate) {{ simd_t }}); |
| 347 |
{% endif %} |
| 348 |
|
| 349 |
impl {{ self_t }} { |
| 350 |
/// All zeroes. |
| 351 |
pub const ZERO: Self = Self::splat({{ zero }}); |
| 352 |
|
| 353 |
/// All ones. |
| 354 |
pub const ONE: Self = Self::splat({{ one }}); |
| 355 |
|
| 356 |
{% if is_signed %} |
| 357 |
/// All negative ones. |
| 358 |
pub const NEG_ONE: Self = Self::splat(-{{ one }}); |
| 359 |
{% endif %} |
| 360 |
|
| 361 |
/// All `{{ scalar_t }}::MIN`. |
| 362 |
pub const MIN: Self = Self::splat({{ scalar_t }}::MIN); |
| 363 |
|
| 364 |
/// All `{{ scalar_t }}::MAX`. |
| 365 |
pub const MAX: Self = Self::splat({{ scalar_t }}::MAX); |
| 366 |
|
| 367 |
{% if is_float %} |
| 368 |
/// All `{{ scalar_t }}::NAN`. |
| 369 |
pub const NAN: Self = Self::splat({{ scalar_t }}::NAN); |
| 370 |
|
| 371 |
/// All `{{ scalar_t }}::INFINITY`. |
| 372 |
pub const INFINITY: Self = Self::splat({{ scalar_t }}::INFINITY); |
| 373 |
|
| 374 |
/// All `{{ scalar_t }}::NEG_INFINITY`. |
| 375 |
pub const NEG_INFINITY: Self = Self::splat({{ scalar_t }}::NEG_INFINITY); |
| 376 |
{% endif %} |
| 377 |
|
| 378 |
{% for i in range(end = dim) %} |
| 379 |
{% set C = components[i] | upper %} |
| 380 |
/// A unit vector pointing along the positive {{ C }} axis. |
| 381 |
pub const {{ C }}: Self = Self::new( |
| 382 |
{% for j in range(end = dim) %} |
| 383 |
{% if i == j %} {{ one }} {% else %} {{ zero }} {% endif %}, |
| 384 |
{%- endfor %} |
| 385 |
); |
| 386 |
{% endfor %} |
| 387 |
|
| 388 |
{% if is_signed %} |
| 389 |
{% for i in range(end = dim) %} |
| 390 |
{% set C = components[i] | upper %} |
| 391 |
/// A unit vector pointing along the negative {{ C }} axis. |
| 392 |
pub const NEG_{{ C }}: Self = Self::new( |
| 393 |
{% for j in range(end = dim) %} |
| 394 |
{% if i == j %} {{ neg_one }} {% else %} {{ zero }} {% endif %}, |
| 395 |
{%- endfor %} |
| 396 |
); |
| 397 |
{% endfor %} |
| 398 |
{% endif %} |
| 399 |
|
| 400 |
/// The unit axes. |
| 401 |
pub const AXES: [Self; {{ dim }}] = [ |
| 402 |
{% for c in components %} |
| 403 |
Self::{{ c | upper }}, |
| 404 |
{% endfor %} |
| 405 |
]; |
| 406 |
|
| 407 |
{% if is_float %} |
| 408 |
/// {{ self_t }} uses Rust Portable SIMD |
| 409 |
pub const USES_CORE_SIMD: bool = {{ is_coresimd }}; |
| 410 |
/// {{ self_t }} uses Arm NEON |
| 411 |
pub const USES_NEON: bool = {{ is_neon }}; |
| 412 |
/// {{ self_t }} uses scalar math |
| 413 |
pub const USES_SCALAR_MATH: bool = {{ is_scalar }}; |
| 414 |
/// {{ self_t }} uses Intel SSE2 |
| 415 |
pub const USES_SSE2: bool = {{ is_sse2 }}; |
| 416 |
/// {{ self_t }} uses WebAssembly 128-bit SIMD |
| 417 |
pub const USES_WASM32_SIMD: bool = {{ is_wasm32 }}; |
| 418 |
{% endif %} |
| 419 |
|
| 420 |
/// Creates a new vector. |
| 421 |
#[inline(always)] |
| 422 |
#[must_use] |
| 423 |
pub const fn new( |
| 424 |
{% for c in components %} |
| 425 |
{{ c }}: {{ scalar_t }}, |
| 426 |
{% endfor %} |
| 427 |
) -> Self { |
| 428 |
{% if is_scalar %} |
| 429 |
Self { |
| 430 |
{% for c in components %} |
| 431 |
{{ c }}, |
| 432 |
{%- endfor %} |
| 433 |
} |
| 434 |
{% elif is_sse2 %} |
| 435 |
unsafe { |
| 436 |
UnionCast { a: [ |
| 437 |
{% if dim == 3 %} |
| 438 |
x, y, z, z |
| 439 |
{% elif dim == 4 %} |
| 440 |
x, y, z, w |
| 441 |
{% endif %} |
| 442 |
] }.v |
| 443 |
} |
| 444 |
{% elif is_wasm32 %} |
| 445 |
Self(f32x4( |
| 446 |
{% if dim == 3 %} |
| 447 |
x, y, z, z |
| 448 |
{% elif dim == 4 %} |
| 449 |
x, y, z, w |
| 450 |
{% endif %} |
| 451 |
)) |
| 452 |
{% elif is_coresimd %} |
| 453 |
Self(f32x4::from_array([ |
| 454 |
x, y, z, |
| 455 |
{% if dim == 3 %} |
| 456 |
z |
| 457 |
{% elif dim == 4 %} |
| 458 |
w |
| 459 |
{% endif %} |
| 460 |
])) |
| 461 |
{% elif is_neon %} |
| 462 |
{% if dim == 3 %} |
| 463 |
unsafe { UnionCast { a: [x, y, z, z] }.v } |
| 464 |
{% elif dim == 4 %} |
| 465 |
unsafe { UnionCast { a: [x, y, z, w] }.v } |
| 466 |
{% endif %} |
| 467 |
{% endif %} |
| 468 |
} |
| 469 |
|
| 470 |
/// Creates a vector with all elements set to `v`. |
| 471 |
#[inline] |
| 472 |
#[must_use] |
| 473 |
pub const fn splat(v: {{ scalar_t }}) -> Self { |
| 474 |
{% if is_scalar %} |
| 475 |
Self { |
| 476 |
{% for c in components %} |
| 477 |
{{ c }}: v, |
| 478 |
{% endfor %} |
| 479 |
} |
| 480 |
{% elif is_wasm32 %} |
| 481 |
Self(f32x4(v, v, v, v)) |
| 482 |
{% elif is_coresimd %} |
| 483 |
Self(Simd::from_array([v; 4])) |
| 484 |
{% else %} |
| 485 |
unsafe { UnionCast { a: [v; 4] }.v } |
| 486 |
{% endif %} |
| 487 |
} |
| 488 |
|
| 489 |
/// Returns a vector containing each element of `self` modified by a mapping function `f`. |
| 490 |
#[inline] |
| 491 |
#[must_use] |
| 492 |
pub fn map<F>(self, f: F) -> Self |
| 493 |
where |
| 494 |
F: Fn({{ scalar_t }}) -> {{ scalar_t }}, |
| 495 |
{ |
| 496 |
Self::new( |
| 497 |
{% for c in components %} |
| 498 |
f(self.{{ c }}), |
| 499 |
{%- endfor %} |
| 500 |
) |
| 501 |
} |
| 502 |
|
| 503 |
/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use |
| 504 |
/// for each element of `self`. |
| 505 |
/// |
| 506 |
/// A true element in the mask uses the corresponding element from `if_true`, and false |
| 507 |
/// uses the element from `if_false`. |
| 508 |
#[inline] |
| 509 |
#[must_use] |
| 510 |
pub fn select(mask: {{ mask_t }}, if_true: Self, if_false: Self) -> Self { |
| 511 |
{% if is_scalar %} |
| 512 |
Self { |
| 513 |
{% for c in components %} |
| 514 |
{{ c }}: if mask.test({{ loop.index0 }}) { if_true.{{ c }} } else { if_false.{{ c }} }, |
| 515 |
{%- endfor %} |
| 516 |
} |
| 517 |
{% elif is_sse2 %} |
| 518 |
Self(unsafe { _mm_or_ps(_mm_andnot_ps(mask.0, if_false.0), _mm_and_ps(if_true.0, mask.0)) }) |
| 519 |
{% elif is_wasm32 %} |
| 520 |
Self(v128_bitselect(if_true.0, if_false.0, mask.0)) |
| 521 |
{% elif is_coresimd %} |
| 522 |
Self(mask.0.select(if_true.0, if_false.0)) |
| 523 |
{% elif is_neon %} |
| 524 |
Self(unsafe { vbslq_f32(mask.0, if_true.0, if_false.0) }) |
| 525 |
{% endif %} |
| 526 |
} |
| 527 |
|
| 528 |
/// Creates a new vector from an array. |
| 529 |
#[inline] |
| 530 |
#[must_use] |
| 531 |
pub const fn from_array(a: [{{ scalar_t }}; {{ dim }}]) -> Self { |
| 532 |
Self::new( |
| 533 |
{% for c in components %} |
| 534 |
a[{{ loop.index0 }}], |
| 535 |
{%- endfor %} |
| 536 |
) |
| 537 |
} |
| 538 |
|
| 539 |
/// Converts `self` to `[{{ components | join(sep=", ") }}]` |
| 540 |
#[inline] |
| 541 |
#[must_use] |
| 542 |
pub const fn to_array(&self) -> [{{ scalar_t }}; {{ dim }}] { |
| 543 |
{% if is_scalar %} |
| 544 |
[ |
| 545 |
{% for c in components %} |
| 546 |
self.{{ c }}, |
| 547 |
{% endfor %} |
| 548 |
] |
| 549 |
{% else %} |
| 550 |
unsafe { *(self as *const Self as *const [{{ scalar_t }}; {{ dim }}]) } |
| 551 |
{% endif %} |
| 552 |
} |
| 553 |
|
| 554 |
/// Creates a vector from the first {{ dim }} values in `slice`. |
| 555 |
/// |
| 556 |
/// # Panics |
| 557 |
/// |
| 558 |
/// Panics if `slice` is less than {{ dim }} elements long. |
| 559 |
#[inline] |
| 560 |
#[must_use] |
| 561 |
pub const fn from_slice(slice: &[{{ scalar_t }}]) -> Self { |
| 562 |
assert!(slice.len() >= {{ dim }}); |
| 563 |
Self::new( |
| 564 |
{% for c in components %} |
| 565 |
slice[{{ loop.index0 }}], |
| 566 |
{%- endfor %} |
| 567 |
) |
| 568 |
} |
| 569 |
|
| 570 |
/// Writes the elements of `self` to the first {{ dim }} elements in `slice`. |
| 571 |
/// |
| 572 |
/// # Panics |
| 573 |
/// |
| 574 |
/// Panics if `slice` is less than {{ dim }} elements long. |
| 575 |
#[inline] |
| 576 |
pub fn write_to_slice(self, slice: &mut [{{ scalar_t }}]) { |
| 577 |
{% if self_t == "Vec4" and is_sse2 %} |
| 578 |
assert!(slice.len() >= 4); |
| 579 |
unsafe { _mm_storeu_ps(slice.as_mut_ptr(), self.0); } |
| 580 |
{% elif self_t == "Vec4" and is_neon %} |
| 581 |
assert!(slice.len() >= 4); |
| 582 |
unsafe { vst1q_f32(slice.as_mut_ptr(), self.0); } |
| 583 |
{% else %} |
| 584 |
slice[..{{ dim }}].copy_from_slice(&self.to_array()); |
| 585 |
{% endif %} |
| 586 |
} |
| 587 |
|
| 588 |
{% if dim == 2 %} |
| 589 |
/// Creates a 3D vector from `self` and the given `z` value. |
| 590 |
#[inline] |
| 591 |
#[must_use] |
| 592 |
pub const fn extend(self, z: {{ scalar_t }}) -> {{ vec3_t }} { |
| 593 |
{{ vec3_t }}::new(self.x, self.y, z) |
| 594 |
} |
| 595 |
{% elif dim == 3 %} |
| 596 |
{% if self_t == "Vec3A" %} |
| 597 |
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`. |
| 598 |
/// |
| 599 |
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop. |
| 600 |
#[inline] |
| 601 |
#[must_use] |
| 602 |
pub fn from_vec4(v: Vec4) -> Self { |
| 603 |
{% if is_scalar %} |
| 604 |
Self { x: v.x, y: v.y, z: v.z } |
| 605 |
{% else %} |
| 606 |
Self(v.0) |
| 607 |
{% endif %} |
| 608 |
} |
| 609 |
{% else %} |
| 610 |
/// Internal method for creating a 3D vector from a 4D vector, discarding `w`. |
| 611 |
#[allow(dead_code)] |
| 612 |
#[inline] |
| 613 |
#[must_use] |
| 614 |
pub(crate) fn from_vec4(v: {{ vec4_t }}) -> Self { |
| 615 |
{% if is_scalar %} |
| 616 |
Self { x: v.x, y: v.y, z: v.z } |
| 617 |
{% else %} |
| 618 |
Self(v.0) |
| 619 |
{% endif %} |
| 620 |
} |
| 621 |
{% endif %} |
| 622 |
|
| 623 |
/// Creates a 4D vector from `self` and the given `w` value. |
| 624 |
#[inline] |
| 625 |
#[must_use] |
| 626 |
pub fn extend(self, w: {{ scalar_t }}) -> {{ vec4_t }} { |
| 627 |
{{ vec4_t }}::new(self.x, self.y, self.z, w) |
| 628 |
} |
| 629 |
|
| 630 |
/// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. |
| 631 |
/// |
| 632 |
/// Truncation may also be performed by using [`self.xy()`][crate::swizzles::Vec3Swizzles::xy()]. |
| 633 |
#[inline] |
| 634 |
#[must_use] |
| 635 |
pub fn truncate(self) -> {{ vec2_t }} { |
| 636 |
use crate::swizzles::Vec3Swizzles; |
| 637 |
self.xy() |
| 638 |
} |
| 639 |
|
| 640 |
{% if is_float %} |
| 641 |
/// Projects a homogeneous coordinate to 3D space by performing perspective divide. |
| 642 |
/// |
| 643 |
/// # Panics |
| 644 |
/// |
| 645 |
/// Will panic if `v.w` is `0` when `glam_assert` is enabled. |
| 646 |
#[inline] |
| 647 |
#[must_use] |
| 648 |
pub fn from_homogeneous(v: {{ vec4_t }}) -> Self { |
| 649 |
glam_assert!(v.w != 0.0); |
| 650 |
Self::from_vec4(v) / v.w |
| 651 |
} |
| 652 |
|
| 653 |
/// Creates a homogeneous coordinate from `self`, equivalent to `self.extend(1.0)`. |
| 654 |
#[inline] |
| 655 |
#[must_use] |
| 656 |
pub fn to_homogeneous(self) -> {{ vec4_t }} { |
| 657 |
self.extend(1.0) |
| 658 |
} |
| 659 |
{% endif %} |
| 660 |
|
| 661 |
{% elif dim == 4 %} |
| 662 |
/// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. |
| 663 |
/// |
| 664 |
/// Truncation to [`{{ vec3_t }}`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()]. |
| 665 |
{%- if scalar_t == "f32" %} |
| 666 |
/// |
| 667 |
/// To truncate to [`Vec3A`] use [`Vec3A::from_vec4()`]. |
| 668 |
{%- endif %} |
| 669 |
#[inline] |
| 670 |
#[must_use] |
| 671 |
pub fn truncate(self) -> {{ vec3_t }} { |
| 672 |
use crate::swizzles::Vec4Swizzles; |
| 673 |
self.xyz() |
| 674 |
} |
| 675 |
|
| 676 |
{% if is_float %} |
| 677 |
/// Projects a homogeneous coordinate to 3D space by performing perspective divide. |
| 678 |
{%- if scalar_t == "f32" %} |
| 679 |
/// |
| 680 |
/// To project to [`Vec3A`] use [`Vec3A::from_homogeneous()`]. |
| 681 |
{%- endif %} |
| 682 |
/// |
| 683 |
/// # Panics |
| 684 |
/// |
| 685 |
/// Will panic if `self.w` is `0` when `glam_assert` is enabled. |
| 686 |
#[inline] |
| 687 |
#[must_use] |
| 688 |
pub fn project(self) -> {{ vec3_t }} { |
| 689 |
{{ vec3_t }}::from_homogeneous(self) |
| 690 |
} |
| 691 |
{% endif %} |
| 692 |
{% endif %} |
| 693 |
|
| 694 |
{% if self_t == "Vec3A" %} |
| 695 |
// Converts `self` to a `Vec3`. |
| 696 |
#[inline] |
| 697 |
#[must_use] |
| 698 |
pub fn to_vec3(self) -> Vec3 { |
| 699 |
Vec3::from(self) |
| 700 |
} |
| 701 |
{% elif self_t == "Vec3" %} |
| 702 |
// Converts `self` to a `Vec3A`. |
| 703 |
#[inline] |
| 704 |
#[must_use] |
| 705 |
pub fn to_vec3a(self) -> Vec3A { |
| 706 |
Vec3A::from(self) |
| 707 |
} |
| 708 |
{% endif %} |
| 709 |
|
| 710 |
{% for c in components %} |
| 711 |
/// Creates a {{ dim }}D vector from `self` with the given value of `{{ c }}`. |
| 712 |
#[inline] |
| 713 |
#[must_use] |
| 714 |
pub fn with_{{ c }}(mut self, {{ c }}: {{ scalar_t }}) -> Self { |
| 715 |
self.{{ c }} = {{ c }}; |
| 716 |
self |
| 717 |
} |
| 718 |
{% endfor %} |
| 719 |
|
| 720 |
/// Computes the dot product of `self` and `rhs`. |
| 721 |
#[inline] |
| 722 |
#[must_use] |
| 723 |
pub fn dot(self, rhs: Self) -> {{ scalar_t }} { |
| 724 |
{% if is_scalar %} |
| 725 |
{% for c in components %} |
| 726 |
(self.{{ c }} * rhs.{{ c }}) {% if not loop.last %} + {% endif %} |
| 727 |
{%- endfor %} |
| 728 |
{% elif is_sse2 %} |
| 729 |
unsafe { dot{{ dim }}(self.0, rhs.0) } |
| 730 |
{% elif is_neon %} |
| 731 |
{% if dim < 4 %} |
| 732 |
// this was faster than intrinsics in testing |
| 733 |
{%- for c in components %} |
| 734 |
(self.{{ c }} * rhs.{{ c }}) {% if not loop.last %} + {% endif %} |
| 735 |
{%- endfor %} |
| 736 |
{% else %} |
| 737 |
unsafe { dot{{ dim }}(self.0, rhs.0) } |
| 738 |
{% endif %} |
| 739 |
{% else %} |
| 740 |
dot{{ dim }}(self.0, rhs.0) |
| 741 |
{% endif %} |
| 742 |
} |
| 743 |
|
| 744 |
/// Returns a vector where every component is the dot product of `self` and `rhs`. |
| 745 |
#[inline] |
| 746 |
#[must_use] |
| 747 |
pub fn dot_into_vec(self, rhs: Self) -> Self { |
| 748 |
{% if is_scalar %} |
| 749 |
Self::splat(self.dot(rhs)) |
| 750 |
{% elif is_sse2 %} |
| 751 |
Self(unsafe { dot{{ dim }}_into_m128(self.0, rhs.0) }) |
| 752 |
{% elif is_wasm32 %} |
| 753 |
Self(dot{{ dim }}_into_v128(self.0, rhs.0)) |
| 754 |
{% elif is_coresimd %} |
| 755 |
Self(dot{{ dim }}_into_f32x4(self.0, rhs.0)) |
| 756 |
{% elif is_neon %} |
| 757 |
Self(unsafe { dot{{ dim }}_into_f32x4(self.0, rhs.0) }) |
| 758 |
{% else %} |
| 759 |
unimplemented!() |
| 760 |
{% endif %} |
| 761 |
} |
| 762 |
|
| 763 |
{% if dim == 3 %} |
| 764 |
/// Computes the cross product of `self` and `rhs`. |
| 765 |
#[inline] |
| 766 |
#[must_use] |
| 767 |
pub fn cross(self, rhs: Self) -> Self { |
| 768 |
{% if is_scalar %} |
| 769 |
Self { |
| 770 |
x: self.y * rhs.z - rhs.y * self.z, |
| 771 |
y: self.z * rhs.x - rhs.z * self.x, |
| 772 |
z: self.x * rhs.y - rhs.x * self.y, |
| 773 |
} |
| 774 |
{% elif is_sse2 %} |
| 775 |
unsafe { |
| 776 |
// x <- a.y*b.z - a.z*b.y |
| 777 |
// y <- a.z*b.x - a.x*b.z |
| 778 |
// z <- a.x*b.y - a.y*b.x |
| 779 |
// We can save a shuffle by grouping it in this wacky order: |
| 780 |
// (self.zxy() * rhs - self * rhs.zxy()).zxy() |
| 781 |
let lhszxy = _mm_shuffle_ps(self.0, self.0, 0b01_01_00_10); |
| 782 |
let rhszxy = _mm_shuffle_ps(rhs.0, rhs.0, 0b01_01_00_10); |
| 783 |
let lhszxy_rhs = _mm_mul_ps(lhszxy, rhs.0); |
| 784 |
let rhszxy_lhs = _mm_mul_ps(rhszxy, self.0); |
| 785 |
let sub = _mm_sub_ps(lhszxy_rhs, rhszxy_lhs); |
| 786 |
Self(_mm_shuffle_ps(sub, sub, 0b01_01_00_10)) |
| 787 |
} |
| 788 |
{% elif is_wasm32 %} |
| 789 |
let lhszxy = i32x4_shuffle::<2, 0, 1, 1>(self.0, self.0); |
| 790 |
let rhszxy = i32x4_shuffle::<2, 0, 1, 1>(rhs.0, rhs.0); |
| 791 |
let lhszxy_rhs = f32x4_mul(lhszxy, rhs.0); |
| 792 |
let rhszxy_lhs = f32x4_mul(rhszxy, self.0); |
| 793 |
let sub = f32x4_sub(lhszxy_rhs, rhszxy_lhs); |
| 794 |
Self(i32x4_shuffle::<2, 0, 1, 1>(sub, sub)) |
| 795 |
{% elif is_coresimd %} |
| 796 |
let lhszxy = simd_swizzle!(self.0, [2, 0, 1, 1]); |
| 797 |
let rhszxy = simd_swizzle!(rhs.0, [2, 0, 1, 1]); |
| 798 |
let lhszxy_rhs = lhszxy * rhs.0; |
| 799 |
let rhszxy_lhs = rhszxy * self.0; |
| 800 |
let sub = lhszxy_rhs - rhszxy_lhs; |
| 801 |
Self(simd_swizzle!(sub, [2, 0, 1, 1])) |
| 802 |
{% elif is_neon %} |
| 803 |
unsafe { |
| 804 |
// Implementation taken from Realtime Math |
| 805 |
let lhs = self.0; |
| 806 |
let rhs = rhs.0; |
| 807 |
// cross(a, b) = (a.yzx * b.zxy) - (a.zxy * b.yzx) |
| 808 |
let lhs_yzwx = vextq_f32(lhs, lhs, 1); |
| 809 |
let rhs_wxyz = vextq_f32(rhs, rhs, 3); |
| 810 |
|
| 811 |
let lhs_yzx = vsetq_lane_f32(vgetq_lane_f32(lhs, 0), lhs_yzwx, 2); |
| 812 |
let rhs_zxy = vsetq_lane_f32(vgetq_lane_f32(rhs, 2), rhs_wxyz, 0); |
| 813 |
|
| 814 |
// part_a = (a.yzx * b.zxy) |
| 815 |
let part_a = vmulq_f32(lhs_yzx, rhs_zxy); |
| 816 |
|
| 817 |
let lhs_wxyz = vextq_f32(lhs, lhs, 3); |
| 818 |
let rhs_yzwx = vextq_f32(rhs, rhs, 1); |
| 819 |
let lhs_zxy = vsetq_lane_f32(vgetq_lane_f32(lhs, 2), lhs_wxyz, 0); |
| 820 |
let rhs_yzx = vsetq_lane_f32(vgetq_lane_f32(rhs, 0), rhs_yzwx, 2); |
| 821 |
|
| 822 |
// result = part_a - (a.zxy * b.yzx) |
| 823 |
let result = vmlsq_f32(part_a, lhs_zxy, rhs_yzx); |
| 824 |
Self(result) |
| 825 |
} |
| 826 |
{% else %} |
| 827 |
unimplemented!() |
| 828 |
{% endif %} |
| 829 |
} |
| 830 |
{% endif %} |
| 831 |
|
| 832 |
/// Returns a vector containing the minimum values for each element of `self` and `rhs`. |
| 833 |
/// |
| 834 |
/// In other words this computes `[min(x, rhs.x), min(self.y, rhs.y), ..]`. |
| 835 |
{%- if is_float %} |
| 836 |
/// |
| 837 |
/// NaN propogation does not follow IEEE 754-2008 semantics for minNum and may differ on |
| 838 |
/// different SIMD architectures. |
| 839 |
{%- endif %} |
| 840 |
#[inline] |
| 841 |
#[must_use] |
| 842 |
pub fn min(self, rhs: Self) -> Self { |
| 843 |
{% if is_scalar %} |
| 844 |
Self { |
| 845 |
{% for c in components %} |
| 846 |
{{ c }}: if self.{{ c }} < rhs.{{ c }} { self.{{ c }} } else { rhs.{{ c }} }, |
| 847 |
{%- endfor %} |
| 848 |
} |
| 849 |
{% elif is_sse2 %} |
| 850 |
Self(unsafe { _mm_min_ps(self.0, rhs.0) }) |
| 851 |
{% elif is_wasm32 %} |
| 852 |
Self(f32x4_pmin(self.0, rhs.0)) |
| 853 |
{% elif is_coresimd %} |
| 854 |
Self(self.0.simd_lt(rhs.0).select(self.0, rhs.0)) |
| 855 |
{% elif is_neon %} |
| 856 |
Self(unsafe { vminq_f32(self.0, rhs.0) }) |
| 857 |
{% else %} |
| 858 |
unimplemented!() |
| 859 |
{% endif %} |
| 860 |
} |
| 861 |
|
| 862 |
/// Returns a vector containing the maximum values for each element of `self` and `rhs`. |
| 863 |
/// |
| 864 |
/// In other words this computes `[max(self.x, rhs.x), max(self.y, rhs.y), ..]`. |
| 865 |
{%- if is_float %} |
| 866 |
/// |
| 867 |
/// NaN propogation does not follow IEEE 754-2008 semantics for maxNum and may differ on |
| 868 |
/// different SIMD architectures. |
| 869 |
{%- endif %} |
| 870 |
#[inline] |
| 871 |
#[must_use] |
| 872 |
pub fn max(self, rhs: Self) -> Self { |
| 873 |
{% if is_scalar %} |
| 874 |
Self { |
| 875 |
{% for c in components %} |
| 876 |
{{ c }}: if self.{{ c }} > rhs.{{ c }} { self.{{ c }} } else { rhs.{{ c }} }, |
| 877 |
{%- endfor %} |
| 878 |
} |
| 879 |
{% elif is_sse2 %} |
| 880 |
Self(unsafe { _mm_max_ps(self.0, rhs.0) }) |
| 881 |
{% elif is_wasm32 %} |
| 882 |
Self(f32x4_pmax(self.0, rhs.0)) |
| 883 |
{% elif is_coresimd %} |
| 884 |
Self(self.0.simd_gt(rhs.0).select(self.0, rhs.0)) |
| 885 |
{% elif is_neon %} |
| 886 |
Self(unsafe { vmaxq_f32(self.0, rhs.0) }) |
| 887 |
{% else %} |
| 888 |
unimplemented!() |
| 889 |
{% endif %} |
| 890 |
} |
| 891 |
|
| 892 |
/// Component-wise clamping of values, similar to [`{{ scalar_t }}::clamp`]. |
| 893 |
/// |
| 894 |
/// Each element in `min` must be less-or-equal to the corresponding element in `max`. |
| 895 |
{%- if is_float %} |
| 896 |
/// |
| 897 |
/// NaN propogation does not follow IEEE 754-2008 semantics and may differ on |
| 898 |
/// different SIMD architectures. |
| 899 |
{%- endif %} |
| 900 |
/// |
| 901 |
/// # Panics |
| 902 |
/// |
| 903 |
/// Will panic if `min` is greater than `max` when `glam_assert` is enabled. |
| 904 |
#[inline] |
| 905 |
#[must_use] |
| 906 |
pub fn clamp(self, min: Self, max: Self) -> Self { |
| 907 |
glam_assert!(min.cmple(max).all(), "clamp: expected min <= max"); |
| 908 |
self.max(min).min(max) |
| 909 |
} |
| 910 |
|
| 911 |
/// Returns the horizontal minimum of `self`. |
| 912 |
/// |
| 913 |
/// In other words this computes `min(x, y, ..)`. |
| 914 |
{%- if is_float %} |
| 915 |
/// |
| 916 |
/// NaN propogation does not follow IEEE 754-2008 semantics and may differ on |
| 917 |
/// different SIMD architectures. |
| 918 |
{%- endif %} |
| 919 |
#[inline] |
| 920 |
#[must_use] |
| 921 |
pub fn min_element(self) -> {{ scalar_t }} { |
| 922 |
{% if is_scalar %} |
| 923 |
let min = |a, b| if a < b { a } else { b }; |
| 924 |
{%- if dim == 2 %} |
| 925 |
min(self.x, self.y) |
| 926 |
{%- elif dim == 3 %} |
| 927 |
min(self.x, min(self.y, self.z)) |
| 928 |
{%- elif dim == 4 %} |
| 929 |
min(self.x, min(self.y, min(self.z, self.w))) |
| 930 |
{% endif %} |
| 931 |
{% elif is_sse2 %} |
| 932 |
{% if dim == 3 %} |
| 933 |
unsafe { |
| 934 |
let v = self.0; |
| 935 |
let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b01_01_10_10)); |
| 936 |
let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 937 |
_mm_cvtss_f32(v) |
| 938 |
} |
| 939 |
{% elif dim == 4 %} |
| 940 |
unsafe { |
| 941 |
let v = self.0; |
| 942 |
let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_11_10)); |
| 943 |
let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 944 |
_mm_cvtss_f32(v) |
| 945 |
} |
| 946 |
{% endif %} |
| 947 |
{% elif is_wasm32 %} |
| 948 |
{% if dim == 3 %} |
| 949 |
let v = self.0; |
| 950 |
let v = f32x4_pmin(v, i32x4_shuffle::<2, 2, 1, 1>(v, v)); |
| 951 |
let v = f32x4_pmin(v, i32x4_shuffle::<1, 0, 0, 0>(v, v)); |
| 952 |
f32x4_extract_lane::<0>(v) |
| 953 |
{% elif dim == 4 %} |
| 954 |
let v = self.0; |
| 955 |
let v = f32x4_pmin(v, i32x4_shuffle::<2, 3, 0, 0>(v, v)); |
| 956 |
let v = f32x4_pmin(v, i32x4_shuffle::<1, 0, 0, 0>(v, v)); |
| 957 |
f32x4_extract_lane::<0>(v) |
| 958 |
{% endif %} |
| 959 |
{% elif is_coresimd %} |
| 960 |
let min = |a: {{ simd_t }}, b: {{ simd_t }}| a.simd_lt(b).select(a, b); |
| 961 |
{%- if dim == 3 %} |
| 962 |
let v = self.0; |
| 963 |
let v = min(v, simd_swizzle!(v, [2, 2, 1, 1])); |
| 964 |
let v = min(v, simd_swizzle!(v, [1, 0, 0, 0])); |
| 965 |
v[0] |
| 966 |
{%- elif dim == 4 %} |
| 967 |
let v = self.0; |
| 968 |
let v = min(v, simd_swizzle!(v, [2, 3, 0, 0])); |
| 969 |
let v = min(v, simd_swizzle!(v, [1, 0, 0, 0])); |
| 970 |
v[0] |
| 971 |
{% endif %} |
| 972 |
{% elif is_neon %} |
| 973 |
{% if dim == 3 %} |
| 974 |
self.x.min(self.y.min(self.z)) |
| 975 |
{% elif dim == 4 %} |
| 976 |
unsafe { vminnmvq_f32(self.0) } |
| 977 |
{% endif %} |
| 978 |
{% else %} |
| 979 |
unimplemented!() |
| 980 |
{% endif %} |
| 981 |
} |
| 982 |
|
| 983 |
/// Returns the horizontal maximum of `self`. |
| 984 |
/// |
| 985 |
/// In other words this computes `max(x, y, ..)`. |
| 986 |
{%- if is_float %} |
| 987 |
/// |
| 988 |
/// NaN propogation does not follow IEEE 754-2008 semantics and may differ on |
| 989 |
/// different SIMD architectures. |
| 990 |
{%- endif %} |
| 991 |
#[inline] |
| 992 |
#[must_use] |
| 993 |
pub fn max_element(self) -> {{ scalar_t }} { |
| 994 |
{% if is_scalar %} |
| 995 |
let max = |a, b| if a > b { a } else { b }; |
| 996 |
{%- if dim == 2 %} |
| 997 |
max(self.x, self.y) |
| 998 |
{%- elif dim == 3 %} |
| 999 |
max(self.x, max(self.y, self.z)) |
| 1000 |
{%- elif dim == 4 %} |
| 1001 |
max(self.x, max(self.y, max(self.z, self.w))) |
| 1002 |
{% endif %} |
| 1003 |
{% elif is_sse2 %} |
| 1004 |
{% if dim == 3 %} |
| 1005 |
unsafe { |
| 1006 |
let v = self.0; |
| 1007 |
let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_10_10)); |
| 1008 |
let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 1009 |
_mm_cvtss_f32(v) |
| 1010 |
} |
| 1011 |
{% elif dim == 4 %} |
| 1012 |
unsafe { |
| 1013 |
let v = self.0; |
| 1014 |
let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_11_10)); |
| 1015 |
let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 1016 |
_mm_cvtss_f32(v) |
| 1017 |
} |
| 1018 |
{% endif %} |
| 1019 |
{% elif is_wasm32 %} |
| 1020 |
{% if dim == 3 %} |
| 1021 |
let v = self.0; |
| 1022 |
let v = f32x4_pmax(v, i32x4_shuffle::<2, 2, 0, 0>(v, v)); |
| 1023 |
let v = f32x4_pmax(v, i32x4_shuffle::<1, 0, 0, 0>(v, v)); |
| 1024 |
f32x4_extract_lane::<0>(v) |
| 1025 |
{% elif dim == 4 %} |
| 1026 |
let v = self.0; |
| 1027 |
let v = f32x4_pmax(v, i32x4_shuffle::<2, 3, 0, 0>(v, v)); |
| 1028 |
let v = f32x4_pmax(v, i32x4_shuffle::<1, 0, 0, 0>(v, v)); |
| 1029 |
f32x4_extract_lane::<0>(v) |
| 1030 |
{% endif %} |
| 1031 |
{% elif is_coresimd %} |
| 1032 |
let max = |a: {{ simd_t }}, b: {{ simd_t }}| a.simd_gt(b).select(a, b); |
| 1033 |
{%- if dim == 3 %} |
| 1034 |
let v = self.0; |
| 1035 |
let v = max(v, simd_swizzle!(v, [2, 2, 0, 0])); |
| 1036 |
let v = max(v, simd_swizzle!(v, [1, 0, 0, 0])); |
| 1037 |
v[0] |
| 1038 |
{%- elif dim == 4 %} |
| 1039 |
let v = self.0; |
| 1040 |
let v = max(v, simd_swizzle!(v, [2, 3, 0, 0])); |
| 1041 |
let v = max(v, simd_swizzle!(v, [1, 0, 0, 0])); |
| 1042 |
v[0] |
| 1043 |
{% endif %} |
| 1044 |
{% elif is_neon %} |
| 1045 |
{% if dim == 3 %} |
| 1046 |
self.x.max(self.y.max(self.z)) |
| 1047 |
{% elif dim == 4 %} |
| 1048 |
unsafe { vmaxnmvq_f32(self.0) } |
| 1049 |
{% endif %} |
| 1050 |
{% else %} |
| 1051 |
unimplemented!() |
| 1052 |
{% endif %} |
| 1053 |
} |
| 1054 |
|
| 1055 |
/// Returns the index of the first minimum element of `self`. |
| 1056 |
#[doc(alias = "argmin")] |
| 1057 |
#[inline] |
| 1058 |
#[must_use] |
| 1059 |
pub fn min_position(self) -> usize { |
| 1060 |
{% if dim == 2 %} |
| 1061 |
if self.x <= self.y { 0 } else { 1 } |
| 1062 |
{% elif dim == 3 %} |
| 1063 |
let mut min = self.x; |
| 1064 |
let mut index = 0; |
| 1065 |
if self.y < min { |
| 1066 |
min = self.y; |
| 1067 |
index = 1; |
| 1068 |
} |
| 1069 |
if self.z < min { |
| 1070 |
index = 2; |
| 1071 |
} |
| 1072 |
index |
| 1073 |
{% elif dim == 4 %} |
| 1074 |
let mut min = self.x; |
| 1075 |
let mut index = 0; |
| 1076 |
if self.y < min { |
| 1077 |
min = self.y; |
| 1078 |
index = 1; |
| 1079 |
} |
| 1080 |
if self.z < min { |
| 1081 |
min = self.z; |
| 1082 |
index = 2; |
| 1083 |
} |
| 1084 |
if self.w < min { |
| 1085 |
index = 3; |
| 1086 |
} |
| 1087 |
index |
| 1088 |
{% endif %} |
| 1089 |
} |
| 1090 |
|
| 1091 |
/// Returns the index of the first maximum element of `self`. |
| 1092 |
#[doc(alias = "argmax")] |
| 1093 |
#[inline] |
| 1094 |
#[must_use] |
| 1095 |
pub fn max_position(self) -> usize { |
| 1096 |
{% if dim == 2 %} |
| 1097 |
if self.x >= self.y { 0 } else { 1 } |
| 1098 |
{% elif dim == 3 %} |
| 1099 |
let mut max = self.x; |
| 1100 |
let mut index = 0; |
| 1101 |
if self.y > max { |
| 1102 |
max = self.y; |
| 1103 |
index = 1; |
| 1104 |
} |
| 1105 |
if self.z > max { |
| 1106 |
index = 2; |
| 1107 |
} |
| 1108 |
index |
| 1109 |
{% elif dim == 4 %} |
| 1110 |
let mut max = self.x; |
| 1111 |
let mut index = 0; |
| 1112 |
if self.y > max { |
| 1113 |
max = self.y; |
| 1114 |
index = 1; |
| 1115 |
} |
| 1116 |
if self.z > max { |
| 1117 |
max = self.z; |
| 1118 |
index = 2; |
| 1119 |
} |
| 1120 |
if self.w > max { |
| 1121 |
index = 3; |
| 1122 |
} |
| 1123 |
index |
| 1124 |
{% endif %} |
| 1125 |
} |
| 1126 |
|
| 1127 |
/// Returns the sum of all elements of `self`. |
| 1128 |
/// |
| 1129 |
/// In other words, this computes `self.x + self.y + ..`. |
| 1130 |
#[inline] |
| 1131 |
#[must_use] |
| 1132 |
pub fn element_sum(self) -> {{ scalar_t }} { |
| 1133 |
{% if is_scalar %} |
| 1134 |
{% for c in components %} |
| 1135 |
self.{{ c }} {% if not loop.last %} + {% endif %} |
| 1136 |
{%- endfor %} |
| 1137 |
{% elif is_sse2 %} |
| 1138 |
{% if dim == 3 %} |
| 1139 |
unsafe { |
| 1140 |
let v = self.0; |
| 1141 |
let v = _mm_add_ps(v, _mm_shuffle_ps(v, Self::ZERO.0, 0b00_11_00_01)); |
| 1142 |
let v = _mm_add_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10)); |
| 1143 |
_mm_cvtss_f32(v) |
| 1144 |
} |
| 1145 |
{% elif dim == 4 %} |
| 1146 |
unsafe { |
| 1147 |
let v = self.0; |
| 1148 |
let v = _mm_add_ps(v, _mm_shuffle_ps(v, v, 0b00_11_00_01)); |
| 1149 |
let v = _mm_add_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10)); |
| 1150 |
_mm_cvtss_f32(v) |
| 1151 |
} |
| 1152 |
{% endif %} |
| 1153 |
{% elif is_wasm32 %} |
| 1154 |
{% if dim == 3 %} |
| 1155 |
let v = self.0; |
| 1156 |
let v = f32x4_add(v, i32x4_shuffle::<1, 0, 4, 0>(v, Self::ZERO.0)); |
| 1157 |
let v = f32x4_add(v, i32x4_shuffle::<2, 0, 0, 0>(v, v)); |
| 1158 |
f32x4_extract_lane::<0>(v) |
| 1159 |
{% elif dim == 4 %} |
| 1160 |
let v = self.0; |
| 1161 |
let v = f32x4_add(v, i32x4_shuffle::<1, 0, 3, 0>(v, v)); |
| 1162 |
let v = f32x4_add(v, i32x4_shuffle::<2, 0, 0, 0>(v, v)); |
| 1163 |
f32x4_extract_lane::<0>(v) |
| 1164 |
{% endif %} |
| 1165 |
{% elif is_coresimd %} |
| 1166 |
{% if dim == 3 %} |
| 1167 |
simd_swizzle!(self.0, Self::ZERO.0, [0, 1, 2, 4]).reduce_sum() |
| 1168 |
{% elif dim == 4 %} |
| 1169 |
self.0.reduce_sum() |
| 1170 |
{% endif %} |
| 1171 |
{% elif is_neon %} |
| 1172 |
{% if dim == 3 %} |
| 1173 |
unsafe { vaddvq_f32(vsetq_lane_f32(0.0, self.0, 3)) } |
| 1174 |
{% elif dim == 4 %} |
| 1175 |
unsafe { vaddvq_f32(self.0) } |
| 1176 |
{% endif %} |
| 1177 |
{% else %} |
| 1178 |
unimplemented!() |
| 1179 |
{% endif %} |
| 1180 |
} |
| 1181 |
|
| 1182 |
/// Returns the product of all elements of `self`. |
| 1183 |
/// |
| 1184 |
/// In other words, this computes `self.x * self.y * ..`. |
| 1185 |
#[inline] |
| 1186 |
#[must_use] |
| 1187 |
pub fn element_product(self) -> {{ scalar_t }} { |
| 1188 |
{% if is_scalar %} |
| 1189 |
{% for c in components %} |
| 1190 |
self.{{ c }} {% if not loop.last %} * {% endif %} |
| 1191 |
{%- endfor %} |
| 1192 |
{% elif is_sse2 %} |
| 1193 |
{% if dim == 3 %} |
| 1194 |
unsafe { |
| 1195 |
let v = self.0; |
| 1196 |
let v = _mm_mul_ps(v, _mm_shuffle_ps(v, Self::ONE.0, 0b00_11_00_01)); |
| 1197 |
let v = _mm_mul_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10)); |
| 1198 |
_mm_cvtss_f32(v) |
| 1199 |
} |
| 1200 |
{% elif dim == 4 %} |
| 1201 |
unsafe { |
| 1202 |
let v = self.0; |
| 1203 |
let v = _mm_mul_ps(v, _mm_shuffle_ps(v, v, 0b00_11_00_01)); |
| 1204 |
let v = _mm_mul_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10)); |
| 1205 |
_mm_cvtss_f32(v) |
| 1206 |
} |
| 1207 |
{% endif %} |
| 1208 |
{% elif is_wasm32 %} |
| 1209 |
{% if dim == 3 %} |
| 1210 |
let v = self.0; |
| 1211 |
let v = f32x4_mul(v, i32x4_shuffle::<1, 0, 4, 0>(v, Self::ONE.0)); |
| 1212 |
let v = f32x4_mul(v, i32x4_shuffle::<2, 0, 0, 0>(v, v)); |
| 1213 |
f32x4_extract_lane::<0>(v) |
| 1214 |
{% elif dim == 4 %} |
| 1215 |
let v = self.0; |
| 1216 |
let v = f32x4_mul(v, i32x4_shuffle::<1, 0, 3, 0>(v, v)); |
| 1217 |
let v = f32x4_mul(v, i32x4_shuffle::<2, 0, 0, 0>(v, v)); |
| 1218 |
f32x4_extract_lane::<0>(v) |
| 1219 |
{% endif %} |
| 1220 |
{% elif is_coresimd %} |
| 1221 |
{% if dim == 3 %} |
| 1222 |
simd_swizzle!(self.0, Self::ONE.0, [0, 1, 2, 4]).reduce_product() |
| 1223 |
{% elif dim == 4 %} |
| 1224 |
self.0.reduce_product() |
| 1225 |
{% endif %} |
| 1226 |
{% elif is_neon %} |
| 1227 |
{% if dim == 3 %} |
| 1228 |
unsafe { |
| 1229 |
let s = vmuls_laneq_f32(vgetq_lane_f32(self.0, 0), self.0, 1); |
| 1230 |
vmuls_laneq_f32(s, self.0, 2) |
| 1231 |
} |
| 1232 |
{% elif dim == 4 %} |
| 1233 |
unsafe { |
| 1234 |
let s = vmuls_laneq_f32(vgetq_lane_f32(self.0, 0), self.0, 1); |
| 1235 |
let s = vmuls_laneq_f32(s, self.0, 2); |
| 1236 |
vmuls_laneq_f32(s, self.0, 3) |
| 1237 |
} |
| 1238 |
{% endif %} |
| 1239 |
{% else %} |
| 1240 |
unimplemented!() |
| 1241 |
{% endif %} |
| 1242 |
} |
| 1243 |
|
| 1244 |
/// Returns a vector mask containing the result of a `==` comparison for each element of |
| 1245 |
/// `self` and `rhs`. |
| 1246 |
/// |
| 1247 |
/// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all |
| 1248 |
/// elements. |
| 1249 |
#[inline] |
| 1250 |
#[must_use] |
| 1251 |
pub fn cmpeq(self, rhs: Self) -> {{ mask_t }} { |
| 1252 |
{% if is_scalar %} |
| 1253 |
{{ mask_t }}::new( |
| 1254 |
{% for c in components %} |
| 1255 |
self.{{ c }}.eq(&rhs.{{ c }}), |
| 1256 |
{%- endfor %} |
| 1257 |
) |
| 1258 |
{% elif is_sse2 %} |
| 1259 |
{{ mask_t }}(unsafe { _mm_cmpeq_ps(self.0, rhs.0) }) |
| 1260 |
{% elif is_wasm32 %} |
| 1261 |
{{ mask_t }}(f32x4_eq(self.0, rhs.0)) |
| 1262 |
{% elif is_coresimd %} |
| 1263 |
{{ mask_t }}(f32x4::simd_eq(self.0, rhs.0)) |
| 1264 |
{% elif is_neon %} |
| 1265 |
{{ mask_t }}(unsafe { vceqq_f32(self.0, rhs.0) }) |
| 1266 |
{% else %} |
| 1267 |
unimplemented!() |
| 1268 |
{% endif %} |
| 1269 |
} |
| 1270 |
|
| 1271 |
/// Returns a vector mask containing the result of a `!=` comparison for each element of |
| 1272 |
/// `self` and `rhs`. |
| 1273 |
/// |
| 1274 |
/// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all |
| 1275 |
/// elements. |
| 1276 |
#[inline] |
| 1277 |
#[must_use] |
| 1278 |
pub fn cmpne(self, rhs: Self) -> {{ mask_t }} { |
| 1279 |
{% if is_scalar %} |
| 1280 |
{{ mask_t }}::new( |
| 1281 |
{% for c in components %} |
| 1282 |
self.{{ c }}.ne(&rhs.{{ c }}), |
| 1283 |
{%- endfor %} |
| 1284 |
) |
| 1285 |
{% elif is_sse2 %} |
| 1286 |
{{ mask_t }}(unsafe { _mm_cmpneq_ps(self.0, rhs.0) }) |
| 1287 |
{% elif is_wasm32 %} |
| 1288 |
{{ mask_t }}(f32x4_ne(self.0, rhs.0)) |
| 1289 |
{% elif is_coresimd %} |
| 1290 |
{{ mask_t }}(f32x4::simd_ne(self.0, rhs.0)) |
| 1291 |
{% elif is_neon %} |
| 1292 |
{{ mask_t }}(unsafe { vmvnq_u32(vceqq_f32(self.0, rhs.0)) }) |
| 1293 |
{% else %} |
| 1294 |
unimplemented!() |
| 1295 |
{% endif %} |
| 1296 |
} |
| 1297 |
|
| 1298 |
/// Returns a vector mask containing the result of a `>=` comparison for each element of |
| 1299 |
/// `self` and `rhs`. |
| 1300 |
/// |
| 1301 |
/// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all |
| 1302 |
/// elements. |
| 1303 |
#[inline] |
| 1304 |
#[must_use] |
| 1305 |
pub fn cmpge(self, rhs: Self) -> {{ mask_t }} { |
| 1306 |
{% if is_scalar %} |
| 1307 |
{{ mask_t }}::new( |
| 1308 |
{% for c in components %} |
| 1309 |
self.{{ c }}.ge(&rhs.{{ c }}), |
| 1310 |
{%- endfor %} |
| 1311 |
) |
| 1312 |
{% elif is_sse2 %} |
| 1313 |
{{ mask_t }}(unsafe { _mm_cmpge_ps(self.0, rhs.0) }) |
| 1314 |
{% elif is_wasm32 %} |
| 1315 |
{{ mask_t }}(f32x4_ge(self.0, rhs.0)) |
| 1316 |
{% elif is_coresimd %} |
| 1317 |
{{ mask_t }}(f32x4::simd_ge(self.0, rhs.0)) |
| 1318 |
{% elif is_neon %} |
| 1319 |
{{ mask_t }}(unsafe { vcgeq_f32(self.0, rhs.0) }) |
| 1320 |
{% else %} |
| 1321 |
unimplemented!() |
| 1322 |
{% endif %} |
| 1323 |
} |
| 1324 |
|
| 1325 |
/// Returns a vector mask containing the result of a `>` comparison for each element of |
| 1326 |
/// `self` and `rhs`. |
| 1327 |
/// |
| 1328 |
/// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all |
| 1329 |
/// elements. |
| 1330 |
#[inline] |
| 1331 |
#[must_use] |
| 1332 |
pub fn cmpgt(self, rhs: Self) -> {{ mask_t }} { |
| 1333 |
{% if is_scalar %} |
| 1334 |
{{ mask_t }}::new( |
| 1335 |
{% for c in components %} |
| 1336 |
self.{{ c }}.gt(&rhs.{{ c }}), |
| 1337 |
{%- endfor %} |
| 1338 |
) |
| 1339 |
{% elif is_sse2 %} |
| 1340 |
{{ mask_t }}(unsafe { _mm_cmpgt_ps(self.0, rhs.0) }) |
| 1341 |
{% elif is_wasm32 %} |
| 1342 |
{{ mask_t }}(f32x4_gt(self.0, rhs.0)) |
| 1343 |
{% elif is_coresimd %} |
| 1344 |
{{ mask_t }}(f32x4::simd_gt(self.0, rhs.0)) |
| 1345 |
{% elif is_neon %} |
| 1346 |
{{ mask_t }}(unsafe { vcgtq_f32(self.0, rhs.0) }) |
| 1347 |
{% else %} |
| 1348 |
unimplemented!() |
| 1349 |
{% endif %} |
| 1350 |
} |
| 1351 |
|
| 1352 |
/// Returns a vector mask containing the result of a `<=` comparison for each element of |
| 1353 |
/// `self` and `rhs`. |
| 1354 |
/// |
| 1355 |
/// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all |
| 1356 |
/// elements. |
| 1357 |
#[inline] |
| 1358 |
#[must_use] |
| 1359 |
pub fn cmple(self, rhs: Self) -> {{ mask_t }} { |
| 1360 |
{% if is_scalar %} |
| 1361 |
{{ mask_t }}::new( |
| 1362 |
{% for c in components %} |
| 1363 |
self.{{ c }}.le(&rhs.{{ c }}), |
| 1364 |
{%- endfor %} |
| 1365 |
) |
| 1366 |
{% elif is_sse2 %} |
| 1367 |
{{ mask_t }}(unsafe { _mm_cmple_ps(self.0, rhs.0) }) |
| 1368 |
{% elif is_wasm32 %} |
| 1369 |
{{ mask_t }}(f32x4_le(self.0, rhs.0)) |
| 1370 |
{% elif is_coresimd %} |
| 1371 |
{{ mask_t }}(f32x4::simd_le(self.0, rhs.0)) |
| 1372 |
{% elif is_neon %} |
| 1373 |
{{ mask_t }}(unsafe { vcleq_f32(self.0, rhs.0) }) |
| 1374 |
{% else %} |
| 1375 |
unimplemented!() |
| 1376 |
{% endif %} |
| 1377 |
} |
| 1378 |
|
| 1379 |
/// Returns a vector mask containing the result of a `<` comparison for each element of |
| 1380 |
/// `self` and `rhs`. |
| 1381 |
/// |
| 1382 |
/// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all |
| 1383 |
/// elements. |
| 1384 |
#[inline] |
| 1385 |
#[must_use] |
| 1386 |
pub fn cmplt(self, rhs: Self) -> {{ mask_t }} { |
| 1387 |
{% if is_scalar %} |
| 1388 |
{{ mask_t }}::new( |
| 1389 |
{% for c in components %} |
| 1390 |
self.{{ c }}.lt(&rhs.{{ c }}), |
| 1391 |
{%- endfor %} |
| 1392 |
) |
| 1393 |
{% elif is_sse2 %} |
| 1394 |
{{ mask_t }}(unsafe { _mm_cmplt_ps(self.0, rhs.0) }) |
| 1395 |
{% elif is_wasm32 %} |
| 1396 |
{{ mask_t }}(f32x4_lt(self.0, rhs.0)) |
| 1397 |
{% elif is_coresimd %} |
| 1398 |
{{ mask_t }}(f32x4::simd_lt(self.0, rhs.0)) |
| 1399 |
{% elif is_neon %} |
| 1400 |
{{ mask_t }}(unsafe { vcltq_f32(self.0, rhs.0) }) |
| 1401 |
{% else %} |
| 1402 |
unimplemented!() |
| 1403 |
{% endif %} |
| 1404 |
} |
| 1405 |
|
| 1406 |
{% if is_signed %} |
| 1407 |
/// Returns a vector containing the absolute value of each element of `self`. |
| 1408 |
#[inline] |
| 1409 |
#[must_use] |
| 1410 |
pub fn abs(self) -> Self { |
| 1411 |
{% if is_scalar %} |
| 1412 |
Self { |
| 1413 |
{% for c in components %} |
| 1414 |
{%- if is_float %} |
| 1415 |
{{ c }}: math::abs(self.{{ c }}), |
| 1416 |
{%- else %} |
| 1417 |
{{ c }}: self.{{ c }}.abs(), |
| 1418 |
{%- endif %} |
| 1419 |
{%- endfor %} |
| 1420 |
} |
| 1421 |
{% elif is_sse2 %} |
| 1422 |
Self(unsafe { crate::sse2::m128_abs(self.0) }) |
| 1423 |
{% elif is_wasm32 %} |
| 1424 |
Self(f32x4_abs(self.0)) |
| 1425 |
{% elif is_coresimd %} |
| 1426 |
Self(self.0.abs()) |
| 1427 |
{% elif is_neon %} |
| 1428 |
Self(unsafe { vabsq_f32(self.0) }) |
| 1429 |
{% else %} |
| 1430 |
unimplemented!() |
| 1431 |
{% endif %} |
| 1432 |
} |
| 1433 |
|
| 1434 |
/// Returns a vector with elements representing the sign of `self`. |
| 1435 |
/// |
| 1436 |
{% if is_float -%} |
| 1437 |
/// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 1438 |
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 1439 |
/// - `NAN` if the number is `NAN` |
| 1440 |
{%- else -%} |
| 1441 |
/// - `0` if the number is zero |
| 1442 |
/// - `1` if the number is positive |
| 1443 |
/// - `-1` if the number is negative |
| 1444 |
{%- endif %} |
| 1445 |
#[inline] |
| 1446 |
#[must_use] |
| 1447 |
pub fn signum(self) -> Self { |
| 1448 |
{% if is_scalar %} |
| 1449 |
Self { |
| 1450 |
{% for c in components %} |
| 1451 |
{%- if is_float %} |
| 1452 |
{{ c }}: math::signum(self.{{ c }}), |
| 1453 |
{%- else %} |
| 1454 |
{{ c }}: self.{{ c }}.signum(), |
| 1455 |
{%- endif %} |
| 1456 |
{%- endfor %} |
| 1457 |
} |
| 1458 |
{% elif is_coresimd %} |
| 1459 |
Self(self.0.signum()) |
| 1460 |
{% elif is_sse2 %} |
| 1461 |
let result = Self(unsafe { _mm_or_ps(_mm_and_ps(self.0, Self::NEG_ONE.0), Self::ONE.0) }); |
| 1462 |
let mask = self.is_nan_mask(); |
| 1463 |
Self::select(mask, self, result) |
| 1464 |
{% elif is_wasm32 %} |
| 1465 |
let result = Self(v128_or(v128_and(self.0, Self::NEG_ONE.0), Self::ONE.0)); |
| 1466 |
let mask = self.is_nan_mask(); |
| 1467 |
Self::select(mask, self, result) |
| 1468 |
{% elif is_neon %} |
| 1469 |
let result = Self(unsafe { |
| 1470 |
vreinterpretq_f32_u32(vorrq_u32( |
| 1471 |
vandq_u32( |
| 1472 |
vreinterpretq_u32_f32(self.0), |
| 1473 |
vreinterpretq_u32_f32(Self::NEG_ONE.0)), |
| 1474 |
vreinterpretq_u32_f32(Self::ONE.0), |
| 1475 |
)) |
| 1476 |
}); |
| 1477 |
let mask = self.is_nan_mask(); |
| 1478 |
Self::select(mask, self, result) |
| 1479 |
{% else %} |
| 1480 |
unimplemented!() |
| 1481 |
{% endif %} |
| 1482 |
} |
| 1483 |
|
| 1484 |
{% if is_float %} |
| 1485 |
/// Returns a vector with signs of `rhs` and the magnitudes of `self`. |
| 1486 |
#[inline] |
| 1487 |
#[must_use] |
| 1488 |
pub fn copysign(self, rhs: Self) -> Self { |
| 1489 |
{% if is_scalar %} |
| 1490 |
Self { |
| 1491 |
{% for c in components %} |
| 1492 |
{{ c }}: math::copysign(self.{{ c }}, rhs.{{ c }}), |
| 1493 |
{%- endfor %} |
| 1494 |
} |
| 1495 |
{% elif is_coresimd %} |
| 1496 |
Self(self.0.copysign(rhs.0)) |
| 1497 |
{% elif is_sse2 %} |
| 1498 |
let mask = Self::splat(-0.0); |
| 1499 |
Self(unsafe { _mm_or_ps(_mm_and_ps(rhs.0, mask.0), _mm_andnot_ps(mask.0, self.0)) }) |
| 1500 |
{% elif is_wasm32 %} |
| 1501 |
let mask = Self::splat(-0.0); |
| 1502 |
Self(v128_or(v128_and(rhs.0, mask.0), v128_andnot(self.0, mask.0))) |
| 1503 |
{% elif is_neon %} |
| 1504 |
let mask = Self::splat(-0.0); |
| 1505 |
Self(unsafe { |
| 1506 |
vreinterpretq_f32_u32(vorrq_u32( |
| 1507 |
vandq_u32(vreinterpretq_u32_f32(rhs.0), vreinterpretq_u32_f32(mask.0)), |
| 1508 |
vandq_u32( |
| 1509 |
vreinterpretq_u32_f32(self.0), |
| 1510 |
vmvnq_u32(vreinterpretq_u32_f32(mask.0))) |
| 1511 |
)) |
| 1512 |
}) |
| 1513 |
{% else %} |
| 1514 |
unimplemented!(); |
| 1515 |
{% endif %} |
| 1516 |
} |
| 1517 |
{% endif %} |
| 1518 |
|
| 1519 |
/// Returns a bitmask with the lowest {{ dim }} bits set to the sign bits from the elements of `self`. |
| 1520 |
/// |
| 1521 |
/// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes |
| 1522 |
/// into the first lowest bit, element `y` into the second, etc. |
| 1523 |
/// |
| 1524 |
/// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign |
| 1525 |
/// bit and negative infinity. |
| 1526 |
#[inline] |
| 1527 |
#[must_use] |
| 1528 |
pub fn is_negative_bitmask(self) -> u32 { |
| 1529 |
{% if is_scalar and is_float %} |
| 1530 |
{% for c in components %} |
| 1531 |
{% if loop.first %} |
| 1532 |
(self.{{ c }}.is_sign_negative() as u32) | |
| 1533 |
{% else %} |
| 1534 |
((self.{{ c }}.is_sign_negative() as u32) << {{ loop.index0 }}) {% if not loop.last %} | {% endif %} |
| 1535 |
{% endif %} |
| 1536 |
{% endfor %} |
| 1537 |
{% elif is_scalar %} |
| 1538 |
{% for c in components %} |
| 1539 |
{% if loop.first %} |
| 1540 |
(self.{{ c }}.is_negative() as u32) | |
| 1541 |
{% else %} |
| 1542 |
((self.{{ c }}.is_negative() as u32) << {{ loop.index0 }}) {% if not loop.last %} | {% endif %} |
| 1543 |
{% endif %} |
| 1544 |
{% endfor %} |
| 1545 |
{% elif is_sse2 %} |
| 1546 |
{% if dim == 3 %} |
| 1547 |
unsafe { (_mm_movemask_ps(self.0) as u32) & 0x7 } |
| 1548 |
{% elif dim == 4 %} |
| 1549 |
unsafe { _mm_movemask_ps(self.0) as u32 } |
| 1550 |
{% endif %} |
| 1551 |
{% elif is_wasm32 %} |
| 1552 |
{% if dim == 3 %} |
| 1553 |
(u32x4_bitmask(self.0) & 0x7) as u32 |
| 1554 |
{% elif dim == 4 %} |
| 1555 |
u32x4_bitmask(self.0) as u32 |
| 1556 |
{% endif %} |
| 1557 |
{% elif is_coresimd %} |
| 1558 |
{% if dim == 3 %} |
| 1559 |
(self.0.is_sign_negative().to_bitmask() & 0x7) as u32 |
| 1560 |
{% elif dim == 4 %} |
| 1561 |
self.0.is_sign_negative().to_bitmask() as u32 |
| 1562 |
{% endif %} |
| 1563 |
{% elif is_neon %} |
| 1564 |
unsafe { |
| 1565 |
let nmask = vreinterpretq_u32_f32(vdupq_n_f32(-0.0)); |
| 1566 |
let m = vandq_u32(vreinterpretq_u32_f32(self.0), nmask); |
| 1567 |
let x = vgetq_lane_u32(m, 0) >> 31; |
| 1568 |
let y = vgetq_lane_u32(m, 1) >> 31; |
| 1569 |
let z = vgetq_lane_u32(m, 2) >> 31; |
| 1570 |
{% if dim == 3 %} |
| 1571 |
x | y << 1 | z << 2 |
| 1572 |
{% elif dim == 4 %} |
| 1573 |
let w = vgetq_lane_u32(m, 3) >> 31; |
| 1574 |
x | y << 1 | z << 2 | w << 3 |
| 1575 |
{% endif %} |
| 1576 |
} |
| 1577 |
{% else %} |
| 1578 |
unimplemented!() |
| 1579 |
{% endif %} |
| 1580 |
} |
| 1581 |
{% endif %} |
| 1582 |
|
| 1583 |
{% if is_float %} |
| 1584 |
/// Returns `true` if, and only if, all elements are finite. If any element is either |
| 1585 |
/// `NaN`, positive or negative infinity, this will return `false`. |
| 1586 |
#[inline] |
| 1587 |
#[must_use] |
| 1588 |
pub fn is_finite(self) -> bool { |
| 1589 |
{% if is_scalar %} |
| 1590 |
{% for c in components %} |
| 1591 |
self.{{ c }}.is_finite() {% if not loop.last %} && {% endif %} |
| 1592 |
{%- endfor %} |
| 1593 |
{% else %} |
| 1594 |
self.is_finite_mask().all() |
| 1595 |
{% endif %} |
| 1596 |
} |
| 1597 |
|
| 1598 |
/// Performs `is_finite` on each element of self, returning a vector mask of the results. |
| 1599 |
/// |
| 1600 |
/// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`. |
| 1601 |
#[inline] |
| 1602 |
#[must_use] |
| 1603 |
pub fn is_finite_mask(self) -> {{ mask_t }} { |
| 1604 |
{% if is_scalar %} |
| 1605 |
{{ mask_t }}::new( |
| 1606 |
{% for c in components %} |
| 1607 |
self.{{ c }}.is_finite(), |
| 1608 |
{%- endfor %} |
| 1609 |
) |
| 1610 |
{% elif is_sse2 %} |
| 1611 |
{{ mask_t }}(unsafe { _mm_cmplt_ps(crate::sse2::m128_abs(self.0), Self::INFINITY.0) }) |
| 1612 |
{% elif is_wasm32 %} |
| 1613 |
{{ mask_t }}(f32x4_lt(f32x4_abs(self.0), Self::INFINITY.0)) |
| 1614 |
{% elif is_coresimd %} |
| 1615 |
{{ mask_t }}(f32x4::is_finite(self.0)) |
| 1616 |
{% elif is_neon %} |
| 1617 |
{{ mask_t }}(unsafe { vcltq_f32(vabsq_f32(self.0), Self::INFINITY.0) }) |
| 1618 |
{% endif %} |
| 1619 |
} |
| 1620 |
|
| 1621 |
/// Returns `true` if any elements are `NaN`. |
| 1622 |
#[inline] |
| 1623 |
#[must_use] |
| 1624 |
pub fn is_nan(self) -> bool { |
| 1625 |
{% if is_scalar %} |
| 1626 |
{% for c in components %} |
| 1627 |
self.{{ c }}.is_nan() {% if not loop.last %} || {% endif %} |
| 1628 |
{%- endfor %} |
| 1629 |
{% else %} |
| 1630 |
self.is_nan_mask().any() |
| 1631 |
{% endif %} |
| 1632 |
} |
| 1633 |
|
| 1634 |
/// Performs `is_nan` on each element of self, returning a vector mask of the results. |
| 1635 |
/// |
| 1636 |
/// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`. |
| 1637 |
#[inline] |
| 1638 |
#[must_use] |
| 1639 |
pub fn is_nan_mask(self) -> {{ mask_t }} { |
| 1640 |
{% if is_scalar %} |
| 1641 |
{{ mask_t }}::new( |
| 1642 |
{% for c in components %} |
| 1643 |
self.{{ c }}.is_nan(), |
| 1644 |
{%- endfor %} |
| 1645 |
) |
| 1646 |
{% elif is_sse2 %} |
| 1647 |
{{ mask_t }}(unsafe { _mm_cmpunord_ps(self.0, self.0) }) |
| 1648 |
{% elif is_wasm32 %} |
| 1649 |
{{ mask_t }}(f32x4_ne(self.0, self.0)) |
| 1650 |
{% elif is_coresimd %} |
| 1651 |
{{ mask_t }}(f32x4::is_nan(self.0)) |
| 1652 |
{% elif is_neon %} |
| 1653 |
{{ mask_t }}(unsafe { vmvnq_u32(vceqq_f32(self.0, self.0)) }) |
| 1654 |
{% endif %} |
| 1655 |
} |
| 1656 |
|
| 1657 |
/// Computes the length of `self`. |
| 1658 |
#[doc(alias = "magnitude")] |
| 1659 |
#[inline] |
| 1660 |
#[must_use] |
| 1661 |
pub fn length(self) -> {{ scalar_t }} { |
| 1662 |
{% if is_scalar or is_neon %} |
| 1663 |
math::sqrt(self.dot(self)) |
| 1664 |
{% elif is_sse2 %} |
| 1665 |
unsafe { |
| 1666 |
let dot = dot{{ dim }}_in_x(self.0, self.0); |
| 1667 |
_mm_cvtss_f32(_mm_sqrt_ps(dot)) |
| 1668 |
} |
| 1669 |
{% elif is_wasm32 %} |
| 1670 |
let dot = dot{{ dim }}_in_x(self.0, self.0); |
| 1671 |
f32x4_extract_lane::<0>(f32x4_sqrt(dot)) |
| 1672 |
{% elif is_coresimd %} |
| 1673 |
let dot = dot{{ dim }}_in_x(self.0, self.0); |
| 1674 |
dot.sqrt()[0] |
| 1675 |
{% endif %} |
| 1676 |
} |
| 1677 |
{% endif %} |
| 1678 |
|
| 1679 |
/// Computes the squared length of `self`. |
| 1680 |
{%- if is_float %} |
| 1681 |
/// |
| 1682 |
/// This is faster than `length()` as it avoids a square root operation. |
| 1683 |
{%- endif %} |
| 1684 |
#[doc(alias = "magnitude2")] |
| 1685 |
#[inline] |
| 1686 |
#[must_use] |
| 1687 |
pub fn length_squared(self) -> {{ scalar_t }} { |
| 1688 |
self.dot(self) |
| 1689 |
} |
| 1690 |
|
| 1691 |
{% if is_float %} |
| 1692 |
/// Computes `1.0 / length()`. |
| 1693 |
/// |
| 1694 |
/// For valid results, `self` must _not_ be of length zero. |
| 1695 |
#[inline] |
| 1696 |
#[must_use] |
| 1697 |
pub fn length_recip(self) -> {{ scalar_t }} { |
| 1698 |
{% if is_scalar or is_neon %} |
| 1699 |
self.length().recip() |
| 1700 |
{% elif is_sse2 %} |
| 1701 |
unsafe { |
| 1702 |
let dot = dot{{ dim }}_in_x(self.0, self.0); |
| 1703 |
_mm_cvtss_f32(_mm_div_ps(Self::ONE.0, _mm_sqrt_ps(dot))) |
| 1704 |
} |
| 1705 |
{% elif is_wasm32 %} |
| 1706 |
let dot = dot{{ dim }}_in_x(self.0, self.0); |
| 1707 |
f32x4_extract_lane::<0>(f32x4_div(Self::ONE.0, f32x4_sqrt(dot))) |
| 1708 |
{% elif is_coresimd %} |
| 1709 |
let dot = dot{{ dim }}_in_x(self.0, self.0); |
| 1710 |
dot.sqrt().recip()[0] |
| 1711 |
{% endif %} |
| 1712 |
} |
| 1713 |
|
| 1714 |
/// Computes the Euclidean distance between two points in space. |
| 1715 |
#[inline] |
| 1716 |
#[must_use] |
| 1717 |
pub fn distance(self, rhs: Self) -> {{ scalar_t }} { |
| 1718 |
(self - rhs).length() |
| 1719 |
} |
| 1720 |
{% endif %} |
| 1721 |
|
| 1722 |
{% if is_signed %} |
| 1723 |
/// Compute the squared euclidean distance between two points in space. |
| 1724 |
#[inline] |
| 1725 |
#[must_use] |
| 1726 |
pub fn distance_squared(self, rhs: Self) -> {{ scalar_t }} { |
| 1727 |
(self - rhs).length_squared() |
| 1728 |
} |
| 1729 |
|
| 1730 |
/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`. |
| 1731 |
{%- if not is_float%} |
| 1732 |
/// |
| 1733 |
/// # Panics |
| 1734 |
/// This function will panic if any `rhs` element is 0 or the division results in overflow. |
| 1735 |
{%- endif %} |
| 1736 |
#[inline] |
| 1737 |
#[must_use] |
| 1738 |
pub fn div_euclid(self, rhs: Self) -> Self { |
| 1739 |
Self::new( |
| 1740 |
{% for c in components %} |
| 1741 |
{% if is_float %} |
| 1742 |
math::div_euclid(self.{{ c }}, rhs.{{ c }}), |
| 1743 |
{% else %} |
| 1744 |
self.{{ c }}.div_euclid(rhs.{{ c }}), |
| 1745 |
{% endif %} |
| 1746 |
{%- endfor %} |
| 1747 |
) |
| 1748 |
} |
| 1749 |
|
| 1750 |
/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`. |
| 1751 |
{%- if not is_float %} |
| 1752 |
/// |
| 1753 |
/// # Panics |
| 1754 |
/// This function will panic if any `rhs` element is 0 or the division results in overflow. |
| 1755 |
{%- endif %} |
| 1756 |
/// |
| 1757 |
/// [Euclidean division]: {{scalar_t}}::rem_euclid |
| 1758 |
#[inline] |
| 1759 |
#[must_use] |
| 1760 |
pub fn rem_euclid(self, rhs: Self) -> Self { |
| 1761 |
Self::new( |
| 1762 |
{% for c in components %} |
| 1763 |
{% if is_float %} |
| 1764 |
math::rem_euclid(self.{{ c }}, rhs.{{ c }}), |
| 1765 |
{% else %} |
| 1766 |
self.{{ c }}.rem_euclid(rhs.{{ c }}), |
| 1767 |
{% endif %} |
| 1768 |
{%- endfor %} |
| 1769 |
) |
| 1770 |
} |
| 1771 |
{% endif %} |
| 1772 |
|
| 1773 |
{% if is_float %} |
| 1774 |
/// Returns `self` normalized to length 1.0. |
| 1775 |
/// |
| 1776 |
/// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero. |
| 1777 |
/// |
| 1778 |
/// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`]. |
| 1779 |
/// |
| 1780 |
/// # Panics |
| 1781 |
/// |
| 1782 |
/// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled. |
| 1783 |
#[inline] |
| 1784 |
#[must_use] |
| 1785 |
pub fn normalize(self) -> Self { |
| 1786 |
{% if is_scalar or is_neon %} |
| 1787 |
#[allow(clippy::let_and_return)] |
| 1788 |
let normalized = self.mul(self.length_recip()); |
| 1789 |
glam_assert!(normalized.is_finite()); |
| 1790 |
normalized |
| 1791 |
{% elif is_sse2 %} |
| 1792 |
unsafe { |
| 1793 |
let length = _mm_sqrt_ps(dot{{ dim }}_into_m128(self.0, self.0)); |
| 1794 |
#[allow(clippy::let_and_return)] |
| 1795 |
let normalized = Self(_mm_div_ps(self.0, length)); |
| 1796 |
glam_assert!(normalized.is_finite()); |
| 1797 |
normalized |
| 1798 |
} |
| 1799 |
{% elif is_wasm32 %} |
| 1800 |
let length = f32x4_sqrt(dot{{ dim }}_into_v128(self.0, self.0)); |
| 1801 |
#[allow(clippy::let_and_return)] |
| 1802 |
let normalized = Self(f32x4_div(self.0, length)); |
| 1803 |
glam_assert!(normalized.is_finite()); |
| 1804 |
normalized |
| 1805 |
{% elif is_coresimd %} |
| 1806 |
let length = dot{{ dim }}_into_f32x4(self.0, self.0).sqrt(); |
| 1807 |
#[allow(clippy::let_and_return)] |
| 1808 |
let normalized = Self(self.0 / length); |
| 1809 |
glam_assert!(normalized.is_finite()); |
| 1810 |
normalized |
| 1811 |
{% else %} |
| 1812 |
unimplemented!() |
| 1813 |
{% endif %} |
| 1814 |
} |
| 1815 |
|
| 1816 |
/// Returns `self` normalized to length 1.0 if possible, else returns `None`. |
| 1817 |
/// |
| 1818 |
/// In particular, if the input is zero (or very close to zero), or non-finite, |
| 1819 |
/// the result of this operation will be `None`. |
| 1820 |
/// |
| 1821 |
/// See also [`Self::normalize_or_zero()`]. |
| 1822 |
#[inline] |
| 1823 |
#[must_use] |
| 1824 |
pub fn try_normalize(self) -> Option<Self> { |
| 1825 |
let rcp = self.length_recip(); |
| 1826 |
if rcp.is_finite() && rcp > 0.0 { |
| 1827 |
Some(self * rcp) |
| 1828 |
} else { |
| 1829 |
None |
| 1830 |
} |
| 1831 |
} |
| 1832 |
|
| 1833 |
/// Returns `self` normalized to length 1.0 if possible, else returns a |
| 1834 |
/// fallback value. |
| 1835 |
/// |
| 1836 |
/// In particular, if the input is zero (or very close to zero), or non-finite, |
| 1837 |
/// the result of this operation will be the fallback value. |
| 1838 |
/// |
| 1839 |
/// See also [`Self::try_normalize()`]. |
| 1840 |
#[inline] |
| 1841 |
#[must_use] |
| 1842 |
pub fn normalize_or(self, fallback: Self) -> Self { |
| 1843 |
let rcp = self.length_recip(); |
| 1844 |
if rcp.is_finite() && rcp > 0.0 { |
| 1845 |
self * rcp |
| 1846 |
} else { |
| 1847 |
fallback |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
/// Returns `self` normalized to length 1.0 if possible, else returns zero. |
| 1852 |
/// |
| 1853 |
/// In particular, if the input is zero (or very close to zero), or non-finite, |
| 1854 |
/// the result of this operation will be zero. |
| 1855 |
/// |
| 1856 |
/// See also [`Self::try_normalize()`]. |
| 1857 |
#[inline] |
| 1858 |
#[must_use] |
| 1859 |
pub fn normalize_or_zero(self) -> Self { |
| 1860 |
self.normalize_or(Self::ZERO) |
| 1861 |
} |
| 1862 |
|
| 1863 |
/// Returns `self` normalized to length 1.0 and the length of `self`. |
| 1864 |
/// |
| 1865 |
/// If `self` is zero length then `(Self::X, 0.0)` is returned. |
| 1866 |
#[inline] |
| 1867 |
#[must_use] |
| 1868 |
pub fn normalize_and_length(self) -> (Self, {{ scalar_t }}) { |
| 1869 |
let length = self.length(); |
| 1870 |
let rcp = 1.0 / length; |
| 1871 |
if rcp.is_finite() && rcp > 0.0 { |
| 1872 |
(self * rcp, length) |
| 1873 |
} else { |
| 1874 |
(Self::X, 0.0) |
| 1875 |
} |
| 1876 |
} |
| 1877 |
|
| 1878 |
/// Returns whether `self` is length `1.0` or not. |
| 1879 |
/// |
| 1880 |
/// Uses a precision threshold of approximately `1e-4`. |
| 1881 |
#[inline] |
| 1882 |
#[must_use] |
| 1883 |
pub fn is_normalized(self) -> bool { |
| 1884 |
math::abs(self.length_squared() - 1.0) <= 2e-4 |
| 1885 |
} |
| 1886 |
|
| 1887 |
/// Returns the vector projection of `self` onto `rhs`. |
| 1888 |
/// |
| 1889 |
/// `rhs` must be of non-zero length. |
| 1890 |
/// |
| 1891 |
/// # Panics |
| 1892 |
/// |
| 1893 |
/// Will panic if `rhs` is zero length when `glam_assert` is enabled. |
| 1894 |
#[inline] |
| 1895 |
#[must_use] |
| 1896 |
pub fn project_onto(self, rhs: Self) -> Self { |
| 1897 |
let other_len_sq_rcp = rhs.dot(rhs).recip(); |
| 1898 |
glam_assert!(other_len_sq_rcp.is_finite()); |
| 1899 |
rhs * self.dot(rhs) * other_len_sq_rcp |
| 1900 |
} |
| 1901 |
|
| 1902 |
/// Returns the vector rejection of `self` from `rhs`. |
| 1903 |
/// |
| 1904 |
/// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 1905 |
/// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`. |
| 1906 |
/// |
| 1907 |
/// `rhs` must be of non-zero length. |
| 1908 |
/// |
| 1909 |
/// # Panics |
| 1910 |
/// |
| 1911 |
/// Will panic if `rhs` has a length of zero when `glam_assert` is enabled. |
| 1912 |
#[doc(alias("plane"))] |
| 1913 |
#[inline] |
| 1914 |
#[must_use] |
| 1915 |
pub fn reject_from(self, rhs: Self) -> Self { |
| 1916 |
self - self.project_onto(rhs) |
| 1917 |
} |
| 1918 |
|
| 1919 |
/// Returns the vector projection of `self` onto `rhs`. |
| 1920 |
/// |
| 1921 |
/// `rhs` must be normalized. |
| 1922 |
/// |
| 1923 |
/// # Panics |
| 1924 |
/// |
| 1925 |
/// Will panic if `rhs` is not normalized when `glam_assert` is enabled. |
| 1926 |
#[inline] |
| 1927 |
#[must_use] |
| 1928 |
pub fn project_onto_normalized(self, rhs: Self) -> Self { |
| 1929 |
glam_assert!(rhs.is_normalized()); |
| 1930 |
rhs * self.dot(rhs) |
| 1931 |
} |
| 1932 |
|
| 1933 |
/// Returns the vector rejection of `self` from `rhs`. |
| 1934 |
/// |
| 1935 |
/// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 1936 |
/// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`. |
| 1937 |
/// |
| 1938 |
/// `rhs` must be normalized. |
| 1939 |
/// |
| 1940 |
/// # Panics |
| 1941 |
/// |
| 1942 |
/// Will panic if `rhs` is not normalized when `glam_assert` is enabled. |
| 1943 |
#[doc(alias("plane"))] |
| 1944 |
#[inline] |
| 1945 |
#[must_use] |
| 1946 |
pub fn reject_from_normalized(self, rhs: Self) -> Self { |
| 1947 |
self - self.project_onto_normalized(rhs) |
| 1948 |
} |
| 1949 |
|
| 1950 |
/// Returns a vector containing the nearest integer to a number for each element of `self`. |
| 1951 |
/// Round half-way cases away from 0.0. |
| 1952 |
#[inline] |
| 1953 |
#[must_use] |
| 1954 |
pub fn round(self) -> Self { |
| 1955 |
{% if is_scalar %} |
| 1956 |
Self { |
| 1957 |
{% for c in components %} |
| 1958 |
{{ c }}: math::round(self.{{ c }}), |
| 1959 |
{%- endfor %} |
| 1960 |
} |
| 1961 |
{% elif is_sse2 %} |
| 1962 |
Self(unsafe { m128_round(self.0) }) |
| 1963 |
{% elif is_wasm32 %} |
| 1964 |
Self(f32x4_nearest(self.0)) |
| 1965 |
{% elif is_coresimd %} |
| 1966 |
Self(self.0.round()) |
| 1967 |
{% elif is_neon %} |
| 1968 |
Self(unsafe { vrndnq_f32(self.0) }) |
| 1969 |
{% else %} |
| 1970 |
unimplemented!() |
| 1971 |
{% endif %} |
| 1972 |
} |
| 1973 |
|
| 1974 |
/// Returns a vector containing the largest integer less than or equal to a number for each |
| 1975 |
/// element of `self`. |
| 1976 |
#[inline] |
| 1977 |
#[must_use] |
| 1978 |
pub fn floor(self) -> Self { |
| 1979 |
{% if is_scalar %} |
| 1980 |
Self { |
| 1981 |
{% for c in components %} |
| 1982 |
{{ c }}: math::floor(self.{{ c }}), |
| 1983 |
{%- endfor %} |
| 1984 |
} |
| 1985 |
{% elif is_sse2 %} |
| 1986 |
Self(unsafe { m128_floor(self.0) }) |
| 1987 |
{% elif is_wasm32 %} |
| 1988 |
Self(f32x4_floor(self.0)) |
| 1989 |
{% elif is_coresimd %} |
| 1990 |
Self(self.0.floor()) |
| 1991 |
{% elif is_neon %} |
| 1992 |
Self(unsafe { vrndmq_f32(self.0) }) |
| 1993 |
{% else %} |
| 1994 |
unimplemented!() |
| 1995 |
{% endif %} |
| 1996 |
} |
| 1997 |
|
| 1998 |
/// Returns a vector containing the smallest integer greater than or equal to a number for |
| 1999 |
/// each element of `self`. |
| 2000 |
#[inline] |
| 2001 |
#[must_use] |
| 2002 |
pub fn ceil(self) -> Self { |
| 2003 |
{% if is_scalar %} |
| 2004 |
Self { |
| 2005 |
{% for c in components %} |
| 2006 |
{{ c }}: math::ceil(self.{{ c }}), |
| 2007 |
{%- endfor %} |
| 2008 |
} |
| 2009 |
{% elif is_sse2 %} |
| 2010 |
Self(unsafe { m128_ceil(self.0) }) |
| 2011 |
{% elif is_wasm32 %} |
| 2012 |
Self(f32x4_ceil(self.0)) |
| 2013 |
{% elif is_coresimd %} |
| 2014 |
Self(self.0.ceil()) |
| 2015 |
{% elif is_neon %} |
| 2016 |
Self(unsafe { vrndpq_f32(self.0) }) |
| 2017 |
{% else %} |
| 2018 |
unimplemented!() |
| 2019 |
{% endif %} |
| 2020 |
} |
| 2021 |
|
| 2022 |
/// Returns a vector containing the integer part each element of `self`. This means numbers are |
| 2023 |
/// always truncated towards zero. |
| 2024 |
#[inline] |
| 2025 |
#[must_use] |
| 2026 |
pub fn trunc(self) -> Self { |
| 2027 |
{% if is_scalar %} |
| 2028 |
Self { |
| 2029 |
{% for c in components %} |
| 2030 |
{{ c }}: math::trunc(self.{{ c }}), |
| 2031 |
{%- endfor %} |
| 2032 |
} |
| 2033 |
{% elif is_sse2 %} |
| 2034 |
Self(unsafe { m128_trunc(self.0) }) |
| 2035 |
{% elif is_wasm32 %} |
| 2036 |
Self(f32x4_trunc(self.0)) |
| 2037 |
{% elif is_coresimd %} |
| 2038 |
Self(self.0.trunc()) |
| 2039 |
{% elif is_neon %} |
| 2040 |
Self(unsafe { vrndq_f32(self.0) }) |
| 2041 |
{% else %} |
| 2042 |
unimplemented!() |
| 2043 |
{% endif %} |
| 2044 |
} |
| 2045 |
|
| 2046 |
/// Returns a vector containing the fractional part of the vector as `self - self.trunc()`. |
| 2047 |
/// |
| 2048 |
/// Note that this differs from the GLSL implementation of `fract` which returns |
| 2049 |
/// `self - self.floor()`. |
| 2050 |
/// |
| 2051 |
/// Note that this is fast but not precise for large numbers. |
| 2052 |
#[inline] |
| 2053 |
#[must_use] |
| 2054 |
pub fn fract(self) -> Self { |
| 2055 |
self - self.trunc() |
| 2056 |
} |
| 2057 |
|
| 2058 |
/// Returns a vector containing the fractional part of the vector as `self - self.floor()`. |
| 2059 |
/// |
| 2060 |
/// Note that this differs from the Rust implementation of `fract` which returns |
| 2061 |
/// `self - self.trunc()`. |
| 2062 |
/// |
| 2063 |
/// Note that this is fast but not precise for large numbers. |
| 2064 |
#[inline] |
| 2065 |
#[must_use] |
| 2066 |
pub fn fract_gl(self) -> Self { |
| 2067 |
self - self.floor() |
| 2068 |
} |
| 2069 |
|
| 2070 |
/// Returns a vector containing `e^self` (the exponential function) for each element of |
| 2071 |
/// `self`. |
| 2072 |
#[inline] |
| 2073 |
#[must_use] |
| 2074 |
pub fn exp(self) -> Self { |
| 2075 |
Self::new( |
| 2076 |
{% for c in components %} |
| 2077 |
math::exp(self.{{ c }}), |
| 2078 |
{%- endfor %} |
| 2079 |
) |
| 2080 |
} |
| 2081 |
|
| 2082 |
/// Returns a vector containing `2^self` for each element of `self`. |
| 2083 |
#[inline] |
| 2084 |
#[must_use] |
| 2085 |
pub fn exp2(self) -> Self { |
| 2086 |
Self::new( |
| 2087 |
{% for c in components %} |
| 2088 |
math::exp2(self.{{ c }}), |
| 2089 |
{%- endfor %} |
| 2090 |
) |
| 2091 |
} |
| 2092 |
|
| 2093 |
/// Returns a vector containing the natural logarithm for each element of `self`. |
| 2094 |
/// This returns NaN when the element is negative and negative infinity when the element is zero. |
| 2095 |
#[inline] |
| 2096 |
#[must_use] |
| 2097 |
pub fn ln(self) -> Self { |
| 2098 |
Self::new( |
| 2099 |
{% for c in components %} |
| 2100 |
math::ln(self.{{ c }}), |
| 2101 |
{%- endfor %} |
| 2102 |
) |
| 2103 |
} |
| 2104 |
|
| 2105 |
/// Returns a vector containing the base 2 logarithm for each element of `self`. |
| 2106 |
/// This returns NaN when the element is negative and negative infinity when the element is zero. |
| 2107 |
#[inline] |
| 2108 |
#[must_use] |
| 2109 |
pub fn log2(self) -> Self { |
| 2110 |
Self::new( |
| 2111 |
{% for c in components %} |
| 2112 |
math::log2(self.{{ c }}), |
| 2113 |
{%- endfor %} |
| 2114 |
) |
| 2115 |
} |
| 2116 |
|
| 2117 |
/// Returns a vector containing each element of `self` raised to the power of `n`. |
| 2118 |
#[inline] |
| 2119 |
#[must_use] |
| 2120 |
pub fn powf(self, n: {{ scalar_t }}) -> Self { |
| 2121 |
Self::new( |
| 2122 |
{% for c in components %} |
| 2123 |
math::powf(self.{{ c }}, n), |
| 2124 |
{%- endfor %} |
| 2125 |
) |
| 2126 |
} |
| 2127 |
|
| 2128 |
/// Returns a vector containing the reciprocal `1.0/n` of each element of `self`. |
| 2129 |
#[inline] |
| 2130 |
#[must_use] |
| 2131 |
pub fn recip(self) -> Self { |
| 2132 |
{% if is_scalar %} |
| 2133 |
Self { |
| 2134 |
{% for c in components %} |
| 2135 |
{{ c }}: 1.0 / self.{{ c }}, |
| 2136 |
{%- endfor %} |
| 2137 |
} |
| 2138 |
{% elif is_sse2 %} |
| 2139 |
Self(unsafe { _mm_div_ps(Self::ONE.0, self.0) }) |
| 2140 |
{% elif is_wasm32 %} |
| 2141 |
Self(f32x4_div(Self::ONE.0, self.0)) |
| 2142 |
{% elif is_coresimd %} |
| 2143 |
Self(self.0.recip()) |
| 2144 |
{% elif is_neon %} |
| 2145 |
Self(unsafe { vdivq_f32(Self::ONE.0, self.0) }) |
| 2146 |
{% else %} |
| 2147 |
unimplemented!() |
| 2148 |
{% endif %} |
| 2149 |
} |
| 2150 |
|
| 2151 |
/// Performs a linear interpolation between `self` and `rhs` based on the value `s`. |
| 2152 |
/// |
| 2153 |
/// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result |
| 2154 |
/// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly |
| 2155 |
/// extrapolated. |
| 2156 |
#[doc(alias = "mix")] |
| 2157 |
#[inline] |
| 2158 |
#[must_use] |
| 2159 |
pub fn lerp(self, rhs: Self, s: {{ scalar_t }}) -> Self { |
| 2160 |
self * (1.0 - s) + rhs * s |
| 2161 |
} |
| 2162 |
|
| 2163 |
/// Moves towards `rhs` based on the value `d`. |
| 2164 |
/// |
| 2165 |
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to |
| 2166 |
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`. |
| 2167 |
#[inline] |
| 2168 |
#[must_use] |
| 2169 |
pub fn move_towards(&self, rhs: Self, d: {{ scalar_t }}) -> Self { |
| 2170 |
let a = rhs - *self; |
| 2171 |
let len = a.length(); |
| 2172 |
if len <= d || len <= 1e-4 { |
| 2173 |
return rhs; |
| 2174 |
} |
| 2175 |
*self + a / len * d |
| 2176 |
} |
| 2177 |
|
| 2178 |
/// Calculates the midpoint between `self` and `rhs`. |
| 2179 |
/// |
| 2180 |
/// The midpoint is the average of, or halfway point between, two vectors. |
| 2181 |
/// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)` |
| 2182 |
/// while being slightly cheaper to compute. |
| 2183 |
#[inline] |
| 2184 |
pub fn midpoint(self, rhs: Self) -> Self { |
| 2185 |
(self + rhs) * 0.5 |
| 2186 |
} |
| 2187 |
|
| 2188 |
/// Returns true if the absolute difference of all elements between `self` and `rhs` is |
| 2189 |
/// less than or equal to `max_abs_diff`. |
| 2190 |
/// |
| 2191 |
/// This can be used to compare if two vectors contain similar elements. It works best when |
| 2192 |
/// comparing with a known value. The `max_abs_diff` that should be used used depends on |
| 2193 |
/// the values being compared against. |
| 2194 |
/// |
| 2195 |
/// For more see |
| 2196 |
/// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 2197 |
#[inline] |
| 2198 |
#[must_use] |
| 2199 |
pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: {{ scalar_t }}) -> bool { |
| 2200 |
self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all() |
| 2201 |
} |
| 2202 |
|
| 2203 |
/// Returns a vector with a length no less than `min` and no more than `max`. |
| 2204 |
/// |
| 2205 |
/// # Panics |
| 2206 |
/// |
| 2207 |
/// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled. |
| 2208 |
#[inline] |
| 2209 |
#[must_use] |
| 2210 |
pub fn clamp_length(self, min: {{ scalar_t }}, max: {{ scalar_t }}) -> Self { |
| 2211 |
glam_assert!(0.0 <= min); |
| 2212 |
glam_assert!(min <= max); |
| 2213 |
let length_sq = self.length_squared(); |
| 2214 |
if length_sq < min * min { |
| 2215 |
min * (self / math::sqrt(length_sq)) |
| 2216 |
} else if length_sq > max * max { |
| 2217 |
max * (self / math::sqrt(length_sq)) |
| 2218 |
} else { |
| 2219 |
self |
| 2220 |
} |
| 2221 |
} |
| 2222 |
|
| 2223 |
/// Returns a vector with a length no more than `max`. |
| 2224 |
/// |
| 2225 |
/// # Panics |
| 2226 |
/// |
| 2227 |
/// Will panic if `max` is negative when `glam_assert` is enabled. |
| 2228 |
#[inline] |
| 2229 |
#[must_use] |
| 2230 |
pub fn clamp_length_max(self, max: {{ scalar_t }}) -> Self { |
| 2231 |
glam_assert!(0.0 <= max); |
| 2232 |
let length_sq = self.length_squared(); |
| 2233 |
if length_sq > max * max { |
| 2234 |
max * (self / math::sqrt(length_sq)) |
| 2235 |
} else { |
| 2236 |
self |
| 2237 |
} |
| 2238 |
} |
| 2239 |
|
| 2240 |
/// Returns a vector with a length no less than `min`. |
| 2241 |
/// |
| 2242 |
/// # Panics |
| 2243 |
/// |
| 2244 |
/// Will panic if `min` is negative when `glam_assert` is enabled. |
| 2245 |
#[inline] |
| 2246 |
#[must_use] |
| 2247 |
pub fn clamp_length_min(self, min: {{ scalar_t }}) -> Self { |
| 2248 |
glam_assert!(0.0 <= min); |
| 2249 |
let length_sq = self.length_squared(); |
| 2250 |
if length_sq < min * min { |
| 2251 |
min * (self / math::sqrt(length_sq)) |
| 2252 |
} else { |
| 2253 |
self |
| 2254 |
} |
| 2255 |
} |
| 2256 |
|
| 2257 |
/// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding |
| 2258 |
/// error, yielding a more accurate result than an unfused multiply-add. |
| 2259 |
/// |
| 2260 |
/// Using `mul_add` *may* be more performant than an unfused multiply-add if the target |
| 2261 |
/// architecture has a dedicated fma CPU instruction. However, this is not always true, |
| 2262 |
/// and will be heavily dependant on designing algorithms with specific target hardware in |
| 2263 |
/// mind. |
| 2264 |
#[inline] |
| 2265 |
#[must_use] |
| 2266 |
pub fn mul_add(self, a: Self, b: Self) -> Self { |
| 2267 |
{% if is_sse2 %} |
| 2268 |
#[cfg(target_feature = "fma")] |
| 2269 |
unsafe { Self(_mm_fmadd_ps(self.0, a.0, b.0)) } |
| 2270 |
#[cfg(not(target_feature = "fma"))] |
| 2271 |
{% endif %} |
| 2272 |
{% if is_coresimd %} |
| 2273 |
Self(self.0.mul_add(a.0, b.0)) |
| 2274 |
{% elif is_neon %} |
| 2275 |
Self(unsafe { vfmaq_f32(b.0, self.0, a.0) }) |
| 2276 |
{% else %} |
| 2277 |
Self::new( |
| 2278 |
{% for c in components %} |
| 2279 |
math::mul_add(self.{{ c }}, a.{{ c }}, b.{{ c }}), |
| 2280 |
{%- endfor %} |
| 2281 |
) |
| 2282 |
{% endif %} |
| 2283 |
} |
| 2284 |
|
| 2285 |
/// Returns the reflection vector for a given incident vector `self` and surface normal |
| 2286 |
/// `normal`. |
| 2287 |
/// |
| 2288 |
/// `normal` must be normalized. |
| 2289 |
/// |
| 2290 |
/// # Panics |
| 2291 |
/// |
| 2292 |
/// Will panic if `normal` is not normalized when `glam_assert` is enabled. |
| 2293 |
#[inline] |
| 2294 |
#[must_use] |
| 2295 |
pub fn reflect(self, normal: Self) -> Self { |
| 2296 |
glam_assert!(normal.is_normalized()); |
| 2297 |
self - 2.0 * self.dot(normal) * normal |
| 2298 |
} |
| 2299 |
|
| 2300 |
/// Returns the refraction direction for a given incident vector `self`, surface normal |
| 2301 |
/// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs, |
| 2302 |
/// a zero vector will be returned. |
| 2303 |
/// |
| 2304 |
/// `self` and `normal` must be normalized. |
| 2305 |
/// |
| 2306 |
/// # Panics |
| 2307 |
/// |
| 2308 |
/// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled. |
| 2309 |
#[inline] |
| 2310 |
#[must_use] |
| 2311 |
pub fn refract(self, normal: Self, eta: {{ scalar_t }}) -> Self { |
| 2312 |
glam_assert!(self.is_normalized()); |
| 2313 |
glam_assert!(normal.is_normalized()); |
| 2314 |
let n_dot_i = normal.dot(self); |
| 2315 |
let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i); |
| 2316 |
if k >= 0.0 { |
| 2317 |
eta * self - (eta * n_dot_i + math::sqrt(k)) * normal |
| 2318 |
} else { |
| 2319 |
Self::ZERO |
| 2320 |
} |
| 2321 |
} |
| 2322 |
|
| 2323 |
{% if dim == 2 %} |
| 2324 |
/// Creates a 2D vector containing `[angle.cos(), angle.sin()]`. This can be used in |
| 2325 |
/// conjunction with the [`rotate()`][Self::rotate()] method, e.g. |
| 2326 |
/// `{{ vec2_t }}::from_angle(PI).rotate({{ vec2_t }}::Y)` will create the vector `[-1, 0]` |
| 2327 |
/// and rotate [`{{ vec2_t }}::Y`] around it returning `-{{ vec2_t }}::Y`. |
| 2328 |
#[inline] |
| 2329 |
#[must_use] |
| 2330 |
pub fn from_angle(angle: {{ scalar_t }}) -> Self { |
| 2331 |
let (sin, cos) = math::sin_cos(angle); |
| 2332 |
Self { |
| 2333 |
x: cos, |
| 2334 |
y: sin, |
| 2335 |
} |
| 2336 |
} |
| 2337 |
|
| 2338 |
/// Returns the angle (in radians) of this vector in the range `[-Ï€, +Ï€]`. |
| 2339 |
/// |
| 2340 |
/// The input does not need to be a unit vector however it must be non-zero. |
| 2341 |
#[inline] |
| 2342 |
#[must_use] |
| 2343 |
pub fn to_angle(self) -> {{ scalar_t }} { |
| 2344 |
math::atan2(self.y, self.x) |
| 2345 |
} |
| 2346 |
|
| 2347 |
#[inline] |
| 2348 |
#[must_use] |
| 2349 |
#[deprecated( |
| 2350 |
since = "0.27.0", |
| 2351 |
note = "Use angle_to() instead, the semantics of angle_between will change in the future." |
| 2352 |
)] |
| 2353 |
pub fn angle_between(self, rhs: Self) -> {{ scalar_t }} { |
| 2354 |
self.angle_to(rhs) |
| 2355 |
} |
| 2356 |
|
| 2357 |
/// Returns the angle of rotation (in radians) from `self` to `rhs` in the range `[-Ï€, +Ï€]`. |
| 2358 |
/// |
| 2359 |
/// The inputs do not need to be unit vectors however they must be non-zero. |
| 2360 |
#[inline] |
| 2361 |
#[must_use] |
| 2362 |
pub fn angle_to(self, rhs: Self) -> {{ scalar_t }} { |
| 2363 |
let angle = math::acos_approx( |
| 2364 |
self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared())); |
| 2365 |
|
| 2366 |
angle * math::signum(self.perp_dot(rhs)) |
| 2367 |
} |
| 2368 |
{% elif dim == 3 %} |
| 2369 |
/// Returns the angle (in radians) between two vectors in the range `[0, +Ï€]`. |
| 2370 |
/// |
| 2371 |
/// The inputs do not need to be unit vectors however they must be non-zero. |
| 2372 |
#[inline] |
| 2373 |
#[must_use] |
| 2374 |
pub fn angle_between(self, rhs: Self) -> {{ scalar_t }} { |
| 2375 |
math::acos_approx( |
| 2376 |
self.dot(rhs).div( |
| 2377 |
math::sqrt(self.length_squared().mul(rhs.length_squared())))) |
| 2378 |
} |
| 2379 |
|
| 2380 |
/// Rotates around the x axis by `angle` (in radians). |
| 2381 |
#[inline] |
| 2382 |
#[must_use] |
| 2383 |
pub fn rotate_x(self, angle: {{ scalar_t }}) -> Self { |
| 2384 |
let (sina, cosa) = math::sin_cos(angle); |
| 2385 |
Self::new( |
| 2386 |
self.x, |
| 2387 |
self.y * cosa - self.z * sina, |
| 2388 |
self.y * sina + self.z * cosa, |
| 2389 |
) |
| 2390 |
} |
| 2391 |
|
| 2392 |
/// Rotates around the y axis by `angle` (in radians). |
| 2393 |
#[inline] |
| 2394 |
#[must_use] |
| 2395 |
pub fn rotate_y(self, angle: {{ scalar_t }}) -> Self { |
| 2396 |
let (sina, cosa) = math::sin_cos(angle); |
| 2397 |
Self::new( |
| 2398 |
self.x * cosa + self.z * sina, |
| 2399 |
self.y, |
| 2400 |
self.x * -sina + self.z * cosa, |
| 2401 |
) |
| 2402 |
} |
| 2403 |
|
| 2404 |
/// Rotates around the z axis by `angle` (in radians). |
| 2405 |
#[inline] |
| 2406 |
#[must_use] |
| 2407 |
pub fn rotate_z(self, angle: {{ scalar_t }}) -> Self { |
| 2408 |
let (sina, cosa) = math::sin_cos(angle); |
| 2409 |
Self::new( |
| 2410 |
self.x * cosa - self.y * sina, |
| 2411 |
self.x * sina + self.y * cosa, |
| 2412 |
self.z, |
| 2413 |
) |
| 2414 |
} |
| 2415 |
|
| 2416 |
/// Rotates around `axis` by `angle` (in radians). |
| 2417 |
/// |
| 2418 |
/// The axis must be a unit vector. |
| 2419 |
/// |
| 2420 |
/// # Panics |
| 2421 |
/// |
| 2422 |
/// Will panic if `axis` is not normalized when `glam_assert` is enabled. |
| 2423 |
#[inline] |
| 2424 |
#[must_use] |
| 2425 |
pub fn rotate_axis(self, axis: Self, angle: {{ scalar_t }}) -> Self { |
| 2426 |
{{ quat_t }}::from_axis_angle(axis{% if self_t == "Vec3A" %}.into(){% endif%}, angle) * self |
| 2427 |
} |
| 2428 |
|
| 2429 |
/// Rotates towards `rhs` up to `max_angle` (in radians). |
| 2430 |
/// |
| 2431 |
/// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to |
| 2432 |
/// `self.angle_between(rhs)`, the result will be parallel to `rhs`. If `max_angle` is negative, |
| 2433 |
/// rotates towards the exact opposite of `rhs`. Will not go past the target. |
| 2434 |
#[inline] |
| 2435 |
#[must_use] |
| 2436 |
pub fn rotate_towards(self, rhs: Self, max_angle: {{ scalar_t }}) -> Self { |
| 2437 |
let angle_between = self.angle_between(rhs); |
| 2438 |
// When `max_angle < 0`, rotate no further than `PI` radians away |
| 2439 |
let angle = max_angle.clamp(angle_between - core::{{ scalar_t }}::consts::PI, angle_between); |
| 2440 |
let axis = self |
| 2441 |
.cross(rhs) |
| 2442 |
.try_normalize() |
| 2443 |
.unwrap_or_else(|| self.any_orthogonal_vector().normalize()); |
| 2444 |
{{ quat_t }}::from_axis_angle(axis{% if self_t == "Vec3A" %}.into(){% endif%}, angle) * self |
| 2445 |
} |
| 2446 |
|
| 2447 |
/// Returns some vector that is orthogonal to the given one. |
| 2448 |
/// |
| 2449 |
/// The input vector must be finite and non-zero. |
| 2450 |
/// |
| 2451 |
/// The output vector is not necessarily unit length. For that use |
| 2452 |
/// [`Self::any_orthonormal_vector()`] instead. |
| 2453 |
#[inline] |
| 2454 |
#[must_use] |
| 2455 |
pub fn any_orthogonal_vector(&self) -> Self { |
| 2456 |
// This can probably be optimized |
| 2457 |
if math::abs(self.x) > math::abs(self.y) { |
| 2458 |
Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y) |
| 2459 |
} else { |
| 2460 |
Self::new(0.0, self.z, -self.y) // self.cross(Self::X) |
| 2461 |
} |
| 2462 |
} |
| 2463 |
|
| 2464 |
/// Returns any unit vector that is orthogonal to the given one. |
| 2465 |
/// |
| 2466 |
/// The input vector must be unit length. |
| 2467 |
/// |
| 2468 |
/// # Panics |
| 2469 |
/// |
| 2470 |
/// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 2471 |
#[inline] |
| 2472 |
#[must_use] |
| 2473 |
pub fn any_orthonormal_vector(&self) -> Self { |
| 2474 |
glam_assert!(self.is_normalized()); |
| 2475 |
// From https://graphics.pixar.com/library/OrthonormalB/paper.pdf |
| 2476 |
let sign = math::signum(self.z); |
| 2477 |
let a = -1.0 / (sign + self.z); |
| 2478 |
let b = self.x * self.y * a; |
| 2479 |
Self::new(b, sign + self.y * self.y * a, -self.y) |
| 2480 |
} |
| 2481 |
|
| 2482 |
/// Given a unit vector return two other vectors that together form an orthonormal |
| 2483 |
/// basis. That is, all three vectors are orthogonal to each other and are normalized. |
| 2484 |
/// |
| 2485 |
/// # Panics |
| 2486 |
/// |
| 2487 |
/// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 2488 |
#[inline] |
| 2489 |
#[must_use] |
| 2490 |
pub fn any_orthonormal_pair(&self) -> (Self, Self) { |
| 2491 |
glam_assert!(self.is_normalized()); |
| 2492 |
// From https://graphics.pixar.com/library/OrthonormalB/paper.pdf |
| 2493 |
let sign = math::signum(self.z); |
| 2494 |
let a = -1.0 / (sign + self.z); |
| 2495 |
let b = self.x * self.y * a; |
| 2496 |
( |
| 2497 |
Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x), |
| 2498 |
Self::new(b, sign + self.y * self.y * a, -self.y), |
| 2499 |
) |
| 2500 |
} |
| 2501 |
|
| 2502 |
/// Performs a spherical linear interpolation between `self` and `rhs` based on the value `s`. |
| 2503 |
/// |
| 2504 |
/// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result |
| 2505 |
/// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly |
| 2506 |
/// extrapolated. |
| 2507 |
#[inline] |
| 2508 |
#[must_use] |
| 2509 |
pub fn slerp(self, rhs: Self, s: {{ scalar_t }}) -> Self { |
| 2510 |
let self_length = self.length(); |
| 2511 |
let rhs_length = rhs.length(); |
| 2512 |
// Cosine of the angle between the vectors [-1, 1], or NaN if either vector has a zero length |
| 2513 |
let dot = self.dot(rhs) / (self_length * rhs_length); |
| 2514 |
// If dot is close to 1 or -1, or is NaN the calculations for t1 and t2 break down |
| 2515 |
if math::abs(dot) < 1.0 - 3e-7 { |
| 2516 |
// Angle between the vectors [0, +Ï€] |
| 2517 |
let theta = math::acos_approx(dot); |
| 2518 |
// Sine of the angle between vectors [0, 1] |
| 2519 |
let sin_theta = math::sin(theta); |
| 2520 |
let t1 = math::sin(theta * (1. - s)); |
| 2521 |
let t2 = math::sin(theta * s); |
| 2522 |
|
| 2523 |
// Interpolate vector lengths |
| 2524 |
let result_length = self_length.lerp(rhs_length, s); |
| 2525 |
// Scale the vectors to the target length and interpolate them |
| 2526 |
return (self * (result_length / self_length) * t1 |
| 2527 |
+ rhs * (result_length / rhs_length) * t2) |
| 2528 |
* sin_theta.recip(); |
| 2529 |
} |
| 2530 |
if dot < 0.0 { |
| 2531 |
// Vectors are almost parallel in opposing directions |
| 2532 |
|
| 2533 |
// Create a rotation from self to rhs along some axis |
| 2534 |
let axis = self.any_orthogonal_vector().normalize(){% if is_align %}.into(){% endif %}; |
| 2535 |
let rotation = {{ quat_t }}::from_axis_angle(axis, core::{{ scalar_t }}::consts::PI * s); |
| 2536 |
// Interpolate vector lengths |
| 2537 |
let result_length = self_length.lerp(rhs_length, s); |
| 2538 |
rotation * self * (result_length / self_length) |
| 2539 |
} else { |
| 2540 |
// Vectors are almost parallel in the same direction, or dot was NaN |
| 2541 |
self.lerp(rhs, s) |
| 2542 |
} |
| 2543 |
} |
| 2544 |
{% endif %} |
| 2545 |
{% endif %} |
| 2546 |
|
| 2547 |
{% if not is_float %} |
| 2548 |
|
| 2549 |
/// Computes the [manhattan distance] between two points. |
| 2550 |
/// |
| 2551 |
/// # Overflow |
| 2552 |
/// This method may overflow if the result is greater than [`{{ unsigned_scalar_t }}::MAX`]. |
| 2553 |
/// |
| 2554 |
/// See also [`checked_manhattan_distance`][{{ self_t }}::checked_manhattan_distance]. |
| 2555 |
/// |
| 2556 |
/// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry |
| 2557 |
#[inline] |
| 2558 |
#[must_use] |
| 2559 |
pub fn manhattan_distance(self, rhs: Self) -> {{ unsigned_scalar_t }} { |
| 2560 |
{% for c in components %} |
| 2561 |
{% if not loop.first %} + {% endif %} |
| 2562 |
self.{{c}}.abs_diff(rhs.{{c}}) |
| 2563 |
{% endfor %} |
| 2564 |
} |
| 2565 |
|
| 2566 |
/// Computes the [manhattan distance] between two points. |
| 2567 |
/// |
| 2568 |
/// This will returns [`None`] if the result is greater than [`{{ unsigned_scalar_t }}::MAX`]. |
| 2569 |
/// |
| 2570 |
/// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry |
| 2571 |
#[inline] |
| 2572 |
#[must_use] |
| 2573 |
pub fn checked_manhattan_distance(self, rhs: Self) -> Option<{{ unsigned_scalar_t }}> { |
| 2574 |
{% for c in components %} |
| 2575 |
{%- if loop.first -%} |
| 2576 |
let d = self.{{c}}.abs_diff(rhs.{{c}}); |
| 2577 |
{%- elif loop.last -%} |
| 2578 |
d.checked_add(self.{{c}}.abs_diff(rhs.{{c}})) |
| 2579 |
{%- else -%} |
| 2580 |
let d = d.checked_add(self.{{c}}.abs_diff(rhs.{{c}}))?; |
| 2581 |
{%- endif -%} |
| 2582 |
{% endfor %} |
| 2583 |
} |
| 2584 |
|
| 2585 |
/// Computes the [chebyshev distance] between two points. |
| 2586 |
/// |
| 2587 |
/// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance |
| 2588 |
#[inline] |
| 2589 |
#[must_use] |
| 2590 |
pub fn chebyshev_distance(self, rhs: Self) -> {{ unsigned_scalar_t }} { |
| 2591 |
// Note: the compiler will eventually optimize out the loop |
| 2592 |
[ |
| 2593 |
{% for c in components %} |
| 2594 |
self.{{c}}.abs_diff(rhs.{{c}}), |
| 2595 |
{% endfor %} |
| 2596 |
].into_iter().max().unwrap() |
| 2597 |
} |
| 2598 |
|
| 2599 |
{% endif %} |
| 2600 |
|
| 2601 |
|
| 2602 |
{% if is_signed and dim == 2 %} |
| 2603 |
/// Returns a vector that is equal to `self` rotated by 90 degrees. |
| 2604 |
#[inline] |
| 2605 |
#[must_use] |
| 2606 |
pub fn perp(self) -> Self { |
| 2607 |
Self { |
| 2608 |
x: -self.y, |
| 2609 |
y: self.x, |
| 2610 |
} |
| 2611 |
} |
| 2612 |
|
| 2613 |
/// The perpendicular dot product of `self` and `rhs`. |
| 2614 |
/// Also known as the wedge product, 2D cross product, and determinant. |
| 2615 |
#[doc(alias = "wedge")] |
| 2616 |
#[doc(alias = "cross")] |
| 2617 |
#[doc(alias = "determinant")] |
| 2618 |
#[inline] |
| 2619 |
#[must_use] |
| 2620 |
pub fn perp_dot(self, rhs: Self) -> {{ scalar_t }} { |
| 2621 |
(self.x * rhs.y) - (self.y * rhs.x) |
| 2622 |
} |
| 2623 |
|
| 2624 |
/// Returns `rhs` rotated by the angle of `self`. If `self` is normalized, |
| 2625 |
/// then this just rotation. This is what you usually want. Otherwise, |
| 2626 |
/// it will be like a rotation with a multiplication by `self`'s length. |
| 2627 |
{%- if is_float %} |
| 2628 |
/// |
| 2629 |
/// This can be used in conjunction with the [`from_angle()`][Self::from_angle()] method, e.g. |
| 2630 |
/// `{{ vec2_t }}::from_angle(PI).rotate({{ vec2_t }}::Y)` will create the vector `[-1, 0]` |
| 2631 |
/// and rotate [`{{ vec2_t }}::Y`] around it returning `-{{ vec2_t }}::Y`. |
| 2632 |
{%- else %} |
| 2633 |
/// |
| 2634 |
/// This can be used to rotate by 90 degree increments, e.g. `[sin(90), cos(90)` = `[1, 0]` or |
| 2635 |
/// `[sin(180), cos(180)]` = `[0, -1]`. |
| 2636 |
{%- endif %} |
| 2637 |
#[inline] |
| 2638 |
#[must_use] |
| 2639 |
pub fn rotate(self, rhs: Self) -> Self { |
| 2640 |
Self { |
| 2641 |
x: self.x * rhs.x - self.y * rhs.y, |
| 2642 |
y: self.y * rhs.x + self.x * rhs.y, |
| 2643 |
} |
| 2644 |
} |
| 2645 |
{% endif %} |
| 2646 |
|
| 2647 |
{% if is_signed and is_float and dim == 2 %} |
| 2648 |
/// Rotates towards `rhs` up to `max_angle` (in radians). |
| 2649 |
/// |
| 2650 |
/// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to |
| 2651 |
/// `self.angle_between(rhs)`, the result will be parallel to `rhs`. If `max_angle` is negative, |
| 2652 |
/// rotates towards the exact opposite of `rhs`. Will not go past the target. |
| 2653 |
#[inline] |
| 2654 |
#[must_use] |
| 2655 |
pub fn rotate_towards(&self, rhs: Self, max_angle: {{ scalar_t }}) -> Self { |
| 2656 |
let a = self.angle_to(rhs); |
| 2657 |
let abs_a = math::abs(a); |
| 2658 |
// When `max_angle < 0`, rotate no further than `PI` radians away |
| 2659 |
let angle = max_angle.clamp(abs_a - core::{{ scalar_t }}::consts::PI, abs_a) * math::signum(a); |
| 2660 |
Self::from_angle(angle).rotate(*self) |
| 2661 |
} |
| 2662 |
{% endif %} |
| 2663 |
|
| 2664 |
{% if scalar_t != "f32" %} |
| 2665 |
{% if dim == 2 %} |
| 2666 |
/// Casts all elements of `self` to `f32`. |
| 2667 |
#[inline] |
| 2668 |
#[must_use] |
| 2669 |
pub fn as_vec2(&self) -> crate::Vec2 { |
| 2670 |
crate::Vec2::new(self.x as f32, self.y as f32) |
| 2671 |
} |
| 2672 |
{% elif dim == 3 %} |
| 2673 |
/// Casts all elements of `self` to `f32`. |
| 2674 |
#[inline] |
| 2675 |
#[must_use] |
| 2676 |
pub fn as_vec3(&self) -> crate::Vec3 { |
| 2677 |
crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32) |
| 2678 |
} |
| 2679 |
|
| 2680 |
/// Casts all elements of `self` to `f32`. |
| 2681 |
#[inline] |
| 2682 |
#[must_use] |
| 2683 |
pub fn as_vec3a(&self) -> crate::Vec3A { |
| 2684 |
crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32) |
| 2685 |
} |
| 2686 |
{% elif dim == 4 %} |
| 2687 |
/// Casts all elements of `self` to `f32`. |
| 2688 |
#[inline] |
| 2689 |
#[must_use] |
| 2690 |
pub fn as_vec4(&self) -> crate::Vec4 { |
| 2691 |
crate::Vec4::new(self.x as f32, self.y as f32, self.z as f32, self.w as f32) |
| 2692 |
} |
| 2693 |
{% endif %} |
| 2694 |
{% endif %} |
| 2695 |
{% if scalar_t != "f64" %} |
| 2696 |
{% if dim == 2 %} |
| 2697 |
/// Casts all elements of `self` to `f64`. |
| 2698 |
#[inline] |
| 2699 |
#[must_use] |
| 2700 |
pub fn as_dvec2(&self) -> crate::DVec2 { |
| 2701 |
crate::DVec2::new(self.x as f64, self.y as f64) |
| 2702 |
} |
| 2703 |
{% elif dim == 3 %} |
| 2704 |
/// Casts all elements of `self` to `f64`. |
| 2705 |
#[inline] |
| 2706 |
#[must_use] |
| 2707 |
pub fn as_dvec3(&self) -> crate::DVec3 { |
| 2708 |
crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64) |
| 2709 |
} |
| 2710 |
{% elif dim == 4 %} |
| 2711 |
/// Casts all elements of `self` to `f64`. |
| 2712 |
#[inline] |
| 2713 |
#[must_use] |
| 2714 |
pub fn as_dvec4(&self) -> crate::DVec4 { |
| 2715 |
crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64) |
| 2716 |
} |
| 2717 |
{% endif %} |
| 2718 |
{% endif %} |
| 2719 |
{% if scalar_t != "i8" %} |
| 2720 |
{% if dim == 2 %} |
| 2721 |
/// Casts all elements of `self` to `i8`. |
| 2722 |
#[inline] |
| 2723 |
#[must_use] |
| 2724 |
pub fn as_i8vec2(&self) -> crate::I8Vec2 { |
| 2725 |
crate::I8Vec2::new(self.x as i8, self.y as i8) |
| 2726 |
} |
| 2727 |
{% elif dim == 3 %} |
| 2728 |
/// Casts all elements of `self` to `i8`. |
| 2729 |
#[inline] |
| 2730 |
#[must_use] |
| 2731 |
pub fn as_i8vec3(&self) -> crate::I8Vec3 { |
| 2732 |
crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8) |
| 2733 |
} |
| 2734 |
{% elif dim == 4 %} |
| 2735 |
/// Casts all elements of `self` to `i8`. |
| 2736 |
#[inline] |
| 2737 |
#[must_use] |
| 2738 |
pub fn as_i8vec4(&self) -> crate::I8Vec4 { |
| 2739 |
crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8) |
| 2740 |
} |
| 2741 |
{% endif %} |
| 2742 |
{% endif %} |
| 2743 |
{% if scalar_t != "u8" %} |
| 2744 |
{% if dim == 2 %} |
| 2745 |
/// Casts all elements of `self` to `u8`. |
| 2746 |
#[inline] |
| 2747 |
#[must_use] |
| 2748 |
pub fn as_u8vec2(&self) -> crate::U8Vec2 { |
| 2749 |
crate::U8Vec2::new(self.x as u8, self.y as u8) |
| 2750 |
} |
| 2751 |
{% elif dim == 3 %} |
| 2752 |
/// Casts all elements of `self` to `u8`. |
| 2753 |
#[inline] |
| 2754 |
#[must_use] |
| 2755 |
pub fn as_u8vec3(&self) -> crate::U8Vec3 { |
| 2756 |
crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8) |
| 2757 |
} |
| 2758 |
{% elif dim == 4 %} |
| 2759 |
/// Casts all elements of `self` to `u8`. |
| 2760 |
#[inline] |
| 2761 |
#[must_use] |
| 2762 |
pub fn as_u8vec4(&self) -> crate::U8Vec4 { |
| 2763 |
crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8) |
| 2764 |
} |
| 2765 |
{% endif %} |
| 2766 |
{% endif %} |
| 2767 |
{% if scalar_t != "i16" %} |
| 2768 |
{% if dim == 2 %} |
| 2769 |
/// Casts all elements of `self` to `i16`. |
| 2770 |
#[inline] |
| 2771 |
#[must_use] |
| 2772 |
pub fn as_i16vec2(&self) -> crate::I16Vec2 { |
| 2773 |
crate::I16Vec2::new(self.x as i16, self.y as i16) |
| 2774 |
} |
| 2775 |
{% elif dim == 3 %} |
| 2776 |
/// Casts all elements of `self` to `i16`. |
| 2777 |
#[inline] |
| 2778 |
#[must_use] |
| 2779 |
pub fn as_i16vec3(&self) -> crate::I16Vec3 { |
| 2780 |
crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16) |
| 2781 |
} |
| 2782 |
{% elif dim == 4 %} |
| 2783 |
/// Casts all elements of `self` to `i16`. |
| 2784 |
#[inline] |
| 2785 |
#[must_use] |
| 2786 |
pub fn as_i16vec4(&self) -> crate::I16Vec4 { |
| 2787 |
crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16) |
| 2788 |
} |
| 2789 |
{% endif %} |
| 2790 |
{% endif %} |
| 2791 |
{% if scalar_t != "u16" %} |
| 2792 |
{% if dim == 2 %} |
| 2793 |
/// Casts all elements of `self` to `u16`. |
| 2794 |
#[inline] |
| 2795 |
#[must_use] |
| 2796 |
pub fn as_u16vec2(&self) -> crate::U16Vec2 { |
| 2797 |
crate::U16Vec2::new(self.x as u16, self.y as u16) |
| 2798 |
} |
| 2799 |
{% elif dim == 3 %} |
| 2800 |
/// Casts all elements of `self` to `u16`. |
| 2801 |
#[inline] |
| 2802 |
#[must_use] |
| 2803 |
pub fn as_u16vec3(&self) -> crate::U16Vec3 { |
| 2804 |
crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16) |
| 2805 |
} |
| 2806 |
{% elif dim == 4 %} |
| 2807 |
/// Casts all elements of `self` to `u16`. |
| 2808 |
#[inline] |
| 2809 |
#[must_use] |
| 2810 |
pub fn as_u16vec4(&self) -> crate::U16Vec4 { |
| 2811 |
crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16) |
| 2812 |
} |
| 2813 |
{% endif %} |
| 2814 |
{% endif %} |
| 2815 |
{% if scalar_t != "i32" %} |
| 2816 |
{% if dim == 2 %} |
| 2817 |
/// Casts all elements of `self` to `i32`. |
| 2818 |
#[inline] |
| 2819 |
#[must_use] |
| 2820 |
pub fn as_ivec2(&self) -> crate::IVec2 { |
| 2821 |
crate::IVec2::new(self.x as i32, self.y as i32) |
| 2822 |
} |
| 2823 |
{% elif dim == 3 %} |
| 2824 |
/// Casts all elements of `self` to `i32`. |
| 2825 |
#[inline] |
| 2826 |
#[must_use] |
| 2827 |
pub fn as_ivec3(&self) -> crate::IVec3 { |
| 2828 |
crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32) |
| 2829 |
} |
| 2830 |
{% elif dim == 4 %} |
| 2831 |
/// Casts all elements of `self` to `i32`. |
| 2832 |
#[inline] |
| 2833 |
#[must_use] |
| 2834 |
pub fn as_ivec4(&self) -> crate::IVec4 { |
| 2835 |
crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32) |
| 2836 |
} |
| 2837 |
{% endif %} |
| 2838 |
{% endif %} |
| 2839 |
{% if scalar_t != "u32" %} |
| 2840 |
{% if dim == 2 %} |
| 2841 |
/// Casts all elements of `self` to `u32`. |
| 2842 |
#[inline] |
| 2843 |
#[must_use] |
| 2844 |
pub fn as_uvec2(&self) -> crate::UVec2 { |
| 2845 |
crate::UVec2::new(self.x as u32, self.y as u32) |
| 2846 |
} |
| 2847 |
{% elif dim == 3 %} |
| 2848 |
/// Casts all elements of `self` to `u32`. |
| 2849 |
#[inline] |
| 2850 |
#[must_use] |
| 2851 |
pub fn as_uvec3(&self) -> crate::UVec3 { |
| 2852 |
crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32) |
| 2853 |
} |
| 2854 |
{% elif dim == 4 %} |
| 2855 |
/// Casts all elements of `self` to `u32`. |
| 2856 |
#[inline] |
| 2857 |
#[must_use] |
| 2858 |
pub fn as_uvec4(&self) -> crate::UVec4 { |
| 2859 |
crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32) |
| 2860 |
} |
| 2861 |
{% endif %} |
| 2862 |
{% endif %} |
| 2863 |
{% if scalar_t != "i64" %} |
| 2864 |
{% if dim == 2 %} |
| 2865 |
/// Casts all elements of `self` to `i64`. |
| 2866 |
#[inline] |
| 2867 |
#[must_use] |
| 2868 |
pub fn as_i64vec2(&self) -> crate::I64Vec2 { |
| 2869 |
crate::I64Vec2::new(self.x as i64, self.y as i64) |
| 2870 |
} |
| 2871 |
{% elif dim == 3 %} |
| 2872 |
/// Casts all elements of `self` to `i64`. |
| 2873 |
#[inline] |
| 2874 |
#[must_use] |
| 2875 |
pub fn as_i64vec3(&self) -> crate::I64Vec3 { |
| 2876 |
crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64) |
| 2877 |
} |
| 2878 |
{% elif dim == 4 %} |
| 2879 |
/// Casts all elements of `self` to `i64`. |
| 2880 |
#[inline] |
| 2881 |
#[must_use] |
| 2882 |
pub fn as_i64vec4(&self) -> crate::I64Vec4 { |
| 2883 |
crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64) |
| 2884 |
} |
| 2885 |
{% endif %} |
| 2886 |
{% endif %} |
| 2887 |
{% if scalar_t != "u64" %} |
| 2888 |
{% if dim == 2 %} |
| 2889 |
/// Casts all elements of `self` to `u64`. |
| 2890 |
#[inline] |
| 2891 |
#[must_use] |
| 2892 |
pub fn as_u64vec2(&self) -> crate::U64Vec2 { |
| 2893 |
crate::U64Vec2::new(self.x as u64, self.y as u64) |
| 2894 |
} |
| 2895 |
{% elif dim == 3 %} |
| 2896 |
/// Casts all elements of `self` to `u64`. |
| 2897 |
#[inline] |
| 2898 |
#[must_use] |
| 2899 |
pub fn as_u64vec3(&self) -> crate::U64Vec3 { |
| 2900 |
crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64) |
| 2901 |
} |
| 2902 |
{% elif dim == 4 %} |
| 2903 |
/// Casts all elements of `self` to `u64`. |
| 2904 |
#[inline] |
| 2905 |
#[must_use] |
| 2906 |
pub fn as_u64vec4(&self) -> crate::U64Vec4 { |
| 2907 |
crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64) |
| 2908 |
} |
| 2909 |
{% endif %} |
| 2910 |
{% endif %} |
| 2911 |
{% if scalar_t != "usize" %} |
| 2912 |
{% if dim == 2 %} |
| 2913 |
/// Casts all elements of `self` to `usize`. |
| 2914 |
#[inline] |
| 2915 |
#[must_use] |
| 2916 |
pub fn as_usizevec2(&self) -> crate::USizeVec2 { |
| 2917 |
crate::USizeVec2::new(self.x as usize, self.y as usize) |
| 2918 |
} |
| 2919 |
{% elif dim == 3 %} |
| 2920 |
/// Casts all elements of `self` to `usize`. |
| 2921 |
#[inline] |
| 2922 |
#[must_use] |
| 2923 |
pub fn as_usizevec3(&self) -> crate::USizeVec3 { |
| 2924 |
crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize) |
| 2925 |
} |
| 2926 |
{% elif dim == 4 %} |
| 2927 |
/// Casts all elements of `self` to `usize`. |
| 2928 |
#[inline] |
| 2929 |
#[must_use] |
| 2930 |
pub fn as_usizevec4(&self) -> crate::USizeVec4 { |
| 2931 |
crate::USizeVec4::new(self.x as usize, self.y as usize, self.z as usize, self.w as usize) |
| 2932 |
} |
| 2933 |
{% endif %} |
| 2934 |
{% endif %} |
| 2935 |
|
| 2936 |
{% if not is_float %} |
| 2937 |
/// Returns a vector containing the wrapping addition of `self` and `rhs`. |
| 2938 |
/// |
| 2939 |
/// In other words this computes `Some([self.x + rhs.x, self.y + rhs.y, ..])` but returns `None` on any overflow. |
| 2940 |
#[inline] |
| 2941 |
#[must_use] |
| 2942 |
pub const fn checked_add(self, rhs: Self) -> Option<Self> { |
| 2943 |
{% for c in components %} |
| 2944 |
let {{ c }} = match self.{{ c }}.checked_add(rhs.{{ c }}) { Some(v) => v, None => {return None} }; |
| 2945 |
{%- endfor %} |
| 2946 |
|
| 2947 |
Some(Self { |
| 2948 |
{% for c in components %} |
| 2949 |
{{ c }}, |
| 2950 |
{%- endfor %} |
| 2951 |
}) |
| 2952 |
} |
| 2953 |
|
| 2954 |
/// Returns a vector containing the wrapping subtraction of `self` and `rhs`. |
| 2955 |
/// |
| 2956 |
/// In other words this computes `Some([self.x - rhs.x, self.y - rhs.y, ..])` but returns `None` on any overflow. |
| 2957 |
#[inline] |
| 2958 |
#[must_use] |
| 2959 |
pub const fn checked_sub(self, rhs: Self) -> Option<Self> { |
| 2960 |
{% for c in components %} |
| 2961 |
let {{ c }} = match self.{{ c }}.checked_sub(rhs.{{ c }}) { Some(v) => v, None => {return None} }; |
| 2962 |
{%- endfor %} |
| 2963 |
|
| 2964 |
Some(Self { |
| 2965 |
{% for c in components %} |
| 2966 |
{{ c }}, |
| 2967 |
{%- endfor %} |
| 2968 |
}) |
| 2969 |
} |
| 2970 |
|
| 2971 |
/// Returns a vector containing the wrapping multiplication of `self` and `rhs`. |
| 2972 |
/// |
| 2973 |
/// In other words this computes `Some([self.x * rhs.x, self.y * rhs.y, ..])` but returns `None` on any overflow. |
| 2974 |
#[inline] |
| 2975 |
#[must_use] |
| 2976 |
pub const fn checked_mul(self, rhs: Self) -> Option<Self> { |
| 2977 |
{% for c in components %} |
| 2978 |
let {{ c }} = match self.{{ c }}.checked_mul(rhs.{{ c }}) { Some(v) => v, None => {return None} }; |
| 2979 |
{%- endfor %} |
| 2980 |
|
| 2981 |
Some(Self { |
| 2982 |
{% for c in components %} |
| 2983 |
{{ c }}, |
| 2984 |
{%- endfor %} |
| 2985 |
}) |
| 2986 |
} |
| 2987 |
|
| 2988 |
/// Returns a vector containing the wrapping division of `self` and `rhs`. |
| 2989 |
/// |
| 2990 |
/// In other words this computes `Some([self.x / rhs.x, self.y / rhs.y, ..])` but returns `None` on any division by zero. |
| 2991 |
#[inline] |
| 2992 |
#[must_use] |
| 2993 |
pub const fn checked_div(self, rhs: Self) -> Option<Self> { |
| 2994 |
{% for c in components %} |
| 2995 |
let {{ c }} = match self.{{ c }}.checked_div(rhs.{{ c }}) { Some(v) => v, None => {return None} }; |
| 2996 |
{%- endfor %} |
| 2997 |
|
| 2998 |
Some(Self { |
| 2999 |
{% for c in components %} |
| 3000 |
{{ c }}, |
| 3001 |
{%- endfor %} |
| 3002 |
}) |
| 3003 |
} |
| 3004 |
|
| 3005 |
/// Returns a vector containing the wrapping addition of `self` and `rhs`. |
| 3006 |
/// |
| 3007 |
/// In other words this computes `[self.x.wrapping_add(rhs.x), self.y.wrapping_add(rhs.y), ..]`. |
| 3008 |
#[inline] |
| 3009 |
#[must_use] |
| 3010 |
pub const fn wrapping_add(self, rhs: Self) -> Self { |
| 3011 |
Self { |
| 3012 |
{% for c in components %} |
| 3013 |
{{ c }}: self.{{ c }}.wrapping_add(rhs.{{ c }}), |
| 3014 |
{%- endfor %} |
| 3015 |
} |
| 3016 |
} |
| 3017 |
|
| 3018 |
/// Returns a vector containing the wrapping subtraction of `self` and `rhs`. |
| 3019 |
/// |
| 3020 |
/// In other words this computes `[self.x.wrapping_sub(rhs.x), self.y.wrapping_sub(rhs.y), ..]`. |
| 3021 |
#[inline] |
| 3022 |
#[must_use] |
| 3023 |
pub const fn wrapping_sub(self, rhs: Self) -> Self { |
| 3024 |
Self { |
| 3025 |
{% for c in components %} |
| 3026 |
{{ c }}: self.{{ c }}.wrapping_sub(rhs.{{ c }}), |
| 3027 |
{%- endfor %} |
| 3028 |
} |
| 3029 |
} |
| 3030 |
|
| 3031 |
/// Returns a vector containing the wrapping multiplication of `self` and `rhs`. |
| 3032 |
/// |
| 3033 |
/// In other words this computes `[self.x.wrapping_mul(rhs.x), self.y.wrapping_mul(rhs.y), ..]`. |
| 3034 |
#[inline] |
| 3035 |
#[must_use] |
| 3036 |
pub const fn wrapping_mul(self, rhs: Self) -> Self { |
| 3037 |
Self { |
| 3038 |
{% for c in components %} |
| 3039 |
{{ c }}: self.{{ c }}.wrapping_mul(rhs.{{ c }}), |
| 3040 |
{%- endfor %} |
| 3041 |
} |
| 3042 |
} |
| 3043 |
|
| 3044 |
/// Returns a vector containing the wrapping division of `self` and `rhs`. |
| 3045 |
/// |
| 3046 |
/// In other words this computes `[self.x.wrapping_div(rhs.x), self.y.wrapping_div(rhs.y), ..]`. |
| 3047 |
#[inline] |
| 3048 |
#[must_use] |
| 3049 |
pub const fn wrapping_div(self, rhs: Self) -> Self { |
| 3050 |
Self { |
| 3051 |
{% for c in components %} |
| 3052 |
{{ c }}: self.{{ c }}.wrapping_div(rhs.{{ c }}), |
| 3053 |
{%- endfor %} |
| 3054 |
} |
| 3055 |
} |
| 3056 |
|
| 3057 |
/// Returns a vector containing the saturating addition of `self` and `rhs`. |
| 3058 |
/// |
| 3059 |
/// In other words this computes `[self.x.saturating_add(rhs.x), self.y.saturating_add(rhs.y), ..]`. |
| 3060 |
#[inline] |
| 3061 |
#[must_use] |
| 3062 |
pub const fn saturating_add(self, rhs: Self) -> Self { |
| 3063 |
Self { |
| 3064 |
{% for c in components %} |
| 3065 |
{{ c }}: self.{{ c }}.saturating_add(rhs.{{ c }}), |
| 3066 |
{%- endfor %} |
| 3067 |
} |
| 3068 |
} |
| 3069 |
|
| 3070 |
/// Returns a vector containing the saturating subtraction of `self` and `rhs`. |
| 3071 |
/// |
| 3072 |
/// In other words this computes `[self.x.saturating_sub(rhs.x), self.y.saturating_sub(rhs.y), ..]`. |
| 3073 |
#[inline] |
| 3074 |
#[must_use] |
| 3075 |
pub const fn saturating_sub(self, rhs: Self) -> Self { |
| 3076 |
Self { |
| 3077 |
{% for c in components %} |
| 3078 |
{{ c }}: self.{{ c }}.saturating_sub(rhs.{{ c }}), |
| 3079 |
{%- endfor %} |
| 3080 |
} |
| 3081 |
} |
| 3082 |
|
| 3083 |
/// Returns a vector containing the saturating multiplication of `self` and `rhs`. |
| 3084 |
/// |
| 3085 |
/// In other words this computes `[self.x.saturating_mul(rhs.x), self.y.saturating_mul(rhs.y), ..]`. |
| 3086 |
#[inline] |
| 3087 |
#[must_use] |
| 3088 |
pub const fn saturating_mul(self, rhs: Self) -> Self { |
| 3089 |
Self { |
| 3090 |
{% for c in components %} |
| 3091 |
{{ c }}: self.{{ c }}.saturating_mul(rhs.{{ c }}), |
| 3092 |
{%- endfor %} |
| 3093 |
} |
| 3094 |
} |
| 3095 |
|
| 3096 |
/// Returns a vector containing the saturating division of `self` and `rhs`. |
| 3097 |
/// |
| 3098 |
/// In other words this computes `[self.x.saturating_div(rhs.x), self.y.saturating_div(rhs.y), ..]`. |
| 3099 |
#[inline] |
| 3100 |
#[must_use] |
| 3101 |
pub const fn saturating_div(self, rhs: Self) -> Self { |
| 3102 |
Self { |
| 3103 |
{% for c in components %} |
| 3104 |
{{ c }}: self.{{ c }}.saturating_div(rhs.{{ c }}), |
| 3105 |
{%- endfor %} |
| 3106 |
} |
| 3107 |
} |
| 3108 |
|
| 3109 |
{% if scalar_t != "usize" %} |
| 3110 |
{% if is_signed %} |
| 3111 |
/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`. |
| 3112 |
/// |
| 3113 |
/// In other words this computes `Some([self.x + rhs.x, self.y + rhs.y, ..])` but returns `None` on any overflow. |
| 3114 |
#[inline] |
| 3115 |
#[must_use] |
| 3116 |
pub const fn checked_add_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Option<Self> { |
| 3117 |
{% for c in components %} |
| 3118 |
let {{ c }} = match self.{{ c }}.checked_add_unsigned(rhs.{{ c }}) { Some(v) => v, None => {return None} }; |
| 3119 |
{%- endfor %} |
| 3120 |
|
| 3121 |
Some(Self { |
| 3122 |
{% for c in components %} |
| 3123 |
{{ c }}, |
| 3124 |
{%- endfor %} |
| 3125 |
}) |
| 3126 |
} |
| 3127 |
|
| 3128 |
/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`. |
| 3129 |
/// |
| 3130 |
/// In other words this computes `Some([self.x - rhs.x, self.y - rhs.y, ..])` but returns `None` on any overflow. |
| 3131 |
#[inline] |
| 3132 |
#[must_use] |
| 3133 |
pub const fn checked_sub_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Option<Self> { |
| 3134 |
{% for c in components %} |
| 3135 |
let {{ c }} = match self.{{ c }}.checked_sub_unsigned(rhs.{{ c }}) { Some(v) => v, None => {return None} }; |
| 3136 |
{%- endfor %} |
| 3137 |
|
| 3138 |
Some(Self { |
| 3139 |
{% for c in components %} |
| 3140 |
{{ c }}, |
| 3141 |
{%- endfor %} |
| 3142 |
}) |
| 3143 |
} |
| 3144 |
|
| 3145 |
/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`. |
| 3146 |
/// |
| 3147 |
/// In other words this computes `[self.x.wrapping_add_unsigned(rhs.x), self.y.wrapping_add_unsigned(rhs.y), ..]`. |
| 3148 |
#[inline] |
| 3149 |
#[must_use] |
| 3150 |
pub const fn wrapping_add_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Self { |
| 3151 |
Self { |
| 3152 |
{% for c in components %} |
| 3153 |
{{ c }}: self.{{ c }}.wrapping_add_unsigned(rhs.{{ c }}), |
| 3154 |
{%- endfor %} |
| 3155 |
} |
| 3156 |
} |
| 3157 |
|
| 3158 |
/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`. |
| 3159 |
/// |
| 3160 |
/// In other words this computes `[self.x.wrapping_sub_unsigned(rhs.x), self.y.wrapping_sub_unsigned(rhs.y), ..]`. |
| 3161 |
#[inline] |
| 3162 |
#[must_use] |
| 3163 |
pub const fn wrapping_sub_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Self { |
| 3164 |
Self { |
| 3165 |
{% for c in components %} |
| 3166 |
{{ c }}: self.{{ c }}.wrapping_sub_unsigned(rhs.{{ c }}), |
| 3167 |
{%- endfor %} |
| 3168 |
} |
| 3169 |
} |
| 3170 |
|
| 3171 |
// Returns a vector containing the saturating addition of `self` and unsigned vector `rhs`. |
| 3172 |
/// |
| 3173 |
/// In other words this computes `[self.x.saturating_add_unsigned(rhs.x), self.y.saturating_add_unsigned(rhs.y), ..]`. |
| 3174 |
#[inline] |
| 3175 |
#[must_use] |
| 3176 |
pub const fn saturating_add_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Self { |
| 3177 |
Self { |
| 3178 |
{% for c in components %} |
| 3179 |
{{ c }}: self.{{ c }}.saturating_add_unsigned(rhs.{{ c }}), |
| 3180 |
{%- endfor %} |
| 3181 |
} |
| 3182 |
} |
| 3183 |
|
| 3184 |
/// Returns a vector containing the saturating subtraction of `self` and unsigned vector `rhs`. |
| 3185 |
/// |
| 3186 |
/// In other words this computes `[self.x.saturating_sub_unsigned(rhs.x), self.y.saturating_sub_unsigned(rhs.y), ..]`. |
| 3187 |
#[inline] |
| 3188 |
#[must_use] |
| 3189 |
pub const fn saturating_sub_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Self { |
| 3190 |
Self { |
| 3191 |
{% for c in components %} |
| 3192 |
{{ c }}: self.{{ c }}.saturating_sub_unsigned(rhs.{{ c }}), |
| 3193 |
{%- endfor %} |
| 3194 |
} |
| 3195 |
} |
| 3196 |
{% else %} |
| 3197 |
/// Returns a vector containing the wrapping addition of `self` and signed vector `rhs`. |
| 3198 |
/// |
| 3199 |
/// In other words this computes `Some([self.x + rhs.x, self.y + rhs.y, ..])` but returns `None` on any overflow. |
| 3200 |
#[inline] |
| 3201 |
#[must_use] |
| 3202 |
pub const fn checked_add_signed(self, rhs: {{ opposite_signedness_t }}) -> Option<Self> { |
| 3203 |
{% for c in components %} |
| 3204 |
let {{ c }} = match self.{{ c }}.checked_add_signed(rhs.{{ c }}) { Some(v) => v, None => {return None} }; |
| 3205 |
{%- endfor %} |
| 3206 |
|
| 3207 |
Some(Self { |
| 3208 |
{% for c in components %} |
| 3209 |
{{ c }}, |
| 3210 |
{%- endfor %} |
| 3211 |
}) |
| 3212 |
} |
| 3213 |
|
| 3214 |
/// Returns a vector containing the wrapping addition of `self` and signed vector `rhs`. |
| 3215 |
/// |
| 3216 |
/// In other words this computes `[self.x.wrapping_add_signed(rhs.x), self.y.wrapping_add_signed(rhs.y), ..]`. |
| 3217 |
#[inline] |
| 3218 |
#[must_use] |
| 3219 |
pub const fn wrapping_add_signed(self, rhs: {{ opposite_signedness_t }}) -> Self { |
| 3220 |
Self { |
| 3221 |
{% for c in components %} |
| 3222 |
{{ c }}: self.{{ c }}.wrapping_add_signed(rhs.{{ c }}), |
| 3223 |
{%- endfor %} |
| 3224 |
} |
| 3225 |
} |
| 3226 |
|
| 3227 |
/// Returns a vector containing the saturating addition of `self` and signed vector `rhs`. |
| 3228 |
/// |
| 3229 |
/// In other words this computes `[self.x.saturating_add_signed(rhs.x), self.y.saturating_add_signed(rhs.y), ..]`. |
| 3230 |
#[inline] |
| 3231 |
#[must_use] |
| 3232 |
pub const fn saturating_add_signed(self, rhs: {{ opposite_signedness_t }}) -> Self { |
| 3233 |
Self { |
| 3234 |
{% for c in components %} |
| 3235 |
{{ c }}: self.{{ c }}.saturating_add_signed(rhs.{{ c }}), |
| 3236 |
{%- endfor %} |
| 3237 |
} |
| 3238 |
} |
| 3239 |
{% endif %} |
| 3240 |
{% endif %} |
| 3241 |
{% endif %} |
| 3242 |
} |
| 3243 |
|
| 3244 |
impl Default for {{ self_t }} { |
| 3245 |
#[inline(always)] |
| 3246 |
fn default() -> Self { |
| 3247 |
Self::ZERO |
| 3248 |
} |
| 3249 |
} |
| 3250 |
|
| 3251 |
{% if not is_scalar %} |
| 3252 |
impl PartialEq for {{ self_t }} { |
| 3253 |
#[inline] |
| 3254 |
fn eq(&self, rhs: &Self) -> bool { |
| 3255 |
self.cmpeq(*rhs).all() |
| 3256 |
} |
| 3257 |
} |
| 3258 |
{% endif %} |
| 3259 |
|
| 3260 |
impl Div for {{ self_t }} { |
| 3261 |
type Output = Self; |
| 3262 |
#[inline] |
| 3263 |
fn div(self, rhs: Self) -> Self { |
| 3264 |
{% if is_scalar %} |
| 3265 |
Self { |
| 3266 |
{% for c in components %} |
| 3267 |
{{ c }}: self.{{ c }}.div(rhs.{{ c }}), |
| 3268 |
{%- endfor %} |
| 3269 |
} |
| 3270 |
{% elif is_sse2 %} |
| 3271 |
Self(unsafe { _mm_div_ps(self.0, rhs.0) }) |
| 3272 |
{% elif is_wasm32 %} |
| 3273 |
Self(f32x4_div(self.0, rhs.0)) |
| 3274 |
{% elif is_coresimd %} |
| 3275 |
Self(self.0 / rhs.0) |
| 3276 |
{% elif is_neon %} |
| 3277 |
Self(unsafe { vdivq_f32(self.0, rhs.0) }) |
| 3278 |
{% else %} |
| 3279 |
unimplemented!() |
| 3280 |
{% endif %} |
| 3281 |
} |
| 3282 |
} |
| 3283 |
|
| 3284 |
{{ macros::impl_ref_binop(imp="Div", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 3285 |
|
| 3286 |
impl DivAssign for {{ self_t }} { |
| 3287 |
#[inline] |
| 3288 |
fn div_assign(&mut self, rhs: Self) { |
| 3289 |
{% if is_scalar %} |
| 3290 |
{% for c in components %} |
| 3291 |
self.{{ c }}.div_assign(rhs.{{ c }}); |
| 3292 |
{%- endfor %} |
| 3293 |
{% elif is_sse2 %} |
| 3294 |
self.0 = unsafe { _mm_div_ps(self.0, rhs.0) }; |
| 3295 |
{% elif is_wasm32 %} |
| 3296 |
self.0 = f32x4_div(self.0, rhs.0); |
| 3297 |
{% elif is_coresimd %} |
| 3298 |
self.0 /= rhs.0; |
| 3299 |
{% elif is_neon %} |
| 3300 |
self.0 = unsafe { vdivq_f32(self.0, rhs.0) }; |
| 3301 |
{% else %} |
| 3302 |
unimplemented!() |
| 3303 |
{% endif %} |
| 3304 |
} |
| 3305 |
} |
| 3306 |
|
| 3307 |
{{ macros::impl_ref_op_assign(imp="Div", self_t=self_t, rhs_t=self_t) }} |
| 3308 |
|
| 3309 |
impl Div<{{ scalar_t }}> for {{ self_t }} { |
| 3310 |
type Output = Self; |
| 3311 |
#[inline] |
| 3312 |
fn div(self, rhs: {{ scalar_t }}) -> Self { |
| 3313 |
{% if is_scalar %} |
| 3314 |
Self { |
| 3315 |
{% for c in components %} |
| 3316 |
{{ c }}: self.{{ c }}.div(rhs), |
| 3317 |
{%- endfor %} |
| 3318 |
} |
| 3319 |
{% elif is_sse2 %} |
| 3320 |
Self(unsafe { _mm_div_ps(self.0, _mm_set1_ps(rhs)) }) |
| 3321 |
{% elif is_wasm32 %} |
| 3322 |
Self(f32x4_div(self.0, f32x4_splat(rhs))) |
| 3323 |
{% elif is_coresimd %} |
| 3324 |
Self(self.0 / f32x4::splat(rhs)) |
| 3325 |
{% elif is_neon %} |
| 3326 |
Self(unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) }) |
| 3327 |
{% else %} |
| 3328 |
unimplemented!() |
| 3329 |
{% endif %} |
| 3330 |
} |
| 3331 |
} |
| 3332 |
|
| 3333 |
{{ macros::impl_ref_binop(imp="Div", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 3334 |
|
| 3335 |
impl DivAssign<{{ scalar_t }}> for {{ self_t }} { |
| 3336 |
#[inline] |
| 3337 |
fn div_assign(&mut self, rhs: {{ scalar_t }}) { |
| 3338 |
{% if is_scalar %} |
| 3339 |
{% for c in components %} |
| 3340 |
self.{{ c }}.div_assign(rhs); |
| 3341 |
{%- endfor %} |
| 3342 |
{% elif is_sse2 %} |
| 3343 |
self.0 = unsafe { _mm_div_ps(self.0, _mm_set1_ps(rhs)) }; |
| 3344 |
{% elif is_wasm32 %} |
| 3345 |
self.0 = f32x4_div(self.0, f32x4_splat(rhs)); |
| 3346 |
{% elif is_coresimd %} |
| 3347 |
self.0 /= f32x4::splat(rhs); |
| 3348 |
{% elif is_neon %} |
| 3349 |
self.0 = unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) }; |
| 3350 |
{% else %} |
| 3351 |
unimplemented!() |
| 3352 |
{% endif %} |
| 3353 |
} |
| 3354 |
} |
| 3355 |
|
| 3356 |
{{ macros::impl_ref_op_assign(imp="Div", self_t=self_t, rhs_t=scalar_t) }} |
| 3357 |
|
| 3358 |
impl Div<{{ self_t }}> for {{ scalar_t }} { |
| 3359 |
type Output = {{ self_t }}; |
| 3360 |
#[inline] |
| 3361 |
fn div(self, rhs: {{ self_t }}) -> {{ self_t }} { |
| 3362 |
{% if is_scalar %} |
| 3363 |
{{ self_t }} { |
| 3364 |
{% for c in components %} |
| 3365 |
{{ c }}: self.div(rhs.{{ c }}), |
| 3366 |
{%- endfor %} |
| 3367 |
} |
| 3368 |
{% elif is_sse2 %} |
| 3369 |
{{ self_t }}(unsafe { _mm_div_ps(_mm_set1_ps(self), rhs.0) }) |
| 3370 |
{% elif is_wasm32 %} |
| 3371 |
{{ self_t }}(f32x4_div(f32x4_splat(self), rhs.0)) |
| 3372 |
{% elif is_coresimd %} |
| 3373 |
{{ self_t }}(f32x4::splat(self) / rhs.0) |
| 3374 |
{% elif is_neon %} |
| 3375 |
{{ self_t }}(unsafe { vdivq_f32(vld1q_dup_f32(&self), rhs.0) }) |
| 3376 |
{% else %} |
| 3377 |
unimplemented!() |
| 3378 |
{% endif %} |
| 3379 |
} |
| 3380 |
} |
| 3381 |
|
| 3382 |
{{ macros::impl_ref_binop(imp="Div", self_t=scalar_t, rhs_t=self_t, output_t=self_t) }} |
| 3383 |
|
| 3384 |
impl Mul for {{ self_t }} { |
| 3385 |
type Output = Self; |
| 3386 |
#[inline] |
| 3387 |
fn mul(self, rhs: Self) -> Self { |
| 3388 |
{% if is_scalar %} |
| 3389 |
Self { |
| 3390 |
{% for c in components %} |
| 3391 |
{{ c }}: self.{{ c }}.mul(rhs.{{ c }}), |
| 3392 |
{%- endfor %} |
| 3393 |
} |
| 3394 |
{% elif is_sse2 %} |
| 3395 |
Self(unsafe { _mm_mul_ps(self.0, rhs.0) }) |
| 3396 |
{% elif is_wasm32 %} |
| 3397 |
Self(f32x4_mul(self.0, rhs.0)) |
| 3398 |
{% elif is_coresimd %} |
| 3399 |
Self(self.0 * rhs.0) |
| 3400 |
{% elif is_neon %} |
| 3401 |
Self(unsafe { vmulq_f32(self.0, rhs.0) }) |
| 3402 |
{% else %} |
| 3403 |
unimplemented!() |
| 3404 |
{% endif %} |
| 3405 |
} |
| 3406 |
} |
| 3407 |
|
| 3408 |
{{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 3409 |
|
| 3410 |
impl MulAssign for {{ self_t }} { |
| 3411 |
#[inline] |
| 3412 |
fn mul_assign(&mut self, rhs: Self) { |
| 3413 |
{% if is_scalar %} |
| 3414 |
{% for c in components %} |
| 3415 |
self.{{ c }}.mul_assign(rhs.{{ c }}); |
| 3416 |
{%- endfor %} |
| 3417 |
{% elif is_sse2 %} |
| 3418 |
self.0 = unsafe { _mm_mul_ps(self.0, rhs.0) }; |
| 3419 |
{% elif is_wasm32 %} |
| 3420 |
self.0 = f32x4_mul(self.0, rhs.0); |
| 3421 |
{% elif is_coresimd %} |
| 3422 |
self.0 *= rhs.0; |
| 3423 |
{% elif is_neon %} |
| 3424 |
self.0 = unsafe { vmulq_f32(self.0, rhs.0) }; |
| 3425 |
{% else %} |
| 3426 |
unimplemented!() |
| 3427 |
{% endif %} |
| 3428 |
} |
| 3429 |
} |
| 3430 |
|
| 3431 |
{{ macros::impl_ref_op_assign(imp="Mul", self_t=self_t, rhs_t=self_t) }} |
| 3432 |
|
| 3433 |
impl Mul<{{ scalar_t }}> for {{ self_t }} { |
| 3434 |
type Output = Self; |
| 3435 |
#[inline] |
| 3436 |
fn mul(self, rhs: {{ scalar_t }}) -> Self { |
| 3437 |
{% if is_scalar %} |
| 3438 |
Self { |
| 3439 |
{% for c in components %} |
| 3440 |
{{ c }}: self.{{ c }}.mul(rhs), |
| 3441 |
{%- endfor %} |
| 3442 |
} |
| 3443 |
{% elif is_sse2 %} |
| 3444 |
Self(unsafe { _mm_mul_ps(self.0, _mm_set1_ps(rhs)) }) |
| 3445 |
{% elif is_wasm32 %} |
| 3446 |
Self(f32x4_mul(self.0, f32x4_splat(rhs))) |
| 3447 |
{% elif is_coresimd %} |
| 3448 |
Self(self.0 * f32x4::splat(rhs)) |
| 3449 |
{% elif is_neon %} |
| 3450 |
Self(unsafe { vmulq_n_f32(self.0, rhs) }) |
| 3451 |
{% else %} |
| 3452 |
unimplemented!() |
| 3453 |
{% endif %} |
| 3454 |
} |
| 3455 |
} |
| 3456 |
|
| 3457 |
{{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 3458 |
|
| 3459 |
impl MulAssign<{{ scalar_t }}> for {{ self_t }} { |
| 3460 |
#[inline] |
| 3461 |
fn mul_assign(&mut self, rhs: {{ scalar_t }}) { |
| 3462 |
{% if is_scalar %} |
| 3463 |
{% for c in components %} |
| 3464 |
self.{{ c }}.mul_assign(rhs); |
| 3465 |
{%- endfor %} |
| 3466 |
{% elif is_sse2 %} |
| 3467 |
self.0 = unsafe { _mm_mul_ps(self.0, _mm_set1_ps(rhs)) }; |
| 3468 |
{% elif is_wasm32 %} |
| 3469 |
self.0 = f32x4_mul(self.0, f32x4_splat(rhs)) |
| 3470 |
{% elif is_coresimd %} |
| 3471 |
self.0 *= f32x4::splat(rhs); |
| 3472 |
{% elif is_neon %} |
| 3473 |
self.0 = unsafe { vmulq_n_f32(self.0, rhs) }; |
| 3474 |
{% else %} |
| 3475 |
unimplemented!() |
| 3476 |
{% endif %} |
| 3477 |
} |
| 3478 |
} |
| 3479 |
|
| 3480 |
{{ macros::impl_ref_op_assign(imp="Mul", self_t=self_t, rhs_t=scalar_t) }} |
| 3481 |
|
| 3482 |
impl Mul<{{ self_t }}> for {{ scalar_t }} { |
| 3483 |
type Output = {{ self_t }}; |
| 3484 |
#[inline] |
| 3485 |
fn mul(self, rhs: {{ self_t }}) -> {{ self_t }} { |
| 3486 |
{% if is_scalar %} |
| 3487 |
{{ self_t }} { |
| 3488 |
{% for c in components %} |
| 3489 |
{{ c }}: self.mul(rhs.{{ c }}), |
| 3490 |
{%- endfor %} |
| 3491 |
} |
| 3492 |
{% elif is_sse2 %} |
| 3493 |
{{ self_t }}(unsafe { _mm_mul_ps(_mm_set1_ps(self), rhs.0) }) |
| 3494 |
{% elif is_wasm32 %} |
| 3495 |
{{ self_t }}(f32x4_mul(f32x4_splat(self), rhs.0)) |
| 3496 |
{% elif is_coresimd %} |
| 3497 |
{{ self_t }}(f32x4::splat(self) * rhs.0) |
| 3498 |
{% elif is_neon %} |
| 3499 |
{{ self_t }}(unsafe { vmulq_n_f32(rhs.0, self) }) |
| 3500 |
{% else %} |
| 3501 |
unimplemented!() |
| 3502 |
{% endif %} |
| 3503 |
} |
| 3504 |
} |
| 3505 |
|
| 3506 |
{{ macros::impl_ref_binop(imp="Mul", self_t=scalar_t, rhs_t=self_t, output_t=self_t) }} |
| 3507 |
|
| 3508 |
impl Add for {{ self_t }} { |
| 3509 |
type Output = Self; |
| 3510 |
#[inline] |
| 3511 |
fn add(self, rhs: Self) -> Self { |
| 3512 |
{% if is_scalar %} |
| 3513 |
Self { |
| 3514 |
{% for c in components %} |
| 3515 |
{{ c }}: self.{{ c }}.add(rhs.{{ c }}), |
| 3516 |
{%- endfor %} |
| 3517 |
} |
| 3518 |
{% elif is_sse2 %} |
| 3519 |
Self(unsafe { _mm_add_ps(self.0, rhs.0) }) |
| 3520 |
{% elif is_wasm32 %} |
| 3521 |
Self(f32x4_add(self.0, rhs.0)) |
| 3522 |
{% elif is_coresimd %} |
| 3523 |
Self(self.0 + rhs.0) |
| 3524 |
{% elif is_neon %} |
| 3525 |
Self(unsafe { vaddq_f32(self.0, rhs.0) }) |
| 3526 |
{% else %} |
| 3527 |
unimplemented!() |
| 3528 |
{% endif %} |
| 3529 |
} |
| 3530 |
} |
| 3531 |
|
| 3532 |
{{ macros::impl_ref_binop(imp="Add", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 3533 |
|
| 3534 |
impl AddAssign for {{ self_t }} { |
| 3535 |
#[inline] |
| 3536 |
fn add_assign(&mut self, rhs: Self) { |
| 3537 |
{% if is_scalar %} |
| 3538 |
{% for c in components %} |
| 3539 |
self.{{ c }}.add_assign(rhs.{{ c }}); |
| 3540 |
{%- endfor %} |
| 3541 |
{% elif is_sse2 %} |
| 3542 |
self.0 = unsafe { _mm_add_ps(self.0, rhs.0) }; |
| 3543 |
{% elif is_wasm32 %} |
| 3544 |
self.0 = f32x4_add(self.0, rhs.0); |
| 3545 |
{% elif is_coresimd %} |
| 3546 |
self.0 += rhs.0; |
| 3547 |
{% elif is_neon %} |
| 3548 |
self.0 = unsafe { vaddq_f32(self.0, rhs.0) }; |
| 3549 |
{% else %} |
| 3550 |
unimplemented!() |
| 3551 |
{% endif %} |
| 3552 |
} |
| 3553 |
} |
| 3554 |
|
| 3555 |
{{ macros::impl_ref_op_assign(imp="Add", self_t=self_t, rhs_t=self_t) }} |
| 3556 |
|
| 3557 |
impl Add<{{ scalar_t }}> for {{ self_t }} { |
| 3558 |
type Output = Self; |
| 3559 |
#[inline] |
| 3560 |
fn add(self, rhs: {{ scalar_t }}) -> Self { |
| 3561 |
{% if is_scalar %} |
| 3562 |
Self { |
| 3563 |
{% for c in components %} |
| 3564 |
{{ c }}: self.{{ c }}.add(rhs), |
| 3565 |
{%- endfor %} |
| 3566 |
} |
| 3567 |
{% elif is_sse2 %} |
| 3568 |
Self(unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) }) |
| 3569 |
{% elif is_wasm32 %} |
| 3570 |
Self(f32x4_add(self.0, f32x4_splat(rhs))) |
| 3571 |
{% elif is_coresimd %} |
| 3572 |
Self(self.0 + f32x4::splat(rhs)) |
| 3573 |
{% elif is_neon %} |
| 3574 |
Self(unsafe { vaddq_f32(self.0, vld1q_dup_f32(&rhs)) }) |
| 3575 |
{% else %} |
| 3576 |
unimplemented!() |
| 3577 |
{% endif %} |
| 3578 |
} |
| 3579 |
} |
| 3580 |
|
| 3581 |
{{ macros::impl_ref_binop(imp="Add", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 3582 |
|
| 3583 |
impl AddAssign<{{ scalar_t }}> for {{ self_t }} { |
| 3584 |
#[inline] |
| 3585 |
fn add_assign(&mut self, rhs: {{ scalar_t }}) { |
| 3586 |
{% if is_scalar %} |
| 3587 |
{% for c in components %} |
| 3588 |
self.{{ c }}.add_assign(rhs); |
| 3589 |
{%- endfor %} |
| 3590 |
{% elif is_sse2 %} |
| 3591 |
self.0 = unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) }; |
| 3592 |
{% elif is_wasm32 %} |
| 3593 |
self.0 = f32x4_add(self.0, f32x4_splat(rhs)); |
| 3594 |
{% elif is_coresimd %} |
| 3595 |
self.0 += f32x4::splat(rhs); |
| 3596 |
{% elif is_neon %} |
| 3597 |
self.0 = unsafe { vaddq_f32(self.0, vld1q_dup_f32(&rhs)) }; |
| 3598 |
{% else %} |
| 3599 |
unimplemented!() |
| 3600 |
{% endif %} |
| 3601 |
} |
| 3602 |
} |
| 3603 |
|
| 3604 |
{{ macros::impl_ref_op_assign(imp="Add", self_t=self_t, rhs_t=scalar_t) }} |
| 3605 |
|
| 3606 |
impl Add<{{ self_t }}> for {{ scalar_t }} { |
| 3607 |
type Output = {{ self_t }}; |
| 3608 |
#[inline] |
| 3609 |
fn add(self, rhs: {{ self_t }}) -> {{ self_t }} { |
| 3610 |
{% if is_scalar %} |
| 3611 |
{{ self_t }} { |
| 3612 |
{% for c in components %} |
| 3613 |
{{ c }}: self.add(rhs.{{ c }}), |
| 3614 |
{%- endfor %} |
| 3615 |
} |
| 3616 |
{% elif is_sse2 %} |
| 3617 |
{{ self_t }}(unsafe { _mm_add_ps(_mm_set1_ps(self), rhs.0) }) |
| 3618 |
{% elif is_wasm32 %} |
| 3619 |
{{ self_t }}(f32x4_add(f32x4_splat(self), rhs.0)) |
| 3620 |
{% elif is_coresimd %} |
| 3621 |
{{ self_t }}(f32x4::splat(self) + rhs.0) |
| 3622 |
{% elif is_neon %} |
| 3623 |
{{ self_t }}(unsafe { vaddq_f32(vld1q_dup_f32(&self), rhs.0) }) |
| 3624 |
{% else %} |
| 3625 |
unimplemented!() |
| 3626 |
{% endif %} |
| 3627 |
} |
| 3628 |
} |
| 3629 |
|
| 3630 |
{{ macros::impl_ref_binop(imp="Add", self_t=scalar_t, rhs_t=self_t, output_t=self_t) }} |
| 3631 |
|
| 3632 |
impl Sub for {{ self_t }} { |
| 3633 |
type Output = Self; |
| 3634 |
#[inline] |
| 3635 |
fn sub(self, rhs: Self) -> Self { |
| 3636 |
{% if is_scalar %} |
| 3637 |
Self { |
| 3638 |
{% for c in components %} |
| 3639 |
{{ c }}: self.{{ c }}.sub(rhs.{{ c }}), |
| 3640 |
{%- endfor %} |
| 3641 |
} |
| 3642 |
{% elif is_sse2 %} |
| 3643 |
Self(unsafe { _mm_sub_ps(self.0, rhs.0) }) |
| 3644 |
{% elif is_wasm32 %} |
| 3645 |
Self(f32x4_sub(self.0, rhs.0)) |
| 3646 |
{% elif is_coresimd %} |
| 3647 |
Self(self.0 - rhs.0) |
| 3648 |
{% elif is_neon %} |
| 3649 |
Self(unsafe { vsubq_f32(self.0, rhs.0) }) |
| 3650 |
{% else %} |
| 3651 |
unimplemented!() |
| 3652 |
{% endif %} |
| 3653 |
} |
| 3654 |
} |
| 3655 |
|
| 3656 |
{{ macros::impl_ref_binop(imp="Sub", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 3657 |
|
| 3658 |
impl SubAssign for {{ self_t }} { |
| 3659 |
#[inline] |
| 3660 |
fn sub_assign(&mut self, rhs: Self) { |
| 3661 |
{% if is_scalar %} |
| 3662 |
{% for c in components %} |
| 3663 |
self.{{ c }}.sub_assign(rhs.{{ c }}); |
| 3664 |
{%- endfor %} |
| 3665 |
{% elif is_sse2 %} |
| 3666 |
self.0 = unsafe { _mm_sub_ps(self.0, rhs.0) }; |
| 3667 |
{% elif is_wasm32 %} |
| 3668 |
self.0 = f32x4_sub(self.0, rhs.0); |
| 3669 |
{% elif is_coresimd %} |
| 3670 |
self.0 -= rhs.0; |
| 3671 |
{% elif is_neon %} |
| 3672 |
self.0 = unsafe { vsubq_f32(self.0, rhs.0) }; |
| 3673 |
{% else %} |
| 3674 |
unimplemented!() |
| 3675 |
{% endif %} |
| 3676 |
} |
| 3677 |
} |
| 3678 |
|
| 3679 |
{{ macros::impl_ref_op_assign(imp="Sub", self_t=self_t, rhs_t=self_t) }} |
| 3680 |
|
| 3681 |
impl Sub<{{ scalar_t }}> for {{ self_t }} { |
| 3682 |
type Output = Self; |
| 3683 |
#[inline] |
| 3684 |
fn sub(self, rhs: {{ scalar_t }}) -> Self { |
| 3685 |
{% if is_scalar %} |
| 3686 |
Self { |
| 3687 |
{% for c in components %} |
| 3688 |
{{ c }}: self.{{ c }}.sub(rhs), |
| 3689 |
{%- endfor %} |
| 3690 |
} |
| 3691 |
{% elif is_sse2 %} |
| 3692 |
Self(unsafe { _mm_sub_ps(self.0, _mm_set1_ps(rhs)) }) |
| 3693 |
{% elif is_wasm32 %} |
| 3694 |
Self(f32x4_sub(self.0, f32x4_splat(rhs))) |
| 3695 |
{% elif is_coresimd %} |
| 3696 |
Self(self.0 - f32x4::splat(rhs)) |
| 3697 |
{% elif is_neon %} |
| 3698 |
Self(unsafe { vsubq_f32(self.0, vld1q_dup_f32(&rhs)) }) |
| 3699 |
{% else %} |
| 3700 |
unimplemented!() |
| 3701 |
{% endif %} |
| 3702 |
} |
| 3703 |
} |
| 3704 |
|
| 3705 |
{{ macros::impl_ref_binop(imp="Sub", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 3706 |
|
| 3707 |
impl SubAssign<{{ scalar_t }}> for {{ self_t }} { |
| 3708 |
#[inline] |
| 3709 |
fn sub_assign(&mut self, rhs: {{ scalar_t }}) { |
| 3710 |
{% if is_scalar %} |
| 3711 |
{% for c in components %} |
| 3712 |
self.{{ c }}.sub_assign(rhs); |
| 3713 |
{%- endfor %} |
| 3714 |
{% elif is_sse2 %} |
| 3715 |
self.0 = unsafe { _mm_sub_ps(self.0, _mm_set1_ps(rhs)) }; |
| 3716 |
{% elif is_wasm32 %} |
| 3717 |
self.0 = f32x4_sub(self.0, f32x4_splat(rhs)) |
| 3718 |
{% elif is_coresimd %} |
| 3719 |
self.0 -= f32x4::splat(rhs); |
| 3720 |
{% elif is_neon %} |
| 3721 |
self.0 = unsafe { vsubq_f32(self.0, vld1q_dup_f32(&rhs)) }; |
| 3722 |
{% else %} |
| 3723 |
unimplemented!() |
| 3724 |
{% endif %} |
| 3725 |
} |
| 3726 |
} |
| 3727 |
|
| 3728 |
{{ macros::impl_ref_op_assign(imp="Sub", self_t=self_t, rhs_t=scalar_t) }} |
| 3729 |
|
| 3730 |
impl Sub<{{ self_t }}> for {{ scalar_t }} { |
| 3731 |
type Output = {{ self_t }}; |
| 3732 |
#[inline] |
| 3733 |
fn sub(self, rhs: {{ self_t }}) -> {{ self_t }} { |
| 3734 |
{% if is_scalar %} |
| 3735 |
{{ self_t }} { |
| 3736 |
{% for c in components %} |
| 3737 |
{{ c }}: self.sub(rhs.{{ c }}), |
| 3738 |
{%- endfor %} |
| 3739 |
} |
| 3740 |
{% elif is_sse2 %} |
| 3741 |
{{ self_t }}(unsafe { _mm_sub_ps(_mm_set1_ps(self), rhs.0) }) |
| 3742 |
{% elif is_wasm32 %} |
| 3743 |
{{ self_t }}(f32x4_sub(f32x4_splat(self), rhs.0)) |
| 3744 |
{% elif is_coresimd %} |
| 3745 |
{{ self_t }}(f32x4::splat(self) - rhs.0) |
| 3746 |
{% elif is_neon %} |
| 3747 |
{{ self_t }}(unsafe { vsubq_f32(vld1q_dup_f32(&self), rhs.0) }) |
| 3748 |
{% else %} |
| 3749 |
unimplemented!() |
| 3750 |
{% endif %} |
| 3751 |
} |
| 3752 |
} |
| 3753 |
|
| 3754 |
{{ macros::impl_ref_binop(imp="Sub", self_t=scalar_t, rhs_t=self_t, output_t=self_t) }} |
| 3755 |
|
| 3756 |
impl Rem for {{ self_t }} { |
| 3757 |
type Output = Self; |
| 3758 |
#[inline] |
| 3759 |
fn rem(self, rhs: Self) -> Self { |
| 3760 |
{% if is_scalar %} |
| 3761 |
Self { |
| 3762 |
{% for c in components %} |
| 3763 |
{{ c }}: self.{{ c }}.rem(rhs.{{ c }}), |
| 3764 |
{%- endfor %} |
| 3765 |
} |
| 3766 |
{% elif is_sse2 %} |
| 3767 |
unsafe { |
| 3768 |
let n = m128_floor(_mm_div_ps(self.0, rhs.0)); |
| 3769 |
Self(_mm_sub_ps(self.0, _mm_mul_ps(n, rhs.0))) |
| 3770 |
} |
| 3771 |
{% elif is_wasm32 %} |
| 3772 |
let n = f32x4_floor(f32x4_div(self.0, rhs.0)); |
| 3773 |
Self(f32x4_sub(self.0, f32x4_mul(n, rhs.0))) |
| 3774 |
{% elif is_coresimd %} |
| 3775 |
Self(self.0 % rhs.0) |
| 3776 |
{% elif is_neon %} |
| 3777 |
unsafe { |
| 3778 |
let n = vrndmq_f32(vdivq_f32(self.0, rhs.0)); |
| 3779 |
Self(vsubq_f32(self.0, vmulq_f32(n, rhs.0))) |
| 3780 |
} |
| 3781 |
{% else %} |
| 3782 |
unimplemented!() |
| 3783 |
{% endif %} |
| 3784 |
} |
| 3785 |
} |
| 3786 |
|
| 3787 |
{{ macros::impl_ref_binop(imp="Rem", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 3788 |
|
| 3789 |
impl RemAssign for {{ self_t }} { |
| 3790 |
#[inline] |
| 3791 |
fn rem_assign(&mut self, rhs: Self) { |
| 3792 |
{% if is_scalar %} |
| 3793 |
{% for c in components %} |
| 3794 |
self.{{ c }}.rem_assign(rhs.{{ c }}); |
| 3795 |
{%- endfor %} |
| 3796 |
{% elif is_coresimd %} |
| 3797 |
self.0 %= rhs.0; |
| 3798 |
{% else %} |
| 3799 |
*self = self.rem(rhs); |
| 3800 |
{% endif %} |
| 3801 |
} |
| 3802 |
} |
| 3803 |
|
| 3804 |
{{ macros::impl_ref_op_assign(imp="Rem", self_t=self_t, rhs_t=self_t) }} |
| 3805 |
|
| 3806 |
impl Rem<{{ scalar_t }}> for {{ self_t }} { |
| 3807 |
type Output = Self; |
| 3808 |
#[inline] |
| 3809 |
fn rem(self, rhs: {{ scalar_t }}) -> Self { |
| 3810 |
{% if is_scalar %} |
| 3811 |
Self { |
| 3812 |
{% for c in components %} |
| 3813 |
{{ c }}: self.{{ c }}.rem(rhs), |
| 3814 |
{%- endfor %} |
| 3815 |
} |
| 3816 |
{% else %} |
| 3817 |
self.rem(Self::splat(rhs)) |
| 3818 |
{% endif %} |
| 3819 |
} |
| 3820 |
} |
| 3821 |
|
| 3822 |
{{ macros::impl_ref_binop(imp="Rem", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 3823 |
|
| 3824 |
impl RemAssign<{{ scalar_t }}> for {{ self_t }} { |
| 3825 |
#[inline] |
| 3826 |
fn rem_assign(&mut self, rhs: {{ scalar_t }}) { |
| 3827 |
{% if is_scalar %} |
| 3828 |
{% for c in components %} |
| 3829 |
self.{{ c }}.rem_assign(rhs); |
| 3830 |
{%- endfor %} |
| 3831 |
{% elif is_coresimd %} |
| 3832 |
self.0 %= f32x4::splat(rhs); |
| 3833 |
{% else %} |
| 3834 |
*self = self.rem(Self::splat(rhs)); |
| 3835 |
{% endif %} |
| 3836 |
} |
| 3837 |
} |
| 3838 |
|
| 3839 |
{{ macros::impl_ref_op_assign(imp="Rem", self_t=self_t, rhs_t=scalar_t) }} |
| 3840 |
|
| 3841 |
impl Rem<{{ self_t }}> for {{ scalar_t }} { |
| 3842 |
type Output = {{ self_t }}; |
| 3843 |
#[inline] |
| 3844 |
fn rem(self, rhs: {{ self_t }}) -> {{ self_t }} { |
| 3845 |
{% if is_scalar %} |
| 3846 |
{{ self_t }} { |
| 3847 |
{% for c in components %} |
| 3848 |
{{ c }}: self.rem(rhs.{{ c }}), |
| 3849 |
{%- endfor %} |
| 3850 |
} |
| 3851 |
{% else %} |
| 3852 |
{{ self_t }}::splat(self).rem(rhs) |
| 3853 |
{% endif %} |
| 3854 |
} |
| 3855 |
} |
| 3856 |
|
| 3857 |
{{ macros::impl_ref_binop(imp="Rem", self_t=scalar_t, rhs_t=self_t, output_t=self_t) }} |
| 3858 |
|
| 3859 |
impl AsRef<[{{ scalar_t }}; {{ dim }}]> for {{ self_t }} { |
| 3860 |
#[inline] |
| 3861 |
fn as_ref(&self) -> &[{{ scalar_t }}; {{ dim }}] { |
| 3862 |
unsafe { &*(self as *const Self as *const [{{ scalar_t }}; {{ dim }}]) } |
| 3863 |
} |
| 3864 |
} |
| 3865 |
|
| 3866 |
impl AsMut<[{{ scalar_t }}; {{ dim }}]> for {{ self_t }} { |
| 3867 |
#[inline] |
| 3868 |
fn as_mut(&mut self) -> &mut [{{ scalar_t }}; {{ dim }}] { |
| 3869 |
unsafe { &mut *(self as *mut Self as *mut [{{ scalar_t }}; {{ dim }}]) } |
| 3870 |
} |
| 3871 |
} |
| 3872 |
|
| 3873 |
impl Sum for {{ self_t }} { |
| 3874 |
#[inline] |
| 3875 |
fn sum<I>(iter: I) -> Self |
| 3876 |
where |
| 3877 |
I: Iterator<Item = Self>, |
| 3878 |
{ |
| 3879 |
iter.fold(Self::ZERO, Self::add) |
| 3880 |
} |
| 3881 |
} |
| 3882 |
|
| 3883 |
impl<'a> Sum<&'a Self> for {{ self_t }} { |
| 3884 |
#[inline] |
| 3885 |
fn sum<I>(iter: I) -> Self |
| 3886 |
where |
| 3887 |
I: Iterator<Item = &'a Self>, |
| 3888 |
{ |
| 3889 |
iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 3890 |
} |
| 3891 |
} |
| 3892 |
|
| 3893 |
impl Product for {{ self_t }} { |
| 3894 |
#[inline] |
| 3895 |
fn product<I>(iter: I) -> Self |
| 3896 |
where |
| 3897 |
I: Iterator<Item = Self>, |
| 3898 |
{ |
| 3899 |
iter.fold(Self::ONE, Self::mul) |
| 3900 |
} |
| 3901 |
} |
| 3902 |
|
| 3903 |
impl<'a> Product<&'a Self> for {{ self_t }} { |
| 3904 |
#[inline] |
| 3905 |
fn product<I>(iter: I) -> Self |
| 3906 |
where |
| 3907 |
I: Iterator<Item = &'a Self>, |
| 3908 |
{ |
| 3909 |
iter.fold(Self::ONE, |a, &b| Self::mul(a, b)) |
| 3910 |
} |
| 3911 |
} |
| 3912 |
|
| 3913 |
{% if is_signed %} |
| 3914 |
impl Neg for {{ self_t }} { |
| 3915 |
type Output = Self; |
| 3916 |
#[inline] |
| 3917 |
fn neg(self) -> Self { |
| 3918 |
{% if is_scalar %} |
| 3919 |
Self { |
| 3920 |
{% for c in components %} |
| 3921 |
{{ c }}: self.{{ c }}.neg(), |
| 3922 |
{%- endfor %} |
| 3923 |
} |
| 3924 |
{% elif is_sse2 %} |
| 3925 |
Self(unsafe { _mm_xor_ps(_mm_set1_ps(-0.0), self.0) }) |
| 3926 |
{% elif is_wasm32 %} |
| 3927 |
Self(f32x4_neg(self.0)) |
| 3928 |
{% elif is_coresimd %} |
| 3929 |
Self(-self.0) |
| 3930 |
{% elif is_neon %} |
| 3931 |
Self(unsafe { vnegq_f32(self.0) }) |
| 3932 |
{% else %} |
| 3933 |
unimplemented!() |
| 3934 |
{% endif %} |
| 3935 |
} |
| 3936 |
} |
| 3937 |
|
| 3938 |
{{ macros::impl_ref_unop(imp="Neg", self_t=self_t, output_t=self_t) }} |
| 3939 |
{% endif %} |
| 3940 |
|
| 3941 |
{% if not is_float %} |
| 3942 |
impl Not for {{ self_t }} { |
| 3943 |
type Output = Self; |
| 3944 |
#[inline] |
| 3945 |
fn not(self) -> Self { |
| 3946 |
Self { |
| 3947 |
{% for c in components %} |
| 3948 |
{{ c }}: self.{{ c }}.not(), |
| 3949 |
{%- endfor %} |
| 3950 |
} |
| 3951 |
} |
| 3952 |
} |
| 3953 |
|
| 3954 |
{{ macros::impl_ref_unop(imp="Not", self_t=self_t, output_t=self_t) }} |
| 3955 |
|
| 3956 |
impl BitAnd for {{ self_t }} { |
| 3957 |
type Output = Self; |
| 3958 |
#[inline] |
| 3959 |
fn bitand(self, rhs: Self) -> Self::Output { |
| 3960 |
Self { |
| 3961 |
{% for c in components %} |
| 3962 |
{{ c }}: self.{{ c }}.bitand(rhs.{{ c }}), |
| 3963 |
{%- endfor %} |
| 3964 |
} |
| 3965 |
} |
| 3966 |
} |
| 3967 |
|
| 3968 |
{{ macros::impl_ref_binop(imp="BitAnd", method="bitand", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 3969 |
|
| 3970 |
{{ macros::impl_op_assign(imp="BitAnd", self_t=self_t, rhs_t=self_t) }} |
| 3971 |
|
| 3972 |
impl BitOr for {{ self_t }} { |
| 3973 |
type Output = Self; |
| 3974 |
#[inline] |
| 3975 |
fn bitor(self, rhs: Self) -> Self::Output { |
| 3976 |
Self { |
| 3977 |
{% for c in components %} |
| 3978 |
{{ c }}: self.{{ c }}.bitor(rhs.{{ c }}), |
| 3979 |
{%- endfor %} |
| 3980 |
} |
| 3981 |
} |
| 3982 |
} |
| 3983 |
|
| 3984 |
{{ macros::impl_ref_binop(imp="BitOr", method="bitor", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 3985 |
|
| 3986 |
{{ macros::impl_op_assign(imp="BitOr", self_t=self_t, rhs_t=self_t) }} |
| 3987 |
|
| 3988 |
impl BitXor for {{ self_t }} { |
| 3989 |
type Output = Self; |
| 3990 |
#[inline] |
| 3991 |
fn bitxor(self, rhs: Self) -> Self::Output { |
| 3992 |
Self { |
| 3993 |
{% for c in components %} |
| 3994 |
{{ c }}: self.{{ c }}.bitxor(rhs.{{ c }}), |
| 3995 |
{%- endfor %} |
| 3996 |
} |
| 3997 |
} |
| 3998 |
} |
| 3999 |
|
| 4000 |
{{ macros::impl_ref_binop(imp="BitXor", method="bitxor", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 4001 |
|
| 4002 |
{{ macros::impl_op_assign(imp="BitXor", self_t=self_t, rhs_t=self_t) }} |
| 4003 |
|
| 4004 |
impl BitAnd<{{ scalar_t }}> for {{ self_t }} { |
| 4005 |
type Output = Self; |
| 4006 |
#[inline] |
| 4007 |
fn bitand(self, rhs: {{ scalar_t }}) -> Self::Output { |
| 4008 |
Self { |
| 4009 |
{% for c in components %} |
| 4010 |
{{ c }}: self.{{ c }}.bitand(rhs), |
| 4011 |
{%- endfor %} |
| 4012 |
} |
| 4013 |
} |
| 4014 |
} |
| 4015 |
|
| 4016 |
{{ macros::impl_ref_binop(imp="BitAnd", method="bitand", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 4017 |
|
| 4018 |
{{ macros::impl_op_assign(imp="BitAnd", self_t=self_t, rhs_t=scalar_t) }} |
| 4019 |
|
| 4020 |
impl BitOr<{{ scalar_t }}> for {{ self_t }} { |
| 4021 |
type Output = Self; |
| 4022 |
#[inline] |
| 4023 |
fn bitor(self, rhs: {{ scalar_t }}) -> Self::Output { |
| 4024 |
Self { |
| 4025 |
{% for c in components %} |
| 4026 |
{{ c }}: self.{{ c }}.bitor(rhs), |
| 4027 |
{%- endfor %} |
| 4028 |
} |
| 4029 |
} |
| 4030 |
} |
| 4031 |
|
| 4032 |
{{ macros::impl_ref_binop(imp="BitOr", method="bitor", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 4033 |
|
| 4034 |
{{ macros::impl_op_assign(imp="BitOr", self_t=self_t, rhs_t=scalar_t) }} |
| 4035 |
|
| 4036 |
impl BitXor<{{ scalar_t }}> for {{ self_t }} { |
| 4037 |
type Output = Self; |
| 4038 |
#[inline] |
| 4039 |
fn bitxor(self, rhs: {{ scalar_t }}) -> Self::Output { |
| 4040 |
Self { |
| 4041 |
{% for c in components %} |
| 4042 |
{{ c }}: self.{{ c }}.bitxor(rhs), |
| 4043 |
{%- endfor %} |
| 4044 |
} |
| 4045 |
} |
| 4046 |
} |
| 4047 |
|
| 4048 |
{{ macros::impl_ref_binop(imp="BitXor", method="bitxor", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 4049 |
|
| 4050 |
{{ macros::impl_op_assign(imp="BitXor", self_t=self_t, rhs_t=scalar_t) }} |
| 4051 |
|
| 4052 |
{% for rhs_t in ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64"] %} |
| 4053 |
impl Shl<{{ rhs_t }}> for {{ self_t }} { |
| 4054 |
type Output = Self; |
| 4055 |
#[inline] |
| 4056 |
fn shl(self, rhs: {{ rhs_t }}) -> Self::Output { |
| 4057 |
Self { |
| 4058 |
{% for c in components %} |
| 4059 |
{{ c }}: self.{{ c }}.shl(rhs), |
| 4060 |
{%- endfor %} |
| 4061 |
} |
| 4062 |
} |
| 4063 |
} |
| 4064 |
|
| 4065 |
{{ macros::impl_ref_binop(imp="Shl", self_t=self_t, rhs_t=rhs_t, output_t=self_t) }} |
| 4066 |
|
| 4067 |
{{ macros::impl_op_assign(imp="Shl", self_t=self_t, rhs_t=rhs_t) }} |
| 4068 |
|
| 4069 |
impl Shr<{{ rhs_t }}> for {{ self_t }} { |
| 4070 |
type Output = Self; |
| 4071 |
#[inline] |
| 4072 |
fn shr(self, rhs: {{ rhs_t }}) -> Self::Output { |
| 4073 |
Self { |
| 4074 |
{% for c in components %} |
| 4075 |
{{ c }}: self.{{ c }}.shr(rhs), |
| 4076 |
{%- endfor %} |
| 4077 |
} |
| 4078 |
} |
| 4079 |
} |
| 4080 |
|
| 4081 |
{{ macros::impl_ref_binop(imp="Shr", self_t=self_t, rhs_t=rhs_t, output_t=self_t) }} |
| 4082 |
|
| 4083 |
{{ macros::impl_op_assign(imp="Shr", self_t=self_t, rhs_t=rhs_t) }} |
| 4084 |
{% endfor %} |
| 4085 |
|
| 4086 |
{% for rhs_t in ["IVec" ~ dim, "UVec" ~ dim] %} |
| 4087 |
impl Shl{% if rhs_t != self_t %}<{{ rhs_t }}>{% endif %} for {{ self_t }} { |
| 4088 |
type Output = Self; |
| 4089 |
#[inline] |
| 4090 |
fn shl(self, rhs: {% if rhs_t != self_t %}{{ rhs_t }}{% else %}Self{% endif %}) -> Self { |
| 4091 |
Self { |
| 4092 |
{% for c in components %} |
| 4093 |
{{ c }}: self.{{ c }}.shl(rhs.{{ c }}), |
| 4094 |
{%- endfor %} |
| 4095 |
} |
| 4096 |
} |
| 4097 |
} |
| 4098 |
|
| 4099 |
{{ macros::impl_ref_binop(imp="Shl", self_t=self_t, rhs_t=rhs_t, output_t=self_t) }} |
| 4100 |
|
| 4101 |
impl Shr{% if rhs_t != self_t %}<{{ rhs_t }}>{% endif %} for {{ self_t }} { |
| 4102 |
type Output = Self; |
| 4103 |
#[inline] |
| 4104 |
fn shr(self, rhs: {% if rhs_t != self_t %}{{ rhs_t }}{% else %}Self{% endif %}) -> Self { |
| 4105 |
Self { |
| 4106 |
{% for c in components %} |
| 4107 |
{{ c }}: self.{{ c }}.shr(rhs.{{ c }}), |
| 4108 |
{%- endfor %} |
| 4109 |
} |
| 4110 |
} |
| 4111 |
} |
| 4112 |
|
| 4113 |
{{ macros::impl_ref_binop(imp="Shr", self_t=self_t, rhs_t=rhs_t, output_t=self_t) }} |
| 4114 |
{% endfor %} |
| 4115 |
{% endif %} |
| 4116 |
|
| 4117 |
impl Index<usize> for {{ self_t }} { |
| 4118 |
type Output = {{ scalar_t }}; |
| 4119 |
#[inline] |
| 4120 |
fn index(&self, index: usize) -> &Self::Output { |
| 4121 |
{% if is_coresimd %} |
| 4122 |
&self.0[index] |
| 4123 |
{% else %} |
| 4124 |
match index { |
| 4125 |
{% for c in components %} |
| 4126 |
{{ loop.index0 }} => &self.{{ c }}, |
| 4127 |
{%- endfor %} |
| 4128 |
_ => panic!("index out of bounds"), |
| 4129 |
} |
| 4130 |
{% endif %} |
| 4131 |
} |
| 4132 |
} |
| 4133 |
|
| 4134 |
impl IndexMut<usize> for {{ self_t }} { |
| 4135 |
#[inline] |
| 4136 |
fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 4137 |
{% if is_coresimd %} |
| 4138 |
&mut self.0[index] |
| 4139 |
{% else %} |
| 4140 |
match index { |
| 4141 |
{% for c in components %} |
| 4142 |
{{ loop.index0 }} => &mut self.{{ c }}, |
| 4143 |
{%- endfor %} |
| 4144 |
_ => panic!("index out of bounds"), |
| 4145 |
} |
| 4146 |
{% endif %} |
| 4147 |
} |
| 4148 |
} |
| 4149 |
|
| 4150 |
impl fmt::Display for {{ self_t }} { |
| 4151 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 4152 |
{% if is_float %} |
| 4153 |
if let Some(p) = f.precision() { |
| 4154 |
{% if dim == 2 %} |
| 4155 |
write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y) |
| 4156 |
{% elif dim == 3 %} |
| 4157 |
write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z) |
| 4158 |
{% elif dim == 4 %} |
| 4159 |
write!(f, "[{:.*}, {:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z, p, self.w) |
| 4160 |
{% endif %} |
| 4161 |
} else { |
| 4162 |
{% endif %} |
| 4163 |
{% if dim == 2 %} |
| 4164 |
write!(f, "[{}, {}]", self.x, self.y) |
| 4165 |
{% elif dim == 3 %} |
| 4166 |
write!(f, "[{}, {}, {}]", self.x, self.y, self.z) |
| 4167 |
{% elif dim == 4 %} |
| 4168 |
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w) |
| 4169 |
{% endif %} |
| 4170 |
{% if is_float %} |
| 4171 |
} |
| 4172 |
{% endif %} |
| 4173 |
} |
| 4174 |
} |
| 4175 |
|
| 4176 |
impl fmt::Debug for {{ self_t }} { |
| 4177 |
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 4178 |
fmt.debug_tuple(stringify!({{ self_t }})) |
| 4179 |
{% for c in components %} |
| 4180 |
.field(&self.{{ c }}) |
| 4181 |
{% endfor %} |
| 4182 |
.finish() |
| 4183 |
} |
| 4184 |
} |
| 4185 |
|
| 4186 |
{% if not is_scalar %} |
| 4187 |
impl From<{{ self_t }}> for {{ simd_t }} { |
| 4188 |
#[inline(always)] |
| 4189 |
fn from(t: {{ self_t }}) -> Self { |
| 4190 |
t.0 |
| 4191 |
} |
| 4192 |
} |
| 4193 |
|
| 4194 |
impl From<{{ simd_t }}> for {{ self_t }} { |
| 4195 |
#[inline(always)] |
| 4196 |
fn from(t: {{ simd_t }}) -> Self { |
| 4197 |
Self(t) |
| 4198 |
} |
| 4199 |
} |
| 4200 |
{% endif %} |
| 4201 |
|
| 4202 |
impl From<[{{ scalar_t }}; {{ dim }}]> for {{ self_t }} { |
| 4203 |
#[inline] |
| 4204 |
fn from(a: [{{ scalar_t }}; {{ dim }}]) -> Self { |
| 4205 |
{% if self_t == "Vec4" and is_sse2 %} |
| 4206 |
Self(unsafe { _mm_loadu_ps(a.as_ptr()) }) |
| 4207 |
{% elif self_t == "Vec4" and is_coresimd %} |
| 4208 |
Self(f32x4::from_array(a)) |
| 4209 |
{% elif self_t == "Vec4" and is_neon %} |
| 4210 |
Self(unsafe { vld1q_f32(a.as_ptr()) }) |
| 4211 |
{% else %} |
| 4212 |
Self::new( |
| 4213 |
{% for c in components %} |
| 4214 |
a[{{ loop.index0 }}], |
| 4215 |
{%- endfor %} |
| 4216 |
) |
| 4217 |
{% endif %} |
| 4218 |
} |
| 4219 |
} |
| 4220 |
|
| 4221 |
impl From<{{ self_t }}> for [{{ scalar_t }}; {{ dim }}] { |
| 4222 |
#[inline] |
| 4223 |
fn from(v: {{ self_t }}) -> Self { |
| 4224 |
{% if is_scalar %} |
| 4225 |
[ |
| 4226 |
{% for c in components %} |
| 4227 |
v.{{ c }}, |
| 4228 |
{%- endfor %} |
| 4229 |
] |
| 4230 |
{% elif is_sse2 %} |
| 4231 |
use core::mem::MaybeUninit; |
| 4232 |
use crate::Align16; |
| 4233 |
let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit(); |
| 4234 |
unsafe { |
| 4235 |
_mm_store_ps(out.as_mut_ptr().cast(), v.0); |
| 4236 |
out.assume_init().0 |
| 4237 |
} |
| 4238 |
{% elif is_wasm32 %} |
| 4239 |
unsafe { |
| 4240 |
*(&v.0 as *const v128 as *const Self) |
| 4241 |
} |
| 4242 |
{% elif is_coresimd %} |
| 4243 |
{% if dim == 3 %} |
| 4244 |
unsafe { |
| 4245 |
*(v.0.as_array().as_ptr() as *const Self) |
| 4246 |
} |
| 4247 |
{% elif dim == 4 %} |
| 4248 |
v.0.to_array() |
| 4249 |
{% endif %} |
| 4250 |
{% elif is_neon %} |
| 4251 |
use core::mem::MaybeUninit; |
| 4252 |
use crate::align16::Align16; |
| 4253 |
let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit(); |
| 4254 |
unsafe { |
| 4255 |
vst1q_f32(out.as_mut_ptr().cast(), v.0); |
| 4256 |
out.assume_init().0 |
| 4257 |
} |
| 4258 |
{% else %} |
| 4259 |
unimplemented!() |
| 4260 |
{% endif %} |
| 4261 |
} |
| 4262 |
} |
| 4263 |
|
| 4264 |
impl From<{{ macros::make_tuple_t(t=scalar_t, n=dim) }}> for {{ self_t }} { |
| 4265 |
#[inline] |
| 4266 |
fn from(t: {{ macros::make_tuple_t(t=scalar_t, n=dim) }}) -> Self { |
| 4267 |
Self::new( |
| 4268 |
{% for c in components %} |
| 4269 |
t.{{ loop.index0 }}, |
| 4270 |
{%- endfor %} |
| 4271 |
) |
| 4272 |
} |
| 4273 |
} |
| 4274 |
|
| 4275 |
impl From<{{ self_t }}> for {{ macros::make_tuple_t(t=scalar_t, n=dim) }} { |
| 4276 |
#[inline] |
| 4277 |
fn from(v: {{ self_t }}) -> Self { |
| 4278 |
( |
| 4279 |
{% for c in components %} |
| 4280 |
v.{{ c }}, |
| 4281 |
{%- endfor %} |
| 4282 |
) |
| 4283 |
} |
| 4284 |
} |
| 4285 |
|
| 4286 |
{% if self_t == "Vec3A" %} |
| 4287 |
impl From<Vec3> for Vec3A { |
| 4288 |
#[inline] |
| 4289 |
fn from(v: Vec3) -> Self { |
| 4290 |
Self::new(v.x, v.y, v.z) |
| 4291 |
} |
| 4292 |
} |
| 4293 |
|
| 4294 |
impl From<Vec3A> for Vec3 { |
| 4295 |
#[inline] |
| 4296 |
fn from(v: Vec3A) -> Self { |
| 4297 |
{% if is_scalar %} |
| 4298 |
Self { |
| 4299 |
x: v.x, |
| 4300 |
y: v.y, |
| 4301 |
z: v.z, |
| 4302 |
} |
| 4303 |
{% elif is_sse2 %} |
| 4304 |
use crate::Align16; |
| 4305 |
use core::mem::MaybeUninit; |
| 4306 |
let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit(); |
| 4307 |
unsafe { |
| 4308 |
_mm_store_ps(out.as_mut_ptr().cast(), v.0); |
| 4309 |
out.assume_init().0 |
| 4310 |
} |
| 4311 |
{% elif is_wasm32 %} |
| 4312 |
unsafe { |
| 4313 |
*(&v.0 as *const v128 as *const Self) |
| 4314 |
} |
| 4315 |
{% elif is_coresimd %} |
| 4316 |
unsafe { |
| 4317 |
*(v.0.as_array().as_ptr() as *const Self) |
| 4318 |
} |
| 4319 |
{% elif is_neon %} |
| 4320 |
use core::mem::MaybeUninit; |
| 4321 |
use crate::align16::Align16; |
| 4322 |
let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit(); |
| 4323 |
unsafe { |
| 4324 |
vst1q_f32(out.as_mut_ptr().cast(), v.0); |
| 4325 |
out.assume_init().0 |
| 4326 |
} |
| 4327 |
{% else %} |
| 4328 |
unimplemented!() |
| 4329 |
{% endif %} |
| 4330 |
} |
| 4331 |
} |
| 4332 |
{% elif self_t == "Vec4" %} |
| 4333 |
impl From<(Vec3A, f32)> for Vec4 { |
| 4334 |
#[inline] |
| 4335 |
fn from((v, w): (Vec3A, f32)) -> Self { |
| 4336 |
v.extend(w) |
| 4337 |
} |
| 4338 |
} |
| 4339 |
|
| 4340 |
impl From<(f32, Vec3A)> for Vec4 { |
| 4341 |
#[inline] |
| 4342 |
fn from((x, v): (f32, Vec3A)) -> Self { |
| 4343 |
Self::new(x, v.x, v.y, v.z) |
| 4344 |
} |
| 4345 |
} |
| 4346 |
{% endif %} |
| 4347 |
|
| 4348 |
{% if dim == 3 %} |
| 4349 |
impl From<({{ vec2_t }}, {{ scalar_t }})> for {{ self_t }} { |
| 4350 |
#[inline] |
| 4351 |
fn from((v, z): ({{ vec2_t }}, {{ scalar_t }})) -> Self { |
| 4352 |
Self::new(v.x, v.y, z) |
| 4353 |
} |
| 4354 |
} |
| 4355 |
{% elif dim == 4 %} |
| 4356 |
impl From<({{ vec3_t }}, {{ scalar_t }})> for {{ self_t }} { |
| 4357 |
#[inline] |
| 4358 |
fn from((v, w): ({{ vec3_t }}, {{ scalar_t }})) -> Self { |
| 4359 |
Self::new(v.x, v.y, v.z, w) |
| 4360 |
} |
| 4361 |
} |
| 4362 |
|
| 4363 |
impl From<({{ scalar_t }}, {{ vec3_t }})> for {{ self_t }} { |
| 4364 |
#[inline] |
| 4365 |
fn from((x, v): ({{ scalar_t }}, {{ vec3_t }})) -> Self { |
| 4366 |
Self::new(x, v.x, v.y, v.z) |
| 4367 |
} |
| 4368 |
} |
| 4369 |
|
| 4370 |
impl From<({{ vec2_t }}, {{ scalar_t }}, {{ scalar_t }})> for {{ self_t }} { |
| 4371 |
#[inline] |
| 4372 |
fn from((v, z, w): ({{ vec2_t }}, {{ scalar_t }}, {{ scalar_t }})) -> Self { |
| 4373 |
Self::new(v.x, v.y, z, w) |
| 4374 |
} |
| 4375 |
} |
| 4376 |
|
| 4377 |
impl From<({{ vec2_t }}, {{ vec2_t }})> for {{ self_t }} { |
| 4378 |
#[inline] |
| 4379 |
fn from((v, u): ({{ vec2_t }}, {{ vec2_t }})) -> Self { |
| 4380 |
Self::new(v.x, v.y, u.x, u.y) |
| 4381 |
} |
| 4382 |
} |
| 4383 |
{% endif %} |
| 4384 |
|
| 4385 |
{% if not is_scalar %} |
| 4386 |
impl Deref for {{ self_t }} { |
| 4387 |
type Target = crate::deref::Vec{{ dim }}<{{ scalar_t }}>; |
| 4388 |
#[inline] |
| 4389 |
fn deref(&self) -> &Self::Target { |
| 4390 |
unsafe { &*(self as *const Self).cast() } |
| 4391 |
} |
| 4392 |
} |
| 4393 |
|
| 4394 |
impl DerefMut for {{ self_t }} { |
| 4395 |
#[inline] |
| 4396 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 4397 |
unsafe { &mut *(self as *mut Self).cast() } |
| 4398 |
} |
| 4399 |
} |
| 4400 |
{% endif %} |
| 4401 |
|
| 4402 |
{% if from_types %} |
| 4403 |
{% for ty in from_types %} |
| 4404 |
impl From<{{ ty }}> for {{ self_t }} { |
| 4405 |
#[inline] |
| 4406 |
fn from(v: {{ ty }}) -> Self { |
| 4407 |
Self::new( |
| 4408 |
{% for c in components %} |
| 4409 |
{{ scalar_t }}::from(v.{{ c }}), |
| 4410 |
{% endfor %} |
| 4411 |
) |
| 4412 |
} |
| 4413 |
} |
| 4414 |
{% endfor %} |
| 4415 |
{% endif %} |
| 4416 |
|
| 4417 |
{% if try_from_types %} |
| 4418 |
{% for ty in try_from_types %} |
| 4419 |
impl TryFrom<{{ ty }}> for {{ self_t }} { |
| 4420 |
type Error = core::num::TryFromIntError; |
| 4421 |
|
| 4422 |
#[inline] |
| 4423 |
fn try_from(v: {{ ty }}) -> Result<Self, Self::Error> { |
| 4424 |
Ok(Self::new( |
| 4425 |
{% for c in components %} |
| 4426 |
{{ scalar_t }}::try_from(v.{{ c }})?, |
| 4427 |
{% endfor %} |
| 4428 |
)) |
| 4429 |
} |
| 4430 |
} |
| 4431 |
{% endfor %} |
| 4432 |
{% endif %} |
| 4433 |
|
| 4434 |
impl From<{{ bvec_from_type }}> for {{ self_t }} { |
| 4435 |
#[inline] |
| 4436 |
fn from(v: {{ bvec_from_type }}) -> Self { |
| 4437 |
Self::new( |
| 4438 |
{% for c in components %} |
| 4439 |
{{ scalar_t }}::from(v.{{ c }}), |
| 4440 |
{% endfor %} |
| 4441 |
) |
| 4442 |
} |
| 4443 |
} |
| 4444 |
|
| 4445 |
{% if bveca_from_type %} |
| 4446 |
{% if bveca_from_type == "BVec4A" %} |
| 4447 |
#[cfg(not(feature = "scalar-math"))] |
| 4448 |
{%- endif %} |
| 4449 |
impl From<{{ bveca_from_type }}> for {{ self_t }} { |
| 4450 |
#[inline] |
| 4451 |
fn from(v: {{ bveca_from_type }}) -> Self { |
| 4452 |
let bool_array: [bool; {{ dim }}] = v.into(); |
| 4453 |
Self::new( |
| 4454 |
{% for c in components %} |
| 4455 |
{{ scalar_t }}::from(bool_array[{{ loop.index0 }}]), |
| 4456 |
{% endfor %} |
| 4457 |
) |
| 4458 |
} |
| 4459 |
} |
| 4460 |
{% endif %} |