Coverage Report

Created: 2025-12-10 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/half-2.7.1/src/num_traits.rs
Line
Count
Source
1
use crate::{bf16, f16};
2
use core::cmp::Ordering;
3
use core::{num::FpCategory, ops::Div};
4
use num_traits::{
5
    AsPrimitive, Bounded, FloatConst, FromBytes, FromPrimitive, Num, NumCast, One, ToBytes,
6
    ToPrimitive, Zero,
7
};
8
9
impl ToPrimitive for f16 {
10
    #[inline]
11
0
    fn to_i64(&self) -> Option<i64> {
12
0
        Self::to_f32(*self).to_i64()
13
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::ToPrimitive>::to_i64
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::ToPrimitive>::to_i64
14
    #[inline]
15
0
    fn to_u64(&self) -> Option<u64> {
16
0
        Self::to_f32(*self).to_u64()
17
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::ToPrimitive>::to_u64
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::ToPrimitive>::to_u64
18
    #[inline]
19
0
    fn to_i8(&self) -> Option<i8> {
20
0
        Self::to_f32(*self).to_i8()
21
0
    }
22
    #[inline]
23
0
    fn to_u8(&self) -> Option<u8> {
24
0
        Self::to_f32(*self).to_u8()
25
0
    }
26
    #[inline]
27
0
    fn to_i16(&self) -> Option<i16> {
28
0
        Self::to_f32(*self).to_i16()
29
0
    }
30
    #[inline]
31
0
    fn to_u16(&self) -> Option<u16> {
32
0
        Self::to_f32(*self).to_u16()
33
0
    }
34
    #[inline]
35
0
    fn to_i32(&self) -> Option<i32> {
36
0
        Self::to_f32(*self).to_i32()
37
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::ToPrimitive>::to_i32
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::ToPrimitive>::to_i32
38
    #[inline]
39
10
    fn to_u32(&self) -> Option<u32> {
40
10
        Self::to_f32(*self).to_u32()
41
10
    }
<half::binary16::f16 as num_traits::cast::ToPrimitive>::to_u32
Line
Count
Source
39
10
    fn to_u32(&self) -> Option<u32> {
40
10
        Self::to_f32(*self).to_u32()
41
10
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::ToPrimitive>::to_u32
42
    #[inline]
43
0
    fn to_f32(&self) -> Option<f32> {
44
0
        Some(Self::to_f32(*self))
45
0
    }
46
    #[inline]
47
0
    fn to_f64(&self) -> Option<f64> {
48
0
        Some(Self::to_f64(*self))
49
0
    }
50
}
51
52
impl FromPrimitive for f16 {
53
    #[inline]
54
40
    fn from_i64(n: i64) -> Option<Self> {
55
40
        n.to_f32().map(Self::from_f32)
56
40
    }
<half::binary16::f16 as num_traits::cast::FromPrimitive>::from_i64
Line
Count
Source
54
40
    fn from_i64(n: i64) -> Option<Self> {
55
40
        n.to_f32().map(Self::from_f32)
56
40
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::FromPrimitive>::from_i64
57
    #[inline]
58
0
    fn from_u64(n: u64) -> Option<Self> {
59
0
        n.to_f32().map(Self::from_f32)
60
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::FromPrimitive>::from_u64
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::FromPrimitive>::from_u64
61
    #[inline]
62
0
    fn from_i8(n: i8) -> Option<Self> {
63
0
        n.to_f32().map(Self::from_f32)
64
0
    }
65
    #[inline]
66
0
    fn from_u8(n: u8) -> Option<Self> {
67
0
        n.to_f32().map(Self::from_f32)
68
0
    }
69
    #[inline]
70
0
    fn from_i16(n: i16) -> Option<Self> {
71
0
        n.to_f32().map(Self::from_f32)
72
0
    }
73
    #[inline]
74
0
    fn from_u16(n: u16) -> Option<Self> {
75
0
        n.to_f32().map(Self::from_f32)
76
0
    }
77
    #[inline]
78
0
    fn from_i32(n: i32) -> Option<Self> {
79
0
        n.to_f32().map(Self::from_f32)
80
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::FromPrimitive>::from_i32
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::FromPrimitive>::from_i32
81
    #[inline]
82
0
    fn from_u32(n: u32) -> Option<Self> {
83
0
        n.to_f32().map(Self::from_f32)
84
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::FromPrimitive>::from_u32
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::FromPrimitive>::from_u32
85
    #[inline]
86
0
    fn from_f32(n: f32) -> Option<Self> {
87
0
        n.to_f32().map(Self::from_f32)
88
0
    }
89
    #[inline]
90
0
    fn from_f64(n: f64) -> Option<Self> {
91
0
        n.to_f64().map(Self::from_f64)
92
0
    }
93
}
94
95
impl Num for f16 {
96
    type FromStrRadixErr = <f32 as Num>::FromStrRadixErr;
97
98
    #[inline]
99
0
    fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
100
0
        Ok(Self::from_f32(f32::from_str_radix(str, radix)?))
101
0
    }
102
}
103
104
impl One for f16 {
105
    #[inline]
106
0
    fn one() -> Self {
107
0
        Self::ONE
108
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::identities::One>::one
Unexecuted instantiation: <half::binary16::f16 as num_traits::identities::One>::one
109
}
110
111
impl Zero for f16 {
112
    #[inline]
113
0
    fn zero() -> Self {
114
0
        Self::ZERO
115
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::identities::Zero>::zero
Unexecuted instantiation: <half::binary16::f16 as num_traits::identities::Zero>::zero
116
117
    #[inline]
118
0
    fn is_zero(&self) -> bool {
119
0
        *self == Self::ZERO
120
0
    }
121
}
122
123
impl NumCast for f16 {
124
    #[inline]
125
0
    fn from<T: ToPrimitive>(n: T) -> Option<Self> {
126
0
        n.to_f32().map(Self::from_f32)
127
0
    }
128
}
129
130
impl num_traits::float::FloatCore for f16 {
131
    #[inline]
132
0
    fn infinity() -> Self {
133
0
        Self::INFINITY
134
0
    }
135
136
    #[inline]
137
0
    fn neg_infinity() -> Self {
138
0
        Self::NEG_INFINITY
139
0
    }
140
141
    #[inline]
142
0
    fn nan() -> Self {
143
0
        Self::NAN
144
0
    }
145
146
    #[inline]
147
0
    fn neg_zero() -> Self {
148
0
        Self::NEG_ZERO
149
0
    }
150
151
    #[inline]
152
0
    fn min_value() -> Self {
153
0
        Self::MIN
154
0
    }
155
156
    #[inline]
157
0
    fn min_positive_value() -> Self {
158
0
        Self::MIN_POSITIVE
159
0
    }
160
161
    #[inline]
162
0
    fn epsilon() -> Self {
163
0
        Self::EPSILON
164
0
    }
165
166
    #[inline]
167
0
    fn max_value() -> Self {
168
0
        Self::MAX
169
0
    }
170
171
    #[inline]
172
0
    fn is_nan(self) -> bool {
173
0
        self.is_nan()
174
0
    }
175
176
    #[inline]
177
0
    fn is_infinite(self) -> bool {
178
0
        self.is_infinite()
179
0
    }
180
181
    #[inline]
182
0
    fn is_finite(self) -> bool {
183
0
        self.is_finite()
184
0
    }
185
186
    #[inline]
187
0
    fn is_normal(self) -> bool {
188
0
        self.is_normal()
189
0
    }
190
191
    #[inline]
192
0
    fn classify(self) -> FpCategory {
193
0
        self.classify()
194
0
    }
195
196
    #[inline]
197
0
    fn floor(self) -> Self {
198
0
        Self::from_f32(self.to_f32().floor())
199
0
    }
200
201
    #[inline]
202
0
    fn ceil(self) -> Self {
203
0
        Self::from_f32(self.to_f32().ceil())
204
0
    }
205
206
    #[inline]
207
0
    fn round(self) -> Self {
208
0
        Self::from_f32(self.to_f32().round())
209
0
    }
210
211
    #[inline]
212
0
    fn trunc(self) -> Self {
213
0
        Self::from_f32(self.to_f32().trunc())
214
0
    }
215
216
    #[inline]
217
0
    fn fract(self) -> Self {
218
0
        Self::from_f32(self.to_f32().fract())
219
0
    }
220
221
    #[inline]
222
0
    fn abs(self) -> Self {
223
0
        Self::from_bits(self.to_bits() & 0x7FFF)
224
0
    }
225
226
    #[inline]
227
0
    fn signum(self) -> Self {
228
0
        self.signum()
229
0
    }
230
231
    #[inline]
232
0
    fn is_sign_positive(self) -> bool {
233
0
        self.is_sign_positive()
234
0
    }
235
236
    #[inline]
237
0
    fn is_sign_negative(self) -> bool {
238
0
        self.is_sign_negative()
239
0
    }
240
241
0
    fn min(self, other: Self) -> Self {
242
0
        match self.partial_cmp(&other) {
243
            None => {
244
0
                if self.is_nan() {
245
0
                    other
246
                } else {
247
0
                    self
248
                }
249
            }
250
0
            Some(Ordering::Greater) | Some(Ordering::Equal) => other,
251
0
            Some(Ordering::Less) => self,
252
        }
253
0
    }
254
255
0
    fn max(self, other: Self) -> Self {
256
0
        match self.partial_cmp(&other) {
257
            None => {
258
0
                if self.is_nan() {
259
0
                    other
260
                } else {
261
0
                    self
262
                }
263
            }
264
0
            Some(Ordering::Greater) | Some(Ordering::Equal) => self,
265
0
            Some(Ordering::Less) => other,
266
        }
267
0
    }
268
269
    #[inline]
270
0
    fn recip(self) -> Self {
271
0
        Self::from_f32(self.to_f32().recip())
272
0
    }
273
274
    #[inline]
275
0
    fn powi(self, exp: i32) -> Self {
276
0
        Self::from_f32(self.to_f32().powi(exp))
277
0
    }
278
279
    #[inline]
280
0
    fn to_degrees(self) -> Self {
281
0
        Self::from_f32(self.to_f32().to_degrees())
282
0
    }
283
284
    #[inline]
285
0
    fn to_radians(self) -> Self {
286
0
        Self::from_f32(self.to_f32().to_radians())
287
0
    }
288
289
    #[inline]
290
0
    fn integer_decode(self) -> (u64, i16, i8) {
291
0
        num_traits::float::FloatCore::integer_decode(self.to_f32())
292
0
    }
293
}
294
295
impl num_traits::float::Float for f16 {
296
    #[inline]
297
0
    fn nan() -> Self {
298
0
        Self::NAN
299
0
    }
300
301
    #[inline]
302
0
    fn infinity() -> Self {
303
0
        Self::INFINITY
304
0
    }
305
306
    #[inline]
307
0
    fn neg_infinity() -> Self {
308
0
        Self::NEG_INFINITY
309
0
    }
310
311
    #[inline]
312
0
    fn neg_zero() -> Self {
313
0
        Self::NEG_ZERO
314
0
    }
315
316
    #[inline]
317
0
    fn min_value() -> Self {
318
0
        Self::MIN
319
0
    }
320
321
    #[inline]
322
0
    fn min_positive_value() -> Self {
323
0
        Self::MIN_POSITIVE
324
0
    }
325
326
    #[inline]
327
0
    fn epsilon() -> Self {
328
0
        Self::EPSILON
329
0
    }
330
331
    #[inline]
332
0
    fn max_value() -> Self {
333
0
        Self::MAX
334
0
    }
335
336
    #[inline]
337
0
    fn is_nan(self) -> bool {
338
0
        self.is_nan()
339
0
    }
340
341
    #[inline]
342
0
    fn is_infinite(self) -> bool {
343
0
        self.is_infinite()
344
0
    }
345
346
    #[inline]
347
0
    fn is_finite(self) -> bool {
348
0
        self.is_finite()
349
0
    }
350
351
    #[inline]
352
0
    fn is_normal(self) -> bool {
353
0
        self.is_normal()
354
0
    }
355
356
    #[inline]
357
0
    fn classify(self) -> FpCategory {
358
0
        self.classify()
359
0
    }
360
361
    #[inline]
362
0
    fn floor(self) -> Self {
363
0
        Self::from_f32(self.to_f32().floor())
364
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::floor
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::floor
365
366
    #[inline]
367
0
    fn ceil(self) -> Self {
368
0
        Self::from_f32(self.to_f32().ceil())
369
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::ceil
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::ceil
370
371
    #[inline]
372
0
    fn round(self) -> Self {
373
0
        Self::from_f32(self.to_f32().round())
374
0
    }
375
376
    #[inline]
377
0
    fn trunc(self) -> Self {
378
0
        Self::from_f32(self.to_f32().trunc())
379
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::trunc
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::trunc
380
381
    #[inline]
382
0
    fn fract(self) -> Self {
383
0
        Self::from_f32(self.to_f32().fract())
384
0
    }
385
386
    #[inline]
387
0
    fn abs(self) -> Self {
388
0
        Self::from_f32(self.to_f32().abs())
389
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::abs
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::abs
390
391
    #[inline]
392
0
    fn signum(self) -> Self {
393
0
        Self::from_f32(self.to_f32().signum())
394
0
    }
395
396
    #[inline]
397
0
    fn is_sign_positive(self) -> bool {
398
0
        self.is_sign_positive()
399
0
    }
400
401
    #[inline]
402
0
    fn is_sign_negative(self) -> bool {
403
0
        self.is_sign_negative()
404
0
    }
405
406
    #[inline]
407
0
    fn mul_add(self, a: Self, b: Self) -> Self {
408
0
        Self::from_f32(self.to_f32().mul_add(a.to_f32(), b.to_f32()))
409
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::mul_add
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::mul_add
410
411
    #[inline]
412
0
    fn recip(self) -> Self {
413
0
        Self::from_f32(self.to_f32().recip())
414
0
    }
415
416
    #[inline]
417
0
    fn powi(self, n: i32) -> Self {
418
0
        Self::from_f32(self.to_f32().powi(n))
419
0
    }
420
421
    #[inline]
422
0
    fn powf(self, n: Self) -> Self {
423
0
        Self::from_f32(self.to_f32().powf(n.to_f32()))
424
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::powf
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::powf
425
426
    #[inline]
427
0
    fn sqrt(self) -> Self {
428
0
        Self::from_f32(self.to_f32().sqrt())
429
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::sqrt
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::sqrt
430
431
    #[inline]
432
0
    fn exp(self) -> Self {
433
0
        Self::from_f32(self.to_f32().exp())
434
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::exp
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::exp
435
436
    #[inline]
437
0
    fn exp2(self) -> Self {
438
0
        Self::from_f32(self.to_f32().exp2())
439
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::exp2
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::exp2
440
441
    #[inline]
442
0
    fn ln(self) -> Self {
443
0
        Self::from_f32(self.to_f32().ln())
444
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::ln
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::ln
445
446
    #[inline]
447
0
    fn log(self, base: Self) -> Self {
448
0
        Self::from_f32(self.to_f32().log(base.to_f32()))
449
0
    }
450
451
    #[inline]
452
0
    fn log2(self) -> Self {
453
0
        Self::from_f32(self.to_f32().log2())
454
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::log2
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::log2
455
456
    #[inline]
457
0
    fn log10(self) -> Self {
458
0
        Self::from_f32(self.to_f32().log10())
459
0
    }
460
461
    #[inline]
462
0
    fn to_degrees(self) -> Self {
463
0
        Self::from_f32(self.to_f32().to_degrees())
464
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::to_degrees
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::to_degrees
465
466
    #[inline]
467
0
    fn to_radians(self) -> Self {
468
0
        Self::from_f32(self.to_f32().to_radians())
469
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::to_radians
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::to_radians
470
471
    #[inline]
472
0
    fn max(self, other: Self) -> Self {
473
0
        self.max(other)
474
0
    }
475
476
    #[inline]
477
0
    fn min(self, other: Self) -> Self {
478
0
        self.min(other)
479
0
    }
480
481
    #[inline]
482
0
    fn abs_sub(self, other: Self) -> Self {
483
0
        Self::from_f32((self.to_f32() - other.to_f32()).max(0.0))
484
0
    }
485
486
    #[inline]
487
0
    fn cbrt(self) -> Self {
488
0
        Self::from_f32(self.to_f32().cbrt())
489
0
    }
490
491
    #[inline]
492
0
    fn hypot(self, other: Self) -> Self {
493
0
        Self::from_f32(self.to_f32().hypot(other.to_f32()))
494
0
    }
495
496
    #[inline]
497
0
    fn sin(self) -> Self {
498
0
        Self::from_f32(self.to_f32().sin())
499
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::sin
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::sin
500
501
    #[inline]
502
0
    fn cos(self) -> Self {
503
0
        Self::from_f32(self.to_f32().cos())
504
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::cos
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::cos
505
506
    #[inline]
507
0
    fn tan(self) -> Self {
508
0
        Self::from_f32(self.to_f32().tan())
509
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::tan
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::tan
510
511
    #[inline]
512
0
    fn asin(self) -> Self {
513
0
        Self::from_f32(self.to_f32().asin())
514
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::asin
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::asin
515
516
    #[inline]
517
0
    fn acos(self) -> Self {
518
0
        Self::from_f32(self.to_f32().acos())
519
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::acos
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::acos
520
521
    #[inline]
522
0
    fn atan(self) -> Self {
523
0
        Self::from_f32(self.to_f32().atan())
524
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::atan
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::atan
525
526
    #[inline]
527
0
    fn atan2(self, other: Self) -> Self {
528
0
        Self::from_f32(self.to_f32().atan2(other.to_f32()))
529
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::atan2
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::atan2
530
531
    #[inline]
532
0
    fn sin_cos(self) -> (Self, Self) {
533
0
        let (sin, cos) = self.to_f32().sin_cos();
534
0
        (Self::from_f32(sin), Self::from_f32(cos))
535
0
    }
536
537
    #[inline]
538
0
    fn exp_m1(self) -> Self {
539
0
        Self::from_f32(self.to_f32().exp_m1())
540
0
    }
541
542
    #[inline]
543
0
    fn ln_1p(self) -> Self {
544
0
        Self::from_f32(self.to_f32().ln_1p())
545
0
    }
546
547
    #[inline]
548
0
    fn sinh(self) -> Self {
549
0
        Self::from_f32(self.to_f32().sinh())
550
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::sinh
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::sinh
551
552
    #[inline]
553
0
    fn cosh(self) -> Self {
554
0
        Self::from_f32(self.to_f32().cosh())
555
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::cosh
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::cosh
556
557
    #[inline]
558
66
    fn tanh(self) -> Self {
559
66
        Self::from_f32(self.to_f32().tanh())
560
66
    }
<half::binary16::f16 as num_traits::float::Float>::tanh
Line
Count
Source
558
66
    fn tanh(self) -> Self {
559
66
        Self::from_f32(self.to_f32().tanh())
560
66
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::tanh
561
562
    #[inline]
563
0
    fn asinh(self) -> Self {
564
0
        Self::from_f32(self.to_f32().asinh())
565
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::asinh
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::asinh
566
567
    #[inline]
568
0
    fn acosh(self) -> Self {
569
0
        Self::from_f32(self.to_f32().acosh())
570
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::acosh
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::acosh
571
572
    #[inline]
573
0
    fn atanh(self) -> Self {
574
0
        Self::from_f32(self.to_f32().atanh())
575
0
    }
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::atanh
Unexecuted instantiation: <half::binary16::f16 as num_traits::float::Float>::atanh
576
577
    #[inline]
578
0
    fn integer_decode(self) -> (u64, i16, i8) {
579
0
        num_traits::float::Float::integer_decode(self.to_f32())
580
0
    }
581
}
582
583
impl FloatConst for f16 {
584
    #[inline]
585
0
    fn E() -> Self {
586
0
        Self::E
587
0
    }
588
589
    #[inline]
590
0
    fn FRAC_1_PI() -> Self {
591
0
        Self::FRAC_1_PI
592
0
    }
593
594
    #[inline]
595
0
    fn FRAC_1_SQRT_2() -> Self {
596
0
        Self::FRAC_1_SQRT_2
597
0
    }
598
599
    #[inline]
600
0
    fn FRAC_2_PI() -> Self {
601
0
        Self::FRAC_2_PI
602
0
    }
603
604
    #[inline]
605
0
    fn FRAC_2_SQRT_PI() -> Self {
606
0
        Self::FRAC_2_SQRT_PI
607
0
    }
608
609
    #[inline]
610
0
    fn FRAC_PI_2() -> Self {
611
0
        Self::FRAC_PI_2
612
0
    }
613
614
    #[inline]
615
0
    fn FRAC_PI_3() -> Self {
616
0
        Self::FRAC_PI_3
617
0
    }
618
619
    #[inline]
620
0
    fn FRAC_PI_4() -> Self {
621
0
        Self::FRAC_PI_4
622
0
    }
623
624
    #[inline]
625
0
    fn FRAC_PI_6() -> Self {
626
0
        Self::FRAC_PI_6
627
0
    }
628
629
    #[inline]
630
0
    fn FRAC_PI_8() -> Self {
631
0
        Self::FRAC_PI_8
632
0
    }
633
634
    #[inline]
635
0
    fn LN_10() -> Self {
636
0
        Self::LN_10
637
0
    }
638
639
    #[inline]
640
0
    fn LN_2() -> Self {
641
0
        Self::LN_2
642
0
    }
643
644
    #[inline]
645
0
    fn LOG10_E() -> Self {
646
0
        Self::LOG10_E
647
0
    }
648
649
    #[inline]
650
0
    fn LOG2_E() -> Self {
651
0
        Self::LOG2_E
652
0
    }
653
654
    #[inline]
655
0
    fn PI() -> Self {
656
0
        Self::PI
657
0
    }
658
659
0
    fn SQRT_2() -> Self {
660
0
        Self::SQRT_2
661
0
    }
662
663
    #[inline]
664
0
    fn LOG10_2() -> Self
665
0
    where
666
0
        Self: Sized + Div<Self, Output = Self>,
667
    {
668
0
        Self::LOG10_2
669
0
    }
670
671
    #[inline]
672
0
    fn LOG2_10() -> Self
673
0
    where
674
0
        Self: Sized + Div<Self, Output = Self>,
675
    {
676
0
        Self::LOG2_10
677
0
    }
678
}
679
680
impl Bounded for f16 {
681
    #[inline]
682
0
    fn min_value() -> Self {
683
0
        f16::MIN
684
0
    }
685
686
    #[inline]
687
0
    fn max_value() -> Self {
688
0
        f16::MAX
689
0
    }
690
}
691
692
macro_rules! impl_as_primitive_to_f16 {
693
    ($ty:ty, $meth:ident) => {
694
        impl AsPrimitive<$ty> for f16 {
695
            #[inline]
696
0
            fn as_(self) -> $ty {
697
0
                self.$meth().as_()
698
0
            }
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<i64>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<u64>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<i8>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<u8>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<i16>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<u16>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<i32>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<u32>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<isize>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<usize>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<f32>>::as_
Unexecuted instantiation: <half::binary16::f16 as num_traits::cast::AsPrimitive<f64>>::as_
699
        }
700
    };
701
}
702
703
impl AsPrimitive<f16> for f16 {
704
    #[inline]
705
0
    fn as_(self) -> f16 {
706
0
        self
707
0
    }
708
}
709
710
impl_as_primitive_to_f16!(i64, to_f32);
711
impl_as_primitive_to_f16!(u64, to_f32);
712
impl_as_primitive_to_f16!(i8, to_f32);
713
impl_as_primitive_to_f16!(u8, to_f32);
714
impl_as_primitive_to_f16!(i16, to_f32);
715
impl_as_primitive_to_f16!(u16, to_f32);
716
impl_as_primitive_to_f16!(i32, to_f32);
717
impl_as_primitive_to_f16!(u32, to_f32);
718
impl_as_primitive_to_f16!(isize, to_f32);
719
impl_as_primitive_to_f16!(usize, to_f32);
720
impl_as_primitive_to_f16!(f32, to_f32);
721
impl_as_primitive_to_f16!(f64, to_f64);
722
impl_as_primitive_to_f16!(bf16, to_f32);
723
724
macro_rules! impl_as_primitive_f16_from {
725
    ($ty:ty, $meth:ident) => {
726
        impl AsPrimitive<f16> for $ty {
727
            #[inline]
728
0
            fn as_(self) -> f16 {
729
0
                f16::$meth(self.as_())
730
0
            }
Unexecuted instantiation: <i64 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <u64 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <i8 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <u8 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <i16 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <u16 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <i32 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <u32 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <isize as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <usize as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <f32 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
Unexecuted instantiation: <f64 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
731
        }
732
    };
733
}
734
735
impl_as_primitive_f16_from!(i64, from_f32);
736
impl_as_primitive_f16_from!(u64, from_f32);
737
impl_as_primitive_f16_from!(i8, from_f32);
738
impl_as_primitive_f16_from!(u8, from_f32);
739
impl_as_primitive_f16_from!(i16, from_f32);
740
impl_as_primitive_f16_from!(u16, from_f32);
741
impl_as_primitive_f16_from!(i32, from_f32);
742
impl_as_primitive_f16_from!(u32, from_f32);
743
impl_as_primitive_f16_from!(isize, from_f32);
744
impl_as_primitive_f16_from!(usize, from_f32);
745
impl_as_primitive_f16_from!(f32, from_f32);
746
impl_as_primitive_f16_from!(f64, from_f64);
747
748
impl ToBytes for f16 {
749
    type Bytes = [u8; 2];
750
751
0
    fn to_be_bytes(&self) -> Self::Bytes {
752
0
        Self::to_be_bytes(*self)
753
0
    }
754
755
0
    fn to_le_bytes(&self) -> Self::Bytes {
756
0
        Self::to_le_bytes(*self)
757
0
    }
758
759
0
    fn to_ne_bytes(&self) -> Self::Bytes {
760
0
        Self::to_ne_bytes(*self)
761
0
    }
762
}
763
764
impl FromBytes for f16 {
765
    type Bytes = [u8; 2];
766
767
0
    fn from_be_bytes(bytes: &Self::Bytes) -> Self {
768
0
        Self::from_be_bytes(*bytes)
769
0
    }
770
771
0
    fn from_le_bytes(bytes: &Self::Bytes) -> Self {
772
0
        Self::from_le_bytes(*bytes)
773
0
    }
774
775
0
    fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
776
0
        Self::from_ne_bytes(*bytes)
777
0
    }
778
}
779
780
impl ToPrimitive for bf16 {
781
    #[inline]
782
0
    fn to_i64(&self) -> Option<i64> {
783
0
        Self::to_f32(*self).to_i64()
784
0
    }
785
    #[inline]
786
0
    fn to_u64(&self) -> Option<u64> {
787
0
        Self::to_f32(*self).to_u64()
788
0
    }
789
    #[inline]
790
0
    fn to_i8(&self) -> Option<i8> {
791
0
        Self::to_f32(*self).to_i8()
792
0
    }
793
    #[inline]
794
0
    fn to_u8(&self) -> Option<u8> {
795
0
        Self::to_f32(*self).to_u8()
796
0
    }
797
    #[inline]
798
0
    fn to_i16(&self) -> Option<i16> {
799
0
        Self::to_f32(*self).to_i16()
800
0
    }
801
    #[inline]
802
0
    fn to_u16(&self) -> Option<u16> {
803
0
        Self::to_f32(*self).to_u16()
804
0
    }
805
    #[inline]
806
0
    fn to_i32(&self) -> Option<i32> {
807
0
        Self::to_f32(*self).to_i32()
808
0
    }
809
    #[inline]
810
0
    fn to_u32(&self) -> Option<u32> {
811
0
        Self::to_f32(*self).to_u32()
812
0
    }
813
    #[inline]
814
0
    fn to_f32(&self) -> Option<f32> {
815
0
        Some(Self::to_f32(*self))
816
0
    }
817
    #[inline]
818
0
    fn to_f64(&self) -> Option<f64> {
819
0
        Some(Self::to_f64(*self))
820
0
    }
821
}
822
823
impl FromPrimitive for bf16 {
824
    #[inline]
825
0
    fn from_i64(n: i64) -> Option<Self> {
826
0
        n.to_f32().map(Self::from_f32)
827
0
    }
828
    #[inline]
829
0
    fn from_u64(n: u64) -> Option<Self> {
830
0
        n.to_f32().map(Self::from_f32)
831
0
    }
832
    #[inline]
833
0
    fn from_i8(n: i8) -> Option<Self> {
834
0
        n.to_f32().map(Self::from_f32)
835
0
    }
836
    #[inline]
837
0
    fn from_u8(n: u8) -> Option<Self> {
838
0
        n.to_f32().map(Self::from_f32)
839
0
    }
840
    #[inline]
841
0
    fn from_i16(n: i16) -> Option<Self> {
842
0
        n.to_f32().map(Self::from_f32)
843
0
    }
844
    #[inline]
845
0
    fn from_u16(n: u16) -> Option<Self> {
846
0
        n.to_f32().map(Self::from_f32)
847
0
    }
848
    #[inline]
849
0
    fn from_i32(n: i32) -> Option<Self> {
850
0
        n.to_f32().map(Self::from_f32)
851
0
    }
852
    #[inline]
853
0
    fn from_u32(n: u32) -> Option<Self> {
854
0
        n.to_f32().map(Self::from_f32)
855
0
    }
856
    #[inline]
857
0
    fn from_f32(n: f32) -> Option<Self> {
858
0
        n.to_f32().map(Self::from_f32)
859
0
    }
860
    #[inline]
861
0
    fn from_f64(n: f64) -> Option<Self> {
862
0
        n.to_f64().map(Self::from_f64)
863
0
    }
864
}
865
866
impl Num for bf16 {
867
    type FromStrRadixErr = <f32 as Num>::FromStrRadixErr;
868
869
    #[inline]
870
0
    fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
871
0
        Ok(Self::from_f32(f32::from_str_radix(str, radix)?))
872
0
    }
873
}
874
875
impl One for bf16 {
876
    #[inline]
877
0
    fn one() -> Self {
878
0
        Self::ONE
879
0
    }
880
}
881
882
impl Zero for bf16 {
883
    #[inline]
884
0
    fn zero() -> Self {
885
0
        Self::ZERO
886
0
    }
887
888
    #[inline]
889
0
    fn is_zero(&self) -> bool {
890
0
        *self == Self::ZERO
891
0
    }
892
}
893
894
impl NumCast for bf16 {
895
    #[inline]
896
0
    fn from<T: ToPrimitive>(n: T) -> Option<Self> {
897
0
        n.to_f32().map(Self::from_f32)
898
0
    }
899
}
900
901
impl num_traits::float::FloatCore for bf16 {
902
    #[inline]
903
0
    fn infinity() -> Self {
904
0
        Self::INFINITY
905
0
    }
906
907
    #[inline]
908
0
    fn neg_infinity() -> Self {
909
0
        Self::NEG_INFINITY
910
0
    }
911
912
    #[inline]
913
0
    fn nan() -> Self {
914
0
        Self::NAN
915
0
    }
916
917
    #[inline]
918
0
    fn neg_zero() -> Self {
919
0
        Self::NEG_ZERO
920
0
    }
921
922
    #[inline]
923
0
    fn min_value() -> Self {
924
0
        Self::MIN
925
0
    }
926
927
    #[inline]
928
0
    fn min_positive_value() -> Self {
929
0
        Self::MIN_POSITIVE
930
0
    }
931
932
    #[inline]
933
0
    fn epsilon() -> Self {
934
0
        Self::EPSILON
935
0
    }
936
937
    #[inline]
938
0
    fn max_value() -> Self {
939
0
        Self::MAX
940
0
    }
941
942
    #[inline]
943
0
    fn is_nan(self) -> bool {
944
0
        self.is_nan()
945
0
    }
946
947
    #[inline]
948
0
    fn is_infinite(self) -> bool {
949
0
        self.is_infinite()
950
0
    }
951
952
    #[inline]
953
0
    fn is_finite(self) -> bool {
954
0
        self.is_finite()
955
0
    }
956
957
    #[inline]
958
0
    fn is_normal(self) -> bool {
959
0
        self.is_normal()
960
0
    }
961
962
    #[inline]
963
0
    fn classify(self) -> FpCategory {
964
0
        self.classify()
965
0
    }
966
967
    #[inline]
968
0
    fn floor(self) -> Self {
969
0
        Self::from_f32(self.to_f32().floor())
970
0
    }
971
972
    #[inline]
973
0
    fn ceil(self) -> Self {
974
0
        Self::from_f32(self.to_f32().ceil())
975
0
    }
976
977
    #[inline]
978
0
    fn round(self) -> Self {
979
0
        Self::from_f32(self.to_f32().round())
980
0
    }
981
982
    #[inline]
983
0
    fn trunc(self) -> Self {
984
0
        Self::from_f32(self.to_f32().trunc())
985
0
    }
986
987
    #[inline]
988
0
    fn fract(self) -> Self {
989
0
        Self::from_f32(self.to_f32().fract())
990
0
    }
991
992
    #[inline]
993
0
    fn abs(self) -> Self {
994
0
        Self::from_bits(self.to_bits() & 0x7FFF)
995
0
    }
996
997
    #[inline]
998
0
    fn signum(self) -> Self {
999
0
        self.signum()
1000
0
    }
1001
1002
    #[inline]
1003
0
    fn is_sign_positive(self) -> bool {
1004
0
        self.is_sign_positive()
1005
0
    }
1006
1007
    #[inline]
1008
0
    fn is_sign_negative(self) -> bool {
1009
0
        self.is_sign_negative()
1010
0
    }
1011
1012
0
    fn min(self, other: Self) -> Self {
1013
0
        match self.partial_cmp(&other) {
1014
            None => {
1015
0
                if self.is_nan() {
1016
0
                    other
1017
                } else {
1018
0
                    self
1019
                }
1020
            }
1021
0
            Some(Ordering::Greater) | Some(Ordering::Equal) => other,
1022
0
            Some(Ordering::Less) => self,
1023
        }
1024
0
    }
1025
1026
0
    fn max(self, other: Self) -> Self {
1027
0
        match self.partial_cmp(&other) {
1028
            None => {
1029
0
                if self.is_nan() {
1030
0
                    other
1031
                } else {
1032
0
                    self
1033
                }
1034
            }
1035
0
            Some(Ordering::Greater) | Some(Ordering::Equal) => self,
1036
0
            Some(Ordering::Less) => other,
1037
        }
1038
0
    }
1039
1040
    #[inline]
1041
0
    fn recip(self) -> Self {
1042
0
        Self::from_f32(self.to_f32().recip())
1043
0
    }
1044
1045
    #[inline]
1046
0
    fn powi(self, exp: i32) -> Self {
1047
0
        Self::from_f32(self.to_f32().powi(exp))
1048
0
    }
1049
1050
    #[inline]
1051
0
    fn to_degrees(self) -> Self {
1052
0
        Self::from_f32(self.to_f32().to_degrees())
1053
0
    }
1054
1055
    #[inline]
1056
0
    fn to_radians(self) -> Self {
1057
0
        Self::from_f32(self.to_f32().to_radians())
1058
0
    }
1059
1060
    #[inline]
1061
0
    fn integer_decode(self) -> (u64, i16, i8) {
1062
0
        num_traits::float::FloatCore::integer_decode(self.to_f32())
1063
0
    }
1064
}
1065
1066
impl num_traits::float::Float for bf16 {
1067
    #[inline]
1068
0
    fn nan() -> Self {
1069
0
        Self::NAN
1070
0
    }
1071
1072
    #[inline]
1073
0
    fn infinity() -> Self {
1074
0
        Self::INFINITY
1075
0
    }
1076
1077
    #[inline]
1078
0
    fn neg_infinity() -> Self {
1079
0
        Self::NEG_INFINITY
1080
0
    }
1081
1082
    #[inline]
1083
0
    fn neg_zero() -> Self {
1084
0
        Self::NEG_ZERO
1085
0
    }
1086
1087
    #[inline]
1088
0
    fn min_value() -> Self {
1089
0
        Self::MIN
1090
0
    }
1091
1092
    #[inline]
1093
0
    fn min_positive_value() -> Self {
1094
0
        Self::MIN_POSITIVE
1095
0
    }
1096
1097
    #[inline]
1098
0
    fn epsilon() -> Self {
1099
0
        Self::EPSILON
1100
0
    }
1101
1102
    #[inline]
1103
0
    fn max_value() -> Self {
1104
0
        Self::MAX
1105
0
    }
1106
1107
    #[inline]
1108
0
    fn is_nan(self) -> bool {
1109
0
        self.is_nan()
1110
0
    }
1111
1112
    #[inline]
1113
0
    fn is_infinite(self) -> bool {
1114
0
        self.is_infinite()
1115
0
    }
1116
1117
    #[inline]
1118
0
    fn is_finite(self) -> bool {
1119
0
        self.is_finite()
1120
0
    }
1121
1122
    #[inline]
1123
0
    fn is_normal(self) -> bool {
1124
0
        self.is_normal()
1125
0
    }
1126
1127
    #[inline]
1128
0
    fn classify(self) -> FpCategory {
1129
0
        self.classify()
1130
0
    }
1131
1132
    #[inline]
1133
0
    fn floor(self) -> Self {
1134
0
        Self::from_f32(self.to_f32().floor())
1135
0
    }
1136
1137
    #[inline]
1138
0
    fn ceil(self) -> Self {
1139
0
        Self::from_f32(self.to_f32().ceil())
1140
0
    }
1141
1142
    #[inline]
1143
0
    fn round(self) -> Self {
1144
0
        Self::from_f32(self.to_f32().round())
1145
0
    }
1146
1147
    #[inline]
1148
0
    fn trunc(self) -> Self {
1149
0
        Self::from_f32(self.to_f32().trunc())
1150
0
    }
1151
1152
    #[inline]
1153
0
    fn fract(self) -> Self {
1154
0
        Self::from_f32(self.to_f32().fract())
1155
0
    }
1156
1157
    #[inline]
1158
0
    fn abs(self) -> Self {
1159
0
        Self::from_f32(self.to_f32().abs())
1160
0
    }
1161
1162
    #[inline]
1163
0
    fn signum(self) -> Self {
1164
0
        Self::from_f32(self.to_f32().signum())
1165
0
    }
1166
1167
    #[inline]
1168
0
    fn is_sign_positive(self) -> bool {
1169
0
        self.is_sign_positive()
1170
0
    }
1171
1172
    #[inline]
1173
0
    fn is_sign_negative(self) -> bool {
1174
0
        self.is_sign_negative()
1175
0
    }
1176
1177
    #[inline]
1178
0
    fn mul_add(self, a: Self, b: Self) -> Self {
1179
0
        Self::from_f32(self.to_f32().mul_add(a.to_f32(), b.to_f32()))
1180
0
    }
1181
1182
    #[inline]
1183
0
    fn recip(self) -> Self {
1184
0
        Self::from_f32(self.to_f32().recip())
1185
0
    }
1186
1187
    #[inline]
1188
0
    fn powi(self, n: i32) -> Self {
1189
0
        Self::from_f32(self.to_f32().powi(n))
1190
0
    }
1191
1192
    #[inline]
1193
0
    fn powf(self, n: Self) -> Self {
1194
0
        Self::from_f32(self.to_f32().powf(n.to_f32()))
1195
0
    }
1196
1197
    #[inline]
1198
0
    fn sqrt(self) -> Self {
1199
0
        Self::from_f32(self.to_f32().sqrt())
1200
0
    }
1201
1202
    #[inline]
1203
0
    fn exp(self) -> Self {
1204
0
        Self::from_f32(self.to_f32().exp())
1205
0
    }
1206
1207
    #[inline]
1208
0
    fn exp2(self) -> Self {
1209
0
        Self::from_f32(self.to_f32().exp2())
1210
0
    }
1211
1212
    #[inline]
1213
0
    fn ln(self) -> Self {
1214
0
        Self::from_f32(self.to_f32().ln())
1215
0
    }
1216
1217
    #[inline]
1218
0
    fn log(self, base: Self) -> Self {
1219
0
        Self::from_f32(self.to_f32().log(base.to_f32()))
1220
0
    }
1221
1222
    #[inline]
1223
0
    fn log2(self) -> Self {
1224
0
        Self::from_f32(self.to_f32().log2())
1225
0
    }
1226
1227
    #[inline]
1228
0
    fn log10(self) -> Self {
1229
0
        Self::from_f32(self.to_f32().log10())
1230
0
    }
1231
1232
    #[inline]
1233
0
    fn to_degrees(self) -> Self {
1234
0
        Self::from_f32(self.to_f32().to_degrees())
1235
0
    }
1236
1237
    #[inline]
1238
0
    fn to_radians(self) -> Self {
1239
0
        Self::from_f32(self.to_f32().to_radians())
1240
0
    }
1241
1242
    #[inline]
1243
0
    fn max(self, other: Self) -> Self {
1244
0
        self.max(other)
1245
0
    }
1246
1247
    #[inline]
1248
0
    fn min(self, other: Self) -> Self {
1249
0
        self.min(other)
1250
0
    }
1251
1252
    #[inline]
1253
0
    fn abs_sub(self, other: Self) -> Self {
1254
0
        Self::from_f32((self.to_f32() - other.to_f32()).max(0.0))
1255
0
    }
1256
1257
    #[inline]
1258
0
    fn cbrt(self) -> Self {
1259
0
        Self::from_f32(self.to_f32().cbrt())
1260
0
    }
1261
1262
    #[inline]
1263
0
    fn hypot(self, other: Self) -> Self {
1264
0
        Self::from_f32(self.to_f32().hypot(other.to_f32()))
1265
0
    }
1266
1267
    #[inline]
1268
0
    fn sin(self) -> Self {
1269
0
        Self::from_f32(self.to_f32().sin())
1270
0
    }
1271
1272
    #[inline]
1273
0
    fn cos(self) -> Self {
1274
0
        Self::from_f32(self.to_f32().cos())
1275
0
    }
1276
1277
    #[inline]
1278
0
    fn tan(self) -> Self {
1279
0
        Self::from_f32(self.to_f32().tan())
1280
0
    }
1281
1282
    #[inline]
1283
0
    fn asin(self) -> Self {
1284
0
        Self::from_f32(self.to_f32().asin())
1285
0
    }
1286
1287
    #[inline]
1288
0
    fn acos(self) -> Self {
1289
0
        Self::from_f32(self.to_f32().acos())
1290
0
    }
1291
1292
    #[inline]
1293
0
    fn atan(self) -> Self {
1294
0
        Self::from_f32(self.to_f32().atan())
1295
0
    }
1296
1297
    #[inline]
1298
0
    fn atan2(self, other: Self) -> Self {
1299
0
        Self::from_f32(self.to_f32().atan2(other.to_f32()))
1300
0
    }
1301
1302
    #[inline]
1303
0
    fn sin_cos(self) -> (Self, Self) {
1304
0
        let (sin, cos) = self.to_f32().sin_cos();
1305
0
        (Self::from_f32(sin), Self::from_f32(cos))
1306
0
    }
1307
1308
    #[inline]
1309
0
    fn exp_m1(self) -> Self {
1310
0
        Self::from_f32(self.to_f32().exp_m1())
1311
0
    }
1312
1313
    #[inline]
1314
0
    fn ln_1p(self) -> Self {
1315
0
        Self::from_f32(self.to_f32().ln_1p())
1316
0
    }
1317
1318
    #[inline]
1319
0
    fn sinh(self) -> Self {
1320
0
        Self::from_f32(self.to_f32().sinh())
1321
0
    }
1322
1323
    #[inline]
1324
0
    fn cosh(self) -> Self {
1325
0
        Self::from_f32(self.to_f32().cosh())
1326
0
    }
1327
1328
    #[inline]
1329
0
    fn tanh(self) -> Self {
1330
0
        Self::from_f32(self.to_f32().tanh())
1331
0
    }
1332
1333
    #[inline]
1334
0
    fn asinh(self) -> Self {
1335
0
        Self::from_f32(self.to_f32().asinh())
1336
0
    }
1337
1338
    #[inline]
1339
0
    fn acosh(self) -> Self {
1340
0
        Self::from_f32(self.to_f32().acosh())
1341
0
    }
1342
1343
    #[inline]
1344
0
    fn atanh(self) -> Self {
1345
0
        Self::from_f32(self.to_f32().atanh())
1346
0
    }
1347
1348
    #[inline]
1349
0
    fn integer_decode(self) -> (u64, i16, i8) {
1350
0
        num_traits::float::Float::integer_decode(self.to_f32())
1351
0
    }
1352
}
1353
1354
impl FloatConst for bf16 {
1355
    #[inline]
1356
0
    fn E() -> Self {
1357
0
        Self::E
1358
0
    }
1359
1360
    #[inline]
1361
0
    fn FRAC_1_PI() -> Self {
1362
0
        Self::FRAC_1_PI
1363
0
    }
1364
1365
    #[inline]
1366
0
    fn FRAC_1_SQRT_2() -> Self {
1367
0
        Self::FRAC_1_SQRT_2
1368
0
    }
1369
1370
    #[inline]
1371
0
    fn FRAC_2_PI() -> Self {
1372
0
        Self::FRAC_2_PI
1373
0
    }
1374
1375
    #[inline]
1376
0
    fn FRAC_2_SQRT_PI() -> Self {
1377
0
        Self::FRAC_2_SQRT_PI
1378
0
    }
1379
1380
    #[inline]
1381
0
    fn FRAC_PI_2() -> Self {
1382
0
        Self::FRAC_PI_2
1383
0
    }
1384
1385
    #[inline]
1386
0
    fn FRAC_PI_3() -> Self {
1387
0
        Self::FRAC_PI_3
1388
0
    }
1389
1390
    #[inline]
1391
0
    fn FRAC_PI_4() -> Self {
1392
0
        Self::FRAC_PI_4
1393
0
    }
1394
1395
    #[inline]
1396
0
    fn FRAC_PI_6() -> Self {
1397
0
        Self::FRAC_PI_6
1398
0
    }
1399
1400
    #[inline]
1401
0
    fn FRAC_PI_8() -> Self {
1402
0
        Self::FRAC_PI_8
1403
0
    }
1404
1405
    #[inline]
1406
0
    fn LN_10() -> Self {
1407
0
        Self::LN_10
1408
0
    }
1409
1410
    #[inline]
1411
0
    fn LN_2() -> Self {
1412
0
        Self::LN_2
1413
0
    }
1414
1415
    #[inline]
1416
0
    fn LOG10_E() -> Self {
1417
0
        Self::LOG10_E
1418
0
    }
1419
1420
    #[inline]
1421
0
    fn LOG2_E() -> Self {
1422
0
        Self::LOG2_E
1423
0
    }
1424
1425
    #[inline]
1426
0
    fn PI() -> Self {
1427
0
        Self::PI
1428
0
    }
1429
1430
    #[inline]
1431
0
    fn SQRT_2() -> Self {
1432
0
        Self::SQRT_2
1433
0
    }
1434
1435
    #[inline]
1436
0
    fn LOG10_2() -> Self
1437
0
    where
1438
0
        Self: Sized + Div<Self, Output = Self>,
1439
    {
1440
0
        Self::LOG10_2
1441
0
    }
1442
1443
    #[inline]
1444
0
    fn LOG2_10() -> Self
1445
0
    where
1446
0
        Self: Sized + Div<Self, Output = Self>,
1447
    {
1448
0
        Self::LOG2_10
1449
0
    }
1450
}
1451
1452
impl Bounded for bf16 {
1453
    #[inline]
1454
0
    fn min_value() -> Self {
1455
0
        bf16::MIN
1456
0
    }
1457
1458
    #[inline]
1459
0
    fn max_value() -> Self {
1460
0
        bf16::MAX
1461
0
    }
1462
}
1463
1464
impl AsPrimitive<bf16> for bf16 {
1465
    #[inline]
1466
0
    fn as_(self) -> bf16 {
1467
0
        self
1468
0
    }
1469
}
1470
1471
macro_rules! impl_as_primitive_to_bf16 {
1472
    ($ty:ty, $meth:ident) => {
1473
        impl AsPrimitive<$ty> for bf16 {
1474
            #[inline]
1475
0
            fn as_(self) -> $ty {
1476
0
                self.$meth().as_()
1477
0
            }
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<i64>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<u64>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<i8>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<u8>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<i16>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<u16>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<i32>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<u32>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<isize>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<usize>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<f32>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<f64>>::as_
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::cast::AsPrimitive<half::binary16::f16>>::as_
1478
        }
1479
    };
1480
}
1481
1482
impl_as_primitive_to_bf16!(i64, to_f32);
1483
impl_as_primitive_to_bf16!(u64, to_f32);
1484
impl_as_primitive_to_bf16!(i8, to_f32);
1485
impl_as_primitive_to_bf16!(u8, to_f32);
1486
impl_as_primitive_to_bf16!(i16, to_f32);
1487
impl_as_primitive_to_bf16!(u16, to_f32);
1488
impl_as_primitive_to_bf16!(i32, to_f32);
1489
impl_as_primitive_to_bf16!(u32, to_f32);
1490
impl_as_primitive_to_bf16!(isize, to_f32);
1491
impl_as_primitive_to_bf16!(usize, to_f32);
1492
impl_as_primitive_to_bf16!(f32, to_f32);
1493
impl_as_primitive_to_bf16!(f64, to_f64);
1494
impl_as_primitive_to_bf16!(f16, to_f32);
1495
1496
macro_rules! impl_as_primitive_bf16_from {
1497
    ($ty:ty, $meth:ident) => {
1498
        impl AsPrimitive<bf16> for $ty {
1499
            #[inline]
1500
0
            fn as_(self) -> bf16 {
1501
0
                bf16::$meth(self.as_())
1502
0
            }
Unexecuted instantiation: <i64 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <u64 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <i8 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <u8 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <i16 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <u16 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <i32 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <u32 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <isize as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <usize as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <f32 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
Unexecuted instantiation: <f64 as num_traits::cast::AsPrimitive<half::bfloat::bf16>>::as_
1503
        }
1504
    };
1505
}
1506
1507
impl_as_primitive_bf16_from!(i64, from_f32);
1508
impl_as_primitive_bf16_from!(u64, from_f32);
1509
impl_as_primitive_bf16_from!(i8, from_f32);
1510
impl_as_primitive_bf16_from!(u8, from_f32);
1511
impl_as_primitive_bf16_from!(i16, from_f32);
1512
impl_as_primitive_bf16_from!(u16, from_f32);
1513
impl_as_primitive_bf16_from!(i32, from_f32);
1514
impl_as_primitive_bf16_from!(u32, from_f32);
1515
impl_as_primitive_bf16_from!(isize, from_f32);
1516
impl_as_primitive_bf16_from!(usize, from_f32);
1517
impl_as_primitive_bf16_from!(f32, from_f32);
1518
impl_as_primitive_bf16_from!(f64, from_f64);
1519
1520
impl ToBytes for bf16 {
1521
    type Bytes = [u8; 2];
1522
1523
0
    fn to_be_bytes(&self) -> Self::Bytes {
1524
0
        Self::to_be_bytes(*self)
1525
0
    }
1526
1527
0
    fn to_le_bytes(&self) -> Self::Bytes {
1528
0
        Self::to_le_bytes(*self)
1529
0
    }
1530
1531
0
    fn to_ne_bytes(&self) -> Self::Bytes {
1532
0
        Self::to_ne_bytes(*self)
1533
0
    }
1534
}
1535
1536
impl FromBytes for bf16 {
1537
    type Bytes = [u8; 2];
1538
1539
0
    fn from_be_bytes(bytes: &Self::Bytes) -> Self {
1540
0
        Self::from_be_bytes(*bytes)
1541
0
    }
1542
1543
0
    fn from_le_bytes(bytes: &Self::Bytes) -> Self {
1544
0
        Self::from_le_bytes(*bytes)
1545
0
    }
1546
1547
0
    fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
1548
0
        Self::from_ne_bytes(*bytes)
1549
0
    }
1550
}
1551
1552
macro_rules! impl_signed {
1553
    ($ty:ty) => {
1554
        impl ::num_traits::Signed for $ty {
1555
            #[inline]
1556
0
            fn abs(&self) -> Self {
1557
0
                ::num_traits::float::Float::abs(*self)
1558
0
            }
Unexecuted instantiation: <half::binary16::f16 as num_traits::sign::Signed>::abs
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::sign::Signed>::abs
1559
1560
            #[inline]
1561
0
            fn abs_sub(&self, other: &Self) -> Self {
1562
0
                ::num_traits::float::Float::abs_sub(*self, *other)
1563
0
            }
Unexecuted instantiation: <half::binary16::f16 as num_traits::sign::Signed>::abs_sub
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::sign::Signed>::abs_sub
1564
1565
            #[inline]
1566
0
            fn signum(&self) -> Self {
1567
0
                ::num_traits::float::Float::signum(*self)
1568
0
            }
Unexecuted instantiation: <half::binary16::f16 as num_traits::sign::Signed>::signum
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::sign::Signed>::signum
1569
1570
            #[inline]
1571
0
            fn is_positive(&self) -> bool {
1572
0
                ::num_traits::float::Float::is_sign_positive(*self)
1573
0
            }
Unexecuted instantiation: <half::binary16::f16 as num_traits::sign::Signed>::is_positive
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::sign::Signed>::is_positive
1574
1575
            #[inline]
1576
0
            fn is_negative(&self) -> bool {
1577
0
                ::num_traits::float::Float::is_sign_negative(*self)
1578
0
            }
Unexecuted instantiation: <half::binary16::f16 as num_traits::sign::Signed>::is_negative
Unexecuted instantiation: <half::bfloat::bf16 as num_traits::sign::Signed>::is_negative
1579
        }
1580
    };
1581
}
1582
1583
impl_signed!(f16);
1584
impl_signed!(bf16);