1 //! Port of LLVM's APFloat software floating-point implementation from the
2 //! following C++ sources (please update commit hash when backporting):
3 //! <https://github.com/llvm-mirror/llvm/tree/23efab2bbd424ed13495a420ad8641cb2c6c28f9>
5 //! * `include/llvm/ADT/APFloat.h` -> `Float` and `FloatConvert` traits
6 //! * `lib/Support/APFloat.cpp` -> `ieee` and `ppc` modules
7 //! * `unittests/ADT/APFloatTest.cpp` -> `tests` directory
9 //! The port contains no unsafe code, global state, or side-effects in general,
10 //! and the only allocations are in the conversion to/from decimal strings.
12 //! Most of the API and the testcases are intact in some form or another,
13 //! with some ergonomic changes, such as idiomatic short names, returning
14 //! new values instead of mutating the receiver, and having separate method
15 //! variants that take a non-default rounding mode (with the suffix `_r`).
16 //! Comments have been preserved where possible, only slightly adapted.
18 //! Instead of keeping a pointer to a configuration struct and inspecting it
19 //! dynamically on every operation, types (e.g., `ieee::Double`), traits
20 //! (e.g., `ieee::Semantics`) and associated constants are employed for
21 //! increased type safety and performance.
23 //! On-heap bigints are replaced everywhere (except in decimal conversion),
24 //! with short arrays of `type Limb = u128` elements (instead of `u64`),
25 //! This allows fitting the largest supported significands in one integer
26 //! (`ieee::Quad` and `ppc::Fallback` use slightly less than 128 bits).
27 //! All of the functions in the `ieee::sig` module operate on slices.
31 //! This API is completely unstable and subject to change.
33 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
35 #![forbid(unsafe_code)]
41 use core::cmp::Ordering;
43 use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
44 use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
45 use core::str::FromStr;
48 /// IEEE-754R 7: Default exception handling.
50 /// UNDERFLOW or OVERFLOW are always returned or-ed with INEXACT.
52 pub struct Status: u8 {
54 const INVALID_OP = 0x01;
55 const DIV_BY_ZERO = 0x02;
56 const OVERFLOW = 0x04;
57 const UNDERFLOW = 0x08;
63 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
64 pub struct StatusAnd<T> {
70 pub fn and<T>(self, value: T) -> StatusAnd<T> {
71 StatusAnd { status: self, value }
75 impl<T> StatusAnd<T> {
76 pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> StatusAnd<U> {
77 StatusAnd { status: self.status, value: f(self.value) }
83 ($status:ident|=, $e:expr) => {
85 $crate::StatusAnd { status, value } => {
91 ($status:ident=, $e:expr) => {
93 $crate::StatusAnd { status, value } => {
101 /// Category of internally-represented number.
102 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
110 /// IEEE-754R 4.3: Rounding-direction attributes.
111 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
122 fn neg(self) -> Round {
124 Round::TowardPositive => Round::TowardNegative,
125 Round::TowardNegative => Round::TowardPositive,
126 Round::NearestTiesToEven | Round::TowardZero | Round::NearestTiesToAway => self,
131 /// A signed type to represent a floating point number's unbiased exponent.
132 pub type ExpInt = i16;
134 // \c ilogb error results.
135 pub const IEK_INF: ExpInt = ExpInt::max_value();
136 pub const IEK_NAN: ExpInt = ExpInt::min_value();
137 pub const IEK_ZERO: ExpInt = ExpInt::min_value() + 1;
139 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
140 pub struct ParseError(pub &'static str);
142 /// A self-contained host- and target-independent arbitrary-precision
143 /// floating-point software implementation.
145 /// `apfloat` uses significand bignum integer arithmetic as provided by functions
146 /// in the `ieee::sig`.
148 /// Written for clarity rather than speed, in particular with a view to use in
149 /// the front-end of a cross compiler so that target arithmetic can be correctly
150 /// performed on the host. Performance should nonetheless be reasonable,
151 /// particularly for its intended use. It may be useful as a base
152 /// implementation for a run-time library during development of a faster
153 /// target-specific one.
155 /// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
156 /// implemented operations. Currently implemented operations are add, subtract,
157 /// multiply, divide, fused-multiply-add, conversion-to-float,
158 /// conversion-to-integer and conversion-from-integer. New rounding modes
159 /// (e.g., away from zero) can be added with three or four lines of code.
161 /// Four formats are built-in: IEEE single precision, double precision,
162 /// quadruple precision, and x87 80-bit extended double (when operating with
163 /// full extended precision). Adding a new format that obeys IEEE semantics
164 /// only requires adding two lines of code: a declaration and definition of the
167 /// All operations return the status of that operation as an exception bit-mask,
168 /// so multiple operations can be done consecutively with their results or-ed
169 /// together. The returned status can be useful for compiler diagnostics; e.g.,
170 /// inexact, underflow and overflow can be easily diagnosed on constant folding,
171 /// and compiler optimizers can determine what exceptions would be raised by
172 /// folding operations and optimize, or perhaps not optimize, accordingly.
174 /// At present, underflow tininess is detected after rounding; it should be
175 /// straight forward to add support for the before-rounding case too.
177 /// The library reads hexadecimal floating point numbers as per C99, and
178 /// correctly rounds if necessary according to the specified rounding mode.
179 /// Syntax is required to have been validated by the caller.
181 /// It also reads decimal floating point numbers and correctly rounds according
182 /// to the specified rounding mode.
184 /// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
185 /// signed exponent, and the significand as an array of integer limbs. After
186 /// normalization of a number of precision P the exponent is within the range of
187 /// the format, and if the number is not denormal the P-th bit of the
188 /// significand is set as an explicit integer bit. For denormals the most
189 /// significant bit is shifted right so that the exponent is maintained at the
190 /// format's minimum, so that the smallest denormal has just the least
191 /// significant bit of the significand set. The sign of zeros and infinities
192 /// is significant; the exponent and significand of such numbers is not stored,
193 /// but has a known implicit (deterministic) value: 0 for the significands, 0
194 /// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and
195 /// significand are deterministic, although not really meaningful, and preserved
196 /// in non-conversion operations. The exponent is implicitly all 1 bits.
198 /// `apfloat` does not provide any exception handling beyond default exception
199 /// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
200 /// by encoding Signaling NaNs with the first bit of its trailing significand
206 /// Some features that may or may not be worth adding:
208 /// Optional ability to detect underflow tininess before rounding.
210 /// New formats: x87 in single and double precision mode (IEEE apart from
211 /// extended exponent range) (hard).
213 /// New operations: sqrt, nexttoward.
218 + FromStr<Err = ParseError>
227 + Add<Output = StatusAnd<Self>>
228 + Sub<Output = StatusAnd<Self>>
229 + Mul<Output = StatusAnd<Self>>
230 + Div<Output = StatusAnd<Self>>
231 + Rem<Output = StatusAnd<Self>>
233 /// Total number of bits in the in-memory format.
236 /// Number of bits in the significand. This includes the integer bit.
237 const PRECISION: usize;
239 /// The largest E such that 2<sup>E</sup> is representable; this matches the
240 /// definition of IEEE 754.
241 const MAX_EXP: ExpInt;
243 /// The smallest E such that 2<sup>E</sup> is a normalized number; this
244 /// matches the definition of IEEE 754.
245 const MIN_EXP: ExpInt;
250 /// Positive Infinity.
251 const INFINITY: Self;
253 /// NaN (Not a Number).
254 // FIXME(eddyb) provide a default when qnan becomes const fn.
257 /// Factory for QNaN values.
258 // FIXME(eddyb) should be const fn.
259 fn qnan(payload: Option<u128>) -> Self;
261 /// Factory for SNaN values.
262 // FIXME(eddyb) should be const fn.
263 fn snan(payload: Option<u128>) -> Self;
265 /// Largest finite number.
266 // FIXME(eddyb) should be const (but FloatPair::largest is nontrivial).
267 fn largest() -> Self;
269 /// Smallest (by magnitude) finite number.
270 /// Might be denormalized, which implies a relative loss of precision.
271 const SMALLEST: Self;
273 /// Smallest (by magnitude) normalized finite number.
274 // FIXME(eddyb) should be const (but FloatPair::smallest_normalized is nontrivial).
275 fn smallest_normalized() -> Self;
279 fn add_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
280 fn sub_r(self, rhs: Self, round: Round) -> StatusAnd<Self> {
281 self.add_r(-rhs, round)
283 fn mul_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
284 fn mul_add_r(self, multiplicand: Self, addend: Self, round: Round) -> StatusAnd<Self>;
285 fn mul_add(self, multiplicand: Self, addend: Self) -> StatusAnd<Self> {
286 self.mul_add_r(multiplicand, addend, Round::NearestTiesToEven)
288 fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
290 // This is not currently correct in all cases.
291 fn ieee_rem(self, rhs: Self) -> StatusAnd<Self> {
295 v = unpack!(status=, v / rhs);
296 if status == Status::DIV_BY_ZERO {
297 return status.and(self);
300 assert!(Self::PRECISION < 128);
303 let x = unpack!(status=, v.to_i128_r(128, Round::NearestTiesToEven, &mut false));
304 if status == Status::INVALID_OP {
305 return status.and(self);
309 let mut v = unpack!(status=, Self::from_i128(x));
310 assert_eq!(status, Status::OK); // should always work
313 v = unpack!(status=, v * rhs);
314 assert_eq!(status - Status::INEXACT, Status::OK); // should not overflow or underflow
317 v = unpack!(status=, self - v);
318 assert_eq!(status - Status::INEXACT, Status::OK); // likewise
321 status.and(v.copy_sign(self)) // IEEE754 requires this
326 /// C fmod, or llvm frem.
327 fn c_fmod(self, rhs: Self) -> StatusAnd<Self>;
328 fn round_to_integral(self, round: Round) -> StatusAnd<Self>;
330 /// IEEE-754R 2008 5.3.1: nextUp.
331 fn next_up(self) -> StatusAnd<Self>;
333 /// IEEE-754R 2008 5.3.1: nextDown.
335 /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
336 /// appropriate sign switching before/after the computation.
337 fn next_down(self) -> StatusAnd<Self> {
338 (-self).next_up().map(|r| -r)
341 fn abs(self) -> Self {
342 if self.is_negative() { -self } else { self }
344 fn copy_sign(self, rhs: Self) -> Self {
345 if self.is_negative() != rhs.is_negative() { -self } else { self }
349 fn from_bits(input: u128) -> Self;
350 fn from_i128_r(input: i128, round: Round) -> StatusAnd<Self> {
352 Self::from_u128_r(input.wrapping_neg() as u128, -round).map(|r| -r)
354 Self::from_u128_r(input as u128, round)
357 fn from_i128(input: i128) -> StatusAnd<Self> {
358 Self::from_i128_r(input, Round::NearestTiesToEven)
360 fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self>;
361 fn from_u128(input: u128) -> StatusAnd<Self> {
362 Self::from_u128_r(input, Round::NearestTiesToEven)
364 fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError>;
365 fn to_bits(self) -> u128;
367 /// Converts a floating point number to an integer according to the
368 /// rounding mode. In case of an invalid operation exception,
369 /// deterministic values are returned, namely zero for NaNs and the
370 /// minimal or maximal value respectively for underflow or overflow.
371 /// If the rounded value is in range but the floating point number is
372 /// not the exact integer, the C standard doesn't require an inexact
373 /// exception to be raised. IEEE-854 does require it so we do that.
375 /// Note that for conversions to integer type the C standard requires
376 /// round-to-zero to always be used.
378 /// The *is_exact output tells whether the result is exact, in the sense
379 /// that converting it back to the original floating point type produces
380 /// the original value. This is almost equivalent to `result == Status::OK`,
381 /// except for negative zeroes.
382 fn to_i128_r(self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<i128> {
384 if self.is_negative() {
386 // Negative zero can't be represented as an int.
389 let r = unpack!(status=, (-self).to_u128_r(width, -round, is_exact));
391 // Check for values that don't fit in the signed integer.
392 if r > (1 << (width - 1)) {
393 // Return the most negative integer for the given width.
395 Status::INVALID_OP.and(-1 << (width - 1))
397 status.and(r.wrapping_neg() as i128)
400 // Positive case is simpler, can pretend it's a smaller unsigned
401 // integer, and `to_u128` will take care of all the edge cases.
402 self.to_u128_r(width - 1, round, is_exact).map(|r| r as i128)
405 fn to_i128(self, width: usize) -> StatusAnd<i128> {
406 self.to_i128_r(width, Round::TowardZero, &mut true)
408 fn to_u128_r(self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<u128>;
409 fn to_u128(self, width: usize) -> StatusAnd<u128> {
410 self.to_u128_r(width, Round::TowardZero, &mut true)
413 fn cmp_abs_normal(self, rhs: Self) -> Ordering;
415 /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
416 fn bitwise_eq(self, rhs: Self) -> bool;
418 // IEEE-754R 5.7.2 General operations.
420 /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
421 /// both are not NaN. If either argument is a NaN, returns the other argument.
422 fn min(self, other: Self) -> Self {
425 } else if other.is_nan() {
427 } else if other.partial_cmp(&self) == Some(Ordering::Less) {
434 /// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
435 /// both are not NaN. If either argument is a NaN, returns the other argument.
436 fn max(self, other: Self) -> Self {
439 } else if other.is_nan() {
441 } else if self.partial_cmp(&other) == Some(Ordering::Less) {
448 /// IEEE-754R isSignMinus: Returns whether the current value is
451 /// This applies to zeros and NaNs as well.
452 fn is_negative(self) -> bool;
454 /// IEEE-754R isNormal: Returns whether the current value is normal.
456 /// This implies that the current value of the float is not zero, subnormal,
457 /// infinite, or NaN following the definition of normality from IEEE-754R.
458 fn is_normal(self) -> bool {
459 !self.is_denormal() && self.is_finite_non_zero()
462 /// Returns `true` if the current value is zero, subnormal, or
465 /// This means that the value is not infinite or NaN.
466 fn is_finite(self) -> bool {
467 !self.is_nan() && !self.is_infinite()
470 /// Returns `true` if the float is plus or minus zero.
471 fn is_zero(self) -> bool {
472 self.category() == Category::Zero
475 /// IEEE-754R isSubnormal(): Returns whether the float is a
477 fn is_denormal(self) -> bool;
479 /// IEEE-754R isInfinite(): Returns whether the float is infinity.
480 fn is_infinite(self) -> bool {
481 self.category() == Category::Infinity
484 /// Returns `true` if the float is a quiet or signaling NaN.
485 fn is_nan(self) -> bool {
486 self.category() == Category::NaN
489 /// Returns `true` if the float is a signaling NaN.
490 fn is_signaling(self) -> bool;
494 fn category(self) -> Category;
495 fn is_non_zero(self) -> bool {
498 fn is_finite_non_zero(self) -> bool {
499 self.is_finite() && !self.is_zero()
501 fn is_pos_zero(self) -> bool {
502 self.is_zero() && !self.is_negative()
504 fn is_neg_zero(self) -> bool {
505 self.is_zero() && self.is_negative()
508 /// Returns `true` if the number has the smallest possible non-zero
509 /// magnitude in the current semantics.
510 fn is_smallest(self) -> bool {
511 Self::SMALLEST.copy_sign(self).bitwise_eq(self)
514 /// Returns `true` if the number has the largest possible finite
515 /// magnitude in the current semantics.
516 fn is_largest(self) -> bool {
517 Self::largest().copy_sign(self).bitwise_eq(self)
520 /// Returns `true` if the number is an exact integer.
521 fn is_integer(self) -> bool {
522 // This could be made more efficient; I'm going for obviously correct.
523 if !self.is_finite() {
526 self.round_to_integral(Round::TowardZero).value.bitwise_eq(self)
529 /// If this value has an exact multiplicative inverse, return it.
530 fn get_exact_inverse(self) -> Option<Self>;
532 /// Returns the exponent of the internal representation of the Float.
534 /// Because the radix of Float is 2, this is equivalent to floor(log2(x)).
535 /// For special Float values, this returns special error codes:
537 /// NaN -> \c IEK_NAN
539 /// Inf -> \c IEK_INF
541 fn ilogb(self) -> ExpInt;
543 /// Returns: self * 2<sup>exp</sup> for integral exponents.
544 /// Equivalent to C standard library function `ldexp`.
545 fn scalbn_r(self, exp: ExpInt, round: Round) -> Self;
546 fn scalbn(self, exp: ExpInt) -> Self {
547 self.scalbn_r(exp, Round::NearestTiesToEven)
550 /// Equivalent to C standard library function with the same name.
552 /// While the C standard says exp is an unspecified value for infinity and nan,
553 /// this returns INT_MAX for infinities, and INT_MIN for NaNs (see `ilogb`).
554 fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self;
555 fn frexp(self, exp: &mut ExpInt) -> Self {
556 self.frexp_r(exp, Round::NearestTiesToEven)
560 pub trait FloatConvert<T: Float>: Float {
561 /// Converts a value of one floating point type to another.
562 /// The return value corresponds to the IEEE754 exceptions. *loses_info
563 /// records whether the transformation lost information, i.e., whether
564 /// converting the result back to the original type will produce the
565 /// original value (this is almost the same as return `value == Status::OK`,
566 /// but there are edge cases where this is not so).
567 fn convert_r(self, round: Round, loses_info: &mut bool) -> StatusAnd<T>;
568 fn convert(self, loses_info: &mut bool) -> StatusAnd<T> {
569 self.convert_r(Round::NearestTiesToEven, loses_info)
573 macro_rules! float_common_impls {
574 ($ty:ident<$t:tt>) => {
575 impl<$t> Default for $ty<$t>
579 fn default() -> Self {
584 impl<$t> ::core::str::FromStr for $ty<$t>
588 type Err = ParseError;
589 fn from_str(s: &str) -> Result<Self, ParseError> {
590 Self::from_str_r(s, Round::NearestTiesToEven).map(|x| x.value)
594 // Rounding ties to the nearest even, by default.
596 impl<$t> ::core::ops::Add for $ty<$t>
600 type Output = StatusAnd<Self>;
601 fn add(self, rhs: Self) -> StatusAnd<Self> {
602 self.add_r(rhs, Round::NearestTiesToEven)
606 impl<$t> ::core::ops::Sub for $ty<$t>
610 type Output = StatusAnd<Self>;
611 fn sub(self, rhs: Self) -> StatusAnd<Self> {
612 self.sub_r(rhs, Round::NearestTiesToEven)
616 impl<$t> ::core::ops::Mul for $ty<$t>
620 type Output = StatusAnd<Self>;
621 fn mul(self, rhs: Self) -> StatusAnd<Self> {
622 self.mul_r(rhs, Round::NearestTiesToEven)
626 impl<$t> ::core::ops::Div for $ty<$t>
630 type Output = StatusAnd<Self>;
631 fn div(self, rhs: Self) -> StatusAnd<Self> {
632 self.div_r(rhs, Round::NearestTiesToEven)
636 impl<$t> ::core::ops::Rem for $ty<$t>
640 type Output = StatusAnd<Self>;
641 fn rem(self, rhs: Self) -> StatusAnd<Self> {
646 impl<$t> ::core::ops::AddAssign for $ty<$t>
650 fn add_assign(&mut self, rhs: Self) {
651 *self = (*self + rhs).value;
655 impl<$t> ::core::ops::SubAssign for $ty<$t>
659 fn sub_assign(&mut self, rhs: Self) {
660 *self = (*self - rhs).value;
664 impl<$t> ::core::ops::MulAssign for $ty<$t>
668 fn mul_assign(&mut self, rhs: Self) {
669 *self = (*self * rhs).value;
673 impl<$t> ::core::ops::DivAssign for $ty<$t>
677 fn div_assign(&mut self, rhs: Self) {
678 *self = (*self / rhs).value;
682 impl<$t> ::core::ops::RemAssign for $ty<$t>
686 fn rem_assign(&mut self, rhs: Self) {
687 *self = (*self % rhs).value;