Auto merge of #43554 - eddyb:apfloat, r=nikomatsakis
APFloat: Rewrite It In Rust and use it for deterministic floating-point CTFE. As part of the CTFE initiative, we're forced to find a solution for floating-point operations. By design, IEEE-754 does not explicitly define everything in a deterministic manner, and there is some variability between platforms, at the very least (e.g. NaN payloads). If types are to evaluate constant expressions involving type (or in the future, const) generics, that evaluation needs to be *fully deterministic*, even across `rustc` host platforms. That is, if `[T; T::X]` was used in a cross-compiled library, and the evaluation of `T::X` executed a floating-point operation, that operation has to be reproducible on *any other host*, only knowing `T` and the definition of the `X` associated const (as either AST or HIR). Failure to uphold those rules allows an associated type (e.g. `<Foo as Iterator>::Item`) to be seen as two (or more) different types, depending on the current host, and such type safety violations typically allow writing of a `transmute` in safe code, given enough generics. The options considered by @rust-lang/compiler were: 1. Ban floating-point operations in generic const-evaluation contexts 2. Emulate floating-point operations in an uniformly deterministic fashion The former option may seem appealing at first, but floating-point operations *are allowed today*, so they can't be banned wholesale, a distinction has to be made between the code that already works, and future generic contexts. *Moreover*, every computation that succeeded *has to be cached*, otherwise the generic case can be reproduced without any generics. IMO there are too many ways it can go wrong, and a single violation can be enough for an unsoundness hole. Not to mention we may end up really wanting floating-point operations *anyway*, in CTFE. I went with the latter option, and seeing how LLVM *already* has a library for this exact purpose (as it needs to perform optimizations independently of host floating-point capabilities), i.e. `APFloat`, that was what I ended up basing this PR on. But having been burned by the low reusability of bindings that link to LLVM, and because I would *rather* the floating-point operations to be wrong than not deterministic or not memory-safe (`APFloat` does far more pointer juggling than I'm comfortable with), I decided to RIIR. This way, we have a guarantee of *no* `unsafe` code, a bit more control over the where native floating-point might accidentally be involved, and non-LLVM backends can share it. I've also ported all the testcases over, *before* any functionality, to catch any mistakes. Currently the PR replaces all CTFE operations to go through `apfloat::ieee::{Single,Double}`, keeping only the bits of the `f32` / `f64` memory representation in between operations. Converting from a string also double-checks that `core::num` and `apfloat` agree on the interpretation of a floating-point number literal, in case either of them has any bugs left around. r? @nikomatsakis f? @nagisa @est31 <hr/> Huge thanks to @edef1c for first demoing usable `APFloat` bindings and to @chandlerc for fielding my questions on IRC about `APFloat` peculiarities (also upstreaming some bugfixes).
This commit is contained in:
commit
2b82b7e50a
8
src/Cargo.lock
generated
8
src/Cargo.lock
generated
@ -1252,6 +1252,13 @@ dependencies = [
|
||||
"syntax_pos 0.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc_apfloat"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"rustc_bitflags 0.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc_asan"
|
||||
version = "0.0.0"
|
||||
@ -1309,6 +1316,7 @@ dependencies = [
|
||||
name = "rustc_const_math"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"rustc_apfloat 0.0.0",
|
||||
"serialize 0.0.0",
|
||||
"syntax 0.0.0",
|
||||
]
|
||||
|
@ -11,9 +11,9 @@
|
||||
//! This module contains `HashStable` implementations for various data types
|
||||
//! from `rustc_const_math` in no particular order.
|
||||
|
||||
impl_stable_hash_for!(enum ::rustc_const_math::ConstFloat {
|
||||
F32(val),
|
||||
F64(val)
|
||||
impl_stable_hash_for!(struct ::rustc_const_math::ConstFloat {
|
||||
ty,
|
||||
bits
|
||||
});
|
||||
|
||||
impl_stable_hash_for!(enum ::rustc_const_math::ConstInt {
|
||||
|
11
src/librustc_apfloat/Cargo.toml
Normal file
11
src/librustc_apfloat/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
authors = ["The Rust Project Developers"]
|
||||
name = "rustc_apfloat"
|
||||
version = "0.0.0"
|
||||
|
||||
[lib]
|
||||
name = "rustc_apfloat"
|
||||
path = "lib.rs"
|
||||
|
||||
[dependencies]
|
||||
rustc_bitflags = { path = "../librustc_bitflags" }
|
2733
src/librustc_apfloat/ieee.rs
Normal file
2733
src/librustc_apfloat/ieee.rs
Normal file
File diff suppressed because it is too large
Load Diff
693
src/librustc_apfloat/lib.rs
Normal file
693
src/librustc_apfloat/lib.rs
Normal file
@ -0,0 +1,693 @@
|
||||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! Port of LLVM's APFloat software floating-point implementation from the
|
||||
//! following C++ sources (please update commit hash when backporting):
|
||||
//! https://github.com/llvm-mirror/llvm/tree/23efab2bbd424ed13495a420ad8641cb2c6c28f9
|
||||
//! * `include/llvm/ADT/APFloat.h` -> `Float` and `FloatConvert` traits
|
||||
//! * `lib/Support/APFloat.cpp` -> `ieee` and `ppc` modules
|
||||
//! * `unittests/ADT/APFloatTest.cpp` -> `tests` directory
|
||||
//!
|
||||
//! The port contains no unsafe code, global state, or side-effects in general,
|
||||
//! and the only allocations are in the conversion to/from decimal strings.
|
||||
//!
|
||||
//! Most of the API and the testcases are intact in some form or another,
|
||||
//! with some ergonomic changes, such as idiomatic short names, returning
|
||||
//! new values instead of mutating the receiver, and having separate method
|
||||
//! variants that take a non-default rounding mode (with the suffix `_r`).
|
||||
//! Comments have been preserved where possible, only slightly adapted.
|
||||
//!
|
||||
//! Instead of keeping a pointer to a configuration struct and inspecting it
|
||||
//! dynamically on every operation, types (e.g. `ieee::Double`), traits
|
||||
//! (e.g. `ieee::Semantics`) and associated constants are employed for
|
||||
//! increased type safety and performance.
|
||||
//!
|
||||
//! On-heap bigints are replaced everywhere (except in decimal conversion),
|
||||
//! with short arrays of `type Limb = u128` elements (instead of `u64`),
|
||||
//! This allows fitting the largest supported significands in one integer
|
||||
//! (`ieee::Quad` and `ppc::Fallback` use slightly less than 128 bits).
|
||||
//! All of the functions in the `ieee::sig` module operate on slices.
|
||||
//!
|
||||
//! # Note
|
||||
//!
|
||||
//! This API is completely unstable and subject to change.
|
||||
|
||||
#![crate_name = "rustc_apfloat"]
|
||||
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
||||
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
||||
html_root_url = "https://doc.rust-lang.org/nightly/")]
|
||||
#![deny(warnings)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
#![feature(const_fn)]
|
||||
#![feature(i128_type)]
|
||||
#![feature(slice_patterns)]
|
||||
#![feature(try_from)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate rustc_bitflags;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt;
|
||||
use std::ops::{Neg, Add, Sub, Mul, Div, Rem};
|
||||
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign, BitOrAssign};
|
||||
use std::str::FromStr;
|
||||
|
||||
bitflags! {
|
||||
/// IEEE-754R 7: Default exception handling.
|
||||
///
|
||||
/// UNDERFLOW or OVERFLOW are always returned or-ed with INEXACT.
|
||||
#[must_use]
|
||||
#[derive(Debug)]
|
||||
flags Status: u8 {
|
||||
const OK = 0x00,
|
||||
const INVALID_OP = 0x01,
|
||||
const DIV_BY_ZERO = 0x02,
|
||||
const OVERFLOW = 0x04,
|
||||
const UNDERFLOW = 0x08,
|
||||
const INEXACT = 0x10
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOrAssign for Status {
|
||||
fn bitor_assign(&mut self, rhs: Self) {
|
||||
*self = *self | rhs;
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||
pub struct StatusAnd<T> {
|
||||
pub status: Status,
|
||||
pub value: T,
|
||||
}
|
||||
|
||||
impl Status {
|
||||
pub fn and<T>(self, value: T) -> StatusAnd<T> {
|
||||
StatusAnd {
|
||||
status: self,
|
||||
value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> StatusAnd<T> {
|
||||
fn map<F: FnOnce(T) -> U, U>(self, f: F) -> StatusAnd<U> {
|
||||
StatusAnd {
|
||||
status: self.status,
|
||||
value: f(self.value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! unpack {
|
||||
($status:ident|=, $e:expr) => {
|
||||
match $e {
|
||||
$crate::StatusAnd { status, value } => {
|
||||
$status |= status;
|
||||
value
|
||||
}
|
||||
}
|
||||
};
|
||||
($status:ident=, $e:expr) => {
|
||||
match $e {
|
||||
$crate::StatusAnd { status, value } => {
|
||||
$status = status;
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Category of internally-represented number.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub enum Category {
|
||||
Infinity,
|
||||
NaN,
|
||||
Normal,
|
||||
Zero,
|
||||
}
|
||||
|
||||
/// IEEE-754R 4.3: Rounding-direction attributes.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub enum Round {
|
||||
NearestTiesToEven,
|
||||
TowardPositive,
|
||||
TowardNegative,
|
||||
TowardZero,
|
||||
NearestTiesToAway,
|
||||
}
|
||||
|
||||
impl Neg for Round {
|
||||
type Output = Round;
|
||||
fn neg(self) -> Round {
|
||||
match self {
|
||||
Round::TowardPositive => Round::TowardNegative,
|
||||
Round::TowardNegative => Round::TowardPositive,
|
||||
Round::NearestTiesToEven | Round::TowardZero | Round::NearestTiesToAway => self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A signed type to represent a floating point number's unbiased exponent.
|
||||
pub type ExpInt = i16;
|
||||
|
||||
// \c ilogb error results.
|
||||
pub const IEK_INF: ExpInt = ExpInt::max_value();
|
||||
pub const IEK_NAN: ExpInt = ExpInt::min_value();
|
||||
pub const IEK_ZERO: ExpInt = ExpInt::min_value() + 1;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub struct ParseError(pub &'static str);
|
||||
|
||||
/// A self-contained host- and target-independent arbitrary-precision
|
||||
/// floating-point software implementation.
|
||||
///
|
||||
/// `apfloat` uses significand bignum integer arithmetic as provided by functions
|
||||
/// in the `ieee::sig`.
|
||||
///
|
||||
/// Written for clarity rather than speed, in particular with a view to use in
|
||||
/// the front-end of a cross compiler so that target arithmetic can be correctly
|
||||
/// performed on the host. Performance should nonetheless be reasonable,
|
||||
/// particularly for its intended use. It may be useful as a base
|
||||
/// implementation for a run-time library during development of a faster
|
||||
/// target-specific one.
|
||||
///
|
||||
/// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
|
||||
/// implemented operations. Currently implemented operations are add, subtract,
|
||||
/// multiply, divide, fused-multiply-add, conversion-to-float,
|
||||
/// conversion-to-integer and conversion-from-integer. New rounding modes
|
||||
/// (e.g. away from zero) can be added with three or four lines of code.
|
||||
///
|
||||
/// Four formats are built-in: IEEE single precision, double precision,
|
||||
/// quadruple precision, and x87 80-bit extended double (when operating with
|
||||
/// full extended precision). Adding a new format that obeys IEEE semantics
|
||||
/// only requires adding two lines of code: a declaration and definition of the
|
||||
/// format.
|
||||
///
|
||||
/// All operations return the status of that operation as an exception bit-mask,
|
||||
/// so multiple operations can be done consecutively with their results or-ed
|
||||
/// together. The returned status can be useful for compiler diagnostics; e.g.,
|
||||
/// inexact, underflow and overflow can be easily diagnosed on constant folding,
|
||||
/// and compiler optimizers can determine what exceptions would be raised by
|
||||
/// folding operations and optimize, or perhaps not optimize, accordingly.
|
||||
///
|
||||
/// At present, underflow tininess is detected after rounding; it should be
|
||||
/// straight forward to add support for the before-rounding case too.
|
||||
///
|
||||
/// The library reads hexadecimal floating point numbers as per C99, and
|
||||
/// correctly rounds if necessary according to the specified rounding mode.
|
||||
/// Syntax is required to have been validated by the caller.
|
||||
///
|
||||
/// It also reads decimal floating point numbers and correctly rounds according
|
||||
/// to the specified rounding mode.
|
||||
///
|
||||
/// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
|
||||
/// signed exponent, and the significand as an array of integer limbs. After
|
||||
/// normalization of a number of precision P the exponent is within the range of
|
||||
/// the format, and if the number is not denormal the P-th bit of the
|
||||
/// significand is set as an explicit integer bit. For denormals the most
|
||||
/// significant bit is shifted right so that the exponent is maintained at the
|
||||
/// format's minimum, so that the smallest denormal has just the least
|
||||
/// significant bit of the significand set. The sign of zeros and infinities
|
||||
/// is significant; the exponent and significand of such numbers is not stored,
|
||||
/// but has a known implicit (deterministic) value: 0 for the significands, 0
|
||||
/// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and
|
||||
/// significand are deterministic, although not really meaningful, and preserved
|
||||
/// in non-conversion operations. The exponent is implicitly all 1 bits.
|
||||
///
|
||||
/// `apfloat` does not provide any exception handling beyond default exception
|
||||
/// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
|
||||
/// by encoding Signaling NaNs with the first bit of its trailing significand as
|
||||
/// 0.
|
||||
///
|
||||
/// Future work
|
||||
/// ===========
|
||||
///
|
||||
/// Some features that may or may not be worth adding:
|
||||
///
|
||||
/// Optional ability to detect underflow tininess before rounding.
|
||||
///
|
||||
/// New formats: x87 in single and double precision mode (IEEE apart from
|
||||
/// extended exponent range) (hard).
|
||||
///
|
||||
/// New operations: sqrt, nexttoward.
|
||||
///
|
||||
pub trait Float
|
||||
: Copy
|
||||
+ Default
|
||||
+ FromStr<Err = ParseError>
|
||||
+ PartialOrd
|
||||
+ fmt::Display
|
||||
+ Neg<Output = Self>
|
||||
+ AddAssign
|
||||
+ SubAssign
|
||||
+ MulAssign
|
||||
+ DivAssign
|
||||
+ RemAssign
|
||||
+ Add<Output = StatusAnd<Self>>
|
||||
+ Sub<Output = StatusAnd<Self>>
|
||||
+ Mul<Output = StatusAnd<Self>>
|
||||
+ Div<Output = StatusAnd<Self>>
|
||||
+ Rem<Output = StatusAnd<Self>> {
|
||||
/// Total number of bits in the in-memory format.
|
||||
const BITS: usize;
|
||||
|
||||
/// Number of bits in the significand. This includes the integer bit.
|
||||
const PRECISION: usize;
|
||||
|
||||
/// The largest E such that 2^E is representable; this matches the
|
||||
/// definition of IEEE 754.
|
||||
const MAX_EXP: ExpInt;
|
||||
|
||||
/// The smallest E such that 2^E is a normalized number; this
|
||||
/// matches the definition of IEEE 754.
|
||||
const MIN_EXP: ExpInt;
|
||||
|
||||
/// Positive Zero.
|
||||
const ZERO: Self;
|
||||
|
||||
/// Positive Infinity.
|
||||
const INFINITY: Self;
|
||||
|
||||
/// NaN (Not a Number).
|
||||
// FIXME(eddyb) provide a default when qnan becomes const fn.
|
||||
const NAN: Self;
|
||||
|
||||
/// Factory for QNaN values.
|
||||
// FIXME(eddyb) should be const fn.
|
||||
fn qnan(payload: Option<u128>) -> Self;
|
||||
|
||||
/// Factory for SNaN values.
|
||||
// FIXME(eddyb) should be const fn.
|
||||
fn snan(payload: Option<u128>) -> Self;
|
||||
|
||||
/// Largest finite number.
|
||||
// FIXME(eddyb) should be const (but FloatPair::largest is nontrivial).
|
||||
fn largest() -> Self;
|
||||
|
||||
/// Smallest (by magnitude) finite number.
|
||||
/// Might be denormalized, which implies a relative loss of precision.
|
||||
const SMALLEST: Self;
|
||||
|
||||
/// Smallest (by magnitude) normalized finite number.
|
||||
// FIXME(eddyb) should be const (but FloatPair::smallest_normalized is nontrivial).
|
||||
fn smallest_normalized() -> Self;
|
||||
|
||||
// Arithmetic
|
||||
|
||||
fn add_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
|
||||
fn sub_r(self, rhs: Self, round: Round) -> StatusAnd<Self> {
|
||||
self.add_r(-rhs, round)
|
||||
}
|
||||
fn mul_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
|
||||
fn mul_add_r(self, multiplicand: Self, addend: Self, round: Round) -> StatusAnd<Self>;
|
||||
fn mul_add(self, multiplicand: Self, addend: Self) -> StatusAnd<Self> {
|
||||
self.mul_add_r(multiplicand, addend, Round::NearestTiesToEven)
|
||||
}
|
||||
fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
|
||||
/// IEEE remainder.
|
||||
// This is not currently correct in all cases.
|
||||
fn ieee_rem(self, rhs: Self) -> StatusAnd<Self> {
|
||||
let mut v = self;
|
||||
|
||||
let status;
|
||||
v = unpack!(status=, v / rhs);
|
||||
if status == Status::DIV_BY_ZERO {
|
||||
return status.and(self);
|
||||
}
|
||||
|
||||
assert!(Self::PRECISION < 128);
|
||||
|
||||
let status;
|
||||
let x = unpack!(status=, v.to_i128_r(128, Round::NearestTiesToEven, &mut false));
|
||||
if status == Status::INVALID_OP {
|
||||
return status.and(self);
|
||||
}
|
||||
|
||||
let status;
|
||||
let mut v = unpack!(status=, Self::from_i128(x));
|
||||
assert_eq!(status, Status::OK); // should always work
|
||||
|
||||
let status;
|
||||
v = unpack!(status=, v * rhs);
|
||||
assert_eq!(status - Status::INEXACT, Status::OK); // should not overflow or underflow
|
||||
|
||||
let status;
|
||||
v = unpack!(status=, self - v);
|
||||
assert_eq!(status - Status::INEXACT, Status::OK); // likewise
|
||||
|
||||
if v.is_zero() {
|
||||
status.and(v.copy_sign(self)) // IEEE754 requires this
|
||||
} else {
|
||||
status.and(v)
|
||||
}
|
||||
}
|
||||
/// C fmod, or llvm frem.
|
||||
fn c_fmod(self, rhs: Self) -> StatusAnd<Self>;
|
||||
fn round_to_integral(self, round: Round) -> StatusAnd<Self>;
|
||||
|
||||
/// IEEE-754R 2008 5.3.1: nextUp.
|
||||
fn next_up(self) -> StatusAnd<Self>;
|
||||
|
||||
/// IEEE-754R 2008 5.3.1: nextDown.
|
||||
///
|
||||
/// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
|
||||
/// appropriate sign switching before/after the computation.
|
||||
fn next_down(self) -> StatusAnd<Self> {
|
||||
(-self).next_up().map(|r| -r)
|
||||
}
|
||||
|
||||
fn abs(self) -> Self {
|
||||
if self.is_negative() { -self } else { self }
|
||||
}
|
||||
fn copy_sign(self, rhs: Self) -> Self {
|
||||
if self.is_negative() != rhs.is_negative() {
|
||||
-self
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
// Conversions
|
||||
fn from_bits(input: u128) -> Self;
|
||||
fn from_i128_r(input: i128, round: Round) -> StatusAnd<Self> {
|
||||
if input < 0 {
|
||||
Self::from_u128_r(-input as u128, -round).map(|r| -r)
|
||||
} else {
|
||||
Self::from_u128_r(input as u128, round)
|
||||
}
|
||||
}
|
||||
fn from_i128(input: i128) -> StatusAnd<Self> {
|
||||
Self::from_i128_r(input, Round::NearestTiesToEven)
|
||||
}
|
||||
fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self>;
|
||||
fn from_u128(input: u128) -> StatusAnd<Self> {
|
||||
Self::from_u128_r(input, Round::NearestTiesToEven)
|
||||
}
|
||||
fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError>;
|
||||
fn to_bits(self) -> u128;
|
||||
|
||||
/// Convert a floating point number to an integer according to the
|
||||
/// rounding mode. In case of an invalid operation exception,
|
||||
/// deterministic values are returned, namely zero for NaNs and the
|
||||
/// minimal or maximal value respectively for underflow or overflow.
|
||||
/// If the rounded value is in range but the floating point number is
|
||||
/// not the exact integer, the C standard doesn't require an inexact
|
||||
/// exception to be raised. IEEE-854 does require it so we do that.
|
||||
///
|
||||
/// Note that for conversions to integer type the C standard requires
|
||||
/// round-to-zero to always be used.
|
||||
///
|
||||
/// The *is_exact output tells whether the result is exact, in the sense
|
||||
/// that converting it back to the original floating point type produces
|
||||
/// the original value. This is almost equivalent to result==Status::OK,
|
||||
/// except for negative zeroes.
|
||||
fn to_i128_r(self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<i128> {
|
||||
let status;
|
||||
if self.is_negative() {
|
||||
if self.is_zero() {
|
||||
// Negative zero can't be represented as an int.
|
||||
*is_exact = false;
|
||||
}
|
||||
let r = unpack!(status=, (-self).to_u128_r(width, -round, is_exact));
|
||||
|
||||
// Check for values that don't fit in the signed integer.
|
||||
if r > (1 << (width - 1)) {
|
||||
// Return the most negative integer for the given width.
|
||||
*is_exact = false;
|
||||
Status::INVALID_OP.and(-1 << (width - 1))
|
||||
} else {
|
||||
status.and(r.wrapping_neg() as i128)
|
||||
}
|
||||
} else {
|
||||
// Positive case is simpler, can pretend it's a smaller unsigned
|
||||
// integer, and `to_u128` will take care of all the edge cases.
|
||||
self.to_u128_r(width - 1, round, is_exact).map(
|
||||
|r| r as i128,
|
||||
)
|
||||
}
|
||||
}
|
||||
fn to_i128(self, width: usize) -> StatusAnd<i128> {
|
||||
self.to_i128_r(width, Round::TowardZero, &mut true)
|
||||
}
|
||||
fn to_u128_r(self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<u128>;
|
||||
fn to_u128(self, width: usize) -> StatusAnd<u128> {
|
||||
self.to_u128_r(width, Round::TowardZero, &mut true)
|
||||
}
|
||||
|
||||
fn cmp_abs_normal(self, rhs: Self) -> Ordering;
|
||||
|
||||
/// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
|
||||
fn bitwise_eq(self, rhs: Self) -> bool;
|
||||
|
||||
// IEEE-754R 5.7.2 General operations.
|
||||
|
||||
/// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
|
||||
/// both are not NaN. If either argument is a NaN, returns the other argument.
|
||||
fn min(self, other: Self) -> Self {
|
||||
if self.is_nan() {
|
||||
other
|
||||
} else if other.is_nan() {
|
||||
self
|
||||
} else if other.partial_cmp(&self) == Some(Ordering::Less) {
|
||||
other
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
|
||||
/// both are not NaN. If either argument is a NaN, returns the other argument.
|
||||
fn max(self, other: Self) -> Self {
|
||||
if self.is_nan() {
|
||||
other
|
||||
} else if other.is_nan() {
|
||||
self
|
||||
} else if self.partial_cmp(&other) == Some(Ordering::Less) {
|
||||
other
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// IEEE-754R isSignMinus: Returns true if and only if the current value is
|
||||
/// negative.
|
||||
///
|
||||
/// This applies to zeros and NaNs as well.
|
||||
fn is_negative(self) -> bool;
|
||||
|
||||
/// IEEE-754R isNormal: Returns true if and only if the current value is normal.
|
||||
///
|
||||
/// This implies that the current value of the float is not zero, subnormal,
|
||||
/// infinite, or NaN following the definition of normality from IEEE-754R.
|
||||
fn is_normal(self) -> bool {
|
||||
!self.is_denormal() && self.is_finite_non_zero()
|
||||
}
|
||||
|
||||
/// Returns true if and only if the current value is zero, subnormal, or
|
||||
/// normal.
|
||||
///
|
||||
/// This means that the value is not infinite or NaN.
|
||||
fn is_finite(self) -> bool {
|
||||
!self.is_nan() && !self.is_infinite()
|
||||
}
|
||||
|
||||
/// Returns true if and only if the float is plus or minus zero.
|
||||
fn is_zero(self) -> bool {
|
||||
self.category() == Category::Zero
|
||||
}
|
||||
|
||||
/// IEEE-754R isSubnormal(): Returns true if and only if the float is a
|
||||
/// denormal.
|
||||
fn is_denormal(self) -> bool;
|
||||
|
||||
/// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
|
||||
fn is_infinite(self) -> bool {
|
||||
self.category() == Category::Infinity
|
||||
}
|
||||
|
||||
/// Returns true if and only if the float is a quiet or signaling NaN.
|
||||
fn is_nan(self) -> bool {
|
||||
self.category() == Category::NaN
|
||||
}
|
||||
|
||||
/// Returns true if and only if the float is a signaling NaN.
|
||||
fn is_signaling(self) -> bool;
|
||||
|
||||
// Simple Queries
|
||||
|
||||
fn category(self) -> Category;
|
||||
fn is_non_zero(self) -> bool {
|
||||
!self.is_zero()
|
||||
}
|
||||
fn is_finite_non_zero(self) -> bool {
|
||||
self.is_finite() && !self.is_zero()
|
||||
}
|
||||
fn is_pos_zero(self) -> bool {
|
||||
self.is_zero() && !self.is_negative()
|
||||
}
|
||||
fn is_neg_zero(self) -> bool {
|
||||
self.is_zero() && self.is_negative()
|
||||
}
|
||||
|
||||
/// Returns true if and only if the number has the smallest possible non-zero
|
||||
/// magnitude in the current semantics.
|
||||
fn is_smallest(self) -> bool {
|
||||
Self::SMALLEST.copy_sign(self).bitwise_eq(self)
|
||||
}
|
||||
|
||||
/// Returns true if and only if the number has the largest possible finite
|
||||
/// magnitude in the current semantics.
|
||||
fn is_largest(self) -> bool {
|
||||
Self::largest().copy_sign(self).bitwise_eq(self)
|
||||
}
|
||||
|
||||
/// Returns true if and only if the number is an exact integer.
|
||||
fn is_integer(self) -> bool {
|
||||
// This could be made more efficient; I'm going for obviously correct.
|
||||
if !self.is_finite() {
|
||||
return false;
|
||||
}
|
||||
self.round_to_integral(Round::TowardZero).value.bitwise_eq(
|
||||
self,
|
||||
)
|
||||
}
|
||||
|
||||
/// If this value has an exact multiplicative inverse, return it.
|
||||
fn get_exact_inverse(self) -> Option<Self>;
|
||||
|
||||
/// Returns the exponent of the internal representation of the Float.
|
||||
///
|
||||
/// Because the radix of Float is 2, this is equivalent to floor(log2(x)).
|
||||
/// For special Float values, this returns special error codes:
|
||||
///
|
||||
/// NaN -> \c IEK_NAN
|
||||
/// 0 -> \c IEK_ZERO
|
||||
/// Inf -> \c IEK_INF
|
||||
///
|
||||
fn ilogb(self) -> ExpInt;
|
||||
|
||||
/// Returns: self * 2^exp for integral exponents.
|
||||
fn scalbn_r(self, exp: ExpInt, round: Round) -> Self;
|
||||
fn scalbn(self, exp: ExpInt) -> Self {
|
||||
self.scalbn_r(exp, Round::NearestTiesToEven)
|
||||
}
|
||||
|
||||
/// Equivalent of C standard library function.
|
||||
///
|
||||
/// While the C standard says exp is an unspecified value for infinity and nan,
|
||||
/// this returns INT_MAX for infinities, and INT_MIN for NaNs (see `ilogb`).
|
||||
fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self;
|
||||
fn frexp(self, exp: &mut ExpInt) -> Self {
|
||||
self.frexp_r(exp, Round::NearestTiesToEven)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FloatConvert<T: Float>: Float {
|
||||
/// Convert a value of one floating point type to another.
|
||||
/// The return value corresponds to the IEEE754 exceptions. *loses_info
|
||||
/// records whether the transformation lost information, i.e. whether
|
||||
/// converting the result back to the original type will produce the
|
||||
/// original value (this is almost the same as return value==Status::OK,
|
||||
/// but there are edge cases where this is not so).
|
||||
fn convert_r(self, round: Round, loses_info: &mut bool) -> StatusAnd<T>;
|
||||
fn convert(self, loses_info: &mut bool) -> StatusAnd<T> {
|
||||
self.convert_r(Round::NearestTiesToEven, loses_info)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! float_common_impls {
|
||||
($ty:ident<$t:tt>) => {
|
||||
impl<$t> Default for $ty<$t> where Self: Float {
|
||||
fn default() -> Self {
|
||||
Self::ZERO
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::str::FromStr for $ty<$t> where Self: Float {
|
||||
type Err = ParseError;
|
||||
fn from_str(s: &str) -> Result<Self, ParseError> {
|
||||
Self::from_str_r(s, Round::NearestTiesToEven).map(|x| x.value)
|
||||
}
|
||||
}
|
||||
|
||||
// Rounding ties to the nearest even, by default.
|
||||
|
||||
impl<$t> ::std::ops::Add for $ty<$t> where Self: Float {
|
||||
type Output = StatusAnd<Self>;
|
||||
fn add(self, rhs: Self) -> StatusAnd<Self> {
|
||||
self.add_r(rhs, Round::NearestTiesToEven)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::Sub for $ty<$t> where Self: Float {
|
||||
type Output = StatusAnd<Self>;
|
||||
fn sub(self, rhs: Self) -> StatusAnd<Self> {
|
||||
self.sub_r(rhs, Round::NearestTiesToEven)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::Mul for $ty<$t> where Self: Float {
|
||||
type Output = StatusAnd<Self>;
|
||||
fn mul(self, rhs: Self) -> StatusAnd<Self> {
|
||||
self.mul_r(rhs, Round::NearestTiesToEven)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::Div for $ty<$t> where Self: Float {
|
||||
type Output = StatusAnd<Self>;
|
||||
fn div(self, rhs: Self) -> StatusAnd<Self> {
|
||||
self.div_r(rhs, Round::NearestTiesToEven)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::Rem for $ty<$t> where Self: Float {
|
||||
type Output = StatusAnd<Self>;
|
||||
fn rem(self, rhs: Self) -> StatusAnd<Self> {
|
||||
self.c_fmod(rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::AddAssign for $ty<$t> where Self: Float {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
*self = (*self + rhs).value;
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::SubAssign for $ty<$t> where Self: Float {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
*self = (*self - rhs).value;
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::MulAssign for $ty<$t> where Self: Float {
|
||||
fn mul_assign(&mut self, rhs: Self) {
|
||||
*self = (*self * rhs).value;
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::DivAssign for $ty<$t> where Self: Float {
|
||||
fn div_assign(&mut self, rhs: Self) {
|
||||
*self = (*self / rhs).value;
|
||||
}
|
||||
}
|
||||
|
||||
impl<$t> ::std::ops::RemAssign for $ty<$t> where Self: Float {
|
||||
fn rem_assign(&mut self, rhs: Self) {
|
||||
*self = (*self % rhs).value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod ieee;
|
||||
pub mod ppc;
|
461
src/librustc_apfloat/ppc.rs
Normal file
461
src/librustc_apfloat/ppc.rs
Normal file
@ -0,0 +1,461 @@
|
||||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use {Category, ExpInt, Float, FloatConvert, Round, ParseError, Status, StatusAnd};
|
||||
use ieee;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt;
|
||||
use std::ops::Neg;
|
||||
|
||||
#[must_use]
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
|
||||
pub struct DoubleFloat<F>(F, F);
|
||||
pub type DoubleDouble = DoubleFloat<ieee::Double>;
|
||||
|
||||
// These are legacy semantics for the Fallback, inaccrurate implementation of
|
||||
// IBM double-double, if the accurate DoubleDouble doesn't handle the
|
||||
// operation. It's equivalent to having an IEEE number with consecutive 106
|
||||
// bits of mantissa and 11 bits of exponent.
|
||||
//
|
||||
// It's not equivalent to IBM double-double. For example, a legit IBM
|
||||
// double-double, 1 + epsilon:
|
||||
//
|
||||
// 1 + epsilon = 1 + (1 >> 1076)
|
||||
//
|
||||
// is not representable by a consecutive 106 bits of mantissa.
|
||||
//
|
||||
// Currently, these semantics are used in the following way:
|
||||
//
|
||||
// DoubleDouble -> (Double, Double) ->
|
||||
// DoubleDouble's Fallback -> IEEE operations
|
||||
//
|
||||
// FIXME: Implement all operations in DoubleDouble, and delete these
|
||||
// semantics.
|
||||
// FIXME(eddyb) This shouldn't need to be `pub`, it's only used in bounds.
|
||||
pub struct FallbackS<F>(F);
|
||||
type Fallback<F> = ieee::IeeeFloat<FallbackS<F>>;
|
||||
impl<F: Float> ieee::Semantics for FallbackS<F> {
|
||||
// Forbid any conversion to/from bits.
|
||||
const BITS: usize = 0;
|
||||
const PRECISION: usize = F::PRECISION * 2;
|
||||
const MAX_EXP: ExpInt = F::MAX_EXP as ExpInt;
|
||||
const MIN_EXP: ExpInt = F::MIN_EXP as ExpInt + F::PRECISION as ExpInt;
|
||||
}
|
||||
|
||||
// Convert number to F. To avoid spurious underflows, we re-
|
||||
// normalize against the F exponent range first, and only *then*
|
||||
// truncate the mantissa. The result of that second conversion
|
||||
// may be inexact, but should never underflow.
|
||||
// FIXME(eddyb) This shouldn't need to be `pub`, it's only used in bounds.
|
||||
pub struct FallbackExtendedS<F>(F);
|
||||
type FallbackExtended<F> = ieee::IeeeFloat<FallbackExtendedS<F>>;
|
||||
impl<F: Float> ieee::Semantics for FallbackExtendedS<F> {
|
||||
// Forbid any conversion to/from bits.
|
||||
const BITS: usize = 0;
|
||||
const PRECISION: usize = Fallback::<F>::PRECISION;
|
||||
const MAX_EXP: ExpInt = F::MAX_EXP as ExpInt;
|
||||
}
|
||||
|
||||
impl<F: Float> From<Fallback<F>> for DoubleFloat<F>
|
||||
where
|
||||
F: FloatConvert<FallbackExtended<F>>,
|
||||
FallbackExtended<F>: FloatConvert<F>,
|
||||
{
|
||||
fn from(x: Fallback<F>) -> Self {
|
||||
let mut status;
|
||||
let mut loses_info = false;
|
||||
|
||||
let extended: FallbackExtended<F> = unpack!(status=, x.convert(&mut loses_info));
|
||||
assert_eq!((status, loses_info), (Status::OK, false));
|
||||
|
||||
let a = unpack!(status=, extended.convert(&mut loses_info));
|
||||
assert_eq!(status - Status::INEXACT, Status::OK);
|
||||
|
||||
// If conversion was exact or resulted in a special case, we're done;
|
||||
// just set the second double to zero. Otherwise, re-convert back to
|
||||
// the extended format and compute the difference. This now should
|
||||
// convert exactly to double.
|
||||
let b = if a.is_finite_non_zero() && loses_info {
|
||||
let u: FallbackExtended<F> = unpack!(status=, a.convert(&mut loses_info));
|
||||
assert_eq!((status, loses_info), (Status::OK, false));
|
||||
let v = unpack!(status=, extended - u);
|
||||
assert_eq!(status, Status::OK);
|
||||
let v = unpack!(status=, v.convert(&mut loses_info));
|
||||
assert_eq!((status, loses_info), (Status::OK, false));
|
||||
v
|
||||
} else {
|
||||
F::ZERO
|
||||
};
|
||||
|
||||
DoubleFloat(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FloatConvert<Self>> From<DoubleFloat<F>> for Fallback<F> {
|
||||
fn from(DoubleFloat(a, b): DoubleFloat<F>) -> Self {
|
||||
let mut status;
|
||||
let mut loses_info = false;
|
||||
|
||||
// Get the first F and convert to our format.
|
||||
let a = unpack!(status=, a.convert(&mut loses_info));
|
||||
assert_eq!((status, loses_info), (Status::OK, false));
|
||||
|
||||
// Unless we have a special case, add in second F.
|
||||
if a.is_finite_non_zero() {
|
||||
let b = unpack!(status=, b.convert(&mut loses_info));
|
||||
assert_eq!((status, loses_info), (Status::OK, false));
|
||||
|
||||
(a + b).value
|
||||
} else {
|
||||
a
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float_common_impls!(DoubleFloat<F>);
|
||||
|
||||
impl<F: Float> Neg for DoubleFloat<F> {
|
||||
type Output = Self;
|
||||
fn neg(self) -> Self {
|
||||
if self.1.is_finite_non_zero() {
|
||||
DoubleFloat(-self.0, -self.1)
|
||||
} else {
|
||||
DoubleFloat(-self.0, self.1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FloatConvert<Fallback<F>>> fmt::Display for DoubleFloat<F> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Display::fmt(&Fallback::from(*self), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FloatConvert<Fallback<F>>> Float for DoubleFloat<F>
|
||||
where
|
||||
Self: From<Fallback<F>>,
|
||||
{
|
||||
const BITS: usize = F::BITS * 2;
|
||||
const PRECISION: usize = Fallback::<F>::PRECISION;
|
||||
const MAX_EXP: ExpInt = Fallback::<F>::MAX_EXP;
|
||||
const MIN_EXP: ExpInt = Fallback::<F>::MIN_EXP;
|
||||
|
||||
const ZERO: Self = DoubleFloat(F::ZERO, F::ZERO);
|
||||
|
||||
const INFINITY: Self = DoubleFloat(F::INFINITY, F::ZERO);
|
||||
|
||||
// FIXME(eddyb) remove when qnan becomes const fn.
|
||||
const NAN: Self = DoubleFloat(F::NAN, F::ZERO);
|
||||
|
||||
fn qnan(payload: Option<u128>) -> Self {
|
||||
DoubleFloat(F::qnan(payload), F::ZERO)
|
||||
}
|
||||
|
||||
fn snan(payload: Option<u128>) -> Self {
|
||||
DoubleFloat(F::snan(payload), F::ZERO)
|
||||
}
|
||||
|
||||
fn largest() -> Self {
|
||||
let status;
|
||||
let mut r = DoubleFloat(F::largest(), F::largest());
|
||||
r.1 = r.1.scalbn(-(F::PRECISION as ExpInt + 1));
|
||||
r.1 = unpack!(status=, r.1.next_down());
|
||||
assert_eq!(status, Status::OK);
|
||||
r
|
||||
}
|
||||
|
||||
const SMALLEST: Self = DoubleFloat(F::SMALLEST, F::ZERO);
|
||||
|
||||
fn smallest_normalized() -> Self {
|
||||
DoubleFloat(
|
||||
F::smallest_normalized().scalbn(F::PRECISION as ExpInt),
|
||||
F::ZERO,
|
||||
)
|
||||
}
|
||||
|
||||
// Implement addition, subtraction, multiplication and division based on:
|
||||
// "Software for Doubled-Precision Floating-Point Computations",
|
||||
// by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283.
|
||||
|
||||
fn add_r(mut self, rhs: Self, round: Round) -> StatusAnd<Self> {
|
||||
match (self.category(), rhs.category()) {
|
||||
(Category::Infinity, Category::Infinity) => {
|
||||
if self.is_negative() != rhs.is_negative() {
|
||||
Status::INVALID_OP.and(Self::NAN.copy_sign(self))
|
||||
} else {
|
||||
Status::OK.and(self)
|
||||
}
|
||||
}
|
||||
|
||||
(_, Category::Zero) |
|
||||
(Category::NaN, _) |
|
||||
(Category::Infinity, Category::Normal) => Status::OK.and(self),
|
||||
|
||||
(Category::Zero, _) |
|
||||
(_, Category::NaN) |
|
||||
(_, Category::Infinity) => Status::OK.and(rhs),
|
||||
|
||||
(Category::Normal, Category::Normal) => {
|
||||
let mut status = Status::OK;
|
||||
let (a, aa, c, cc) = (self.0, self.1, rhs.0, rhs.1);
|
||||
let mut z = a;
|
||||
z = unpack!(status|=, z.add_r(c, round));
|
||||
if !z.is_finite() {
|
||||
if !z.is_infinite() {
|
||||
return status.and(DoubleFloat(z, F::ZERO));
|
||||
}
|
||||
status = Status::OK;
|
||||
let a_cmp_c = a.cmp_abs_normal(c);
|
||||
z = cc;
|
||||
z = unpack!(status|=, z.add_r(aa, round));
|
||||
if a_cmp_c == Ordering::Greater {
|
||||
// z = cc + aa + c + a;
|
||||
z = unpack!(status|=, z.add_r(c, round));
|
||||
z = unpack!(status|=, z.add_r(a, round));
|
||||
} else {
|
||||
// z = cc + aa + a + c;
|
||||
z = unpack!(status|=, z.add_r(a, round));
|
||||
z = unpack!(status|=, z.add_r(c, round));
|
||||
}
|
||||
if !z.is_finite() {
|
||||
return status.and(DoubleFloat(z, F::ZERO));
|
||||
}
|
||||
self.0 = z;
|
||||
let mut zz = aa;
|
||||
zz = unpack!(status|=, zz.add_r(cc, round));
|
||||
if a_cmp_c == Ordering::Greater {
|
||||
// self.1 = a - z + c + zz;
|
||||
self.1 = a;
|
||||
self.1 = unpack!(status|=, self.1.sub_r(z, round));
|
||||
self.1 = unpack!(status|=, self.1.add_r(c, round));
|
||||
self.1 = unpack!(status|=, self.1.add_r(zz, round));
|
||||
} else {
|
||||
// self.1 = c - z + a + zz;
|
||||
self.1 = c;
|
||||
self.1 = unpack!(status|=, self.1.sub_r(z, round));
|
||||
self.1 = unpack!(status|=, self.1.add_r(a, round));
|
||||
self.1 = unpack!(status|=, self.1.add_r(zz, round));
|
||||
}
|
||||
} else {
|
||||
// q = a - z;
|
||||
let mut q = a;
|
||||
q = unpack!(status|=, q.sub_r(z, round));
|
||||
|
||||
// zz = q + c + (a - (q + z)) + aa + cc;
|
||||
// Compute a - (q + z) as -((q + z) - a) to avoid temporary copies.
|
||||
let mut zz = q;
|
||||
zz = unpack!(status|=, zz.add_r(c, round));
|
||||
q = unpack!(status|=, q.add_r(z, round));
|
||||
q = unpack!(status|=, q.sub_r(a, round));
|
||||
q = -q;
|
||||
zz = unpack!(status|=, zz.add_r(q, round));
|
||||
zz = unpack!(status|=, zz.add_r(aa, round));
|
||||
zz = unpack!(status|=, zz.add_r(cc, round));
|
||||
if zz.is_zero() && !zz.is_negative() {
|
||||
return Status::OK.and(DoubleFloat(z, F::ZERO));
|
||||
}
|
||||
self.0 = z;
|
||||
self.0 = unpack!(status|=, self.0.add_r(zz, round));
|
||||
if !self.0.is_finite() {
|
||||
self.1 = F::ZERO;
|
||||
return status.and(self);
|
||||
}
|
||||
self.1 = z;
|
||||
self.1 = unpack!(status|=, self.1.sub_r(self.0, round));
|
||||
self.1 = unpack!(status|=, self.1.add_r(zz, round));
|
||||
}
|
||||
status.and(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn mul_r(mut self, rhs: Self, round: Round) -> StatusAnd<Self> {
|
||||
// Interesting observation: For special categories, finding the lowest
|
||||
// common ancestor of the following layered graph gives the correct
|
||||
// return category:
|
||||
//
|
||||
// NaN
|
||||
// / \
|
||||
// Zero Inf
|
||||
// \ /
|
||||
// Normal
|
||||
//
|
||||
// e.g. NaN * NaN = NaN
|
||||
// Zero * Inf = NaN
|
||||
// Normal * Zero = Zero
|
||||
// Normal * Inf = Inf
|
||||
match (self.category(), rhs.category()) {
|
||||
(Category::NaN, _) => Status::OK.and(self),
|
||||
|
||||
(_, Category::NaN) => Status::OK.and(rhs),
|
||||
|
||||
(Category::Zero, Category::Infinity) |
|
||||
(Category::Infinity, Category::Zero) => Status::OK.and(Self::NAN),
|
||||
|
||||
(Category::Zero, _) |
|
||||
(Category::Infinity, _) => Status::OK.and(self),
|
||||
|
||||
(_, Category::Zero) |
|
||||
(_, Category::Infinity) => Status::OK.and(rhs),
|
||||
|
||||
(Category::Normal, Category::Normal) => {
|
||||
let mut status = Status::OK;
|
||||
let (a, b, c, d) = (self.0, self.1, rhs.0, rhs.1);
|
||||
// t = a * c
|
||||
let mut t = a;
|
||||
t = unpack!(status|=, t.mul_r(c, round));
|
||||
if !t.is_finite_non_zero() {
|
||||
return status.and(DoubleFloat(t, F::ZERO));
|
||||
}
|
||||
|
||||
// tau = fmsub(a, c, t), that is -fmadd(-a, c, t).
|
||||
let mut tau = a;
|
||||
tau = unpack!(status|=, tau.mul_add_r(c, -t, round));
|
||||
// v = a * d
|
||||
let mut v = a;
|
||||
v = unpack!(status|=, v.mul_r(d, round));
|
||||
// w = b * c
|
||||
let mut w = b;
|
||||
w = unpack!(status|=, w.mul_r(c, round));
|
||||
v = unpack!(status|=, v.add_r(w, round));
|
||||
// tau += v + w
|
||||
tau = unpack!(status|=, tau.add_r(v, round));
|
||||
// u = t + tau
|
||||
let mut u = t;
|
||||
u = unpack!(status|=, u.add_r(tau, round));
|
||||
|
||||
self.0 = u;
|
||||
if !u.is_finite() {
|
||||
self.1 = F::ZERO;
|
||||
} else {
|
||||
// self.1 = (t - u) + tau
|
||||
t = unpack!(status|=, t.sub_r(u, round));
|
||||
t = unpack!(status|=, t.add_r(tau, round));
|
||||
self.1 = t;
|
||||
}
|
||||
status.and(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn mul_add_r(self, multiplicand: Self, addend: Self, round: Round) -> StatusAnd<Self> {
|
||||
Fallback::from(self)
|
||||
.mul_add_r(Fallback::from(multiplicand), Fallback::from(addend), round)
|
||||
.map(Self::from)
|
||||
}
|
||||
|
||||
fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self> {
|
||||
Fallback::from(self).div_r(Fallback::from(rhs), round).map(
|
||||
Self::from,
|
||||
)
|
||||
}
|
||||
|
||||
fn c_fmod(self, rhs: Self) -> StatusAnd<Self> {
|
||||
Fallback::from(self).c_fmod(Fallback::from(rhs)).map(
|
||||
Self::from,
|
||||
)
|
||||
}
|
||||
|
||||
fn round_to_integral(self, round: Round) -> StatusAnd<Self> {
|
||||
Fallback::from(self).round_to_integral(round).map(
|
||||
Self::from,
|
||||
)
|
||||
}
|
||||
|
||||
fn next_up(self) -> StatusAnd<Self> {
|
||||
Fallback::from(self).next_up().map(Self::from)
|
||||
}
|
||||
|
||||
fn from_bits(input: u128) -> Self {
|
||||
let (a, b) = (input, input >> F::BITS);
|
||||
DoubleFloat(
|
||||
F::from_bits(a & ((1 << F::BITS) - 1)),
|
||||
F::from_bits(b & ((1 << F::BITS) - 1)),
|
||||
)
|
||||
}
|
||||
|
||||
fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self> {
|
||||
Fallback::from_u128_r(input, round).map(Self::from)
|
||||
}
|
||||
|
||||
fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError> {
|
||||
Fallback::from_str_r(s, round).map(|r| r.map(Self::from))
|
||||
}
|
||||
|
||||
fn to_bits(self) -> u128 {
|
||||
self.0.to_bits() | (self.1.to_bits() << F::BITS)
|
||||
}
|
||||
|
||||
fn to_u128_r(self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<u128> {
|
||||
Fallback::from(self).to_u128_r(width, round, is_exact)
|
||||
}
|
||||
|
||||
fn cmp_abs_normal(self, rhs: Self) -> Ordering {
|
||||
self.0.cmp_abs_normal(rhs.0).then_with(|| {
|
||||
let result = self.1.cmp_abs_normal(rhs.1);
|
||||
if result != Ordering::Equal {
|
||||
let against = self.0.is_negative() ^ self.1.is_negative();
|
||||
let rhs_against = rhs.0.is_negative() ^ rhs.1.is_negative();
|
||||
(!against).cmp(&!rhs_against).then_with(|| if against {
|
||||
result.reverse()
|
||||
} else {
|
||||
result
|
||||
})
|
||||
} else {
|
||||
result
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn bitwise_eq(self, rhs: Self) -> bool {
|
||||
self.0.bitwise_eq(rhs.0) && self.1.bitwise_eq(rhs.1)
|
||||
}
|
||||
|
||||
fn is_negative(self) -> bool {
|
||||
self.0.is_negative()
|
||||
}
|
||||
|
||||
fn is_denormal(self) -> bool {
|
||||
self.category() == Category::Normal &&
|
||||
(self.0.is_denormal() || self.0.is_denormal() ||
|
||||
// (double)(Hi + Lo) == Hi defines a normal number.
|
||||
!(self.0 + self.1).value.bitwise_eq(self.0))
|
||||
}
|
||||
|
||||
fn is_signaling(self) -> bool {
|
||||
self.0.is_signaling()
|
||||
}
|
||||
|
||||
fn category(self) -> Category {
|
||||
self.0.category()
|
||||
}
|
||||
|
||||
fn get_exact_inverse(self) -> Option<Self> {
|
||||
Fallback::from(self).get_exact_inverse().map(Self::from)
|
||||
}
|
||||
|
||||
fn ilogb(self) -> ExpInt {
|
||||
self.0.ilogb()
|
||||
}
|
||||
|
||||
fn scalbn_r(self, exp: ExpInt, round: Round) -> Self {
|
||||
DoubleFloat(self.0.scalbn_r(exp, round), self.1.scalbn_r(exp, round))
|
||||
}
|
||||
|
||||
fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self {
|
||||
let a = self.0.frexp_r(exp, round);
|
||||
let mut b = self.1;
|
||||
if self.category() == Category::Normal {
|
||||
b = b.scalbn_r(-*exp, round);
|
||||
}
|
||||
DoubleFloat(a, b)
|
||||
}
|
||||
}
|
6891
src/librustc_apfloat/tests/ieee.rs
Normal file
6891
src/librustc_apfloat/tests/ieee.rs
Normal file
File diff suppressed because it is too large
Load Diff
655
src/librustc_apfloat/tests/ppc.rs
Normal file
655
src/librustc_apfloat/tests/ppc.rs
Normal file
@ -0,0 +1,655 @@
|
||||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
extern crate rustc_apfloat;
|
||||
|
||||
use rustc_apfloat::{Category, Float, Round};
|
||||
use rustc_apfloat::ppc::DoubleDouble;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double() {
|
||||
let test = DoubleDouble::ZERO;
|
||||
let expected = "0x0p+0".parse::<DoubleDouble>().unwrap();
|
||||
assert!(test.is_zero());
|
||||
assert!(!test.is_negative());
|
||||
assert!(test.bitwise_eq(expected));
|
||||
assert_eq!(0, test.to_bits());
|
||||
|
||||
let test = -DoubleDouble::ZERO;
|
||||
let expected = "-0x0p+0".parse::<DoubleDouble>().unwrap();
|
||||
assert!(test.is_zero());
|
||||
assert!(test.is_negative());
|
||||
assert!(test.bitwise_eq(expected));
|
||||
assert_eq!(0x8000000000000000, test.to_bits());
|
||||
|
||||
let test = "1.0".parse::<DoubleDouble>().unwrap();
|
||||
assert_eq!(0x3ff0000000000000, test.to_bits());
|
||||
|
||||
// LDBL_MAX
|
||||
let test = "1.79769313486231580793728971405301e+308"
|
||||
.parse::<DoubleDouble>()
|
||||
.unwrap();
|
||||
assert_eq!(0x7c8ffffffffffffe_7fefffffffffffff, test.to_bits());
|
||||
|
||||
// LDBL_MIN
|
||||
let test = "2.00416836000897277799610805135016e-292"
|
||||
.parse::<DoubleDouble>()
|
||||
.unwrap();
|
||||
assert_eq!(0x0000000000000000_0360000000000000, test.to_bits());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_add_special() {
|
||||
let data = [
|
||||
// (1 + 0) + (-1 + 0) = Category::Zero
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0xbff0000000000000,
|
||||
Category::Zero,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// LDBL_MAX + (1.1 >> (1023 - 106) + 0)) = Category::Infinity
|
||||
(
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
0x7948000000000000,
|
||||
Category::Infinity,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// FIXME: change the 4th 0x75effffffffffffe to 0x75efffffffffffff when
|
||||
// DoubleDouble's fallback is gone.
|
||||
// LDBL_MAX + (1.011111... >> (1023 - 106) + (1.1111111...0 >> (1023 -
|
||||
// 160))) = Category::Normal
|
||||
(
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
0x75effffffffffffe_7947ffffffffffff,
|
||||
Category::Normal,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// LDBL_MAX + (1.1 >> (1023 - 106) + 0)) = Category::Infinity
|
||||
(
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
Category::Infinity,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// NaN + (1 + 0) = Category::NaN
|
||||
(
|
||||
0x7ff8000000000000,
|
||||
0x3ff0000000000000,
|
||||
Category::NaN,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected, round) in &data {
|
||||
{
|
||||
let mut a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
a1 = a1.add_r(a2, round).value;
|
||||
|
||||
assert_eq!(expected, a1.category(), "{:#x} + {:#x}", op1, op2);
|
||||
}
|
||||
{
|
||||
let a1 = DoubleDouble::from_bits(op1);
|
||||
let mut a2 = DoubleDouble::from_bits(op2);
|
||||
a2 = a2.add_r(a1, round).value;
|
||||
|
||||
assert_eq!(expected, a2.category(), "{:#x} + {:#x}", op2, op1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_add() {
|
||||
let data = [
|
||||
// (1 + 0) + (1e-105 + 0) = (1 + 1e-105)
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0x3960000000000000,
|
||||
0x3960000000000000_3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// (1 + 0) + (1e-106 + 0) = (1 + 1e-106)
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0x3950000000000000,
|
||||
0x3950000000000000_3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// (1 + 1e-106) + (1e-106 + 0) = (1 + 1e-105)
|
||||
(
|
||||
0x3950000000000000_3ff0000000000000,
|
||||
0x3950000000000000,
|
||||
0x3960000000000000_3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// (1 + 0) + (epsilon + 0) = (1 + epsilon)
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0x0000000000000001,
|
||||
0x0000000000000001_3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// FIXME: change 0xf950000000000000 to 0xf940000000000000, when
|
||||
// DoubleDouble's fallback is gone.
|
||||
// (DBL_MAX - 1 << (1023 - 105)) + (1 << (1023 - 53) + 0) = DBL_MAX +
|
||||
// 1.11111... << (1023 - 52)
|
||||
(
|
||||
0xf950000000000000_7fefffffffffffff,
|
||||
0x7c90000000000000,
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// FIXME: change 0xf950000000000000 to 0xf940000000000000, when
|
||||
// DoubleDouble's fallback is gone.
|
||||
// (1 << (1023 - 53) + 0) + (DBL_MAX - 1 << (1023 - 105)) = DBL_MAX +
|
||||
// 1.11111... << (1023 - 52)
|
||||
(
|
||||
0x7c90000000000000,
|
||||
0xf950000000000000_7fefffffffffffff,
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected, round) in &data {
|
||||
{
|
||||
let mut a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
a1 = a1.add_r(a2, round).value;
|
||||
|
||||
assert_eq!(expected, a1.to_bits(), "{:#x} + {:#x}", op1, op2);
|
||||
}
|
||||
{
|
||||
let a1 = DoubleDouble::from_bits(op1);
|
||||
let mut a2 = DoubleDouble::from_bits(op2);
|
||||
a2 = a2.add_r(a1, round).value;
|
||||
|
||||
assert_eq!(expected, a2.to_bits(), "{:#x} + {:#x}", op2, op1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_subtract() {
|
||||
let data = [
|
||||
// (1 + 0) - (-1e-105 + 0) = (1 + 1e-105)
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0xb960000000000000,
|
||||
0x3960000000000000_3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// (1 + 0) - (-1e-106 + 0) = (1 + 1e-106)
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0xb950000000000000,
|
||||
0x3950000000000000_3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected, round) in &data {
|
||||
let mut a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
a1 = a1.sub_r(a2, round).value;
|
||||
|
||||
assert_eq!(expected, a1.to_bits(), "{:#x} - {:#x}", op1, op2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_multiply_special() {
|
||||
let data = [
|
||||
// Category::NaN * Category::NaN = Category::NaN
|
||||
(
|
||||
0x7ff8000000000000,
|
||||
0x7ff8000000000000,
|
||||
Category::NaN,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// Category::NaN * Category::Zero = Category::NaN
|
||||
(
|
||||
0x7ff8000000000000,
|
||||
0,
|
||||
Category::NaN,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// Category::NaN * Category::Infinity = Category::NaN
|
||||
(
|
||||
0x7ff8000000000000,
|
||||
0x7ff0000000000000,
|
||||
Category::NaN,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// Category::NaN * Category::Normal = Category::NaN
|
||||
(
|
||||
0x7ff8000000000000,
|
||||
0x3ff0000000000000,
|
||||
Category::NaN,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// Category::Infinity * Category::Infinity = Category::Infinity
|
||||
(
|
||||
0x7ff0000000000000,
|
||||
0x7ff0000000000000,
|
||||
Category::Infinity,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// Category::Infinity * Category::Zero = Category::NaN
|
||||
(
|
||||
0x7ff0000000000000,
|
||||
0,
|
||||
Category::NaN,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// Category::Infinity * Category::Normal = Category::Infinity
|
||||
(
|
||||
0x7ff0000000000000,
|
||||
0x3ff0000000000000,
|
||||
Category::Infinity,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// Category::Zero * Category::Zero = Category::Zero
|
||||
(0, 0, Category::Zero, Round::NearestTiesToEven),
|
||||
// Category::Zero * Category::Normal = Category::Zero
|
||||
(
|
||||
0,
|
||||
0x3ff0000000000000,
|
||||
Category::Zero,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected, round) in &data {
|
||||
{
|
||||
let mut a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
a1 = a1.mul_r(a2, round).value;
|
||||
|
||||
assert_eq!(expected, a1.category(), "{:#x} * {:#x}", op1, op2);
|
||||
}
|
||||
{
|
||||
let a1 = DoubleDouble::from_bits(op1);
|
||||
let mut a2 = DoubleDouble::from_bits(op2);
|
||||
a2 = a2.mul_r(a1, round).value;
|
||||
|
||||
assert_eq!(expected, a2.category(), "{:#x} * {:#x}", op2, op1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_multiply() {
|
||||
let data = [
|
||||
// 1/3 * 3 = 1.0
|
||||
(
|
||||
0x3c75555555555556_3fd5555555555555,
|
||||
0x4008000000000000,
|
||||
0x3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// (1 + epsilon) * (1 + 0) = Category::Zero
|
||||
(
|
||||
0x0000000000000001_3ff0000000000000,
|
||||
0x3ff0000000000000,
|
||||
0x0000000000000001_3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// (1 + epsilon) * (1 + epsilon) = 1 + 2 * epsilon
|
||||
(
|
||||
0x0000000000000001_3ff0000000000000,
|
||||
0x0000000000000001_3ff0000000000000,
|
||||
0x0000000000000002_3ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// -(1 + epsilon) * (1 + epsilon) = -1
|
||||
(
|
||||
0x0000000000000001_bff0000000000000,
|
||||
0x0000000000000001_3ff0000000000000,
|
||||
0xbff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// (0.5 + 0) * (1 + 2 * epsilon) = 0.5 + epsilon
|
||||
(
|
||||
0x3fe0000000000000,
|
||||
0x0000000000000002_3ff0000000000000,
|
||||
0x0000000000000001_3fe0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// (0.5 + 0) * (1 + epsilon) = 0.5
|
||||
(
|
||||
0x3fe0000000000000,
|
||||
0x0000000000000001_3ff0000000000000,
|
||||
0x3fe0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// __LDBL_MAX__ * (1 + 1 << 106) = inf
|
||||
(
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
0x3950000000000000_3ff0000000000000,
|
||||
0x7ff0000000000000,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// __LDBL_MAX__ * (1 + 1 << 107) > __LDBL_MAX__, but not inf, yes =_=|||
|
||||
(
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
0x3940000000000000_3ff0000000000000,
|
||||
0x7c8fffffffffffff_7fefffffffffffff,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
// __LDBL_MAX__ * (1 + 1 << 108) = __LDBL_MAX__
|
||||
(
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
0x3930000000000000_3ff0000000000000,
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected, round) in &data {
|
||||
{
|
||||
let mut a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
a1 = a1.mul_r(a2, round).value;
|
||||
|
||||
assert_eq!(expected, a1.to_bits(), "{:#x} * {:#x}", op1, op2);
|
||||
}
|
||||
{
|
||||
let a1 = DoubleDouble::from_bits(op1);
|
||||
let mut a2 = DoubleDouble::from_bits(op2);
|
||||
a2 = a2.mul_r(a1, round).value;
|
||||
|
||||
assert_eq!(expected, a2.to_bits(), "{:#x} * {:#x}", op2, op1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_divide() {
|
||||
// FIXME: Only a sanity check for now. Add more edge cases when the
|
||||
// double-double algorithm is implemented.
|
||||
let data = [
|
||||
// 1 / 3 = 1/3
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0x4008000000000000,
|
||||
0x3c75555555555556_3fd5555555555555,
|
||||
Round::NearestTiesToEven,
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected, round) in &data {
|
||||
let mut a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
a1 = a1.div_r(a2, round).value;
|
||||
|
||||
assert_eq!(expected, a1.to_bits(), "{:#x} / {:#x}", op1, op2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_remainder() {
|
||||
let data = [
|
||||
// ieee_rem(3.0 + 3.0 << 53, 1.25 + 1.25 << 53) = (0.5 + 0.5 << 53)
|
||||
(
|
||||
0x3cb8000000000000_4008000000000000,
|
||||
0x3ca4000000000000_3ff4000000000000,
|
||||
0x3c90000000000000_3fe0000000000000,
|
||||
),
|
||||
// ieee_rem(3.0 + 3.0 << 53, 1.75 + 1.75 << 53) = (-0.5 - 0.5 << 53)
|
||||
(
|
||||
0x3cb8000000000000_4008000000000000,
|
||||
0x3cac000000000000_3ffc000000000000,
|
||||
0xbc90000000000000_bfe0000000000000,
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected) in &data {
|
||||
let a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
let result = a1.ieee_rem(a2).value;
|
||||
|
||||
assert_eq!(
|
||||
expected,
|
||||
result.to_bits(),
|
||||
"ieee_rem({:#x}, {:#x})",
|
||||
op1,
|
||||
op2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_mod() {
|
||||
let data = [
|
||||
// mod(3.0 + 3.0 << 53, 1.25 + 1.25 << 53) = (0.5 + 0.5 << 53)
|
||||
(
|
||||
0x3cb8000000000000_4008000000000000,
|
||||
0x3ca4000000000000_3ff4000000000000,
|
||||
0x3c90000000000000_3fe0000000000000,
|
||||
),
|
||||
// mod(3.0 + 3.0 << 53, 1.75 + 1.75 << 53) = (1.25 + 1.25 << 53)
|
||||
// 0xbc98000000000000 doesn't seem right, but it's what we currently have.
|
||||
// FIXME: investigate
|
||||
(
|
||||
0x3cb8000000000000_4008000000000000,
|
||||
0x3cac000000000000_3ffc000000000000,
|
||||
0xbc98000000000000_3ff4000000000001,
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected) in &data {
|
||||
let a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
let r = (a1 % a2).value;
|
||||
|
||||
assert_eq!(expected, r.to_bits(), "fmod({:#x}, {:#x})", op1, op2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_fma() {
|
||||
// Sanity check for now.
|
||||
let mut a = "2".parse::<DoubleDouble>().unwrap();
|
||||
a = a.mul_add(
|
||||
"3".parse::<DoubleDouble>().unwrap(),
|
||||
"4".parse::<DoubleDouble>().unwrap(),
|
||||
).value;
|
||||
assert_eq!(
|
||||
Some(Ordering::Equal),
|
||||
"10".parse::<DoubleDouble>().unwrap().partial_cmp(&a)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_round_to_integral() {
|
||||
{
|
||||
let a = "1.5".parse::<DoubleDouble>().unwrap();
|
||||
let a = a.round_to_integral(Round::NearestTiesToEven).value;
|
||||
assert_eq!(
|
||||
Some(Ordering::Equal),
|
||||
"2".parse::<DoubleDouble>().unwrap().partial_cmp(&a)
|
||||
);
|
||||
}
|
||||
{
|
||||
let a = "2.5".parse::<DoubleDouble>().unwrap();
|
||||
let a = a.round_to_integral(Round::NearestTiesToEven).value;
|
||||
assert_eq!(
|
||||
Some(Ordering::Equal),
|
||||
"2".parse::<DoubleDouble>().unwrap().partial_cmp(&a)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_compare() {
|
||||
let data = [
|
||||
// (1 + 0) = (1 + 0)
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0x3ff0000000000000,
|
||||
Some(Ordering::Equal),
|
||||
),
|
||||
// (1 + 0) < (1.00...1 + 0)
|
||||
(0x3ff0000000000000, 0x3ff0000000000001, Some(Ordering::Less)),
|
||||
// (1.00...1 + 0) > (1 + 0)
|
||||
(
|
||||
0x3ff0000000000001,
|
||||
0x3ff0000000000000,
|
||||
Some(Ordering::Greater),
|
||||
),
|
||||
// (1 + 0) < (1 + epsilon)
|
||||
(
|
||||
0x3ff0000000000000,
|
||||
0x0000000000000001_3ff0000000000001,
|
||||
Some(Ordering::Less),
|
||||
),
|
||||
// NaN != NaN
|
||||
(0x7ff8000000000000, 0x7ff8000000000000, None),
|
||||
// (1 + 0) != NaN
|
||||
(0x3ff0000000000000, 0x7ff8000000000000, None),
|
||||
// Inf = Inf
|
||||
(
|
||||
0x7ff0000000000000,
|
||||
0x7ff0000000000000,
|
||||
Some(Ordering::Equal),
|
||||
),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected) in &data {
|
||||
let a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
assert_eq!(
|
||||
expected,
|
||||
a1.partial_cmp(&a2),
|
||||
"compare({:#x}, {:#x})",
|
||||
op1,
|
||||
op2,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_bitwise_eq() {
|
||||
let data = [
|
||||
// (1 + 0) = (1 + 0)
|
||||
(0x3ff0000000000000, 0x3ff0000000000000, true),
|
||||
// (1 + 0) != (1.00...1 + 0)
|
||||
(0x3ff0000000000000, 0x3ff0000000000001, false),
|
||||
// NaN = NaN
|
||||
(0x7ff8000000000000, 0x7ff8000000000000, true),
|
||||
// NaN != NaN with a different bit pattern
|
||||
(
|
||||
0x7ff8000000000000,
|
||||
0x3ff0000000000000_7ff8000000000000,
|
||||
false,
|
||||
),
|
||||
// Inf = Inf
|
||||
(0x7ff0000000000000, 0x7ff0000000000000, true),
|
||||
];
|
||||
|
||||
for &(op1, op2, expected) in &data {
|
||||
let a1 = DoubleDouble::from_bits(op1);
|
||||
let a2 = DoubleDouble::from_bits(op2);
|
||||
assert_eq!(expected, a1.bitwise_eq(a2), "{:#x} = {:#x}", op1, op2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_change_sign() {
|
||||
let float = DoubleDouble::from_bits(0xbcb0000000000000_400f000000000000);
|
||||
{
|
||||
let actual = float.copy_sign("1".parse::<DoubleDouble>().unwrap());
|
||||
assert_eq!(0xbcb0000000000000_400f000000000000, actual.to_bits());
|
||||
}
|
||||
{
|
||||
let actual = float.copy_sign("-1".parse::<DoubleDouble>().unwrap());
|
||||
assert_eq!(0x3cb0000000000000_c00f000000000000, actual.to_bits());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_factories() {
|
||||
assert_eq!(0, DoubleDouble::ZERO.to_bits());
|
||||
assert_eq!(
|
||||
0x7c8ffffffffffffe_7fefffffffffffff,
|
||||
DoubleDouble::largest().to_bits()
|
||||
);
|
||||
assert_eq!(0x0000000000000001, DoubleDouble::SMALLEST.to_bits());
|
||||
assert_eq!(
|
||||
0x0360000000000000,
|
||||
DoubleDouble::smallest_normalized().to_bits()
|
||||
);
|
||||
assert_eq!(
|
||||
0x0000000000000000_8000000000000000,
|
||||
(-DoubleDouble::ZERO).to_bits()
|
||||
);
|
||||
assert_eq!(
|
||||
0xfc8ffffffffffffe_ffefffffffffffff,
|
||||
(-DoubleDouble::largest()).to_bits()
|
||||
);
|
||||
assert_eq!(
|
||||
0x0000000000000000_8000000000000001,
|
||||
(-DoubleDouble::SMALLEST).to_bits()
|
||||
);
|
||||
assert_eq!(
|
||||
0x0000000000000000_8360000000000000,
|
||||
(-DoubleDouble::smallest_normalized()).to_bits()
|
||||
);
|
||||
assert!(DoubleDouble::SMALLEST.is_smallest());
|
||||
assert!(DoubleDouble::largest().is_largest());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_is_denormal() {
|
||||
assert!(DoubleDouble::SMALLEST.is_denormal());
|
||||
assert!(!DoubleDouble::largest().is_denormal());
|
||||
assert!(!DoubleDouble::smallest_normalized().is_denormal());
|
||||
{
|
||||
// (4 + 3) is not normalized
|
||||
let data = 0x4008000000000000_4010000000000000;
|
||||
assert!(DoubleDouble::from_bits(data).is_denormal());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_exact_inverse() {
|
||||
assert!(
|
||||
"2.0"
|
||||
.parse::<DoubleDouble>()
|
||||
.unwrap()
|
||||
.get_exact_inverse()
|
||||
.unwrap()
|
||||
.bitwise_eq("0.5".parse::<DoubleDouble>().unwrap())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_scalbn() {
|
||||
// 3.0 + 3.0 << 53
|
||||
let input = 0x3cb8000000000000_4008000000000000;
|
||||
let result = DoubleDouble::from_bits(input).scalbn(1);
|
||||
// 6.0 + 6.0 << 53
|
||||
assert_eq!(0x3cc8000000000000_4018000000000000, result.to_bits());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ppc_double_double_frexp() {
|
||||
// 3.0 + 3.0 << 53
|
||||
let input = 0x3cb8000000000000_4008000000000000;
|
||||
let mut exp = 0;
|
||||
// 0.75 + 0.75 << 53
|
||||
let result = DoubleDouble::from_bits(input).frexp(&mut exp);
|
||||
assert_eq!(2, exp);
|
||||
assert_eq!(0x3c98000000000000_3fe8000000000000, result.to_bits());
|
||||
}
|
@ -26,6 +26,7 @@ use rustc::util::nodemap::DefIdMap;
|
||||
|
||||
use syntax::abi::Abi;
|
||||
use syntax::ast;
|
||||
use syntax::attr;
|
||||
use rustc::hir::{self, Expr};
|
||||
use syntax_pos::Span;
|
||||
|
||||
@ -560,8 +561,15 @@ fn cast_const_int<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
ty::TyUint(ast::UintTy::Us) => {
|
||||
Ok(Integral(Usize(ConstUsize::new_truncating(v, tcx.sess.target.uint_type))))
|
||||
},
|
||||
ty::TyFloat(ast::FloatTy::F64) => Ok(Float(F64(val.to_f64()))),
|
||||
ty::TyFloat(ast::FloatTy::F32) => Ok(Float(F32(val.to_f32()))),
|
||||
ty::TyFloat(fty) => {
|
||||
if let Some(i) = val.to_u128() {
|
||||
Ok(Float(ConstFloat::from_u128(i, fty)))
|
||||
} else {
|
||||
// The value must be negative, go through signed integers.
|
||||
let i = val.to_u128_unchecked() as i128;
|
||||
Ok(Float(ConstFloat::from_i128(i, fty)))
|
||||
}
|
||||
}
|
||||
ty::TyRawPtr(_) => Err(ErrKind::UnimplementedConstVal("casting an address to a raw ptr")),
|
||||
ty::TyChar => match val {
|
||||
U8(u) => Ok(Char(u as char)),
|
||||
@ -574,30 +582,25 @@ fn cast_const_int<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
fn cast_const_float<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
val: ConstFloat,
|
||||
ty: Ty<'tcx>) -> CastResult<'tcx> {
|
||||
let int_width = |ty| {
|
||||
ty::layout::Integer::from_attr(tcx, ty).size().bits() as usize
|
||||
};
|
||||
match ty.sty {
|
||||
ty::TyInt(_) | ty::TyUint(_) => {
|
||||
let i = match val {
|
||||
F32(f) if f >= 0.0 => U128(f as u128),
|
||||
F64(f) if f >= 0.0 => U128(f as u128),
|
||||
|
||||
F32(f) => I128(f as i128),
|
||||
F64(f) => I128(f as i128)
|
||||
};
|
||||
|
||||
if let (I128(_), &ty::TyUint(_)) = (i, &ty.sty) {
|
||||
return Err(CannotCast);
|
||||
ty::TyInt(ity) => {
|
||||
if let Some(i) = val.to_i128(int_width(attr::SignedInt(ity))) {
|
||||
cast_const_int(tcx, I128(i), ty)
|
||||
} else {
|
||||
Err(CannotCast)
|
||||
}
|
||||
|
||||
cast_const_int(tcx, i, ty)
|
||||
}
|
||||
ty::TyFloat(ast::FloatTy::F64) => Ok(Float(F64(match val {
|
||||
F32(f) => f as f64,
|
||||
F64(f) => f
|
||||
}))),
|
||||
ty::TyFloat(ast::FloatTy::F32) => Ok(Float(F32(match val {
|
||||
F64(f) => f as f32,
|
||||
F32(f) => f
|
||||
}))),
|
||||
ty::TyUint(uty) => {
|
||||
if let Some(i) = val.to_u128(int_width(attr::UnsignedInt(uty))) {
|
||||
cast_const_int(tcx, U128(i), ty)
|
||||
} else {
|
||||
Err(CannotCast)
|
||||
}
|
||||
}
|
||||
ty::TyFloat(fty) => Ok(Float(val.convert(fty))),
|
||||
_ => Err(CannotCast),
|
||||
}
|
||||
}
|
||||
@ -691,11 +694,7 @@ fn lit_to_const<'a, 'tcx>(lit: &ast::LitKind,
|
||||
|
||||
fn parse_float<'tcx>(num: &str, fty: ast::FloatTy)
|
||||
-> Result<ConstFloat, ErrKind<'tcx>> {
|
||||
let val = match fty {
|
||||
ast::FloatTy::F32 => num.parse::<f32>().map(F32),
|
||||
ast::FloatTy::F64 => num.parse::<f64>().map(F64)
|
||||
};
|
||||
val.map_err(|_| {
|
||||
ConstFloat::from_str(num, fty).map_err(|_| {
|
||||
// FIXME(#31407) this is only necessary because float parsing is buggy
|
||||
UnimplementedConstVal("could not evaluate float literal (see issue #31407)")
|
||||
})
|
||||
|
@ -9,5 +9,6 @@ path = "lib.rs"
|
||||
crate-type = ["dylib"]
|
||||
|
||||
[dependencies]
|
||||
rustc_apfloat = { path = "../librustc_apfloat" }
|
||||
serialize = { path = "../libserialize" }
|
||||
syntax = { path = "../libsyntax" }
|
||||
|
@ -9,102 +9,164 @@
|
||||
// except according to those terms.
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::hash;
|
||||
use std::mem::transmute;
|
||||
use std::num::ParseFloatError;
|
||||
|
||||
use syntax::ast;
|
||||
|
||||
use rustc_apfloat::{Float, FloatConvert, Status};
|
||||
use rustc_apfloat::ieee::{Single, Double};
|
||||
|
||||
use super::err::*;
|
||||
|
||||
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
|
||||
pub enum ConstFloat {
|
||||
F32(f32),
|
||||
F64(f64)
|
||||
// Note that equality for `ConstFloat` means that the it is the same
|
||||
// constant, not that the rust values are equal. In particular, `NaN
|
||||
// == NaN` (at least if it's the same NaN; distinct encodings for NaN
|
||||
// are considering unequal).
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
||||
pub struct ConstFloat {
|
||||
pub ty: ast::FloatTy,
|
||||
|
||||
// This is a bit inefficient but it makes conversions below more
|
||||
// ergonomic, and all of this will go away once `miri` is merged.
|
||||
pub bits: u128,
|
||||
}
|
||||
pub use self::ConstFloat::*;
|
||||
|
||||
impl ConstFloat {
|
||||
/// Description of the type, not the value
|
||||
pub fn description(&self) -> &'static str {
|
||||
match *self {
|
||||
F32(_) => "f32",
|
||||
F64(_) => "f64",
|
||||
}
|
||||
self.ty.ty_to_string()
|
||||
}
|
||||
|
||||
pub fn is_nan(&self) -> bool {
|
||||
match *self {
|
||||
F32(f) => f.is_nan(),
|
||||
F64(f) => f.is_nan(),
|
||||
match self.ty {
|
||||
ast::FloatTy::F32 => Single::from_bits(self.bits).is_nan(),
|
||||
ast::FloatTy::F64 => Double::from_bits(self.bits).is_nan(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compares the values if they are of the same type
|
||||
pub fn try_cmp(self, rhs: Self) -> Result<Ordering, ConstMathErr> {
|
||||
match (self, rhs) {
|
||||
(F64(a), F64(b)) => {
|
||||
match (self.ty, rhs.ty) {
|
||||
(ast::FloatTy::F64, ast::FloatTy::F64) => {
|
||||
let a = Double::from_bits(self.bits);
|
||||
let b = Double::from_bits(rhs.bits);
|
||||
// This is pretty bad but it is the existing behavior.
|
||||
Ok(if a == b {
|
||||
Ordering::Equal
|
||||
} else if a < b {
|
||||
Ordering::Less
|
||||
} else {
|
||||
Ordering::Greater
|
||||
})
|
||||
Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
|
||||
}
|
||||
|
||||
(F32(a), F32(b)) => {
|
||||
Ok(if a == b {
|
||||
Ordering::Equal
|
||||
} else if a < b {
|
||||
Ordering::Less
|
||||
} else {
|
||||
Ordering::Greater
|
||||
})
|
||||
(ast::FloatTy::F32, ast::FloatTy::F32) => {
|
||||
let a = Single::from_bits(self.bits);
|
||||
let b = Single::from_bits(rhs.bits);
|
||||
Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
|
||||
}
|
||||
|
||||
_ => Err(CmpBetweenUnequalTypes),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Note that equality for `ConstFloat` means that the it is the same
|
||||
/// constant, not that the rust values are equal. In particular, `NaN
|
||||
/// == NaN` (at least if it's the same NaN; distinct encodings for NaN
|
||||
/// are considering unequal).
|
||||
impl PartialEq for ConstFloat {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (*self, *other) {
|
||||
(F64(a), F64(b)) => {
|
||||
unsafe{transmute::<_,u64>(a) == transmute::<_,u64>(b)}
|
||||
pub fn from_i128(input: i128, ty: ast::FloatTy) -> Self {
|
||||
let bits = match ty {
|
||||
ast::FloatTy::F32 => Single::from_i128(input).value.to_bits(),
|
||||
ast::FloatTy::F64 => Double::from_i128(input).value.to_bits()
|
||||
};
|
||||
ConstFloat { bits, ty }
|
||||
}
|
||||
|
||||
pub fn from_u128(input: u128, ty: ast::FloatTy) -> Self {
|
||||
let bits = match ty {
|
||||
ast::FloatTy::F32 => Single::from_u128(input).value.to_bits(),
|
||||
ast::FloatTy::F64 => Double::from_u128(input).value.to_bits()
|
||||
};
|
||||
ConstFloat { bits, ty }
|
||||
}
|
||||
|
||||
pub fn from_str(num: &str, ty: ast::FloatTy) -> Result<Self, ParseFloatError> {
|
||||
let bits = match ty {
|
||||
ast::FloatTy::F32 => {
|
||||
let rust_bits = num.parse::<f32>()?.to_bits() as u128;
|
||||
let apfloat = num.parse::<Single>().unwrap_or_else(|e| {
|
||||
panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e);
|
||||
});
|
||||
let apfloat_bits = apfloat.to_bits();
|
||||
assert!(rust_bits == apfloat_bits,
|
||||
"apfloat::ieee::Single gave different result for `{}`: \
|
||||
{}({:#x}) vs Rust's {}({:#x})",
|
||||
num, apfloat, apfloat_bits,
|
||||
Single::from_bits(rust_bits), rust_bits);
|
||||
apfloat_bits
|
||||
}
|
||||
(F32(a), F32(b)) => {
|
||||
unsafe{transmute::<_,u32>(a) == transmute::<_,u32>(b)}
|
||||
ast::FloatTy::F64 => {
|
||||
let rust_bits = num.parse::<f64>()?.to_bits() as u128;
|
||||
let apfloat = num.parse::<Double>().unwrap_or_else(|e| {
|
||||
panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e);
|
||||
});
|
||||
let apfloat_bits = apfloat.to_bits();
|
||||
assert!(rust_bits == apfloat_bits,
|
||||
"apfloat::ieee::Double gave different result for `{}`: \
|
||||
{}({:#x}) vs Rust's {}({:#x})",
|
||||
num, apfloat, apfloat_bits,
|
||||
Double::from_bits(rust_bits), rust_bits);
|
||||
apfloat_bits
|
||||
}
|
||||
_ => false
|
||||
};
|
||||
Ok(ConstFloat { bits, ty })
|
||||
}
|
||||
|
||||
pub fn to_i128(self, width: usize) -> Option<i128> {
|
||||
assert!(width <= 128);
|
||||
let r = match self.ty {
|
||||
ast::FloatTy::F32 => Single::from_bits(self.bits).to_i128(width),
|
||||
ast::FloatTy::F64 => Double::from_bits(self.bits).to_i128(width)
|
||||
};
|
||||
if r.status.intersects(Status::INVALID_OP) {
|
||||
None
|
||||
} else {
|
||||
Some(r.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for ConstFloat {}
|
||||
|
||||
impl hash::Hash for ConstFloat {
|
||||
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
||||
match *self {
|
||||
F64(a) => {
|
||||
unsafe { transmute::<_,u64>(a) }.hash(state)
|
||||
}
|
||||
F32(a) => {
|
||||
unsafe { transmute::<_,u32>(a) }.hash(state)
|
||||
}
|
||||
pub fn to_u128(self, width: usize) -> Option<u128> {
|
||||
assert!(width <= 128);
|
||||
let r = match self.ty {
|
||||
ast::FloatTy::F32 => Single::from_bits(self.bits).to_u128(width),
|
||||
ast::FloatTy::F64 => Double::from_bits(self.bits).to_u128(width)
|
||||
};
|
||||
if r.status.intersects(Status::INVALID_OP) {
|
||||
None
|
||||
} else {
|
||||
Some(r.value)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn convert(self, to: ast::FloatTy) -> Self {
|
||||
let bits = match (self.ty, to) {
|
||||
(ast::FloatTy::F32, ast::FloatTy::F32) |
|
||||
(ast::FloatTy::F64, ast::FloatTy::F64) => return self,
|
||||
|
||||
(ast::FloatTy::F32, ast::FloatTy::F64) => {
|
||||
Double::to_bits(Single::from_bits(self.bits).convert(&mut false).value)
|
||||
}
|
||||
(ast::FloatTy::F64, ast::FloatTy::F32) => {
|
||||
Single::to_bits(Double::from_bits(self.bits).convert(&mut false).value)
|
||||
}
|
||||
};
|
||||
ConstFloat { bits, ty: to }
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Display for ConstFloat {
|
||||
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
||||
match *self {
|
||||
F32(f) => write!(fmt, "{}f32", f),
|
||||
F64(f) => write!(fmt, "{}f64", f),
|
||||
match self.ty {
|
||||
ast::FloatTy::F32 => write!(fmt, "{:#}", Single::from_bits(self.bits))?,
|
||||
ast::FloatTy::F64 => write!(fmt, "{:#}", Double::from_bits(self.bits))?,
|
||||
}
|
||||
write!(fmt, "{}", self.ty)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for ConstFloat {
|
||||
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
||||
::std::fmt::Display::fmt(self, fmt)
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,11 +175,20 @@ macro_rules! derive_binop {
|
||||
impl ::std::ops::$op for ConstFloat {
|
||||
type Output = Result<Self, ConstMathErr>;
|
||||
fn $func(self, rhs: Self) -> Result<Self, ConstMathErr> {
|
||||
match (self, rhs) {
|
||||
(F32(a), F32(b)) => Ok(F32(a.$func(b))),
|
||||
(F64(a), F64(b)) => Ok(F64(a.$func(b))),
|
||||
_ => Err(UnequalTypes(Op::$op)),
|
||||
}
|
||||
let bits = match (self.ty, rhs.ty) {
|
||||
(ast::FloatTy::F32, ast::FloatTy::F32) =>{
|
||||
let a = Single::from_bits(self.bits);
|
||||
let b = Single::from_bits(rhs.bits);
|
||||
a.$func(b).value.to_bits()
|
||||
}
|
||||
(ast::FloatTy::F64, ast::FloatTy::F64) => {
|
||||
let a = Double::from_bits(self.bits);
|
||||
let b = Double::from_bits(rhs.bits);
|
||||
a.$func(b).value.to_bits()
|
||||
}
|
||||
_ => return Err(UnequalTypes(Op::$op)),
|
||||
};
|
||||
Ok(ConstFloat { bits, ty: self.ty })
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -132,9 +203,10 @@ derive_binop!(Rem, rem);
|
||||
impl ::std::ops::Neg for ConstFloat {
|
||||
type Output = Self;
|
||||
fn neg(self) -> Self {
|
||||
match self {
|
||||
F32(f) => F32(-f),
|
||||
F64(f) => F64(-f),
|
||||
}
|
||||
let bits = match self.ty {
|
||||
ast::FloatTy::F32 => (-Single::from_bits(self.bits)).to_bits(),
|
||||
ast::FloatTy::F64 => (-Double::from_bits(self.bits)).to_bits(),
|
||||
};
|
||||
ConstFloat { bits, ty: self.ty }
|
||||
}
|
||||
}
|
||||
|
@ -211,48 +211,6 @@ impl ConstInt {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_f32(self) -> f32 {
|
||||
match self {
|
||||
I8(i) => i as f32,
|
||||
I16(i) => i as f32,
|
||||
I32(i) => i as f32,
|
||||
I64(i) => i as f32,
|
||||
I128(i) => i as f32,
|
||||
Isize(Is16(i)) => i as f32,
|
||||
Isize(Is32(i)) => i as f32,
|
||||
Isize(Is64(i)) => i as f32,
|
||||
U8(i) => i as f32,
|
||||
U16(i) => i as f32,
|
||||
U32(i) => i as f32,
|
||||
U64(i) => i as f32,
|
||||
U128(i) => i as f32,
|
||||
Usize(Us16(i)) => i as f32,
|
||||
Usize(Us32(i)) => i as f32,
|
||||
Usize(Us64(i)) => i as f32,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_f64(self) -> f64 {
|
||||
match self {
|
||||
I8(i) => i as f64,
|
||||
I16(i) => i as f64,
|
||||
I32(i) => i as f64,
|
||||
I64(i) => i as f64,
|
||||
I128(i) => i as f64,
|
||||
Isize(Is16(i)) => i as f64,
|
||||
Isize(Is32(i)) => i as f64,
|
||||
Isize(Is64(i)) => i as f64,
|
||||
U8(i) => i as f64,
|
||||
U16(i) => i as f64,
|
||||
U32(i) => i as f64,
|
||||
U64(i) => i as f64,
|
||||
U128(i) => i as f64,
|
||||
Usize(Us16(i)) => i as f64,
|
||||
Usize(Us32(i)) => i as f64,
|
||||
Usize(Us64(i)) => i as f64,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_negative(&self) -> bool {
|
||||
match *self {
|
||||
I8(v) => v < 0,
|
||||
|
@ -26,6 +26,8 @@
|
||||
#![feature(i128)]
|
||||
#![feature(i128_type)]
|
||||
|
||||
extern crate rustc_apfloat;
|
||||
|
||||
extern crate syntax;
|
||||
|
||||
extern crate serialize as rustc_serialize; // used by deriving
|
||||
|
@ -598,7 +598,6 @@ extern "C" {
|
||||
// Operations on scalar constants
|
||||
pub fn LLVMConstInt(IntTy: TypeRef, N: c_ulonglong, SignExtend: Bool) -> ValueRef;
|
||||
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: TypeRef, Wn: c_uint, Ws: *const u64) -> ValueRef;
|
||||
pub fn LLVMConstReal(RealTy: TypeRef, N: f64) -> ValueRef;
|
||||
pub fn LLVMConstIntGetZExtValue(ConstantVal: ValueRef) -> c_ulonglong;
|
||||
pub fn LLVMConstIntGetSExtValue(ConstantVal: ValueRef) -> c_longlong;
|
||||
pub fn LLVMRustConstInt128Get(ConstantVal: ValueRef, SExt: bool,
|
||||
|
@ -844,8 +844,7 @@ fn create_imps(sess: &Session,
|
||||
let imp = llvm::LLVMAddGlobal(llvm_module.llmod,
|
||||
i8p_ty.to_ref(),
|
||||
imp_name.as_ptr() as *const _);
|
||||
let init = llvm::LLVMConstBitCast(val, i8p_ty.to_ref());
|
||||
llvm::LLVMSetInitializer(imp, init);
|
||||
llvm::LLVMSetInitializer(imp, consts::ptrcast(val, i8p_ty));
|
||||
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
|
||||
}
|
||||
}
|
||||
|
@ -223,12 +223,6 @@ pub fn C_big_integral(t: Type, u: u128) -> ValueRef {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn C_floating_f64(f: f64, t: Type) -> ValueRef {
|
||||
unsafe {
|
||||
llvm::LLVMConstReal(t.to_ref(), f)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn C_nil(ccx: &CrateContext) -> ValueRef {
|
||||
C_struct(ccx, &[], false)
|
||||
}
|
||||
|
@ -36,6 +36,12 @@ pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bitcast(val: ValueRef, ty: Type) -> ValueRef {
|
||||
unsafe {
|
||||
llvm::LLVMConstBitCast(val, ty.to_ref())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn addr_of_mut(ccx: &CrateContext,
|
||||
cv: ValueRef,
|
||||
align: machine::llalign,
|
||||
|
@ -11,7 +11,6 @@
|
||||
use llvm::{self, ValueRef};
|
||||
use rustc::middle::const_val::{ConstEvalErr, ConstVal, ErrKind};
|
||||
use rustc_const_math::ConstInt::*;
|
||||
use rustc_const_math::ConstFloat::*;
|
||||
use rustc_const_math::{ConstInt, ConstMathErr};
|
||||
use rustc::hir::def_id::DefId;
|
||||
use rustc::infer::TransNormalize;
|
||||
@ -27,7 +26,7 @@ use abi::{self, Abi};
|
||||
use callee;
|
||||
use builder::Builder;
|
||||
use common::{self, CrateContext, const_get_elt, val_ty};
|
||||
use common::{C_array, C_bool, C_bytes, C_floating_f64, C_integral, C_big_integral};
|
||||
use common::{C_array, C_bool, C_bytes, C_integral, C_big_integral, C_u32, C_u64};
|
||||
use common::{C_null, C_struct, C_str_slice, C_undef, C_uint, C_vector, is_undef};
|
||||
use common::const_to_opt_u128;
|
||||
use consts;
|
||||
@ -37,6 +36,7 @@ use type_::Type;
|
||||
use value::Value;
|
||||
|
||||
use syntax_pos::Span;
|
||||
use syntax::ast;
|
||||
|
||||
use std::fmt;
|
||||
use std::ptr;
|
||||
@ -95,8 +95,13 @@ impl<'tcx> Const<'tcx> {
|
||||
-> Const<'tcx> {
|
||||
let llty = type_of::type_of(ccx, ty);
|
||||
let val = match cv {
|
||||
ConstVal::Float(F32(v)) => C_floating_f64(v as f64, llty),
|
||||
ConstVal::Float(F64(v)) => C_floating_f64(v, llty),
|
||||
ConstVal::Float(v) => {
|
||||
let bits = match v.ty {
|
||||
ast::FloatTy::F32 => C_u32(ccx, v.bits as u32),
|
||||
ast::FloatTy::F64 => C_u64(ccx, v.bits as u64)
|
||||
};
|
||||
consts::bitcast(bits, llty)
|
||||
}
|
||||
ConstVal::Bool(v) => C_bool(ccx, v),
|
||||
ConstVal::Integral(ref i) => return Const::from_constint(ccx, i),
|
||||
ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
|
||||
|
@ -25,7 +25,7 @@ fn main() {}
|
||||
// bb0: {
|
||||
// _2 = _1;
|
||||
// _3 = _2;
|
||||
// _0 = Baz { x: _3, y: const F32(0), z: const false };
|
||||
// _0 = Baz { x: _3, y: const 0f32, z: const false };
|
||||
// return;
|
||||
// }
|
||||
// END rustc.node13.Deaggregator.before.mir
|
||||
@ -34,7 +34,7 @@ fn main() {}
|
||||
// _2 = _1;
|
||||
// _3 = _2;
|
||||
// (_0.0: usize) = _3;
|
||||
// (_0.1: f32) = const F32(0);
|
||||
// (_0.1: f32) = const 0f32;
|
||||
// (_0.2: bool) = const false;
|
||||
// return;
|
||||
// }
|
||||
|
Loading…
x
Reference in New Issue
Block a user