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