aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/f32.rs
diff options
context:
space:
mode:
authorFenrir <[email protected]>2018-01-21 14:06:28 -0700
committerFenrirWolf <[email protected]>2018-01-21 19:16:33 -0700
commit23be3f4885688e5e0011005e2295c75168854c0a (patch)
treedd0850f9c73c489e114a761d5c0757f3dbec3a65 /ctr-std/src/f32.rs
parentUpdate CI for Rust nightly-2017-12-01 + other fixes (diff)
downloadarchived-ctru-rs-23be3f4885688e5e0011005e2295c75168854c0a.tar.xz
archived-ctru-rs-23be3f4885688e5e0011005e2295c75168854c0a.zip
Recreate ctr-std from latest nightly
Diffstat (limited to 'ctr-std/src/f32.rs')
-rw-r--r--ctr-std/src/f32.rs403
1 files changed, 115 insertions, 288 deletions
diff --git a/ctr-std/src/f32.rs b/ctr-std/src/f32.rs
index f793909..5e5695f 100644
--- a/ctr-std/src/f32.rs
+++ b/ctr-std/src/f32.rs
@@ -8,9 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-//! The 32-bit floating point type.
+//! This module provides constants which are specific to the implementation
+//! of the `f32` floating point data type.
//!
-//! *[See also the `f32` primitive type](../primitive.f32.html).*
+//! Mathematically significant numbers are provided in the `consts` sub-module.
+//!
+//! *[See also the `f32` primitive type](../../std/primitive.f32.html).*
#![stable(feature = "rust1", since = "1.0.0")]
#![allow(missing_docs)]
@@ -20,10 +23,9 @@ use core::num;
#[cfg(not(test))]
use intrinsics;
#[cfg(not(test))]
-use libc::c_int;
-#[cfg(not(test))]
use num::FpCategory;
-
+#[cfg(not(test))]
+use sys::cmath;
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
@@ -36,110 +38,6 @@ pub use core::f32::{MIN, MIN_POSITIVE, MAX};
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::f32::consts;
-#[allow(dead_code)]
-mod cmath {
- use libc::{c_float, c_int};
-
- extern {
- pub fn cbrtf(n: c_float) -> c_float;
- pub fn erff(n: c_float) -> c_float;
- pub fn erfcf(n: c_float) -> c_float;
- pub fn expm1f(n: c_float) -> c_float;
- pub fn fdimf(a: c_float, b: c_float) -> c_float;
- pub fn fmaxf(a: c_float, b: c_float) -> c_float;
- pub fn fminf(a: c_float, b: c_float) -> c_float;
- pub fn fmodf(a: c_float, b: c_float) -> c_float;
- pub fn ilogbf(n: c_float) -> c_int;
- pub fn logbf(n: c_float) -> c_float;
- pub fn log1pf(n: c_float) -> c_float;
- pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
- pub fn nextafterf(x: c_float, y: c_float) -> c_float;
- pub fn tgammaf(n: c_float) -> c_float;
-
- #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
- pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
- #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
- pub fn hypotf(x: c_float, y: c_float) -> c_float;
- }
-
- // See the comments in the `floor` function for why MSVC is special
- // here.
- #[cfg(not(target_env = "msvc"))]
- extern {
- pub fn acosf(n: c_float) -> c_float;
- pub fn asinf(n: c_float) -> c_float;
- pub fn atan2f(a: c_float, b: c_float) -> c_float;
- pub fn atanf(n: c_float) -> c_float;
- pub fn coshf(n: c_float) -> c_float;
- pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
- pub fn ldexpf(x: c_float, n: c_int) -> c_float;
- pub fn sinhf(n: c_float) -> c_float;
- pub fn tanf(n: c_float) -> c_float;
- pub fn tanhf(n: c_float) -> c_float;
- }
-
- #[cfg(target_env = "msvc")]
- pub use self::shims::*;
- #[cfg(target_env = "msvc")]
- mod shims {
- use libc::{c_float, c_int};
-
- #[inline]
- pub unsafe fn acosf(n: c_float) -> c_float {
- f64::acos(n as f64) as c_float
- }
-
- #[inline]
- pub unsafe fn asinf(n: c_float) -> c_float {
- f64::asin(n as f64) as c_float
- }
-
- #[inline]
- pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
- f64::atan2(n as f64, b as f64) as c_float
- }
-
- #[inline]
- pub unsafe fn atanf(n: c_float) -> c_float {
- f64::atan(n as f64) as c_float
- }
-
- #[inline]
- pub unsafe fn coshf(n: c_float) -> c_float {
- f64::cosh(n as f64) as c_float
- }
-
- #[inline]
- #[allow(deprecated)]
- pub unsafe fn frexpf(x: c_float, value: &mut c_int) -> c_float {
- let (a, b) = f64::frexp(x as f64);
- *value = b as c_int;
- a as c_float
- }
-
- #[inline]
- #[allow(deprecated)]
- pub unsafe fn ldexpf(x: c_float, n: c_int) -> c_float {
- f64::ldexp(x as f64, n as isize) as c_float
- }
-
- #[inline]
- pub unsafe fn sinhf(n: c_float) -> c_float {
- f64::sinh(n as f64) as c_float
- }
-
- #[inline]
- pub unsafe fn tanf(n: c_float) -> c_float {
- f64::tan(n as f64) as c_float
- }
-
- #[inline]
- pub unsafe fn tanhf(n: c_float) -> c_float {
- f64::tanh(n as f64) as c_float
- }
- }
-}
-
#[cfg(not(test))]
#[lang = "f32"]
impl f32 {
@@ -381,39 +279,29 @@ impl f32 {
#[inline]
pub fn signum(self) -> f32 { num::Float::signum(self) }
- /// Returns `true` if `self`'s sign bit is positive, including
- /// `+0.0` and `INFINITY`.
+ /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
+ /// positive sign bit and positive infinity.
///
/// ```
- /// use std::f32;
- ///
- /// let nan = f32::NAN;
/// let f = 7.0_f32;
/// let g = -7.0_f32;
///
/// assert!(f.is_sign_positive());
/// assert!(!g.is_sign_positive());
- /// // Requires both tests to determine if is `NaN`
- /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
- /// Returns `true` if `self`'s sign is negative, including `-0.0`
- /// and `NEG_INFINITY`.
+ /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
+ /// negative sign bit and negative infinity.
///
/// ```
- /// use std::f32;
- ///
- /// let nan = f32::NAN;
/// let f = 7.0f32;
/// let g = -7.0f32;
///
/// assert!(!f.is_sign_negative());
/// assert!(g.is_sign_negative());
- /// // Requires both tests to determine if is `NaN`.
- /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
@@ -584,20 +472,19 @@ impl f32 {
/// Returns the logarithm of the number with respect to an arbitrary base.
///
+ /// The result may not be correctly rounded owing to implementation details;
+ /// `self.log2()` can produce more accurate results for base 2, and
+ /// `self.log10()` can produce more accurate results for base 10.
+ ///
/// ```
/// use std::f32;
///
- /// let ten = 10.0f32;
- /// let two = 2.0f32;
- ///
- /// // log10(10) - 1 == 0
- /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
+ /// let five = 5.0f32;
///
- /// // log2(2) - 1 == 0
- /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
+ /// // log5(5) - 1 == 0
+ /// let abs_difference = (five.log(5.0) - 1.0).abs();
///
- /// assert!(abs_difference_10 <= f32::EPSILON);
- /// assert!(abs_difference_2 <= f32::EPSILON);
+ /// assert!(abs_difference <= f32::EPSILON);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
@@ -676,89 +563,6 @@ impl f32 {
#[inline]
pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
- /// Constructs a floating point number of `x*2^exp`.
- ///
- /// ```
- /// #![feature(float_extras)]
- ///
- /// use std::f32;
- /// // 3*2^2 - 12 == 0
- /// let abs_difference = (f32::ldexp(3.0, 2) - 12.0).abs();
- ///
- /// assert!(abs_difference <= f32::EPSILON);
- /// ```
- #[unstable(feature = "float_extras",
- reason = "pending integer conventions",
- issue = "27752")]
- #[rustc_deprecated(since = "1.11.0",
- reason = "never really came to fruition and easily \
- implementable outside the standard library")]
- #[inline]
- pub fn ldexp(x: f32, exp: isize) -> f32 {
- unsafe { cmath::ldexpf(x, exp as c_int) }
- }
-
- /// Breaks the number into a normalized fraction and a base-2 exponent,
- /// satisfying:
- ///
- /// * `self = x * 2^exp`
- /// * `0.5 <= abs(x) < 1.0`
- ///
- /// ```
- /// #![feature(float_extras)]
- ///
- /// use std::f32;
- ///
- /// let x = 4.0f32;
- ///
- /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
- /// let f = x.frexp();
- /// let abs_difference_0 = (f.0 - 0.5).abs();
- /// let abs_difference_1 = (f.1 as f32 - 3.0).abs();
- ///
- /// assert!(abs_difference_0 <= f32::EPSILON);
- /// assert!(abs_difference_1 <= f32::EPSILON);
- /// ```
- #[unstable(feature = "float_extras",
- reason = "pending integer conventions",
- issue = "27752")]
- #[rustc_deprecated(since = "1.11.0",
- reason = "never really came to fruition and easily \
- implementable outside the standard library")]
- #[inline]
- pub fn frexp(self) -> (f32, isize) {
- unsafe {
- let mut exp = 0;
- let x = cmath::frexpf(self, &mut exp);
- (x, exp as isize)
- }
- }
-
- /// Returns the next representable floating-point value in the direction of
- /// `other`.
- ///
- /// ```
- /// #![feature(float_extras)]
- ///
- /// use std::f32;
- ///
- /// let x = 1.0f32;
- ///
- /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
- ///
- /// assert!(abs_diff <= f32::EPSILON);
- /// ```
- #[unstable(feature = "float_extras",
- reason = "unsure about its place in the world",
- issue = "27752")]
- #[rustc_deprecated(since = "1.11.0",
- reason = "never really came to fruition and easily \
- implementable outside the standard library")]
- #[inline]
- pub fn next_after(self, other: f32) -> f32 {
- unsafe { cmath::nextafterf(self, other) }
- }
-
/// Returns the maximum of the two numbers.
///
/// ```
@@ -772,7 +576,7 @@ impl f32 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn max(self, other: f32) -> f32 {
- unsafe { cmath::fmaxf(self, other) }
+ num::Float::max(self, other)
}
/// Returns the minimum of the two numbers.
@@ -788,7 +592,7 @@ impl f32 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn min(self, other: f32) -> f32 {
- unsafe { cmath::fminf(self, other) }
+ num::Float::min(self, other)
}
/// The positive difference of two numbers.
@@ -1190,6 +994,74 @@ impl f32 {
pub fn atanh(self) -> f32 {
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
}
+
+ /// Raw transmutation to `u32`.
+ ///
+ /// This is currently identical to `transmute::<f32, u32>(self)` on all platforms.
+ ///
+ /// See `from_bits` for some discussion of the portability of this operation
+ /// (there are almost no issues).
+ ///
+ /// Note that this function is distinct from `as` casting, which attempts to
+ /// preserve the *numeric* value, and not the bitwise value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting!
+ /// assert_eq!((12.5f32).to_bits(), 0x41480000);
+ ///
+ /// ```
+ #[stable(feature = "float_bits_conv", since = "1.20.0")]
+ #[inline]
+ pub fn to_bits(self) -> u32 {
+ unsafe { ::mem::transmute(self) }
+ }
+
+ /// Raw transmutation from `u32`.
+ ///
+ /// This is currently identical to `transmute::<u32, f32>(v)` on all platforms.
+ /// It turns out this is incredibly portable, for two reasons:
+ ///
+ /// * Floats and Ints have the same endianess on all supported platforms.
+ /// * IEEE-754 very precisely specifies the bit layout of floats.
+ ///
+ /// However there is one caveat: prior to the 2008 version of IEEE-754, how
+ /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
+ /// (notably x86 and ARM) picked the interpretation that was ultimately
+ /// standardized in 2008, but some didn't (notably MIPS). As a result, all
+ /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
+ ///
+ /// Rather than trying to preserve signaling-ness cross-platform, this
+ /// implementation favours preserving the exact bits. This means that
+ /// any payloads encoded in NaNs will be preserved even if the result of
+ /// this method is sent over the network from an x86 machine to a MIPS one.
+ ///
+ /// If the results of this method are only manipulated by the same
+ /// architecture that produced them, then there is no portability concern.
+ ///
+ /// If the input isn't NaN, then there is no portability concern.
+ ///
+ /// If you don't care about signalingness (very likely), then there is no
+ /// portability concern.
+ ///
+ /// Note that this function is distinct from `as` casting, which attempts to
+ /// preserve the *numeric* value, and not the bitwise value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::f32;
+ /// let v = f32::from_bits(0x41480000);
+ /// let difference = (v - 12.5).abs();
+ /// assert!(difference <= 1e-5);
+ /// ```
+ #[stable(feature = "float_bits_conv", since = "1.20.0")]
+ #[inline]
+ pub fn from_bits(v: u32) -> Self {
+ // It turns out the safety issues with sNaN were overblown! Hooray!
+ unsafe { ::mem::transmute(v) }
+ }
}
#[cfg(test)]
@@ -1223,7 +1095,7 @@ mod tests {
assert!(!nan.is_infinite());
assert!(!nan.is_finite());
assert!(!nan.is_normal());
- assert!(!nan.is_sign_positive());
+ assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative());
assert_eq!(Fp::Nan, nan.classify());
}
@@ -1365,23 +1237,6 @@ mod tests {
}
#[test]
- #[allow(deprecated)]
- fn test_integer_decode() {
- assert_eq!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
- assert_eq!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
- assert_eq!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
- assert_eq!(0f32.integer_decode(), (0, -150, 1));
- assert_eq!((-0f32).integer_decode(), (0, -150, -1));
- assert_eq!(INFINITY.integer_decode(), (8388608, 105, 1));
- assert_eq!(NEG_INFINITY.integer_decode(), (8388608, 105, -1));
-
- // Ignore the "sign" (quiet / signalling flag) of NAN.
- // It can vary between runtime operations and LLVM folding.
- let (nan_m, nan_e, _nan_s) = NAN.integer_decode();
- assert_eq!((nan_m, nan_e), (12582912, 105));
- }
-
- #[test]
fn test_floor() {
assert_approx_eq!(1.0f32.floor(), 1.0f32);
assert_approx_eq!(1.3f32.floor(), 1.0f32);
@@ -1484,7 +1339,8 @@ mod tests {
assert!(!(-1f32).is_sign_positive());
assert!(!NEG_INFINITY.is_sign_positive());
assert!(!(1f32/NEG_INFINITY).is_sign_positive());
- assert!(!NAN.is_sign_positive());
+ assert!(NAN.is_sign_positive());
+ assert!(!(-NAN).is_sign_positive());
}
#[test]
@@ -1497,6 +1353,7 @@ mod tests {
assert!(NEG_INFINITY.is_sign_negative());
assert!((1f32/NEG_INFINITY).is_sign_negative());
assert!(!NAN.is_sign_negative());
+ assert!((-NAN).is_sign_negative());
}
#[test]
@@ -1693,58 +1550,6 @@ mod tests {
}
#[test]
- #[allow(deprecated)]
- fn test_ldexp() {
- let f1 = 2.0f32.powi(-123);
- let f2 = 2.0f32.powi(-111);
- let f3 = 1.75 * 2.0f32.powi(-12);
- assert_eq!(f32::ldexp(1f32, -123), f1);
- assert_eq!(f32::ldexp(1f32, -111), f2);
- assert_eq!(f32::ldexp(1.75f32, -12), f3);
-
- assert_eq!(f32::ldexp(0f32, -123), 0f32);
- assert_eq!(f32::ldexp(-0f32, -123), -0f32);
-
- let inf: f32 = f32::INFINITY;
- let neg_inf: f32 = f32::NEG_INFINITY;
- let nan: f32 = f32::NAN;
- assert_eq!(f32::ldexp(inf, -123), inf);
- assert_eq!(f32::ldexp(neg_inf, -123), neg_inf);
- assert!(f32::ldexp(nan, -123).is_nan());
- }
-
- #[test]
- #[allow(deprecated)]
- fn test_frexp() {
- let f1 = 2.0f32.powi(-123);
- let f2 = 2.0f32.powi(-111);
- let f3 = 1.75 * 2.0f32.powi(-123);
- let (x1, exp1) = f1.frexp();
- let (x2, exp2) = f2.frexp();
- let (x3, exp3) = f3.frexp();
- assert_eq!((x1, exp1), (0.5f32, -122));
- assert_eq!((x2, exp2), (0.5f32, -110));
- assert_eq!((x3, exp3), (0.875f32, -122));
- assert_eq!(f32::ldexp(x1, exp1), f1);
- assert_eq!(f32::ldexp(x2, exp2), f2);
- assert_eq!(f32::ldexp(x3, exp3), f3);
-
- assert_eq!(0f32.frexp(), (0f32, 0));
- assert_eq!((-0f32).frexp(), (-0f32, 0));
- }
-
- #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
- #[allow(deprecated)]
- fn test_frexp_nowin() {
- let inf: f32 = f32::INFINITY;
- let neg_inf: f32 = f32::NEG_INFINITY;
- let nan: f32 = f32::NAN;
- assert_eq!(match inf.frexp() { (x, _) => x }, inf);
- assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
- assert!(match nan.frexp() { (x, _) => x.is_nan() })
- }
-
- #[test]
fn test_asinh() {
assert_eq!(0.0f32.asinh(), 0.0f32);
assert_eq!((-0.0f32).asinh(), -0.0f32);
@@ -1834,4 +1639,26 @@ mod tests {
assert_approx_eq!(ln_2, 2f32.ln());
assert_approx_eq!(ln_10, 10f32.ln());
}
+
+ #[test]
+ fn test_float_bits_conv() {
+ assert_eq!((1f32).to_bits(), 0x3f800000);
+ assert_eq!((12.5f32).to_bits(), 0x41480000);
+ assert_eq!((1337f32).to_bits(), 0x44a72000);
+ assert_eq!((-14.25f32).to_bits(), 0xc1640000);
+ assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
+ assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
+ assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
+ assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
+
+ // Check that NaNs roundtrip their bits regardless of signalingness
+ // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
+ let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
+ let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
+ assert!(f32::from_bits(masked_nan1).is_nan());
+ assert!(f32::from_bits(masked_nan2).is_nan());
+
+ assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
+ assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
+ }
}