Skip to content

Commit 18ab16b

Browse files
committed
Move intrinsics-based float methods out of libcore into libstd
Affected methods are `abs`, `signum`, and `powi`. CC rust-lang#32110 (comment)
1 parent 8a374f2 commit 18ab16b

10 files changed

+54
-104
lines changed

src/libcore/num/f32.rs

-27
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
1818
#![stable(feature = "rust1", since = "1.0.0")]
1919

20-
use intrinsics;
2120
use mem;
2221
use num::Float;
2322
#[cfg(not(stage0))] use num::FpCategory;
@@ -189,27 +188,6 @@ impl Float for f32 {
189188
}
190189
}
191190

192-
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
193-
/// number is `Float::nan()`.
194-
#[inline]
195-
fn abs(self) -> f32 {
196-
unsafe { intrinsics::fabsf32(self) }
197-
}
198-
199-
/// Returns a number that represents the sign of `self`.
200-
///
201-
/// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
202-
/// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
203-
/// - `Float::nan()` if the number is `Float::nan()`
204-
#[inline]
205-
fn signum(self) -> f32 {
206-
if self.is_nan() {
207-
NAN
208-
} else {
209-
unsafe { intrinsics::copysignf32(1.0, self) }
210-
}
211-
}
212-
213191
/// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
214192
/// positive sign bit and positive infinity.
215193
#[inline]
@@ -232,11 +210,6 @@ impl Float for f32 {
232210
1.0 / self
233211
}
234212

235-
#[inline]
236-
fn powi(self, n: i32) -> f32 {
237-
unsafe { intrinsics::powif32(self, n) }
238-
}
239-
240213
/// Converts to degrees, assuming the number is in radians.
241214
#[inline]
242215
fn to_degrees(self) -> f32 {

src/libcore/num/f64.rs

-27
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
1818
#![stable(feature = "rust1", since = "1.0.0")]
1919

20-
use intrinsics;
2120
use mem;
2221
use num::Float;
2322
#[cfg(not(stage0))] use num::FpCategory;
@@ -189,27 +188,6 @@ impl Float for f64 {
189188
}
190189
}
191190

192-
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
193-
/// number is `Float::nan()`.
194-
#[inline]
195-
fn abs(self) -> f64 {
196-
unsafe { intrinsics::fabsf64(self) }
197-
}
198-
199-
/// Returns a number that represents the sign of `self`.
200-
///
201-
/// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
202-
/// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
203-
/// - `Float::nan()` if the number is `Float::nan()`
204-
#[inline]
205-
fn signum(self) -> f64 {
206-
if self.is_nan() {
207-
NAN
208-
} else {
209-
unsafe { intrinsics::copysignf64(1.0, self) }
210-
}
211-
}
212-
213191
/// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
214192
/// positive sign bit and positive infinity.
215193
#[inline]
@@ -230,11 +208,6 @@ impl Float for f64 {
230208
1.0 / self
231209
}
232210

233-
#[inline]
234-
fn powi(self, n: i32) -> f64 {
235-
unsafe { intrinsics::powif64(self, n) }
236-
}
237-
238211
/// Converts to degrees, assuming the number is in radians.
239212
#[inline]
240213
fn to_degrees(self) -> f64 {

src/libcore/num/mod.rs

-18
Original file line numberDiff line numberDiff line change
@@ -4125,18 +4125,6 @@ pub trait Float: Sized {
41254125
#[stable(feature = "core", since = "1.6.0")]
41264126
fn classify(self) -> FpCategory;
41274127

4128-
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
4129-
/// number is `Float::nan()`.
4130-
#[stable(feature = "core", since = "1.6.0")]
4131-
fn abs(self) -> Self;
4132-
/// Returns a number that represents the sign of `self`.
4133-
///
4134-
/// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
4135-
/// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
4136-
/// - `Float::nan()` if the number is `Float::nan()`
4137-
#[stable(feature = "core", since = "1.6.0")]
4138-
fn signum(self) -> Self;
4139-
41404128
/// Returns `true` if `self` is positive, including `+0.0` and
41414129
/// `Float::infinity()`.
41424130
#[stable(feature = "core", since = "1.6.0")]
@@ -4150,12 +4138,6 @@ pub trait Float: Sized {
41504138
#[stable(feature = "core", since = "1.6.0")]
41514139
fn recip(self) -> Self;
41524140

4153-
/// Raise a number to an integer power.
4154-
///
4155-
/// Using this function is generally faster than using `powf`
4156-
#[stable(feature = "core", since = "1.6.0")]
4157-
fn powi(self, n: i32) -> Self;
4158-
41594141
/// Convert radians to degrees.
41604142
#[stable(feature = "deg_rad_conversions", since="1.7.0")]
41614143
fn to_degrees(self) -> Self;

src/librustc_typeck/diagnostics.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4526,23 +4526,23 @@ but the type of the numeric value or binding could not be identified.
45264526
The error happens on numeric literals:
45274527
45284528
```compile_fail,E0689
4529-
2.0.powi(2);
4529+
2.0.recip();
45304530
```
45314531
45324532
and on numeric bindings without an identified concrete type:
45334533
45344534
```compile_fail,E0689
45354535
let x = 2.0;
4536-
x.powi(2); // same error as above
4536+
x.recip(); // same error as above
45374537
```
45384538
45394539
Because of this, you must give the numeric literal or binding a type:
45404540
45414541
```
4542-
let _ = 2.0_f32.powi(2);
4542+
let _ = 2.0_f32.recip();
45434543
let x: f32 = 2.0;
4544-
let _ = x.powi(2);
4545-
let _ = (2.0 as f32).powi(2);
4544+
let _ = x.recip();
4545+
let _ = (2.0 as f32).recip();
45464546
```
45474547
"##,
45484548

src/libstd/f32.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![allow(missing_docs)]
2020

2121
#[cfg(not(test))]
22+
#[cfg(stage0)]
2223
use core::num::Float;
2324
#[cfg(not(test))]
2425
use intrinsics;
@@ -163,7 +164,9 @@ impl f32 {
163164
/// ```
164165
#[stable(feature = "rust1", since = "1.0.0")]
165166
#[inline]
166-
pub fn abs(self) -> f32 { Float::abs(self) }
167+
pub fn abs(self) -> f32 {
168+
unsafe { intrinsics::fabsf32(self) }
169+
}
167170

168171
/// Returns a number that represents the sign of `self`.
169172
///
@@ -183,7 +186,13 @@ impl f32 {
183186
/// ```
184187
#[stable(feature = "rust1", since = "1.0.0")]
185188
#[inline]
186-
pub fn signum(self) -> f32 { Float::signum(self) }
189+
pub fn signum(self) -> f32 {
190+
if self.is_nan() {
191+
NAN
192+
} else {
193+
unsafe { intrinsics::copysignf32(1.0, self) }
194+
}
195+
}
187196

188197
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
189198
/// error. This produces a more accurate result with better performance than
@@ -272,7 +281,9 @@ impl f32 {
272281
/// ```
273282
#[stable(feature = "rust1", since = "1.0.0")]
274283
#[inline]
275-
pub fn powi(self, n: i32) -> f32 { Float::powi(self, n) }
284+
pub fn powi(self, n: i32) -> f32 {
285+
unsafe { intrinsics::powif32(self, n) }
286+
}
276287

277288
/// Raises a number to a floating point power.
278289
///

src/libstd/f64.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![allow(missing_docs)]
2020

2121
#[cfg(not(test))]
22+
#[cfg(stage0)]
2223
use core::num::Float;
2324
#[cfg(not(test))]
2425
use intrinsics;
@@ -141,7 +142,9 @@ impl f64 {
141142
/// ```
142143
#[stable(feature = "rust1", since = "1.0.0")]
143144
#[inline]
144-
pub fn abs(self) -> f64 { Float::abs(self) }
145+
pub fn abs(self) -> f64 {
146+
unsafe { intrinsics::fabsf64(self) }
147+
}
145148

146149
/// Returns a number that represents the sign of `self`.
147150
///
@@ -161,7 +164,13 @@ impl f64 {
161164
/// ```
162165
#[stable(feature = "rust1", since = "1.0.0")]
163166
#[inline]
164-
pub fn signum(self) -> f64 { Float::signum(self) }
167+
pub fn signum(self) -> f64 {
168+
if self.is_nan() {
169+
NAN
170+
} else {
171+
unsafe { intrinsics::copysignf64(1.0, self) }
172+
}
173+
}
165174

166175
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
167176
/// error. This produces a more accurate result with better performance than
@@ -245,7 +254,9 @@ impl f64 {
245254
/// ```
246255
#[stable(feature = "rust1", since = "1.0.0")]
247256
#[inline]
248-
pub fn powi(self, n: i32) -> f64 { Float::powi(self, n) }
257+
pub fn powi(self, n: i32) -> f64 {
258+
unsafe { intrinsics::powif64(self, n) }
259+
}
249260

250261
/// Raises a number to a floating point power.
251262
///

src/test/ui/macros/macro-backtrace-invalid-internals.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ macro_rules! fake_anon_field_expr {
4848

4949
macro_rules! real_method_stmt {
5050
() => {
51-
2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
51+
2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
5252
}
5353
}
5454

5555
macro_rules! real_method_expr {
5656
() => {
57-
2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
57+
2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
5858
}
5959
}
6060

src/test/ui/macros/macro-backtrace-invalid-internals.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ LL | (1).0 //~ ERROR doesn't have fields
2525
LL | fake_anon_field_stmt!();
2626
| ------------------------ in this macro invocation
2727

28-
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
28+
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
2929
--> $DIR/macro-backtrace-invalid-internals.rs:51:15
3030
|
31-
LL | 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
32-
| ^^^^
31+
LL | 2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
32+
| ^^^^^
3333
...
3434
LL | real_method_stmt!();
3535
| -------------------- in this macro invocation
3636
help: you must specify a concrete type for this numeric value, like `f32`
3737
|
38-
LL | 2.0_f32.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
38+
LL | 2.0_f32.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
3939
| ^^^^^^^
4040

4141
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
6565
LL | let _ = fake_anon_field_expr!();
6666
| ----------------------- in this macro invocation
6767

68-
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
68+
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
6969
--> $DIR/macro-backtrace-invalid-internals.rs:57:15
7070
|
71-
LL | 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
72-
| ^^^^
71+
LL | 2.0.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
72+
| ^^^^^
7373
...
7474
LL | let _ = real_method_expr!();
7575
| ------------------- in this macro invocation
7676
help: you must specify a concrete type for this numeric value, like `f32`
7777
|
78-
LL | 2.0_f32.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
78+
LL | 2.0_f32.recip() //~ ERROR can't call method `recip` on ambiguous numeric type `{float}`
7979
| ^^^^^^^
8080

8181
error: aborting due to 8 previous errors

src/test/ui/suggestions/method-on-ambiguous-numeric-type.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
let x = 2.0.powi(2);
13-
//~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
12+
let x = 2.0.recip();
13+
//~^ ERROR can't call method `recip` on ambiguous numeric type `{float}`
1414
let y = 2.0;
15-
let x = y.powi(2);
16-
//~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
15+
let x = y.recip();
16+
//~^ ERROR can't call method `recip` on ambiguous numeric type `{float}`
1717
println!("{:?}", x);
1818
}

src/test/ui/suggestions/method-on-ambiguous-numeric-type.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
1+
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
22
--> $DIR/method-on-ambiguous-numeric-type.rs:12:17
33
|
4-
LL | let x = 2.0.powi(2);
5-
| ^^^^
4+
LL | let x = 2.0.recip();
5+
| ^^^^^
66
help: you must specify a concrete type for this numeric value, like `f32`
77
|
8-
LL | let x = 2.0_f32.powi(2);
8+
LL | let x = 2.0_f32.recip();
99
| ^^^^^^^
1010

11-
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
11+
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
1212
--> $DIR/method-on-ambiguous-numeric-type.rs:15:15
1313
|
14-
LL | let x = y.powi(2);
15-
| ^^^^
14+
LL | let x = y.recip();
15+
| ^^^^^
1616
help: you must specify a type for this binding, like `f32`
1717
|
1818
LL | let y: f32 = 2.0;

0 commit comments

Comments
 (0)