compiler_builtins\libm\src\math\support/
float_traits.rs1#![allow(unknown_lints)] use core::{fmt, mem, ops};
4
5use super::int_traits::{CastFrom, Int, MinInt};
6
7#[allow(dead_code)] pub trait Float:
11 Copy
12 + fmt::Debug
13 + PartialEq
14 + PartialOrd
15 + ops::AddAssign
16 + ops::MulAssign
17 + ops::Add<Output = Self>
18 + ops::Sub<Output = Self>
19 + ops::Mul<Output = Self>
20 + ops::Div<Output = Self>
21 + ops::Rem<Output = Self>
22 + ops::Neg<Output = Self>
23 + 'static
24{
25 type Int: Int<OtherSign = Self::SignedInt, Unsigned = Self::Int>;
27
28 type SignedInt: Int
30 + MinInt<OtherSign = Self::Int, Unsigned = Self::Int>
31 + ops::Neg<Output = Self::SignedInt>;
32
33 const ZERO: Self;
34 const NEG_ZERO: Self;
35 const ONE: Self;
36 const NEG_ONE: Self;
37 const INFINITY: Self;
38 const NEG_INFINITY: Self;
39 const NAN: Self;
40 const NEG_NAN: Self;
41 const MAX: Self;
42 const MIN: Self;
43 const EPSILON: Self;
44 const PI: Self;
45 const NEG_PI: Self;
46 const FRAC_PI_2: Self;
47
48 const MIN_POSITIVE_NORMAL: Self;
49
50 const BITS: u32;
52
53 const SIG_BITS: u32;
55
56 const EXP_BITS: u32 = Self::BITS - Self::SIG_BITS - 1;
58
59 const EXP_SAT: u32 = (1 << Self::EXP_BITS) - 1;
64
65 const EXP_BIAS: u32 = Self::EXP_SAT >> 1;
67
68 const EXP_MAX: i32 = Self::EXP_BIAS as i32;
70
71 const EXP_MIN: i32 = -(Self::EXP_MAX - 1);
73
74 const EXP_MIN_SUBNORM: i32 = Self::EXP_MIN - Self::SIG_BITS as i32;
76
77 const SIGN_MASK: Self::Int;
79
80 const SIG_MASK: Self::Int;
82
83 const EXP_MASK: Self::Int;
85
86 const IMPLICIT_BIT: Self::Int;
88
89 fn to_bits(self) -> Self::Int;
91
92 #[allow(dead_code)]
94 fn to_bits_signed(self) -> Self::SignedInt {
95 self.to_bits().signed()
96 }
97
98 #[allow(dead_code)]
100 fn biteq(self, rhs: Self) -> bool {
101 self.to_bits() == rhs.to_bits()
102 }
103
104 #[allow(dead_code)]
110 fn eq_repr(self, rhs: Self) -> bool {
111 if self.is_nan() && rhs.is_nan() {
112 true
113 } else {
114 self.biteq(rhs)
115 }
116 }
117
118 fn is_nan(self) -> bool;
120
121 fn is_infinite(self) -> bool;
123
124 fn is_sign_negative(self) -> bool;
126
127 fn is_sign_positive(self) -> bool {
129 !self.is_sign_negative()
130 }
131
132 #[allow(dead_code)]
134 fn is_subnormal(self) -> bool {
135 (self.to_bits() & Self::EXP_MASK) == Self::Int::ZERO
136 }
137
138 fn ex(self) -> u32 {
140 u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_SAT
141 }
142
143 fn exp_unbiased(self) -> i32 {
145 self.ex().signed() - (Self::EXP_BIAS as i32)
146 }
147
148 #[allow(dead_code)]
150 fn frac(self) -> Self::Int {
151 self.to_bits() & Self::SIG_MASK
152 }
153
154 fn from_bits(a: Self::Int) -> Self;
156
157 fn from_parts(negative: bool, exponent: u32, significand: Self::Int) -> Self {
159 let sign = if negative {
160 Self::Int::ONE
161 } else {
162 Self::Int::ZERO
163 };
164 Self::from_bits(
165 (sign << (Self::BITS - 1))
166 | (Self::Int::cast_from(exponent & Self::EXP_SAT) << Self::SIG_BITS)
167 | (significand & Self::SIG_MASK),
168 )
169 }
170
171 #[allow(dead_code)]
172 fn abs(self) -> Self;
173
174 fn copysign(self, other: Self) -> Self;
176
177 fn fma(self, y: Self, z: Self) -> Self;
179
180 #[allow(dead_code)]
182 fn normalize(significand: Self::Int) -> (i32, Self::Int);
183
184 #[allow(dead_code)]
186 fn signum(self) -> Self {
187 if self.is_nan() {
188 self
189 } else {
190 Self::ONE.copysign(self)
191 }
192 }
193
194 fn canonicalize(self) -> Self {
197 self * Self::ONE
201 }
202}
203
204pub type IntTy<F> = <F as Float>::Int;
206
207macro_rules! float_impl {
208 (
209 $ty:ident,
210 $ity:ident,
211 $sity:ident,
212 $bits:expr,
213 $significand_bits:expr,
214 $from_bits:path,
215 $to_bits:path,
216 $fma_fn:ident,
217 $fma_intrinsic:ident
218 ) => {
219 impl Float for $ty {
220 type Int = $ity;
221 type SignedInt = $sity;
222
223 const ZERO: Self = 0.0;
224 const NEG_ZERO: Self = -0.0;
225 const ONE: Self = 1.0;
226 const NEG_ONE: Self = -1.0;
227 const INFINITY: Self = Self::INFINITY;
228 const NEG_INFINITY: Self = Self::NEG_INFINITY;
229 const NAN: Self = Self::NAN;
230 const NEG_NAN: Self = $from_bits($to_bits(Self::NAN) | Self::SIGN_MASK);
233 const MAX: Self = -Self::MIN;
234 const MIN: Self = $from_bits(Self::Int::MAX & !(1 << Self::SIG_BITS));
236 const EPSILON: Self = <$ty>::EPSILON;
237
238 const MIN_POSITIVE_NORMAL: Self = $from_bits(1 << Self::SIG_BITS);
240
241 const PI: Self = core::$ty::consts::PI;
242 const NEG_PI: Self = -Self::PI;
243 const FRAC_PI_2: Self = core::$ty::consts::FRAC_PI_2;
244
245 const BITS: u32 = $bits;
246 const SIG_BITS: u32 = $significand_bits;
247
248 const SIGN_MASK: Self::Int = 1 << (Self::BITS - 1);
249 const SIG_MASK: Self::Int = (1 << Self::SIG_BITS) - 1;
250 const EXP_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIG_MASK);
251 const IMPLICIT_BIT: Self::Int = 1 << Self::SIG_BITS;
252
253 fn to_bits(self) -> Self::Int {
254 self.to_bits()
255 }
256 fn is_nan(self) -> bool {
257 self.is_nan()
258 }
259 fn is_infinite(self) -> bool {
260 self.is_infinite()
261 }
262 fn is_sign_negative(self) -> bool {
263 self.is_sign_negative()
264 }
265 fn from_bits(a: Self::Int) -> Self {
266 Self::from_bits(a)
267 }
268 fn abs(self) -> Self {
269 cfg_if! {
270 if #[cfg(intrinsics_enabled)] {
272 self.abs()
273 } else {
274 super::super::generic::fabs(self)
275 }
276 }
277 }
278 fn copysign(self, other: Self) -> Self {
279 cfg_if! {
280 if #[cfg(intrinsics_enabled)] {
282 self.copysign(other)
283 } else {
284 super::super::generic::copysign(self, other)
285 }
286 }
287 }
288 fn fma(self, y: Self, z: Self) -> Self {
289 cfg_if! {
290 if #[cfg(intrinsics_enabled)] {
292 unsafe{ core::intrinsics::$fma_intrinsic(self, y, z) }
293 } else {
294 super::super::$fma_fn(self, y, z)
295 }
296 }
297 }
298 fn normalize(significand: Self::Int) -> (i32, Self::Int) {
299 let shift = significand.leading_zeros().wrapping_sub(Self::EXP_BITS);
300 (
301 1i32.wrapping_sub(shift as i32),
302 significand << shift as Self::Int,
303 )
304 }
305 }
306 };
307}
308
309#[cfg(f16_enabled)]
310float_impl!(
311 f16,
312 u16,
313 i16,
314 16,
315 10,
316 f16::from_bits,
317 f16::to_bits,
318 fmaf16,
319 fmaf16
320);
321float_impl!(
322 f32,
323 u32,
324 i32,
325 32,
326 23,
327 f32_from_bits,
328 f32_to_bits,
329 fmaf,
330 fmaf32
331);
332float_impl!(
333 f64,
334 u64,
335 i64,
336 64,
337 52,
338 f64_from_bits,
339 f64_to_bits,
340 fma,
341 fmaf64
342);
343#[cfg(f128_enabled)]
344float_impl!(
345 f128,
346 u128,
347 i128,
348 128,
349 112,
350 f128::from_bits,
351 f128::to_bits,
352 fmaf128,
353 fmaf128
354);
355
356#[allow(unnecessary_transmutes)] pub const fn f32_from_bits(bits: u32) -> f32 {
361 unsafe { mem::transmute::<u32, f32>(bits) }
363}
364
365#[allow(unnecessary_transmutes)] pub const fn f32_to_bits(x: f32) -> u32 {
368 unsafe { mem::transmute::<f32, u32>(x) }
370}
371
372#[allow(unnecessary_transmutes)] pub const fn f64_from_bits(bits: u64) -> f64 {
375 unsafe { mem::transmute::<u64, f64>(bits) }
377}
378
379#[allow(unnecessary_transmutes)] pub const fn f64_to_bits(x: f64) -> u64 {
382 unsafe { mem::transmute::<f64, u64>(x) }
384}
385
386pub trait DFloat: Float {
388 type H: HFloat<D = Self>;
390
391 fn narrow(self) -> Self::H;
393}
394
395pub trait HFloat: Float {
397 type D: DFloat<H = Self>;
399
400 fn widen(self) -> Self::D;
402}
403
404macro_rules! impl_d_float {
405 ($($X:ident $D:ident),*) => {
406 $(
407 impl DFloat for $D {
408 type H = $X;
409
410 fn narrow(self) -> Self::H {
411 self as $X
412 }
413 }
414 )*
415 };
416}
417
418macro_rules! impl_h_float {
419 ($($H:ident $X:ident),*) => {
420 $(
421 impl HFloat for $H {
422 type D = $X;
423
424 fn widen(self) -> Self::D {
425 self as $X
426 }
427 }
428 )*
429 };
430}
431
432impl_d_float!(f32 f64);
433#[cfg(f16_enabled)]
434impl_d_float!(f16 f32);
435#[cfg(f128_enabled)]
436impl_d_float!(f64 f128);
437
438impl_h_float!(f32 f64);
439#[cfg(f16_enabled)]
440impl_h_float!(f16 f32);
441#[cfg(f128_enabled)]
442impl_h_float!(f64 f128);
443
444#[cfg(test)]
445mod tests {
446 use super::*;
447
448 #[test]
449 #[cfg(f16_enabled)]
450 fn check_f16() {
451 assert_eq!(f16::EXP_SAT, 0b11111);
453 assert_eq!(f16::EXP_BIAS, 15);
454 assert_eq!(f16::EXP_MAX, 15);
455 assert_eq!(f16::EXP_MIN, -14);
456 assert_eq!(f16::EXP_MIN_SUBNORM, -24);
457
458 assert_eq!(f16::FRAC_PI_2.exp_unbiased(), 0);
460 assert_eq!((1.0f16 / 2.0).exp_unbiased(), -1);
461 assert_eq!(f16::MAX.exp_unbiased(), 15);
462 assert_eq!(f16::MIN.exp_unbiased(), 15);
463 assert_eq!(f16::MIN_POSITIVE.exp_unbiased(), -14);
464 assert_eq!(f16::ZERO.exp_unbiased(), -15);
467 assert_eq!(f16::from_bits(0x1).exp_unbiased(), -15);
468 assert_eq!(f16::MIN_POSITIVE, f16::MIN_POSITIVE_NORMAL);
469
470 assert_biteq!(f16::from_parts(true, f16::EXP_BIAS, 0), -1.0f16);
472 assert_biteq!(f16::from_parts(false, 0, 1), f16::from_bits(0x1));
473 }
474
475 #[test]
476 fn check_f32() {
477 assert_eq!(f32::EXP_SAT, 0b11111111);
479 assert_eq!(f32::EXP_BIAS, 127);
480 assert_eq!(f32::EXP_MAX, 127);
481 assert_eq!(f32::EXP_MIN, -126);
482 assert_eq!(f32::EXP_MIN_SUBNORM, -149);
483
484 assert_eq!(f32::FRAC_PI_2.exp_unbiased(), 0);
486 assert_eq!((1.0f32 / 2.0).exp_unbiased(), -1);
487 assert_eq!(f32::MAX.exp_unbiased(), 127);
488 assert_eq!(f32::MIN.exp_unbiased(), 127);
489 assert_eq!(f32::MIN_POSITIVE.exp_unbiased(), -126);
490 assert_eq!(f32::ZERO.exp_unbiased(), -127);
493 assert_eq!(f32::from_bits(0x1).exp_unbiased(), -127);
494 assert_eq!(f32::MIN_POSITIVE, f32::MIN_POSITIVE_NORMAL);
495
496 assert_biteq!(f32::from_parts(true, f32::EXP_BIAS, 0), -1.0f32);
498 assert_biteq!(
499 f32::from_parts(false, 10 + f32::EXP_BIAS, 0),
500 hf32!("0x1p10")
501 );
502 assert_biteq!(f32::from_parts(false, 0, 1), f32::from_bits(0x1));
503 }
504
505 #[test]
506 fn check_f64() {
507 assert_eq!(f64::EXP_SAT, 0b11111111111);
509 assert_eq!(f64::EXP_BIAS, 1023);
510 assert_eq!(f64::EXP_MAX, 1023);
511 assert_eq!(f64::EXP_MIN, -1022);
512 assert_eq!(f64::EXP_MIN_SUBNORM, -1074);
513
514 assert_eq!(f64::FRAC_PI_2.exp_unbiased(), 0);
516 assert_eq!((1.0f64 / 2.0).exp_unbiased(), -1);
517 assert_eq!(f64::MAX.exp_unbiased(), 1023);
518 assert_eq!(f64::MIN.exp_unbiased(), 1023);
519 assert_eq!(f64::MIN_POSITIVE.exp_unbiased(), -1022);
520 assert_eq!(f64::ZERO.exp_unbiased(), -1023);
523 assert_eq!(f64::from_bits(0x1).exp_unbiased(), -1023);
524 assert_eq!(f64::MIN_POSITIVE, f64::MIN_POSITIVE_NORMAL);
525
526 assert_biteq!(f64::from_parts(true, f64::EXP_BIAS, 0), -1.0f64);
528 assert_biteq!(
529 f64::from_parts(false, 10 + f64::EXP_BIAS, 0),
530 hf64!("0x1p10")
531 );
532 assert_biteq!(f64::from_parts(false, 0, 1), f64::from_bits(0x1));
533 }
534
535 #[test]
536 #[cfg(f128_enabled)]
537 fn check_f128() {
538 assert_eq!(f128::EXP_SAT, 0b111111111111111);
540 assert_eq!(f128::EXP_BIAS, 16383);
541 assert_eq!(f128::EXP_MAX, 16383);
542 assert_eq!(f128::EXP_MIN, -16382);
543 assert_eq!(f128::EXP_MIN_SUBNORM, -16494);
544
545 assert_eq!(f128::FRAC_PI_2.exp_unbiased(), 0);
547 assert_eq!((1.0f128 / 2.0).exp_unbiased(), -1);
548 assert_eq!(f128::MAX.exp_unbiased(), 16383);
549 assert_eq!(f128::MIN.exp_unbiased(), 16383);
550 assert_eq!(f128::MIN_POSITIVE.exp_unbiased(), -16382);
551 assert_eq!(f128::ZERO.exp_unbiased(), -16383);
554 assert_eq!(f128::from_bits(0x1).exp_unbiased(), -16383);
555 assert_eq!(f128::MIN_POSITIVE, f128::MIN_POSITIVE_NORMAL);
556
557 assert_biteq!(f128::from_parts(true, f128::EXP_BIAS, 0), -1.0f128);
559 assert_biteq!(f128::from_parts(false, 0, 1), f128::from_bits(0x1));
560 }
561}