Skip to content

Commit cadb7f4

Browse files
jhprattgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#131784 - Urgau:stabilize-midpoint, r=dtolnay
Stabilize unsigned and float variants of `num_midpoint` feature This PR proposes that we stabilize the unsigned variants of the [`num_midpoint`](rust-lang#110840 (comment)) feature as well as the floats variants, since they are not subject to any unresolved questions, which is equivalent to doing `(a + b) / 2` (and `(a + b) >> 1`) in a sufficiently large number. The stabilized API surface would be: ```rust /// Calculates the middle point of `self` and `rhs`. /// /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a sufficiently-large unsigned integral type. /// This implies that the result is always rounded towards negative infinity and that no overflow will ever occur. impl u{8,16,32,64,128,size} { pub const fn midpoint(self, rhs: Self) -> Self; } impl NonZeroU{8,16,32,64,size} { pub const fn midpoint(self, rhs: Self) -> Self; } impl f{32,64} { pub const fn midpoint(self, rhs: Self) -> Self; } ``` The signed variants `u{8,16,32,64,128,size}` would remain gated, until a decision is made about the rounding mode, in other words that the [unresolved questions](rust-lang#110840 (comment)) are resolved. cc `@rust-lang/libs-api` cc `@scottmcm` r? libs-api
2 parents 7b70200 + 8ffd6d7 commit cadb7f4

File tree

7 files changed

+34
-37
lines changed

7 files changed

+34
-37
lines changed

core/src/num/f128.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ impl f128 {
807807
///
808808
/// ```
809809
/// #![feature(f128)]
810-
/// #![feature(num_midpoint)]
811810
/// # // Using aarch64 because `reliable_f128_math` is needed
812811
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
813812
///
@@ -817,8 +816,8 @@ impl f128 {
817816
/// ```
818817
#[inline]
819818
#[unstable(feature = "f128", issue = "116909")]
820-
// #[unstable(feature = "num_midpoint", issue = "110840")]
821-
pub fn midpoint(self, other: f128) -> f128 {
819+
#[rustc_const_unstable(feature = "f128", issue = "116909")]
820+
pub const fn midpoint(self, other: f128) -> f128 {
822821
const LO: f128 = f128::MIN_POSITIVE * 2.;
823822
const HI: f128 = f128::MAX / 2.;
824823

core/src/num/f16.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,6 @@ impl f16 {
795795
///
796796
/// ```
797797
/// #![feature(f16)]
798-
/// #![feature(num_midpoint)]
799798
/// # #[cfg(target_arch = "aarch64")] { // FIXME(f16_F128): rust-lang/rust#123885
800799
///
801800
/// assert_eq!(1f16.midpoint(4.0), 2.5);
@@ -804,8 +803,8 @@ impl f16 {
804803
/// ```
805804
#[inline]
806805
#[unstable(feature = "f16", issue = "116909")]
807-
// #[unstable(feature = "num_midpoint", issue = "110840")]
808-
pub fn midpoint(self, other: f16) -> f16 {
806+
#[rustc_const_unstable(feature = "f128", issue = "116909")]
807+
pub const fn midpoint(self, other: f16) -> f16 {
809808
const LO: f16 = f16::MIN_POSITIVE * 2.;
810809
const HI: f16 = f16::MAX / 2.;
811810

core/src/num/f32.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -984,27 +984,27 @@ impl f32 {
984984
/// # Examples
985985
///
986986
/// ```
987-
/// #![feature(num_midpoint)]
988987
/// assert_eq!(1f32.midpoint(4.0), 2.5);
989988
/// assert_eq!((-5.5f32).midpoint(8.0), 1.25);
990989
/// ```
991990
#[inline]
992-
#[unstable(feature = "num_midpoint", issue = "110840")]
993-
pub fn midpoint(self, other: f32) -> f32 {
991+
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
992+
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
993+
pub const fn midpoint(self, other: f32) -> f32 {
994994
cfg_if! {
995+
// Allow faster implementation that have known good 64-bit float
996+
// implementations. Falling back to the branchy code on targets that don't
997+
// have 64-bit hardware floats or buggy implementations.
998+
// https://github.com/rust-lang/rust/pull/121062#issuecomment-2123408114
995999
if #[cfg(any(
9961000
target_arch = "x86_64",
9971001
target_arch = "aarch64",
998-
all(any(target_arch="riscv32", target_arch= "riscv64"), target_feature="d"),
999-
all(target_arch = "arm", target_feature="vfp2"),
1002+
all(any(target_arch = "riscv32", target_arch = "riscv64"), target_feature = "d"),
1003+
all(target_arch = "arm", target_feature = "vfp2"),
10001004
target_arch = "wasm32",
10011005
target_arch = "wasm64",
10021006
))] {
1003-
// whitelist the faster implementation to targets that have known good 64-bit float
1004-
// implementations. Falling back to the branchy code on targets that don't have
1005-
// 64-bit hardware floats or buggy implementations.
1006-
// see: https://github.com/rust-lang/rust/pull/121062#issuecomment-2123408114
1007-
((f64::from(self) + f64::from(other)) / 2.0) as f32
1007+
((self as f64 + other as f64) / 2.0) as f32
10081008
} else {
10091009
const LO: f32 = f32::MIN_POSITIVE * 2.;
10101010
const HI: f32 = f32::MAX / 2.;

core/src/num/f64.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1002,13 +1002,13 @@ impl f64 {
10021002
/// # Examples
10031003
///
10041004
/// ```
1005-
/// #![feature(num_midpoint)]
10061005
/// assert_eq!(1f64.midpoint(4.0), 2.5);
10071006
/// assert_eq!((-5.5f64).midpoint(8.0), 1.25);
10081007
/// ```
10091008
#[inline]
1010-
#[unstable(feature = "num_midpoint", issue = "110840")]
1011-
pub fn midpoint(self, other: f64) -> f64 {
1009+
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
1010+
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
1011+
pub const fn midpoint(self, other: f64) -> f64 {
10121012
const LO: f64 = f64::MIN_POSITIVE * 2.;
10131013
const HI: f64 = f64::MAX / 2.;
10141014

core/src/num/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,18 @@ macro_rules! midpoint_impl {
103103
($SelfT:ty, unsigned) => {
104104
/// Calculates the middle point of `self` and `rhs`.
105105
///
106-
/// `midpoint(a, b)` is `(a + b) >> 1` as if it were performed in a
107-
/// sufficiently-large signed integral type. This implies that the result is
108-
/// always rounded towards negative infinity and that no overflow will ever occur.
106+
/// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
107+
/// sufficiently-large unsigned integral type. This implies that the result is
108+
/// always rounded towards zero and that no overflow will ever occur.
109109
///
110110
/// # Examples
111111
///
112112
/// ```
113-
/// #![feature(num_midpoint)]
114113
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
115114
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
116115
/// ```
117-
#[unstable(feature = "num_midpoint", issue = "110840")]
116+
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
117+
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
118118
#[must_use = "this returns the result of the operation, \
119119
without modifying the original"]
120120
#[inline]
@@ -134,14 +134,14 @@ macro_rules! midpoint_impl {
134134
/// # Examples
135135
///
136136
/// ```
137-
/// #![feature(num_midpoint)]
137+
/// #![feature(num_midpoint_signed)]
138138
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
139139
#[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
140140
#[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
141141
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
142142
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
143143
/// ```
144-
#[unstable(feature = "num_midpoint", issue = "110840")]
144+
#[unstable(feature = "num_midpoint_signed", issue = "110840")]
145145
#[must_use = "this returns the result of the operation, \
146146
without modifying the original"]
147147
#[inline]
@@ -157,18 +157,18 @@ macro_rules! midpoint_impl {
157157
($SelfT:ty, $WideT:ty, unsigned) => {
158158
/// Calculates the middle point of `self` and `rhs`.
159159
///
160-
/// `midpoint(a, b)` is `(a + b) >> 1` as if it were performed in a
161-
/// sufficiently-large signed integral type. This implies that the result is
162-
/// always rounded towards negative infinity and that no overflow will ever occur.
160+
/// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
161+
/// sufficiently-large unsigned integral type. This implies that the result is
162+
/// always rounded towards zero and that no overflow will ever occur.
163163
///
164164
/// # Examples
165165
///
166166
/// ```
167-
/// #![feature(num_midpoint)]
168167
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
169168
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
170169
/// ```
171-
#[unstable(feature = "num_midpoint", issue = "110840")]
170+
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
171+
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
172172
#[must_use = "this returns the result of the operation, \
173173
without modifying the original"]
174174
#[inline]
@@ -186,14 +186,14 @@ macro_rules! midpoint_impl {
186186
/// # Examples
187187
///
188188
/// ```
189-
/// #![feature(num_midpoint)]
189+
/// #![feature(num_midpoint_signed)]
190190
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
191191
#[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
192192
#[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
193193
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
194194
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
195195
/// ```
196-
#[unstable(feature = "num_midpoint", issue = "110840")]
196+
#[unstable(feature = "num_midpoint_signed", issue = "110840")]
197197
#[must_use = "this returns the result of the operation, \
198198
without modifying the original"]
199199
#[inline]

core/src/num/nonzero.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1509,8 +1509,6 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
15091509
/// # Examples
15101510
///
15111511
/// ```
1512-
/// #![feature(num_midpoint)]
1513-
///
15141512
/// # use std::num::NonZero;
15151513
/// #
15161514
/// # fn main() { test().unwrap(); }
@@ -1524,7 +1522,8 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
15241522
/// # Some(())
15251523
/// # }
15261524
/// ```
1527-
#[unstable(feature = "num_midpoint", issue = "110840")]
1525+
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
1526+
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
15281527
#[must_use = "this returns the result of the operation, \
15291528
without modifying the original"]
15301529
#[inline]

core/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
#![feature(min_specialization)]
6565
#![feature(never_type)]
6666
#![feature(noop_waker)]
67-
#![feature(num_midpoint)]
67+
#![feature(num_midpoint_signed)]
6868
#![feature(numfmt)]
6969
#![feature(pattern)]
7070
#![feature(pointer_is_aligned_to)]

0 commit comments

Comments
 (0)