Skip to content

Commit

Permalink
make ::new usable in a const context to avoid need of using const uni…
Browse files Browse the repository at this point in the history
…on hack (#181)

* make xx::new usable in a const context to avoid need of using const union hack

* use core instead of std transmute

* use core instead of std
  • Loading branch information
mcroomp authored Dec 3, 2024
1 parent a9b883f commit adaf48b
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 158 deletions.
10 changes: 6 additions & 4 deletions src/f32x4_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ pick! {
macro_rules! const_f32_as_f32x4 {
($i:ident, $f:expr) => {
#[allow(non_upper_case_globals)]
pub const $i: f32x4 =
unsafe { ConstUnionHack128bit { f32a4: [$f; 4] }.f32x4 };
pub const $i: f32x4 = f32x4::new([$f; 4]);
};
}

Expand Down Expand Up @@ -475,8 +474,11 @@ impl CmpLt for f32x4 {
impl f32x4 {
#[inline]
#[must_use]
pub fn new(array: [f32; 4]) -> Self {
Self::from(array)
pub const fn new(array: [f32; 4]) -> Self {
#[allow(non_upper_case_globals)]
unsafe {
core::intrinsics::transmute(array)
}
}

#[inline]
Expand Down
7 changes: 3 additions & 4 deletions src/f32x8_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ pick! {
macro_rules! const_f32_as_f32x8 {
($i:ident, $f:expr) => {
#[allow(non_upper_case_globals)]
pub const $i: f32x8 =
unsafe { ConstUnionHack256bit { f32a8: [$f; 8] }.f32x8 };
pub const $i: f32x8 = f32x8::new([$f; 8]);
};
}

Expand Down Expand Up @@ -357,8 +356,8 @@ impl CmpLt for f32x8 {
impl f32x8 {
#[inline]
#[must_use]
pub fn new(array: [f32; 8]) -> Self {
Self::from(array)
pub const fn new(array: [f32; 8]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}
#[inline]
#[must_use]
Expand Down
7 changes: 3 additions & 4 deletions src/f64x2_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ pick! {
macro_rules! const_f64_as_f64x2 {
($i:ident, $f:expr) => {
#[allow(non_upper_case_globals)]
pub const $i: f64x2 =
unsafe { ConstUnionHack128bit { f64a2: [$f; 2] }.f64x2 };
pub const $i: f64x2 = f64x2::new([$f; 2]);
};
}

Expand Down Expand Up @@ -454,8 +453,8 @@ impl CmpLt for f64x2 {
impl f64x2 {
#[inline]
#[must_use]
pub fn new(array: [f64; 2]) -> Self {
Self::from(array)
pub const fn new(array: [f64; 2]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}
#[inline]
#[must_use]
Expand Down
20 changes: 4 additions & 16 deletions src/f64x4_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pick! {

macro_rules! const_f64_as_f64x4 {
($i:ident, $f:expr) => {
pub const $i: f64x4 =
unsafe { ConstUnionHack256bit { f64a4: [$f; 4] }.f64x4 };
#[allow(non_upper_case_globals)]
pub const $i: f64x4 = f64x4::new([$f; 4]);
};
}

Expand Down Expand Up @@ -356,8 +356,8 @@ impl CmpLt for f64x4 {
impl f64x4 {
#[inline]
#[must_use]
pub fn new(array: [f64; 4]) -> Self {
Self::from(array)
pub const fn new(array: [f64; 4]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}
#[inline]
#[must_use]
Expand Down Expand Up @@ -645,7 +645,6 @@ impl f64x4 {
(self & magnitude_mask) | (sign & Self::from(-0.0))
}

#[allow(non_upper_case_globals)]
#[inline]
pub fn asin_acos(self) -> (Self, Self) {
// Based on the Agner Fog "vector class library":
Expand Down Expand Up @@ -738,7 +737,6 @@ impl f64x4 {
(asin, acos)
}

#[allow(non_upper_case_globals)]
#[inline]
pub fn acos(self) -> Self {
// Based on the Agner Fog "vector class library":
Expand Down Expand Up @@ -826,7 +824,6 @@ impl f64x4 {
}
#[inline]
#[must_use]
#[allow(non_upper_case_globals)]
pub fn asin(self) -> Self {
// Based on the Agner Fog "vector class library":
// https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
Expand Down Expand Up @@ -912,7 +909,6 @@ impl f64x4 {
asin
}

#[allow(non_upper_case_globals)]
#[inline]
pub fn atan(self) -> Self {
// Based on the Agner Fog "vector class library":
Expand Down Expand Up @@ -969,7 +965,6 @@ impl f64x4 {
re
}

#[allow(non_upper_case_globals)]
#[inline]
pub fn atan2(self, x: Self) -> Self {
// Based on the Agner Fog "vector class library":
Expand Down Expand Up @@ -1052,7 +1047,6 @@ impl f64x4 {

#[inline]
#[must_use]
#[allow(non_upper_case_globals)]
pub fn sin_cos(self) -> (Self, Self) {
// Based on the Agner Fog "vector class library":
// https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
Expand Down Expand Up @@ -1194,7 +1188,6 @@ impl f64x4 {
}

#[inline]
#[allow(non_upper_case_globals)]
fn vm_pow2n(self) -> Self {
const_f64_as_f64x4!(pow2_52, 4503599627370496.0);
const_f64_as_f64x4!(bias, 1023.0);
Expand All @@ -1206,7 +1199,6 @@ impl f64x4 {
/// Calculate the exponent of a packed `f64x4`
#[inline]
#[must_use]
#[allow(non_upper_case_globals)]
pub fn exp(self) -> Self {
const_f64_as_f64x4!(P2, 1.0 / 2.0);
const_f64_as_f64x4!(P3, 1.0 / 6.0);
Expand Down Expand Up @@ -1237,7 +1229,6 @@ impl f64x4 {
}

#[inline]
#[allow(non_upper_case_globals)]
fn exponent(self) -> f64x4 {
const_f64_as_f64x4!(pow2_52, 4503599627370496.0);
const_f64_as_f64x4!(bias, 1023.0);
Expand All @@ -1250,7 +1241,6 @@ impl f64x4 {
}

#[inline]
#[allow(non_upper_case_globals)]
fn fraction_2(self) -> Self {
let t1 = cast::<_, u64x4>(self);
let t2 = cast::<_, u64x4>(
Expand Down Expand Up @@ -1304,7 +1294,6 @@ impl f64x4 {
/// Natural log (ln(x))
#[inline]
#[must_use]
#[allow(non_upper_case_globals)]
pub fn ln(self) -> Self {
const_f64_as_f64x4!(HALF, 0.5);
const_f64_as_f64x4!(P0, 7.70838733755885391666E0);
Expand Down Expand Up @@ -1366,7 +1355,6 @@ impl f64x4 {

#[inline]
#[must_use]
#[allow(non_upper_case_globals)]
pub fn pow_f64x4(self, y: Self) -> Self {
const_f64_as_f64x4!(ln2d_hi, 0.693145751953125);
const_f64_as_f64x4!(ln2d_lo, 1.42860682030941723212E-6);
Expand Down
6 changes: 3 additions & 3 deletions src/i16x16_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pick! {
}
}

int_uint_consts!(i16, 16, i16x16, i16x16, i16a16, const_i16_as_i16x16, 256);
int_uint_consts!(i16, 16, i16x16, 256);

unsafe impl Zeroable for i16x16 {}
unsafe impl Pod for i16x16 {}
Expand Down Expand Up @@ -304,8 +304,8 @@ impl From<u8x16> for i16x16 {
impl i16x16 {
#[inline]
#[must_use]
pub fn new(array: [i16; 16]) -> Self {
Self::from(array)
pub const fn new(array: [i16; 16]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions src/i16x8_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pick! {
}
}

int_uint_consts!(i16, 8, i16x8, i16x8, i16a8, const_i16_as_i16x8, 128);
int_uint_consts!(i16, 8, i16x8, 128);

unsafe impl Zeroable for i16x8 {}
unsafe impl Pod for i16x8 {}
Expand Down Expand Up @@ -441,8 +441,8 @@ impl CmpLt for i16x8 {
impl i16x8 {
#[inline]
#[must_use]
pub fn new(array: [i16; 8]) -> Self {
Self::from(array)
pub const fn new(array: [i16; 8]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions src/i32x4_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pick! {
}
}

int_uint_consts!(i32, 4, i32x4, i32x4, i32a4, const_i32_as_i32x4, 128);
int_uint_consts!(i32, 4, i32x4, 128);

unsafe impl Zeroable for i32x4 {}
unsafe impl Pod for i32x4 {}
Expand Down Expand Up @@ -472,8 +472,8 @@ impl CmpLt for i32x4 {
impl i32x4 {
#[inline]
#[must_use]
pub fn new(array: [i32; 4]) -> Self {
Self::from(array)
pub const fn new(array: [i32; 4]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}
#[inline]
#[must_use]
Expand Down
6 changes: 3 additions & 3 deletions src/i32x8_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pick! {
}
}

int_uint_consts!(i32, 8, i32x8, i32x8, i32a8, const_i32_as_i32x8, 256);
int_uint_consts!(i32, 8, i32x8, 256);

unsafe impl Zeroable for i32x8 {}
unsafe impl Pod for i32x8 {}
Expand Down Expand Up @@ -347,8 +347,8 @@ impl From<i16x8> for i32x8 {
impl i32x8 {
#[inline]
#[must_use]
pub fn new(array: [i32; 8]) -> Self {
Self::from(array)
pub const fn new(array: [i32; 8]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}

/// widens and sign extends to `i32x8`
Expand Down
6 changes: 3 additions & 3 deletions src/i64x2_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pick! {
}
}

int_uint_consts!(i64, 2, i64x2, i64x2, i64a2, const_i64_as_i64x2, 128);
int_uint_consts!(i64, 2, i64x2, 128);

unsafe impl Zeroable for i64x2 {}
unsafe impl Pod for i64x2 {}
Expand Down Expand Up @@ -379,8 +379,8 @@ impl CmpLt for i64x2 {
impl i64x2 {
#[inline]
#[must_use]
pub fn new(array: [i64; 2]) -> Self {
Self::from(array)
pub const fn new(array: [i64; 2]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}
#[inline]
#[must_use]
Expand Down
6 changes: 3 additions & 3 deletions src/i64x4_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pick! {
}
}

int_uint_consts!(i64, 4, i64x4, i64x4, i64a4, const_i64_as_i64x4, 256);
int_uint_consts!(i64, 4, i64x4, 256);

unsafe impl Zeroable for i64x4 {}
unsafe impl Pod for i64x4 {}
Expand Down Expand Up @@ -290,8 +290,8 @@ impl CmpLt for i64x4 {
impl i64x4 {
#[inline]
#[must_use]
pub fn new(array: [i64; 4]) -> Self {
Self::from(array)
pub const fn new(array: [i64; 4]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}
#[inline]
#[must_use]
Expand Down
6 changes: 3 additions & 3 deletions src/i8x16_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pick! {
}
}

int_uint_consts!(i8, 16, i8x16, i8x16, i8a16, const_i8_as_i8x16, 128);
int_uint_consts!(i8, 16, i8x16, 128);

unsafe impl Zeroable for i8x16 {}
unsafe impl Pod for i8x16 {}
Expand Down Expand Up @@ -387,8 +387,8 @@ impl CmpLt for i8x16 {
impl i8x16 {
#[inline]
#[must_use]
pub fn new(array: [i8; 16]) -> Self {
Self::from(array)
pub const fn new(array: [i8; 16]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}

/// converts `i16` to `i8`, saturating values that are too large
Expand Down
6 changes: 3 additions & 3 deletions src/i8x32_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pick! {
}
}

int_uint_consts!(i8, 32, i8x32, i8x32, i8a32, const_i8_as_i8x32, 256);
int_uint_consts!(i8, 32, i8x32, 256);

unsafe impl Zeroable for i8x32 {}
unsafe impl Pod for i8x32 {}
Expand Down Expand Up @@ -200,8 +200,8 @@ impl CmpLt for i8x32 {
impl i8x32 {
#[inline]
#[must_use]
pub fn new(array: [i8; 32]) -> Self {
Self::from(array)
pub const fn new(array: [i8; 32]) -> Self {
unsafe { core::intrinsics::transmute(array) }
}
#[inline]
#[must_use]
Expand Down
Loading

0 comments on commit adaf48b

Please sign in to comment.