hpt_types/scalars/
_f32.rs1use crate::type_promote::{
2 BitWiseOut2, Eval2, FloatOutBinary2, FloatOutUnary2, NormalOut2, NormalOutUnary2,
3};
4impl FloatOutBinary2 for f32 {
5 #[inline(always)]
6 fn __div(self, rhs: Self) -> Self {
7 self / rhs
8 }
9 #[inline(always)]
10 fn __log(self, base: Self) -> Self {
11 self.log(base)
12 }
13 #[inline(always)]
14 fn __hypot(self, rhs: Self) -> Self {
15 self.hypot(rhs)
16 }
17 #[inline(always)]
18 fn __pow(self, rhs: Self) -> Self {
19 self.powf(rhs)
20 }
21}
22
23impl NormalOut2 for f32 {
24 #[inline(always)]
25 fn __add(self, rhs: Self) -> Self {
26 self + rhs
27 }
28
29 #[inline(always)]
30 fn __sub(self, rhs: Self) -> Self {
31 self - rhs
32 }
33
34 #[inline(always)]
35 fn __mul_add(self, a: Self, b: Self) -> Self {
36 #[cfg(target_feature = "fma")]
37 return self.mul_add(a, b);
38 #[cfg(all(not(target_feature = "fma"), target_arch = "x86_64"))]
39 return std::hint::black_box((self * a) + b);
40 #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
41 return self.mul_add(a, b);
42 }
43
44 #[inline(always)]
45 fn __mul(self, rhs: Self) -> Self {
46 self * rhs
47 }
48
49 #[inline(always)]
50 fn __rem(self, rhs: Self) -> Self {
51 self % rhs
52 }
53
54 #[inline(always)]
55 fn __max(self, rhs: Self) -> Self {
56 self.max(rhs)
57 }
58
59 #[inline(always)]
60 fn __min(self, rhs: Self) -> Self {
61 self.min(rhs)
62 }
63
64 #[inline(always)]
65 fn __clamp(self, min: Self, max: Self) -> Self {
66 self.clamp(min, max)
67 }
68}
69
70impl NormalOutUnary2 for f32 {
71 #[inline(always)]
72 fn __square(self) -> Self {
73 self * self
74 }
75
76 #[inline(always)]
77 fn __abs(self) -> Self {
78 self.abs()
79 }
80
81 #[inline(always)]
82 fn __ceil(self) -> Self {
83 self.ceil()
84 }
85
86 #[inline(always)]
87 fn __floor(self) -> Self {
88 self.floor()
89 }
90
91 #[inline(always)]
92 fn __neg(self) -> Self {
93 -self
94 }
95
96 #[inline(always)]
97 fn __round(self) -> Self {
98 self.round()
99 }
100
101 #[inline(always)]
102 fn __signum(self) -> Self {
103 self.signum()
104 }
105
106 #[inline(always)]
107 fn __trunc(self) -> Self {
108 self.trunc()
109 }
110
111 #[inline(always)]
112 fn __leaky_relu(self, alpha: Self) -> Self {
113 self.max(0.0) + alpha * self.min(0.0)
114 }
115
116 #[inline(always)]
117 fn __relu(self) -> Self {
118 self.max(0.0)
119 }
120
121 #[inline(always)]
122 fn __relu6(self) -> Self {
123 self.max(0.0).min(6.0)
124 }
125
126 #[inline(always)]
127 fn __copysign(self, rhs: Self) -> Self {
128 self.copysign(rhs)
129 }
130}
131
132impl BitWiseOut2 for f32 {
133 #[inline(always)]
134 fn __bitand(self, rhs: Self) -> Self {
135 f32::from_bits(self.to_bits() & rhs.to_bits())
136 }
137
138 #[inline(always)]
139 fn __bitor(self, rhs: Self) -> Self {
140 f32::from_bits(self.to_bits() | rhs.to_bits())
141 }
142
143 #[inline(always)]
144 fn __bitxor(self, rhs: Self) -> Self {
145 f32::from_bits(self.to_bits() ^ rhs.to_bits())
146 }
147
148 #[inline(always)]
149 fn __not(self) -> Self {
150 f32::from_bits(!self.to_bits())
151 }
152
153 #[inline(always)]
154 fn __shl(self, _: Self) -> Self {
155 panic!("Shift operations are not supported for f32")
156 }
157
158 #[inline(always)]
159 fn __shr(self, _: Self) -> Self {
160 panic!("Shift operations are not supported for f32")
161 }
162}
163
164impl Eval2 for f32 {
165 type Output = bool;
166 #[inline(always)]
167 fn __is_nan(&self) -> Self::Output {
168 self.is_nan()
169 }
170
171 #[inline(always)]
172 fn __is_true(&self) -> Self::Output {
173 *self != 0.0 && !self.is_nan()
174 }
175
176 #[inline(always)]
177 fn __is_inf(&self) -> Self::Output {
178 self.is_infinite()
179 }
180}
181
182impl FloatOutUnary2 for f32 {
183 #[inline(always)]
184 fn __exp(self) -> Self {
185 self.exp()
186 }
187 #[inline(always)]
188 fn __expm1(self) -> Self {
189 self.exp_m1()
190 }
191 #[inline(always)]
192 fn __exp2(self) -> Self {
193 self.exp2()
194 }
195 #[inline(always)]
196 fn __ln(self) -> Self {
197 self.ln()
198 }
199 #[inline(always)]
200 fn __log1p(self) -> Self {
201 self.ln_1p()
202 }
203 #[inline(always)]
204 fn __celu(self, scale: Self) -> Self {
205 let gt_mask = (self > 0.0) as i32 as f32;
206 gt_mask * self + (1.0 - gt_mask) * (scale * (self.exp() - 1.0))
207 }
208 #[inline(always)]
209 fn __log2(self) -> Self {
210 self.log2()
211 }
212 #[inline(always)]
213 fn __log10(self) -> Self {
214 self.log10()
215 }
216 #[inline(always)]
217 fn __sqrt(self) -> Self {
218 self.sqrt()
219 }
220 #[inline(always)]
221 fn __sin(self) -> Self {
222 self.sin()
223 }
224 #[inline(always)]
225 fn __cos(self) -> Self {
226 self.cos()
227 }
228 #[inline(always)]
229 fn __tan(self) -> Self {
230 self.tan()
231 }
232 #[inline(always)]
233 fn __asin(self) -> Self {
234 self.asin()
235 }
236 #[inline(always)]
237 fn __acos(self) -> Self {
238 self.acos()
239 }
240 #[inline(always)]
241 fn __atan(self) -> Self {
242 self.atan()
243 }
244 #[inline(always)]
245 fn __sinh(self) -> Self {
246 self.sinh()
247 }
248 #[inline(always)]
249 fn __cosh(self) -> Self {
250 self.cosh()
251 }
252 #[inline(always)]
253 fn __tanh(self) -> Self {
254 self.tanh()
255 }
256 #[inline(always)]
257 fn __asinh(self) -> Self {
258 self.asinh()
259 }
260 #[inline(always)]
261 fn __acosh(self) -> Self {
262 self.acosh()
263 }
264 #[inline(always)]
265 fn __atanh(self) -> Self {
266 self.atanh()
267 }
268 #[inline(always)]
269 fn __recip(self) -> Self {
270 self.recip()
271 }
272 #[inline(always)]
273 fn __erf(self) -> Self {
274 libm::erff(self)
275 }
276
277 #[inline(always)]
278 fn __sigmoid(self) -> Self {
279 1.0 / (1.0 + (-self).exp())
280 }
281
282 fn __elu(self, alpha: Self) -> Self {
283 self.max(0.0) + alpha * self.exp_m1().min(0.0)
284 }
285
286 fn __gelu(self) -> Self {
287 0.5 * self * (libm::erff(self * std::f32::consts::FRAC_1_SQRT_2) + 1.0)
288 }
289
290 fn __selu(self, alpha: Self, scale: Self) -> Self {
291 scale * self.__elu(alpha)
292 }
293
294 fn __hard_sigmoid(self) -> Self {
295 let result = self * (1.0 / 6.0) + 0.5;
296 result.min(1.0).max(0.0)
297 }
298
299 fn __hard_swish(self) -> Self {
300 self * ((self + 3.0).clamp(0.0, 6.0) / 6.0)
301 }
302
303 fn __softplus(self) -> Self {
304 (1.0 + self.exp()).ln()
305 }
306
307 fn __softsign(self) -> Self {
308 self / (1.0 + self.abs())
309 }
310
311 fn __mish(self) -> Self {
312 self * ((1.0 + self.exp()).ln()).tanh()
313 }
314
315 fn __cbrt(self) -> Self {
316 libm::cbrtf(self)
317 }
318
319 fn __sincos(self) -> (Self, Self) {
320 self.sin_cos()
321 }
322
323 fn __atan2(self, rhs: Self) -> Self {
324 self.atan2(rhs)
325 }
326
327 fn __exp10(self) -> Self {
328 10f32.powf(self)
329 }
330}