Skip to content

Commit 1ddee80

Browse files
committed
Remove dependencies on libm functions from libcore.
There wasn't any particular reason the functions needed to be there anyway, so just get rid of them, and adjust libstd to compensate. With this change, libcore depends on exactly two floating-point functions: fmod and fmodf. They are implicitly referenced because they are used to implement "%".
1 parent 7b7fc67 commit 1ddee80

File tree

15 files changed

+194
-318
lines changed

15 files changed

+194
-318
lines changed

src/libcore/num/f32.rs

-140
Original file line numberDiff line numberDiff line change
@@ -222,63 +222,6 @@ impl Float for f32 {
222222
(mantissa as u64, exponent, sign)
223223
}
224224

225-
/// Rounds towards minus infinity.
226-
#[inline]
227-
fn floor(self) -> f32 {
228-
return floorf(self);
229-
230-
// On MSVC LLVM will lower many math intrinsics to a call to the
231-
// corresponding function. On MSVC, however, many of these functions
232-
// aren't actually available as symbols to call, but rather they are all
233-
// `static inline` functions in header files. This means that from a C
234-
// perspective it's "compatible", but not so much from an ABI
235-
// perspective (which we're worried about).
236-
//
237-
// The inline header functions always just cast to a f64 and do their
238-
// operation, so we do that here as well, but only for MSVC targets.
239-
//
240-
// Note that there are many MSVC-specific float operations which
241-
// redirect to this comment, so `floorf` is just one case of a missing
242-
// function on MSVC, but there are many others elsewhere.
243-
#[cfg(target_env = "msvc")]
244-
fn floorf(f: f32) -> f32 { (f as f64).floor() as f32 }
245-
#[cfg(not(target_env = "msvc"))]
246-
fn floorf(f: f32) -> f32 { unsafe { intrinsics::floorf32(f) } }
247-
}
248-
249-
/// Rounds towards plus infinity.
250-
#[inline]
251-
fn ceil(self) -> f32 {
252-
return ceilf(self);
253-
254-
// see notes above in `floor`
255-
#[cfg(target_env = "msvc")]
256-
fn ceilf(f: f32) -> f32 { (f as f64).ceil() as f32 }
257-
#[cfg(not(target_env = "msvc"))]
258-
fn ceilf(f: f32) -> f32 { unsafe { intrinsics::ceilf32(f) } }
259-
}
260-
261-
/// Rounds to nearest integer. Rounds half-way cases away from zero.
262-
#[inline]
263-
fn round(self) -> f32 {
264-
unsafe { intrinsics::roundf32(self) }
265-
}
266-
267-
/// Returns the integer part of the number (rounds towards zero).
268-
#[inline]
269-
fn trunc(self) -> f32 {
270-
unsafe { intrinsics::truncf32(self) }
271-
}
272-
273-
/// The fractional part of the number, satisfying:
274-
///
275-
/// ```
276-
/// let x = 1.65f32;
277-
/// assert!(x == x.trunc() + x.fract())
278-
/// ```
279-
#[inline]
280-
fn fract(self) -> f32 { self - self.trunc() }
281-
282225
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
283226
/// number is `Float::nan()`.
284227
#[inline]
@@ -314,14 +257,6 @@ impl Float for f32 {
314257
self < 0.0 || (1.0 / self) == Float::neg_infinity()
315258
}
316259

317-
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
318-
/// error. This produces a more accurate result with better performance than
319-
/// a separate multiplication operation followed by an add.
320-
#[inline]
321-
fn mul_add(self, a: f32, b: f32) -> f32 {
322-
unsafe { intrinsics::fmaf32(self, a, b) }
323-
}
324-
325260
/// Returns the reciprocal (multiplicative inverse) of the number.
326261
#[inline]
327262
fn recip(self) -> f32 { 1.0 / self }
@@ -331,81 +266,6 @@ impl Float for f32 {
331266
unsafe { intrinsics::powif32(self, n) }
332267
}
333268

334-
#[inline]
335-
fn powf(self, n: f32) -> f32 {
336-
return powf(self, n);
337-
338-
// see notes above in `floor`
339-
#[cfg(target_env = "msvc")]
340-
fn powf(f: f32, n: f32) -> f32 { (f as f64).powf(n as f64) as f32 }
341-
#[cfg(not(target_env = "msvc"))]
342-
fn powf(f: f32, n: f32) -> f32 { unsafe { intrinsics::powf32(f, n) } }
343-
}
344-
345-
#[inline]
346-
fn sqrt(self) -> f32 {
347-
if self < 0.0 {
348-
NAN
349-
} else {
350-
unsafe { intrinsics::sqrtf32(self) }
351-
}
352-
}
353-
354-
#[inline]
355-
fn rsqrt(self) -> f32 { self.sqrt().recip() }
356-
357-
/// Returns the exponential of the number.
358-
#[inline]
359-
fn exp(self) -> f32 {
360-
return expf(self);
361-
362-
// see notes above in `floor`
363-
#[cfg(target_env = "msvc")]
364-
fn expf(f: f32) -> f32 { (f as f64).exp() as f32 }
365-
#[cfg(not(target_env = "msvc"))]
366-
fn expf(f: f32) -> f32 { unsafe { intrinsics::expf32(f) } }
367-
}
368-
369-
/// Returns 2 raised to the power of the number.
370-
#[inline]
371-
fn exp2(self) -> f32 {
372-
unsafe { intrinsics::exp2f32(self) }
373-
}
374-
375-
/// Returns the natural logarithm of the number.
376-
#[inline]
377-
fn ln(self) -> f32 {
378-
return logf(self);
379-
380-
// see notes above in `floor`
381-
#[cfg(target_env = "msvc")]
382-
fn logf(f: f32) -> f32 { (f as f64).ln() as f32 }
383-
#[cfg(not(target_env = "msvc"))]
384-
fn logf(f: f32) -> f32 { unsafe { intrinsics::logf32(f) } }
385-
}
386-
387-
/// Returns the logarithm of the number with respect to an arbitrary base.
388-
#[inline]
389-
fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
390-
391-
/// Returns the base 2 logarithm of the number.
392-
#[inline]
393-
fn log2(self) -> f32 {
394-
unsafe { intrinsics::log2f32(self) }
395-
}
396-
397-
/// Returns the base 10 logarithm of the number.
398-
#[inline]
399-
fn log10(self) -> f32 {
400-
return log10f(self);
401-
402-
// see notes above in `floor`
403-
#[cfg(target_env = "msvc")]
404-
fn log10f(f: f32) -> f32 { (f as f64).log10() as f32 }
405-
#[cfg(not(target_env = "msvc"))]
406-
fn log10f(f: f32) -> f32 { unsafe { intrinsics::log10f32(f) } }
407-
}
408-
409269
/// Converts to degrees, assuming the number is in radians.
410270
#[inline]
411271
fn to_degrees(self) -> f32 { self * (180.0f32 / consts::PI) }

src/libcore/num/f64.rs

-92
Original file line numberDiff line numberDiff line change
@@ -222,39 +222,6 @@ impl Float for f64 {
222222
(mantissa, exponent, sign)
223223
}
224224

225-
/// Rounds towards minus infinity.
226-
#[inline]
227-
fn floor(self) -> f64 {
228-
unsafe { intrinsics::floorf64(self) }
229-
}
230-
231-
/// Rounds towards plus infinity.
232-
#[inline]
233-
fn ceil(self) -> f64 {
234-
unsafe { intrinsics::ceilf64(self) }
235-
}
236-
237-
/// Rounds to nearest integer. Rounds half-way cases away from zero.
238-
#[inline]
239-
fn round(self) -> f64 {
240-
unsafe { intrinsics::roundf64(self) }
241-
}
242-
243-
/// Returns the integer part of the number (rounds towards zero).
244-
#[inline]
245-
fn trunc(self) -> f64 {
246-
unsafe { intrinsics::truncf64(self) }
247-
}
248-
249-
/// The fractional part of the number, satisfying:
250-
///
251-
/// ```
252-
/// let x = 1.65f64;
253-
/// assert!(x == x.trunc() + x.fract())
254-
/// ```
255-
#[inline]
256-
fn fract(self) -> f64 { self - self.trunc() }
257-
258225
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
259226
/// number is `Float::nan()`.
260227
#[inline]
@@ -290,74 +257,15 @@ impl Float for f64 {
290257
self < 0.0 || (1.0 / self) == Float::neg_infinity()
291258
}
292259

293-
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
294-
/// error. This produces a more accurate result with better performance than
295-
/// a separate multiplication operation followed by an add.
296-
#[inline]
297-
fn mul_add(self, a: f64, b: f64) -> f64 {
298-
unsafe { intrinsics::fmaf64(self, a, b) }
299-
}
300-
301260
/// Returns the reciprocal (multiplicative inverse) of the number.
302261
#[inline]
303262
fn recip(self) -> f64 { 1.0 / self }
304263

305-
#[inline]
306-
fn powf(self, n: f64) -> f64 {
307-
unsafe { intrinsics::powf64(self, n) }
308-
}
309-
310264
#[inline]
311265
fn powi(self, n: i32) -> f64 {
312266
unsafe { intrinsics::powif64(self, n) }
313267
}
314268

315-
#[inline]
316-
fn sqrt(self) -> f64 {
317-
if self < 0.0 {
318-
NAN
319-
} else {
320-
unsafe { intrinsics::sqrtf64(self) }
321-
}
322-
}
323-
324-
#[inline]
325-
fn rsqrt(self) -> f64 { self.sqrt().recip() }
326-
327-
/// Returns the exponential of the number.
328-
#[inline]
329-
fn exp(self) -> f64 {
330-
unsafe { intrinsics::expf64(self) }
331-
}
332-
333-
/// Returns 2 raised to the power of the number.
334-
#[inline]
335-
fn exp2(self) -> f64 {
336-
unsafe { intrinsics::exp2f64(self) }
337-
}
338-
339-
/// Returns the natural logarithm of the number.
340-
#[inline]
341-
fn ln(self) -> f64 {
342-
unsafe { intrinsics::logf64(self) }
343-
}
344-
345-
/// Returns the logarithm of the number with respect to an arbitrary base.
346-
#[inline]
347-
fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
348-
349-
/// Returns the base 2 logarithm of the number.
350-
#[inline]
351-
fn log2(self) -> f64 {
352-
unsafe { intrinsics::log2f64(self) }
353-
}
354-
355-
/// Returns the base 10 logarithm of the number.
356-
#[inline]
357-
fn log10(self) -> f64 {
358-
unsafe { intrinsics::log10f64(self) }
359-
}
360-
361269
/// Converts to degrees, assuming the number is in radians.
362270
#[inline]
363271
fn to_degrees(self) -> f64 { self * (180.0f64 / consts::PI) }

src/libcore/num/flt2dec/decoder.rs

-5
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,15 @@ pub enum FullDecoded {
5353

5454
/// A floating point type which can be `decode`d.
5555
pub trait DecodableFloat: Float + Copy {
56-
/// Returns `x * 2^exp`. Almost same to `std::{f32,f64}::ldexp`.
57-
/// This is used for testing.
58-
fn ldexpi(f: i64, exp: isize) -> Self;
5956
/// The minimum positive normalized value.
6057
fn min_pos_norm_value() -> Self;
6158
}
6259

6360
impl DecodableFloat for f32 {
64-
fn ldexpi(f: i64, exp: isize) -> Self { f as Self * (exp as Self).exp2() }
6561
fn min_pos_norm_value() -> Self { f32::MIN_POSITIVE }
6662
}
6763

6864
impl DecodableFloat for f64 {
69-
fn ldexpi(f: i64, exp: isize) -> Self { f as Self * (exp as Self).exp2() }
7065
fn min_pos_norm_value() -> Self { f64::MIN_POSITIVE }
7166
}
7267

src/libcore/num/flt2dec/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ functions.
131131

132132
use prelude::v1::*;
133133
use i16;
134-
use num::Float;
135134
use slice::bytes;
136135
pub use self::decoder::{decode, DecodableFloat, FullDecoded, Decoded};
137136

src/libcore/num/flt2dec/strategy/dragon.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Almost direct (but slightly optimized) Rust translation of Figure 3 of [1].
1717

1818
use prelude::v1::*;
1919

20-
use num::Float;
2120
use cmp::Ordering;
2221

2322
use num::flt2dec::{Decoded, MAX_SIG_DIGITS, round_up};

src/libcore/num/flt2dec/strategy/grisu.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Rust adaptation of Grisu3 algorithm described in [1]. It uses about
1818

1919
use prelude::v1::*;
2020

21-
use num::Float;
22-
2321
use num::flt2dec::{Decoded, MAX_SIG_DIGITS, round_up};
2422

2523
/// A custom 64-bit floating point type, representing `f * 2^e`.

src/libcore/num/mod.rs

-38
Original file line numberDiff line numberDiff line change
@@ -1296,18 +1296,6 @@ pub trait Float {
12961296
/// Returns the mantissa, exponent and sign as integers, respectively.
12971297
fn integer_decode(self) -> (u64, i16, i8);
12981298

1299-
/// Return the largest integer less than or equal to a number.
1300-
fn floor(self) -> Self;
1301-
/// Return the smallest integer greater than or equal to a number.
1302-
fn ceil(self) -> Self;
1303-
/// Return the nearest integer to a number. Round half-way cases away from
1304-
/// `0.0`.
1305-
fn round(self) -> Self;
1306-
/// Return the integer part of a number.
1307-
fn trunc(self) -> Self;
1308-
/// Return the fractional part of a number.
1309-
fn fract(self) -> Self;
1310-
13111299
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
13121300
/// number is `Float::nan()`.
13131301
fn abs(self) -> Self;
@@ -1324,39 +1312,13 @@ pub trait Float {
13241312
/// `Float::neg_infinity()`.
13251313
fn is_negative(self) -> bool;
13261314

1327-
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
1328-
/// error. This produces a more accurate result with better performance than
1329-
/// a separate multiplication operation followed by an add.
1330-
fn mul_add(self, a: Self, b: Self) -> Self;
13311315
/// Take the reciprocal (inverse) of a number, `1/x`.
13321316
fn recip(self) -> Self;
13331317

13341318
/// Raise a number to an integer power.
13351319
///
13361320
/// Using this function is generally faster than using `powf`
13371321
fn powi(self, n: i32) -> Self;
1338-
/// Raise a number to a floating point power.
1339-
fn powf(self, n: Self) -> Self;
1340-
1341-
/// Take the square root of a number.
1342-
///
1343-
/// Returns NaN if `self` is a negative number.
1344-
fn sqrt(self) -> Self;
1345-
/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
1346-
fn rsqrt(self) -> Self;
1347-
1348-
/// Returns `e^(self)`, (the exponential function).
1349-
fn exp(self) -> Self;
1350-
/// Returns 2 raised to the power of the number, `2^(self)`.
1351-
fn exp2(self) -> Self;
1352-
/// Returns the natural logarithm of the number.
1353-
fn ln(self) -> Self;
1354-
/// Returns the logarithm of the number with respect to an arbitrary base.
1355-
fn log(self, base: Self) -> Self;
1356-
/// Returns the base 2 logarithm of the number.
1357-
fn log2(self) -> Self;
1358-
/// Returns the base 10 logarithm of the number.
1359-
fn log10(self) -> Self;
13601322

13611323
/// Convert radians to degrees.
13621324
fn to_degrees(self) -> Self;

src/libcore/ops.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,9 @@ rem_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
445445
impl Rem for f32 {
446446
type Output = f32;
447447

448-
// see notes in `core::f32::Float::floor`
448+
// The builtin f32 rem operator is broken when targeting
449+
// MSVC; see comment in std::f32::floor.
450+
// FIXME: See also #27859.
449451
#[inline]
450452
#[cfg(target_env = "msvc")]
451453
fn rem(self, other: f32) -> f32 {

0 commit comments

Comments
 (0)