Trait rustc_apfloat::Float [−][src]
pub trait Float: Copy + Default + FromStr<Err = ParseError> + PartialOrd + 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>> { const BITS: usize; const PRECISION: usize; const MAX_EXP: ExpInt; const MIN_EXP: ExpInt; const ZERO: Self; const INFINITY: Self; const NAN: Self; const SMALLEST: Self; fn qnan(payload: Option<u128>) -> Self; fn snan(payload: Option<u128>) -> Self; fn largest() -> Self; fn smallest_normalized() -> Self; fn add_r(self, rhs: Self, round: Round) -> StatusAnd<Self>; fn mul_r(self, rhs: Self, round: Round) -> StatusAnd<Self>; fn mul_add_r(
self,
multiplicand: Self,
addend: Self,
round: Round
) -> StatusAnd<Self>; fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self>; fn c_fmod(self, rhs: Self) -> StatusAnd<Self>; fn round_to_integral(self, round: Round) -> StatusAnd<Self>; fn next_up(self) -> StatusAnd<Self>; fn from_bits(input: u128) -> Self; fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self>; fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError>; fn to_bits(self) -> u128; fn to_u128_r(
self,
width: usize,
round: Round,
is_exact: &mut bool
) -> StatusAnd<u128>; fn cmp_abs_normal(self, rhs: Self) -> Ordering; fn bitwise_eq(self, rhs: Self) -> bool; fn is_negative(self) -> bool; fn is_denormal(self) -> bool; fn is_signaling(self) -> bool; fn category(self) -> Category; fn get_exact_inverse(self) -> Option<Self>; fn ilogb(self) -> ExpInt; fn scalbn_r(self, exp: ExpInt, round: Round) -> Self; fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self; fn sub_r(self, rhs: Self, round: Round) -> StatusAnd<Self> { ... } fn mul_add(self, multiplicand: Self, addend: Self) -> StatusAnd<Self> { ... } fn ieee_rem(self, rhs: Self) -> StatusAnd<Self> { ... } fn next_down(self) -> StatusAnd<Self> { ... } fn abs(self) -> Self { ... } fn copy_sign(self, rhs: Self) -> Self { ... } fn from_i128_r(input: i128, round: Round) -> StatusAnd<Self> { ... } fn from_i128(input: i128) -> StatusAnd<Self> { ... } fn from_u128(input: u128) -> StatusAnd<Self> { ... } fn to_i128_r(
self,
width: usize,
round: Round,
is_exact: &mut bool
) -> StatusAnd<i128> { ... } fn to_i128(self, width: usize) -> StatusAnd<i128> { ... } fn to_u128(self, width: usize) -> StatusAnd<u128> { ... } fn min(self, other: Self) -> Self { ... } fn max(self, other: Self) -> Self { ... } fn is_normal(self) -> bool { ... } fn is_finite(self) -> bool { ... } fn is_zero(self) -> bool { ... } fn is_infinite(self) -> bool { ... } fn is_nan(self) -> bool { ... } fn is_non_zero(self) -> bool { ... } fn is_finite_non_zero(self) -> bool { ... } fn is_pos_zero(self) -> bool { ... } fn is_neg_zero(self) -> bool { ... } fn is_smallest(self) -> bool { ... } fn is_largest(self) -> bool { ... } fn is_integer(self) -> bool { ... } fn scalbn(self, exp: ExpInt) -> Self { ... } fn frexp(self, exp: &mut ExpInt) -> Self { ... } }
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.
Associated Constants
const BITS: usize
Total number of bits in the in-memory format.
const PRECISION: usize
Number of bits in the significand. This includes the integer bit.
const MAX_EXP: ExpInt
The largest E such that 2E is representable; this matches the definition of IEEE 754.
const MIN_EXP: ExpInt
The smallest E such that 2E is a normalized number; this matches the definition of IEEE 754.
const ZERO: Self
Positive Zero.
const INFINITY: Self
Positive Infinity.
const NAN: Self
NaN (Not a Number).
const SMALLEST: Self
Smallest (by magnitude) finite number. Might be denormalized, which implies a relative loss of precision.
Required Methods
fn qnan(payload: Option<u128>) -> Self
Factory for QNaN values.
fn snan(payload: Option<u128>) -> Self
Factory for SNaN values.
fn largest() -> Self
Largest finite number.
fn smallest_normalized() -> Self
Smallest (by magnitude) normalized finite number.
fn add_r(self, rhs: Self, round: Round) -> StatusAnd<Self>
fn mul_r(self, rhs: Self, round: Round) -> StatusAnd<Self>
fn mul_add_r(
self,
multiplicand: Self,
addend: Self,
round: Round
) -> StatusAnd<Self>
self,
multiplicand: Self,
addend: Self,
round: Round
) -> StatusAnd<Self>
fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self>
fn c_fmod(self, rhs: Self) -> StatusAnd<Self>
C fmod, or llvm frem.
fn round_to_integral(self, round: Round) -> StatusAnd<Self>
fn next_up(self) -> StatusAnd<Self>
IEEE-754R 2008 5.3.1: nextUp.
fn from_bits(input: u128) -> Self
fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self>
fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError>
fn to_bits(self) -> u128
fn to_u128_r(
self,
width: usize,
round: Round,
is_exact: &mut bool
) -> StatusAnd<u128>
self,
width: usize,
round: Round,
is_exact: &mut bool
) -> StatusAnd<u128>
fn cmp_abs_normal(self, rhs: Self) -> Ordering
fn bitwise_eq(self, rhs: Self) -> bool
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
fn is_negative(self) -> bool
IEEE-754R isSignMinus: Returns true if and only if the current value is negative.
This applies to zeros and NaNs as well.
fn is_denormal(self) -> bool
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
fn is_signaling(self) -> bool
Returns true if and only if the float is a signaling NaN.
fn category(self) -> Category
fn get_exact_inverse(self) -> Option<Self>
If this value has an exact multiplicative inverse, return it.
fn ilogb(self) -> ExpInt
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 scalbn_r(self, exp: ExpInt, round: Round) -> Self
Returns: self * 2exp for integral exponents.
fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self
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
).
Provided Methods
fn sub_r(self, rhs: Self, round: Round) -> StatusAnd<Self>
fn mul_add(self, multiplicand: Self, addend: Self) -> StatusAnd<Self>
fn ieee_rem(self, rhs: Self) -> StatusAnd<Self>
IEEE remainder.
fn next_down(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 abs(self) -> Self
fn copy_sign(self, rhs: Self) -> Self
fn from_i128_r(input: i128, round: Round) -> StatusAnd<Self>
fn from_i128(input: i128) -> StatusAnd<Self>
fn from_u128(input: u128) -> StatusAnd<Self>
fn to_i128_r(
self,
width: usize,
round: Round,
is_exact: &mut bool
) -> StatusAnd<i128>
self,
width: usize,
round: Round,
is_exact: &mut bool
) -> StatusAnd<i128>
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(self, width: usize) -> StatusAnd<i128>
fn to_u128(self, width: usize) -> StatusAnd<u128>
fn min(self, other: Self) -> Self
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 max(self, other: Self) -> 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 is_normal(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_finite(self) -> bool
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_zero(self) -> bool
Returns true if and only if the float is plus or minus zero.
fn is_infinite(self) -> bool
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
fn is_nan(self) -> bool
Returns true if and only if the float is a quiet or signaling NaN.
fn is_non_zero(self) -> bool
fn is_finite_non_zero(self) -> bool
fn is_pos_zero(self) -> bool
fn is_neg_zero(self) -> bool
fn is_smallest(self) -> bool
Returns true if and only if the number has the smallest possible non-zero magnitude in the current semantics.
fn is_largest(self) -> bool
Returns true if and only if the number has the largest possible finite magnitude in the current semantics.
fn is_integer(self) -> bool
Returns true if and only if the number is an exact integer.
fn scalbn(self, exp: ExpInt) -> Self
fn frexp(self, exp: &mut ExpInt) -> Self
Implementors
impl<S: Semantics> Float for IeeeFloat<S>
impl<F: FloatConvert<IeeeFloat<FallbackS<F>>>> Float for DoubleFloat<F> where
Self: From<IeeeFloat<FallbackS<F>>>,