Skip to content

Commit 707d656

Browse files
authored
Unrolled build for rust-lang#122470
Rollup merge of rust-lang#122470 - tgross35:f16-f128-step4-libs-min, r=Amanieu `f16` and `f128` step 4: basic library support This is the next step after rust-lang#121926, another portion of rust-lang#114607 Tracking issue: rust-lang#116909 This PR adds the most basic operations to `f16` and `f128` that get lowered as LLVM intrinsics. This is a very small step but it seemed reasonable enough to add unopinionated basic operations before the larger modules that are built on top of them. r? ```@Amanieu``` since you were pretty involved in the RFC cc ```@compiler-errors``` ```@rustbot``` label +T-libs-api +S-blocked +F-f16_and_f128
2 parents aa067fb + 311ad55 commit 707d656

File tree

20 files changed

+482
-50
lines changed

20 files changed

+482
-50
lines changed

library/core/src/clone.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,10 @@ mod impls {
227227
impl_clone! {
228228
usize u8 u16 u32 u64 u128
229229
isize i8 i16 i32 i64 i128
230-
f32 f64
230+
f16 f32 f64 f128
231231
bool char
232232
}
233233

234-
#[cfg(not(bootstrap))]
235-
impl_clone! { f16 f128 }
236-
237234
#[unstable(feature = "never_type", issue = "35121")]
238235
impl Clone for ! {
239236
#[inline]

library/core/src/cmp.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1497,12 +1497,9 @@ mod impls {
14971497
}
14981498

14991499
partial_eq_impl! {
1500-
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64
1500+
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
15011501
}
15021502

1503-
#[cfg(not(bootstrap))]
1504-
partial_eq_impl! { f16 f128 }
1505-
15061503
macro_rules! eq_impl {
15071504
($($t:ty)*) => ($(
15081505
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1553,10 +1550,7 @@ mod impls {
15531550
}
15541551
}
15551552

1556-
partial_ord_impl! { f32 f64 }
1557-
1558-
#[cfg(not(bootstrap))]
1559-
partial_ord_impl! { f16 f128 }
1553+
partial_ord_impl! { f16 f32 f64 f128 }
15601554

15611555
macro_rules! ord_impl {
15621556
($($t:ty)*) => ($(

library/core/src/convert/num.rs

+7
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ macro_rules! impl_float_to_int {
3434
}
3535
}
3636

37+
impl_float_to_int!(f16 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
3738
impl_float_to_int!(f32 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
3839
impl_float_to_int!(f64 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
40+
impl_float_to_int!(f128 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
3941

4042
// Conversion traits for primitive integer and float types
4143
// Conversions T -> T are covered by a blanket impl and therefore excluded
@@ -163,7 +165,12 @@ impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
163165
impl_from!(u32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
164166

165167
// float -> float
168+
impl_from!(f16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
169+
impl_from!(f16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
170+
impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
166171
impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
172+
impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
173+
impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
167174

168175
macro_rules! impl_float_from_bool {
169176
($float:ty) => {

library/core/src/fmt/nofloat.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ macro_rules! floating {
1111
};
1212
}
1313

14+
floating! { f16 }
1415
floating! { f32 }
1516
floating! { f64 }
17+
floating! { f128 }

library/core/src/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@
200200
//
201201
// Language features:
202202
// tidy-alphabetical-start
203-
#![cfg_attr(not(bootstrap), feature(f128))]
204-
#![cfg_attr(not(bootstrap), feature(f16))]
205203
#![feature(abi_unadjusted)]
206204
#![feature(adt_const_params)]
207205
#![feature(allow_internal_unsafe)]
@@ -226,6 +224,8 @@
226224
#![feature(doc_notable_trait)]
227225
#![feature(effects)]
228226
#![feature(extern_types)]
227+
#![feature(f128)]
228+
#![feature(f16)]
229229
#![feature(freeze_impls)]
230230
#![feature(fundamental)]
231231
#![feature(generic_arg_infer)]
@@ -347,6 +347,10 @@ pub mod u8;
347347
#[path = "num/shells/usize.rs"]
348348
pub mod usize;
349349

350+
#[path = "num/f128.rs"]
351+
pub mod f128;
352+
#[path = "num/f16.rs"]
353+
pub mod f16;
350354
#[path = "num/f32.rs"]
351355
pub mod f32;
352356
#[path = "num/f64.rs"]

library/core/src/marker.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -422,17 +422,11 @@ marker_impls! {
422422
Copy for
423423
usize, u8, u16, u32, u64, u128,
424424
isize, i8, i16, i32, i64, i128,
425-
f32, f64,
425+
f16, f32, f64, f128,
426426
bool, char,
427427
{T: ?Sized} *const T,
428428
{T: ?Sized} *mut T,
429-
}
430429

431-
#[cfg(not(bootstrap))]
432-
marker_impls! {
433-
#[stable(feature = "rust1", since = "1.0.0")]
434-
Copy for
435-
f16, f128,
436430
}
437431

438432
#[unstable(feature = "never_type", issue = "35121")]

library/core/src/num/f128.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Constants for the `f128` quadruple-precision floating point type.
2+
//!
3+
//! *[See also the `f128` primitive type][f128].*
4+
//!
5+
//! Mathematically significant numbers are provided in the `consts` sub-module.
6+
//!
7+
//! For the constants defined directly in this module
8+
//! (as distinct from those defined in the `consts` sub-module),
9+
//! new code should instead use the associated constants
10+
//! defined directly on the `f128` type.
11+
12+
#![unstable(feature = "f128", issue = "116909")]
13+
14+
/// Basic mathematical constants.
15+
#[unstable(feature = "f128", issue = "116909")]
16+
pub mod consts {}

library/core/src/num/f16.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Constants for the `f16` half-precision floating point type.
2+
//!
3+
//! *[See also the `f16` primitive type][f16].*
4+
//!
5+
//! Mathematically significant numbers are provided in the `consts` sub-module.
6+
//!
7+
//! For the constants defined directly in this module
8+
//! (as distinct from those defined in the `consts` sub-module),
9+
//! new code should instead use the associated constants
10+
//! defined directly on the `f16` type.
11+
12+
#![unstable(feature = "f16", issue = "116909")]
13+
14+
/// Basic mathematical constants.
15+
#[unstable(feature = "f16", issue = "116909")]
16+
pub mod consts {}

library/core/src/ops/arith.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ macro_rules! add_impl {
109109
)*)
110110
}
111111

112-
add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
112+
add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
113113

114114
/// The subtraction operator `-`.
115115
///
@@ -218,7 +218,7 @@ macro_rules! sub_impl {
218218
)*)
219219
}
220220

221-
sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
221+
sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
222222

223223
/// The multiplication operator `*`.
224224
///
@@ -348,7 +348,7 @@ macro_rules! mul_impl {
348348
)*)
349349
}
350350

351-
mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
351+
mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
352352

353353
/// The division operator `/`.
354354
///
@@ -506,7 +506,7 @@ macro_rules! div_impl_float {
506506
)*)
507507
}
508508

509-
div_impl_float! { f32 f64 }
509+
div_impl_float! { f16 f32 f64 f128 }
510510

511511
/// The remainder operator `%`.
512512
///
@@ -623,7 +623,7 @@ macro_rules! rem_impl_float {
623623
)*)
624624
}
625625

626-
rem_impl_float! { f32 f64 }
626+
rem_impl_float! { f16 f32 f64 f128 }
627627

628628
/// The unary negation operator `-`.
629629
///
@@ -698,7 +698,7 @@ macro_rules! neg_impl {
698698
)*)
699699
}
700700

701-
neg_impl! { isize i8 i16 i32 i64 i128 f32 f64 }
701+
neg_impl! { isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
702702

703703
/// The addition assignment operator `+=`.
704704
///
@@ -765,7 +765,7 @@ macro_rules! add_assign_impl {
765765
)+)
766766
}
767767

768-
add_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
768+
add_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
769769

770770
/// The subtraction assignment operator `-=`.
771771
///
@@ -832,7 +832,7 @@ macro_rules! sub_assign_impl {
832832
)+)
833833
}
834834

835-
sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
835+
sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
836836

837837
/// The multiplication assignment operator `*=`.
838838
///
@@ -890,7 +890,7 @@ macro_rules! mul_assign_impl {
890890
)+)
891891
}
892892

893-
mul_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
893+
mul_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
894894

895895
/// The division assignment operator `/=`.
896896
///
@@ -947,7 +947,7 @@ macro_rules! div_assign_impl {
947947
)+)
948948
}
949949

950-
div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
950+
div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
951951

952952
/// The remainder assignment operator `%=`.
953953
///
@@ -1008,4 +1008,4 @@ macro_rules! rem_assign_impl {
10081008
)+)
10091009
}
10101010

1011-
rem_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
1011+
rem_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }

library/core/src/primitive_docs.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,22 @@ mod prim_tuple {}
10741074
#[doc(hidden)]
10751075
impl<T> (T,) {}
10761076

1077+
#[rustc_doc_primitive = "f16"]
1078+
#[doc(alias = "half")]
1079+
/// A 16-bit floating point type (specifically, the "binary16" type defined in IEEE 754-2008).
1080+
///
1081+
/// This type is very similar to [`prim@f32`] but has decreased precision because it uses half as many
1082+
/// bits. Please see [the documentation for [`prim@f32`] or [Wikipedia on
1083+
/// half-precision values][wikipedia] for more information.
1084+
///
1085+
/// *[See also the `std::f16::consts` module](crate::f16::consts).*
1086+
///
1087+
/// [wikipedia]: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
1088+
#[unstable(feature = "f16", issue = "116909")]
1089+
mod prim_f16 {}
1090+
10771091
#[rustc_doc_primitive = "f32"]
1092+
#[doc(alias = "single")]
10781093
/// A 32-bit floating point type (specifically, the "binary32" type defined in IEEE 754-2008).
10791094
///
10801095
/// This type can represent a wide range of decimal numbers, like `3.5`, `27`,
@@ -1143,6 +1158,7 @@ impl<T> (T,) {}
11431158
mod prim_f32 {}
11441159

11451160
#[rustc_doc_primitive = "f64"]
1161+
#[doc(alias = "double")]
11461162
/// A 64-bit floating point type (specifically, the "binary64" type defined in IEEE 754-2008).
11471163
///
11481164
/// This type is very similar to [`f32`], but has increased
@@ -1157,6 +1173,20 @@ mod prim_f32 {}
11571173
#[stable(feature = "rust1", since = "1.0.0")]
11581174
mod prim_f64 {}
11591175

1176+
#[rustc_doc_primitive = "f128"]
1177+
#[doc(alias = "quad")]
1178+
/// A 128-bit floating point type (specifically, the "binary128" type defined in IEEE 754-2008).
1179+
///
1180+
/// This type is very similar to [`prim@f32`] and [`prim@f64`], but has increased precision by using twice
1181+
/// as many bits as `f64`. Please see [the documentation for [`prim@f32`] or [Wikipedia on
1182+
/// quad-precision values][wikipedia] for more information.
1183+
///
1184+
/// *[See also the `std::f128::consts` module](crate::f128::consts).*
1185+
///
1186+
/// [wikipedia]: https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format
1187+
#[unstable(feature = "f128", issue = "116909")]
1188+
mod prim_f128 {}
1189+
11601190
#[rustc_doc_primitive = "i8"]
11611191
//
11621192
/// The 8-bit signed integer type.

library/std/src/f128.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Constants for the `f128` double-precision floating point type.
2+
//!
3+
//! *[See also the `f128` primitive type](primitive@f128).*
4+
//!
5+
//! Mathematically significant numbers are provided in the `consts` sub-module.
6+
7+
#[cfg(test)]
8+
mod tests;
9+
10+
#[unstable(feature = "f128", issue = "116909")]
11+
pub use core::f128::consts;

library/std/src/f128/tests.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![allow(dead_code)] // FIXME(f16_f128): remove once constants are used
2+
3+
/// Smallest number
4+
const TINY_BITS: u128 = 0x1;
5+
/// Next smallest number
6+
const TINY_UP_BITS: u128 = 0x2;
7+
/// Exponent = 0b11...10, Sifnificand 0b1111..10. Min val > 0
8+
const MAX_DOWN_BITS: u128 = 0x7ffeffffffffffffffffffffffffffff;
9+
/// Zeroed exponent, full significant
10+
const LARGEST_SUBNORMAL_BITS: u128 = 0x0000ffffffffffffffffffffffffffff;
11+
/// Exponent = 0b1, zeroed significand
12+
const SMALLEST_NORMAL_BITS: u128 = 0x00010000000000000000000000000000;
13+
/// First pattern over the mantissa
14+
const NAN_MASK1: u128 = 0x0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa;
15+
/// Second pattern over the mantissa
16+
const NAN_MASK2: u128 = 0x00005555555555555555555555555555;
17+
18+
/// Compare by value
19+
#[allow(unused_macros)]
20+
macro_rules! assert_f128_eq {
21+
($a:expr, $b:expr) => {
22+
let (l, r): (&f128, &f128) = (&$a, &$b);
23+
assert_eq!(*l, *r, "\na: {:#0130x}\nb: {:#0130x}", l.to_bits(), r.to_bits())
24+
};
25+
}
26+
27+
/// Compare by representation
28+
#[allow(unused_macros)]
29+
macro_rules! assert_f128_biteq {
30+
($a:expr, $b:expr) => {
31+
let (l, r): (&f128, &f128) = (&$a, &$b);
32+
let lb = l.to_bits();
33+
let rb = r.to_bits();
34+
assert_eq!(
35+
lb, rb,
36+
"float {:?} is not bitequal to {:?}.\na: {:#0130x}\nb: {:#0130x}",
37+
*l, *r, lb, rb
38+
);
39+
};
40+
}

library/std/src/f16.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Constants for the `f16` double-precision floating point type.
2+
//!
3+
//! *[See also the `f16` primitive type](primitive@f16).*
4+
//!
5+
//! Mathematically significant numbers are provided in the `consts` sub-module.
6+
7+
#[cfg(test)]
8+
mod tests;
9+
10+
#[unstable(feature = "f16", issue = "116909")]
11+
pub use core::f16::consts;

0 commit comments

Comments
 (0)