std/portable-simd/crates/std_float/src/
lib.rs1#![cfg_attr(
2 feature = "as_crate",
3 feature(core_intrinsics),
4 feature(portable_simd),
5 feature(f16),
6 allow(internal_features)
7)]
8#[cfg(not(feature = "as_crate"))]
9use core::simd;
10#[cfg(feature = "as_crate")]
11use core_simd::simd;
12
13use core::intrinsics::simd as intrinsics;
14
15use simd::Simd;
16
17#[cfg(feature = "as_crate")]
18mod experimental {
19 pub trait Sealed {}
20}
21
22#[cfg(feature = "as_crate")]
23use experimental as sealed;
24
25use crate::sealed::Sealed;
26
27pub trait StdFloat: Sealed + Sized {
47 #[inline]
55 #[must_use = "method returns a new vector and does not mutate the original value"]
56 fn mul_add(self, a: Self, b: Self) -> Self {
57 unsafe { intrinsics::simd_fma(self, a, b) }
58 }
59
60 #[inline]
63 #[must_use = "method returns a new vector and does not mutate the original value"]
64 fn sqrt(self) -> Self {
65 unsafe { intrinsics::simd_fsqrt(self) }
66 }
67
68 #[inline]
71 #[must_use = "method returns a new vector and does not mutate the original value"]
72 fn sin(self) -> Self {
73 unsafe { intrinsics::simd_fsin(self) }
74 }
75
76 #[inline]
79 #[must_use = "method returns a new vector and does not mutate the original value"]
80 fn cos(self) -> Self {
81 unsafe { intrinsics::simd_fcos(self) }
82 }
83
84 #[inline]
87 #[must_use = "method returns a new vector and does not mutate the original value"]
88 fn exp(self) -> Self {
89 unsafe { intrinsics::simd_fexp(self) }
90 }
91
92 #[inline]
95 #[must_use = "method returns a new vector and does not mutate the original value"]
96 fn exp2(self) -> Self {
97 unsafe { intrinsics::simd_fexp2(self) }
98 }
99
100 #[inline]
103 #[must_use = "method returns a new vector and does not mutate the original value"]
104 fn ln(self) -> Self {
105 unsafe { intrinsics::simd_flog(self) }
106 }
107
108 #[inline]
111 #[must_use = "method returns a new vector and does not mutate the original value"]
112 fn log(self, base: Self) -> Self {
113 unsafe { intrinsics::simd_div(self.ln(), base.ln()) }
114 }
115
116 #[inline]
119 #[must_use = "method returns a new vector and does not mutate the original value"]
120 fn log2(self) -> Self {
121 unsafe { intrinsics::simd_flog2(self) }
122 }
123
124 #[inline]
127 #[must_use = "method returns a new vector and does not mutate the original value"]
128 fn log10(self) -> Self {
129 unsafe { intrinsics::simd_flog10(self) }
130 }
131
132 #[must_use = "method returns a new vector and does not mutate the original value"]
134 #[inline]
135 fn ceil(self) -> Self {
136 unsafe { intrinsics::simd_ceil(self) }
137 }
138
139 #[must_use = "method returns a new vector and does not mutate the original value"]
141 #[inline]
142 fn floor(self) -> Self {
143 unsafe { intrinsics::simd_floor(self) }
144 }
145
146 #[must_use = "method returns a new vector and does not mutate the original value"]
148 #[inline]
149 fn round(self) -> Self {
150 unsafe { intrinsics::simd_round(self) }
151 }
152
153 #[must_use = "method returns a new vector and does not mutate the original value"]
155 #[inline]
156 fn trunc(self) -> Self {
157 unsafe { intrinsics::simd_trunc(self) }
158 }
159
160 #[must_use = "method returns a new vector and does not mutate the original value"]
163 #[inline]
164 fn round_ties_even(self) -> Self {
165 unsafe { intrinsics::simd_round_ties_even(self) }
166 }
167
168 #[must_use = "method returns a new vector and does not mutate the original value"]
170 fn fract(self) -> Self;
171}
172
173impl<const N: usize> Sealed for Simd<f16, N> {}
174impl<const N: usize> Sealed for Simd<f32, N> {}
175impl<const N: usize> Sealed for Simd<f64, N> {}
176
177impl<const N: usize> StdFloat for Simd<f16, N> {
178 #[inline]
179 fn fract(self) -> Self {
180 self - self.trunc()
181 }
182}
183
184impl<const N: usize> StdFloat for Simd<f32, N> {
185 #[inline]
186 fn fract(self) -> Self {
187 self - self.trunc()
188 }
189}
190
191impl<const N: usize> StdFloat for Simd<f64, N> {
192 #[inline]
193 fn fract(self) -> Self {
194 self - self.trunc()
195 }
196}