提交 18ab16b5 编写于 作者: S Simon Sapin

Move intrinsics-based float methods out of libcore into libstd

Affected methods are `abs`, `signum`, and `powi`.
CC https://github.com/rust-lang/rust/issues/32110#issuecomment-379503183
上级 8a374f28
......@@ -17,7 +17,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
use intrinsics;
use mem;
use num::Float;
#[cfg(not(stage0))] use num::FpCategory;
......@@ -189,27 +188,6 @@ fn classify(self) -> Fp {
}
}
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
/// number is `Float::nan()`.
#[inline]
fn abs(self) -> f32 {
unsafe { intrinsics::fabsf32(self) }
}
/// Returns a number that represents the sign of `self`.
///
/// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
/// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
/// - `Float::nan()` if the number is `Float::nan()`
#[inline]
fn signum(self) -> f32 {
if self.is_nan() {
NAN
} else {
unsafe { intrinsics::copysignf32(1.0, self) }
}
}
/// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
/// positive sign bit and positive infinity.
#[inline]
......@@ -232,11 +210,6 @@ fn recip(self) -> f32 {
1.0 / self
}
#[inline]
fn powi(self, n: i32) -> f32 {
unsafe { intrinsics::powif32(self, n) }
}
/// Converts to degrees, assuming the number is in radians.
#[inline]
fn to_degrees(self) -> f32 {
......
......@@ -17,7 +17,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
use intrinsics;
use mem;
use num::Float;
#[cfg(not(stage0))] use num::FpCategory;
......@@ -189,27 +188,6 @@ fn classify(self) -> Fp {
}
}
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
/// number is `Float::nan()`.
#[inline]
fn abs(self) -> f64 {
unsafe { intrinsics::fabsf64(self) }
}
/// Returns a number that represents the sign of `self`.
///
/// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
/// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
/// - `Float::nan()` if the number is `Float::nan()`
#[inline]
fn signum(self) -> f64 {
if self.is_nan() {
NAN
} else {
unsafe { intrinsics::copysignf64(1.0, self) }
}
}
/// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
/// positive sign bit and positive infinity.
#[inline]
......@@ -230,11 +208,6 @@ fn recip(self) -> f64 {
1.0 / self
}
#[inline]
fn powi(self, n: i32) -> f64 {
unsafe { intrinsics::powif64(self, n) }
}
/// Converts to degrees, assuming the number is in radians.
#[inline]
fn to_degrees(self) -> f64 {
......
......@@ -4125,18 +4125,6 @@ pub trait Float: Sized {
#[stable(feature = "core", since = "1.6.0")]
fn classify(self) -> FpCategory;
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
/// number is `Float::nan()`.
#[stable(feature = "core", since = "1.6.0")]
fn abs(self) -> Self;
/// Returns a number that represents the sign of `self`.
///
/// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
/// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
/// - `Float::nan()` if the number is `Float::nan()`
#[stable(feature = "core", since = "1.6.0")]
fn signum(self) -> Self;
/// Returns `true` if `self` is positive, including `+0.0` and
/// `Float::infinity()`.
#[stable(feature = "core", since = "1.6.0")]
......@@ -4150,12 +4138,6 @@ pub trait Float: Sized {
#[stable(feature = "core", since = "1.6.0")]
fn recip(self) -> Self;
/// Raise a number to an integer power.
///
/// Using this function is generally faster than using `powf`
#[stable(feature = "core", since = "1.6.0")]
fn powi(self, n: i32) -> Self;
/// Convert radians to degrees.
#[stable(feature = "deg_rad_conversions", since="1.7.0")]
fn to_degrees(self) -> Self;
......
......@@ -4526,23 +4526,23 @@ fn foo<U: Iterator>(&self, _: &U) { } // error method `foo` has incompatible
The error happens on numeric literals:
```compile_fail,E0689
2.0.powi(2);
2.0.recip();
```
and on numeric bindings without an identified concrete type:
```compile_fail,E0689
let x = 2.0;
x.powi(2); // same error as above
x.recip(); // same error as above
```
Because of this, you must give the numeric literal or binding a type:
```
let _ = 2.0_f32.powi(2);
let _ = 2.0_f32.recip();
let x: f32 = 2.0;
let _ = x.powi(2);
let _ = (2.0 as f32).powi(2);
let _ = x.recip();
let _ = (2.0 as f32).recip();
```
"##,
......
......@@ -19,6 +19,7 @@
#![allow(missing_docs)]
#[cfg(not(test))]
#[cfg(stage0)]
use core::num::Float;
#[cfg(not(test))]
use intrinsics;
......@@ -163,7 +164,9 @@ pub fn fract(self) -> f32 { self - self.trunc() }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn abs(self) -> f32 { Float::abs(self) }
pub fn abs(self) -> f32 {
unsafe { intrinsics::fabsf32(self) }
}
/// Returns a number that represents the sign of `self`.
///
......@@ -183,7 +186,13 @@ pub fn abs(self) -> f32 { Float::abs(self) }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn signum(self) -> f32 { Float::signum(self) }
pub fn signum(self) -> f32 {
if self.is_nan() {
NAN
} else {
unsafe { intrinsics::copysignf32(1.0, self) }
}
}
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
/// error. This produces a more accurate result with better performance than
......@@ -272,7 +281,9 @@ pub fn mod_euc(self, rhs: f32) -> f32 {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn powi(self, n: i32) -> f32 { Float::powi(self, n) }
pub fn powi(self, n: i32) -> f32 {
unsafe { intrinsics::powif32(self, n) }
}
/// Raises a number to a floating point power.
///
......
......@@ -19,6 +19,7 @@
#![allow(missing_docs)]
#[cfg(not(test))]
#[cfg(stage0)]
use core::num::Float;
#[cfg(not(test))]
use intrinsics;
......@@ -141,7 +142,9 @@ pub fn fract(self) -> f64 { self - self.trunc() }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn abs(self) -> f64 { Float::abs(self) }
pub fn abs(self) -> f64 {
unsafe { intrinsics::fabsf64(self) }
}
/// Returns a number that represents the sign of `self`.
///
......@@ -161,7 +164,13 @@ pub fn abs(self) -> f64 { Float::abs(self) }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn signum(self) -> f64 { Float::signum(self) }
pub fn signum(self) -> f64 {
if self.is_nan() {
NAN
} else {
unsafe { intrinsics::copysignf64(1.0, self) }
}
}
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
/// error. This produces a more accurate result with better performance than
......@@ -245,7 +254,9 @@ pub fn mod_euc(self, rhs: f64) -> f64 {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn powi(self, n: i32) -> f64 { Float::powi(self, n) }
pub fn powi(self, n: i32) -> f64 {
unsafe { intrinsics::powif64(self, n) }
}
/// Raises a number to a floating point power.
///
......
......@@ -48,13 +48,13 @@
macro_rules! real_method_stmt {
() => {
2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
}
}
macro_rules! real_method_expr {
() => {
2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
}
}
......
......@@ -25,17 +25,17 @@ LL | (1).0 //~ ERROR doesn't have fields
LL | fake_anon_field_stmt!();
| ------------------------ in this macro invocation
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/macro-backtrace-invalid-internals.rs:51:15
|
LL | 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^
LL | 2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
| ^^^^^
...
LL | real_method_stmt!();
| -------------------- in this macro invocation
help: you must specify a concrete type for this numeric value, like `f32`
|
LL | 2.0_f32.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
LL | 2.0_f32.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
| ^^^^^^^
error[E0599]: no method named `fake` found for type `{integer}` in the current scope
......@@ -65,17 +65,17 @@ LL | (1).0 //~ ERROR doesn't have fields
LL | let _ = fake_anon_field_expr!();
| ----------------------- in this macro invocation
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/macro-backtrace-invalid-internals.rs:57:15
|
LL | 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^
LL | 2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
| ^^^^^
...
LL | let _ = real_method_expr!();
| ------------------- in this macro invocation
help: you must specify a concrete type for this numeric value, like `f32`
|
LL | 2.0_f32.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
LL | 2.0_f32.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
| ^^^^^^^
error: aborting due to 8 previous errors
......
......@@ -9,10 +9,10 @@
// except according to those terms.
fn main() {
let x = 2.0.powi(2);
//~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
let x = 2.0.recip();
//~^ ERROR can't call method `recip` on ambiguous numeric type `{float}`
let y = 2.0;
let x = y.powi(2);
//~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
let x = y.recip();
//~^ ERROR can't call method `recip` on ambiguous numeric type `{float}`
println!("{:?}", x);
}
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/method-on-ambiguous-numeric-type.rs:12:17
|
LL | let x = 2.0.powi(2);
| ^^^^
LL | let x = 2.0.recip();
| ^^^^^
help: you must specify a concrete type for this numeric value, like `f32`
|
LL | let x = 2.0_f32.powi(2);
LL | let x = 2.0_f32.recip();
| ^^^^^^^
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/method-on-ambiguous-numeric-type.rs:15:15
|
LL | let x = y.powi(2);
| ^^^^
LL | let x = y.recip();
| ^^^^^
help: you must specify a type for this binding, like `f32`
|
LL | let y: f32 = 2.0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册