Skip to content

Commit

Permalink
Decode a float into integers
Browse files Browse the repository at this point in the history
The `integer_decode()` function decodes a float (f32/f64)
into integers containing the mantissa, exponent and sign.

It's needed for `rationalize()` implementation of #9838.

The code got ported from ABCL [1].

[1] http://abcl.org/trac/browser/trunk/abcl/src/org/armedbear/lisp/FloatFunctions.java?rev=14465#L94
  • Loading branch information
vmx committed Dec 4, 2013
1 parent 9169579 commit 30a9c6e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/libstd/num/f32.rs
Expand Up @@ -708,6 +708,23 @@ impl Float for f32 {
fn next_after(&self, other: f32) -> f32 {
next_after(*self, other)
}

/// Returns the mantissa, exponent and sign as integers.
fn integer_decode(&self) -> (u64, i16, i8) {
let bits: u32 = unsafe {
::cast::transmute(*self)
};
let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
let mantissa = if exponent == 0 {
(bits & 0x7fffff) << 1
} else {
(bits & 0x7fffff) | 0x800000
};
// Exponent bias + mantissa shift
exponent -= 127 + 23;
(mantissa as u64, exponent, sign)
}
}

//
Expand Down Expand Up @@ -1273,4 +1290,16 @@ mod tests {
assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf)
assert!(match nan.frexp() { (x, _) => x.is_nan() })
}

#[test]
fn test_integer_decode() {
assert_eq!(3.14159265359f32.integer_decode(), (13176795u64, -22i16, 1i8));
assert_eq!((-8573.5918555f32).integer_decode(), (8779358u64, -10i16, -1i8));
assert_eq!(2f32.pow(&100.0).integer_decode(), (8388608u64, 77i16, 1i8));
assert_eq!(0f32.integer_decode(), (0u64, -150i16, 1i8));
assert_eq!((-0f32).integer_decode(), (0u64, -150i16, -1i8));
assert_eq!(INFINITY.integer_decode(), (8388608u64, 105i16, 1i8));
assert_eq!(NEG_INFINITY.integer_decode(), (8388608u64, 105i16, -1i8));
assert_eq!(NAN.integer_decode(), (12582912u64, 105i16, 1i8));
}
}
29 changes: 29 additions & 0 deletions src/libstd/num/f64.rs
Expand Up @@ -756,6 +756,23 @@ impl Float for f64 {
fn next_after(&self, other: f64) -> f64 {
next_after(*self, other)
}

/// Returns the mantissa, exponent and sign as integers.
fn integer_decode(&self) -> (u64, i16, i8) {
let bits: u64 = unsafe {
::cast::transmute(*self)
};
let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
let mantissa = if exponent == 0 {
(bits & 0xfffffffffffff) << 1
} else {
(bits & 0xfffffffffffff) | 0x10000000000000
};
// Exponent bias + mantissa shift
exponent -= 1023 + 52;
(mantissa, exponent, sign)
}
}

//
Expand Down Expand Up @@ -1323,4 +1340,16 @@ mod tests {
assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf)
assert!(match nan.frexp() { (x, _) => x.is_nan() })
}

#[test]
fn test_integer_decode() {
assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906u64, -51i16, 1i8));
assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931u64, -39i16, -1i8));
assert_eq!(2f64.pow(&100.0).integer_decode(), (4503599627370496u64, 48i16, 1i8));
assert_eq!(0f64.integer_decode(), (0u64, -1075i16, 1i8));
assert_eq!((-0f64).integer_decode(), (0u64, -1075i16, -1i8));
assert_eq!(INFINITY.integer_decode(), (4503599627370496u64, 972i16, 1i8));
assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
assert_eq!(NAN.integer_decode(), (6755399441055744u64, 972i16, 1i8));
}
}
2 changes: 2 additions & 0 deletions src/libstd/num/mod.rs
Expand Up @@ -532,6 +532,8 @@ pub trait Float: Real
fn ln_1p(&self) -> Self;
fn mul_add(&self, a: Self, b: Self) -> Self;
fn next_after(&self, other: Self) -> Self;

fn integer_decode(&self) -> (u64, i16, i8);
}

/// Returns the exponential of the number, minus `1`, `exp(n) - 1`, in a way
Expand Down

5 comments on commit 30a9c6e

@bors
Copy link
Contributor

@bors bors commented on 30a9c6e Dec 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from cmr
at vmx@30a9c6e

@bors
Copy link
Contributor

@bors bors commented on 30a9c6e Dec 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging vmx/rust/integer-decode = 30a9c6e into auto

@bors
Copy link
Contributor

@bors bors commented on 30a9c6e Dec 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vmx/rust/integer-decode = 30a9c6e merged ok, testing candidate = 64bcfd2

@bors
Copy link
Contributor

@bors bors commented on 30a9c6e Dec 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 30a9c6e Dec 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 64bcfd2

Please sign in to comment.