mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-13 04:26:48 +00:00
Rollup merge of #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`](https://github.com/rust-lang/rust/issues/110840#issue-1684506201) 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](https://github.com/rust-lang/rust/issues/110840#issue-1684506201) are resolved. cc `@rust-lang/libs-api` cc `@scottmcm` r? libs-api
This commit is contained in:
commit
5880752b9a
@ -807,7 +807,6 @@ impl f128 {
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(f128)]
|
||||
/// #![feature(num_midpoint)]
|
||||
/// # // Using aarch64 because `reliable_f128_math` is needed
|
||||
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
|
||||
///
|
||||
@ -817,8 +816,8 @@ impl f128 {
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "f128", issue = "116909")]
|
||||
// #[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
pub fn midpoint(self, other: f128) -> f128 {
|
||||
#[rustc_const_unstable(feature = "f128", issue = "116909")]
|
||||
pub const fn midpoint(self, other: f128) -> f128 {
|
||||
const LO: f128 = f128::MIN_POSITIVE * 2.;
|
||||
const HI: f128 = f128::MAX / 2.;
|
||||
|
||||
|
@ -795,7 +795,6 @@ impl f16 {
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(f16)]
|
||||
/// #![feature(num_midpoint)]
|
||||
/// # #[cfg(target_arch = "aarch64")] { // FIXME(f16_F128): rust-lang/rust#123885
|
||||
///
|
||||
/// assert_eq!(1f16.midpoint(4.0), 2.5);
|
||||
@ -804,8 +803,8 @@ impl f16 {
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "f16", issue = "116909")]
|
||||
// #[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
pub fn midpoint(self, other: f16) -> f16 {
|
||||
#[rustc_const_unstable(feature = "f128", issue = "116909")]
|
||||
pub const fn midpoint(self, other: f16) -> f16 {
|
||||
const LO: f16 = f16::MIN_POSITIVE * 2.;
|
||||
const HI: f16 = f16::MAX / 2.;
|
||||
|
||||
|
@ -984,27 +984,27 @@ impl f32 {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(num_midpoint)]
|
||||
/// assert_eq!(1f32.midpoint(4.0), 2.5);
|
||||
/// assert_eq!((-5.5f32).midpoint(8.0), 1.25);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
pub fn midpoint(self, other: f32) -> f32 {
|
||||
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub const fn midpoint(self, other: f32) -> f32 {
|
||||
cfg_if! {
|
||||
// Allow faster implementation that have known good 64-bit float
|
||||
// implementations. Falling back to the branchy code on targets that don't
|
||||
// have 64-bit hardware floats or buggy implementations.
|
||||
// https://github.com/rust-lang/rust/pull/121062#issuecomment-2123408114
|
||||
if #[cfg(any(
|
||||
target_arch = "x86_64",
|
||||
target_arch = "aarch64",
|
||||
all(any(target_arch="riscv32", target_arch= "riscv64"), target_feature="d"),
|
||||
all(target_arch = "arm", target_feature="vfp2"),
|
||||
all(any(target_arch = "riscv32", target_arch = "riscv64"), target_feature = "d"),
|
||||
all(target_arch = "arm", target_feature = "vfp2"),
|
||||
target_arch = "wasm32",
|
||||
target_arch = "wasm64",
|
||||
))] {
|
||||
// whitelist the faster implementation to targets that have known good 64-bit float
|
||||
// implementations. Falling back to the branchy code on targets that don't have
|
||||
// 64-bit hardware floats or buggy implementations.
|
||||
// see: https://github.com/rust-lang/rust/pull/121062#issuecomment-2123408114
|
||||
((f64::from(self) + f64::from(other)) / 2.0) as f32
|
||||
((self as f64 + other as f64) / 2.0) as f32
|
||||
} else {
|
||||
const LO: f32 = f32::MIN_POSITIVE * 2.;
|
||||
const HI: f32 = f32::MAX / 2.;
|
||||
|
@ -1002,13 +1002,13 @@ impl f64 {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(num_midpoint)]
|
||||
/// assert_eq!(1f64.midpoint(4.0), 2.5);
|
||||
/// assert_eq!((-5.5f64).midpoint(8.0), 1.25);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
pub fn midpoint(self, other: f64) -> f64 {
|
||||
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
pub const fn midpoint(self, other: f64) -> f64 {
|
||||
const LO: f64 = f64::MIN_POSITIVE * 2.;
|
||||
const HI: f64 = f64::MAX / 2.;
|
||||
|
||||
|
@ -103,18 +103,18 @@ macro_rules! midpoint_impl {
|
||||
($SelfT:ty, unsigned) => {
|
||||
/// Calculates the middle point of `self` and `rhs`.
|
||||
///
|
||||
/// `midpoint(a, b)` is `(a + b) >> 1` as if it were performed in a
|
||||
/// sufficiently-large signed integral type. This implies that the result is
|
||||
/// always rounded towards negative infinity and that no overflow will ever occur.
|
||||
/// `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 zero and that no overflow will ever occur.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(num_midpoint)]
|
||||
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
|
||||
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
|
||||
/// ```
|
||||
#[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline]
|
||||
@ -134,14 +134,14 @@ macro_rules! midpoint_impl {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(num_midpoint)]
|
||||
/// #![feature(num_midpoint_signed)]
|
||||
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
|
||||
#[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
|
||||
#[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
|
||||
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
|
||||
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
|
||||
/// ```
|
||||
#[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
#[unstable(feature = "num_midpoint_signed", issue = "110840")]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline]
|
||||
@ -157,18 +157,18 @@ macro_rules! midpoint_impl {
|
||||
($SelfT:ty, $WideT:ty, unsigned) => {
|
||||
/// Calculates the middle point of `self` and `rhs`.
|
||||
///
|
||||
/// `midpoint(a, b)` is `(a + b) >> 1` as if it were performed in a
|
||||
/// sufficiently-large signed integral type. This implies that the result is
|
||||
/// always rounded towards negative infinity and that no overflow will ever occur.
|
||||
/// `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 zero and that no overflow will ever occur.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(num_midpoint)]
|
||||
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
|
||||
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
|
||||
/// ```
|
||||
#[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline]
|
||||
@ -186,14 +186,14 @@ macro_rules! midpoint_impl {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(num_midpoint)]
|
||||
/// #![feature(num_midpoint_signed)]
|
||||
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
|
||||
#[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
|
||||
#[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
|
||||
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
|
||||
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
|
||||
/// ```
|
||||
#[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
#[unstable(feature = "num_midpoint_signed", issue = "110840")]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline]
|
||||
|
@ -1509,8 +1509,6 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(num_midpoint)]
|
||||
///
|
||||
/// # use std::num::NonZero;
|
||||
/// #
|
||||
/// # fn main() { test().unwrap(); }
|
||||
@ -1524,7 +1522,8 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
|
||||
/// # Some(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[unstable(feature = "num_midpoint", issue = "110840")]
|
||||
#[stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[rustc_const_stable(feature = "num_midpoint", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline]
|
||||
|
@ -64,7 +64,7 @@
|
||||
#![feature(min_specialization)]
|
||||
#![feature(never_type)]
|
||||
#![feature(noop_waker)]
|
||||
#![feature(num_midpoint)]
|
||||
#![feature(num_midpoint_signed)]
|
||||
#![feature(numfmt)]
|
||||
#![feature(pattern)]
|
||||
#![feature(pointer_is_aligned_to)]
|
||||
|
Loading…
Reference in New Issue
Block a user