Skip to main content

simply_simd/register/
float.rs

1use num_traits::NumCast;
2
3use crate::architectures::interface::*;
4use crate::register::Simd;
5use crate::simd_types::*;
6
7impl<T: SimdFloat, A: Arch> Simd<T, A> {
8    #[inline(always)]
9    pub fn floor(self) -> Self {
10        unsafe {
11            Self::new(match T::TYPE {
12                SimdType::F64 => self.data.floor_f64(),
13                SimdType::F32 => self.data.floor_f32(),
14                _ => unreachable!(),
15            })
16        }
17    }
18
19    #[inline(always)]
20    pub fn round(self) -> Self {
21        unsafe {
22            Self::new(match T::TYPE {
23                SimdType::F64 => self.data.round_f64(),
24                SimdType::F32 => self.data.round_f32(),
25                _ => unreachable!(),
26            })
27        }
28    }
29
30    #[inline(always)]
31    pub fn ceil(self) -> Self {
32        unsafe {
33            Self::new(match T::TYPE {
34                SimdType::F64 => self.data.ceil_f64(),
35                SimdType::F32 => self.data.ceil_f32(),
36                _ => unreachable!(),
37            })
38        }
39    }
40
41    #[inline(always)]
42    pub fn fract(self) -> Self {
43        self - self.floor()
44    }
45
46    #[inline(always)]
47    pub fn mul_add(self, mult: Self, add: Self) -> Self {
48        unsafe {
49            Self::new(match T::TYPE {
50                SimdType::F64 => self.data.mul_add_f64(mult.data, add.data),
51                SimdType::F32 => self.data.mul_add_f32(mult.data, add.data),
52                _ => unreachable!(),
53            })
54        }
55    }
56
57    #[inline(always)]
58    pub fn mul_sub(self, mult: Self, sub: Self) -> Self {
59        unsafe {
60            Self::new(match T::TYPE {
61                SimdType::F64 => self.data.mul_sub_f64(mult.data, sub.data),
62                SimdType::F32 => self.data.mul_sub_f32(mult.data, sub.data),
63                _ => unreachable!(),
64            })
65        }
66    }
67
68    #[inline(always)]
69    pub fn negated_mul_add(self, mult: Self, add: Self) -> Self {
70        unsafe {
71            Self::new(match T::TYPE {
72                SimdType::F64 => self.data.negated_mul_add_f64(mult.data, add.data),
73                SimdType::F32 => self.data.negated_mul_add_f32(mult.data, add.data),
74                _ => unreachable!(),
75            })
76        }
77    }
78
79    #[inline(always)]
80    pub fn negated_mul_sub(self, mult: Self, sub: Self) -> Self {
81        unsafe {
82            Self::new(match T::TYPE {
83                SimdType::F64 => self.data.negated_mul_sub_f64(mult.data, sub.data),
84                SimdType::F32 => self.data.negated_mul_sub_f32(mult.data, sub.data),
85                _ => unreachable!(),
86            })
87        }
88    }
89
90    // === Casts ===
91
92    #[inline(always)]
93    pub fn cast_int_trunc(self) -> Simd<T::Signed, A> {
94        unsafe { Simd::new(self.data.float_to_int_trunc()) }
95    }
96
97    #[inline(always)]
98    pub fn cast_int_round(self) -> Simd<T::Signed, A> {
99        unsafe { Simd::new(self.data.float_to_int_round()) }
100    }
101
102    // TODO: INCORRECT for edge cases.
103    #[inline(always)]
104    pub fn cast_uint_trunc(self) -> Simd<T::Unsigned, A> {
105        unsafe { Simd::new(self.data.float_to_int_trunc()) }
106    }
107
108    #[inline(always)]
109    pub fn cast_uint_round(self) -> Simd<T::Unsigned, A> {
110        unsafe { Simd::new(self.data.float_to_int_round()) }
111    }
112
113    // TODO: Move this into quick-noise later.
114    #[inline(always)]
115    pub fn quintic_lerp(self) -> Self {
116        let six = Self::splat(NumCast::from(6.0).unwrap());
117        let ten = Self::splat(NumCast::from(10.0).unwrap());
118        let fifteen = Self::splat(NumCast::from(15.0).unwrap());
119        let t = self;
120        t * t * t * t.mul_add(t.mul_sub(six, fifteen), ten)
121    }
122
123    #[inline(always)]
124    pub fn cubic_lerp(self) -> Self {
125        let neg_two = Self::splat(NumCast::from(-2.0).unwrap());
126        let three = Self::splat(NumCast::from(3.0).unwrap());
127        let t = self;
128        t * t * t.mul_add(neg_two, three)
129    }
130
131    pub fn sqrt(self) -> Self {
132        unsafe {
133            Self::new(match T::TYPE {
134                SimdType::F64 => self.data.sqrt_f64(),
135                SimdType::F32 => self.data.sqrt_f32(),
136                _ => unreachable!(),
137            })
138        }
139    }
140
141    pub fn abs(self) -> Self {
142        unsafe {
143            Self::new(match T::TYPE {
144                SimdType::F64 => Simd::<u64, A>::splat(T::SIGN_MASK as u64)
145                    .data
146                    .and(self.data),
147                SimdType::F32 => Simd::<u32, A>::splat(T::SIGN_MASK as u32)
148                    .data
149                    .and(self.data),
150                _ => unreachable!(),
151            })
152        }
153    }
154
155    /// Only rsqrt_f32 currently supported.
156    pub fn rsqrt(self) -> Self {
157        unsafe { Self::new(self.data.rsqrt_f32()) }
158    }
159}
160
161