Skip to content

Make algebraic functions in f16, f32, f64, and f128 into const fn items. #139712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 62 additions & 11 deletions library/core/src/num/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#![unstable(feature = "f128", issue = "116909")]

use crate::convert::FloatToInt;
use crate::intrinsics::{self, const_eval_select};
use crate::mem;
use crate::num::FpCategory;
use crate::panic::const_assert;
use crate::{intrinsics, mem};

/// Basic mathematical constants.
#[unstable(feature = "f128", issue = "116909")]
Expand Down Expand Up @@ -1368,48 +1369,98 @@ impl f128 {
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_add(self, rhs: f128) -> f128 {
intrinsics::fadd_algebraic(self, rhs)
pub const fn algebraic_add(self, rhs: f128) -> f128 {
const fn const_algebraic_add(lhs: f128, rhs: f128) -> f128 {
lhs + rhs
}

#[inline(always)]
fn rt_algebraic_add(lhs: f128, rhs: f128) -> f128 {
intrinsics::fadd_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
}

/// Float subtraction that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_sub(self, rhs: f128) -> f128 {
intrinsics::fsub_algebraic(self, rhs)
pub const fn algebraic_sub(self, rhs: f128) -> f128 {
const fn const_algebraic_sub(lhs: f128, rhs: f128) -> f128 {
lhs - rhs
}

#[inline(always)]
fn rt_algebraic_sub(lhs: f128, rhs: f128) -> f128 {
intrinsics::fsub_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
}

/// Float multiplication that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_mul(self, rhs: f128) -> f128 {
intrinsics::fmul_algebraic(self, rhs)
pub const fn algebraic_mul(self, rhs: f128) -> f128 {
const fn const_algebraic_mul(lhs: f128, rhs: f128) -> f128 {
lhs * rhs
}

#[inline(always)]
fn rt_algebraic_mul(lhs: f128, rhs: f128) -> f128 {
intrinsics::fmul_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
}

/// Float division that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_div(self, rhs: f128) -> f128 {
intrinsics::fdiv_algebraic(self, rhs)
pub const fn algebraic_div(self, rhs: f128) -> f128 {
const fn const_algebraic_div(lhs: f128, rhs: f128) -> f128 {
lhs / rhs
}

#[inline(always)]
fn rt_algebraic_div(lhs: f128, rhs: f128) -> f128 {
intrinsics::fdiv_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
}

/// Float remainder that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_rem(self, rhs: f128) -> f128 {
intrinsics::frem_algebraic(self, rhs)
pub const fn algebraic_rem(self, rhs: f128) -> f128 {
const fn const_algebraic_rem(lhs: f128, rhs: f128) -> f128 {
lhs % rhs
}

#[inline(always)]
fn rt_algebraic_rem(lhs: f128, rhs: f128) -> f128 {
intrinsics::frem_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
}
}
73 changes: 62 additions & 11 deletions library/core/src/num/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#![unstable(feature = "f16", issue = "116909")]

use crate::convert::FloatToInt;
use crate::intrinsics::{self, const_eval_select};
use crate::mem;
use crate::num::FpCategory;
use crate::panic::const_assert;
use crate::{intrinsics, mem};

/// Basic mathematical constants.
#[unstable(feature = "f16", issue = "116909")]
Expand Down Expand Up @@ -1344,48 +1345,98 @@ impl f16 {
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_add(self, rhs: f16) -> f16 {
intrinsics::fadd_algebraic(self, rhs)
pub const fn algebraic_add(self, rhs: f16) -> f16 {
const fn const_algebraic_add(lhs: f16, rhs: f16) -> f16 {
lhs + rhs
}

#[inline(always)]
fn rt_algebraic_add(lhs: f16, rhs: f16) -> f16 {
intrinsics::fadd_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
}

/// Float subtraction that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_sub(self, rhs: f16) -> f16 {
intrinsics::fsub_algebraic(self, rhs)
pub const fn algebraic_sub(self, rhs: f16) -> f16 {
const fn const_algebraic_sub(lhs: f16, rhs: f16) -> f16 {
lhs - rhs
}

#[inline(always)]
fn rt_algebraic_sub(lhs: f16, rhs: f16) -> f16 {
intrinsics::fsub_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
}

/// Float multiplication that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_mul(self, rhs: f16) -> f16 {
intrinsics::fmul_algebraic(self, rhs)
pub const fn algebraic_mul(self, rhs: f16) -> f16 {
const fn const_algebraic_mul(lhs: f16, rhs: f16) -> f16 {
lhs * rhs
}

#[inline(always)]
fn rt_algebraic_mul(lhs: f16, rhs: f16) -> f16 {
intrinsics::fmul_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
}

/// Float division that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_div(self, rhs: f16) -> f16 {
intrinsics::fdiv_algebraic(self, rhs)
pub const fn algebraic_div(self, rhs: f16) -> f16 {
const fn const_algebraic_div(lhs: f16, rhs: f16) -> f16 {
lhs / rhs
}

#[inline(always)]
fn rt_algebraic_div(lhs: f16, rhs: f16) -> f16 {
intrinsics::fdiv_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
}

/// Float remainder that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_rem(self, rhs: f16) -> f16 {
intrinsics::frem_algebraic(self, rhs)
pub const fn algebraic_rem(self, rhs: f16) -> f16 {
const fn const_algebraic_rem(lhs: f16, rhs: f16) -> f16 {
lhs % rhs
}

#[inline(always)]
fn rt_algebraic_rem(lhs: f16, rhs: f16) -> f16 {
intrinsics::frem_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
}
}
73 changes: 62 additions & 11 deletions library/core/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::convert::FloatToInt;
use crate::intrinsics::{self, const_eval_select};
use crate::num::FpCategory;
use crate::panic::const_assert;
use crate::{cfg_match, intrinsics, mem};
use crate::{cfg_match, mem};

/// The radix or base of the internal representation of `f32`.
/// Use [`f32::RADIX`] instead.
Expand Down Expand Up @@ -1510,48 +1511,98 @@ impl f32 {
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_add(self, rhs: f32) -> f32 {
intrinsics::fadd_algebraic(self, rhs)
pub const fn algebraic_add(self, rhs: f32) -> f32 {
const fn const_algebraic_add(lhs: f32, rhs: f32) -> f32 {
lhs + rhs
}

#[inline(always)]
fn rt_algebraic_add(lhs: f32, rhs: f32) -> f32 {
intrinsics::fadd_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_add, rt_algebraic_add)
}

/// Float subtraction that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_sub(self, rhs: f32) -> f32 {
intrinsics::fsub_algebraic(self, rhs)
pub const fn algebraic_sub(self, rhs: f32) -> f32 {
const fn const_algebraic_sub(lhs: f32, rhs: f32) -> f32 {
lhs - rhs
}

#[inline(always)]
fn rt_algebraic_sub(lhs: f32, rhs: f32) -> f32 {
intrinsics::fsub_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_sub, rt_algebraic_sub)
}

/// Float multiplication that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_mul(self, rhs: f32) -> f32 {
intrinsics::fmul_algebraic(self, rhs)
pub const fn algebraic_mul(self, rhs: f32) -> f32 {
const fn const_algebraic_mul(lhs: f32, rhs: f32) -> f32 {
lhs * rhs
}

#[inline(always)]
fn rt_algebraic_mul(lhs: f32, rhs: f32) -> f32 {
intrinsics::fmul_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_mul, rt_algebraic_mul)
}

/// Float division that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_div(self, rhs: f32) -> f32 {
intrinsics::fdiv_algebraic(self, rhs)
pub const fn algebraic_div(self, rhs: f32) -> f32 {
const fn const_algebraic_div(lhs: f32, rhs: f32) -> f32 {
lhs / rhs
}

#[inline(always)]
fn rt_algebraic_div(lhs: f32, rhs: f32) -> f32 {
intrinsics::fdiv_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_div, rt_algebraic_div)
}

/// Float remainder that allows optimizations based on algebraic rules.
///
/// See [algebraic operators](primitive@f32#algebraic-operators) for more info.
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_algebraic", issue = "136469")]
#[rustc_const_unstable(feature = "float_algebraic", issue = "136469")]
#[inline]
pub fn algebraic_rem(self, rhs: f32) -> f32 {
intrinsics::frem_algebraic(self, rhs)
pub const fn algebraic_rem(self, rhs: f32) -> f32 {
const fn const_algebraic_rem(lhs: f32, rhs: f32) -> f32 {
lhs % rhs
}

#[inline(always)]
fn rt_algebraic_rem(lhs: f32, rhs: f32) -> f32 {
intrinsics::frem_algebraic(lhs, rhs)
}

const_eval_select((self, rhs), const_algebraic_rem, rt_algebraic_rem)
}
}
Loading
Loading