]> git.lizzy.rs Git - rust.git/blob - src/librustc_apfloat/lib.rs
Rollup merge of #44542 - alexcrichton:fix-windows-emscripten, r=nikomatsakis
[rust.git] / src / librustc_apfloat / lib.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Port of LLVM's APFloat software floating-point implementation from the
12 //! following C++ sources (please update commit hash when backporting):
13 //! https://github.com/llvm-mirror/llvm/tree/23efab2bbd424ed13495a420ad8641cb2c6c28f9
14 //! * `include/llvm/ADT/APFloat.h` -> `Float` and `FloatConvert` traits
15 //! * `lib/Support/APFloat.cpp` -> `ieee` and `ppc` modules
16 //! * `unittests/ADT/APFloatTest.cpp` -> `tests` directory
17 //!
18 //! The port contains no unsafe code, global state, or side-effects in general,
19 //! and the only allocations are in the conversion to/from decimal strings.
20 //!
21 //! Most of the API and the testcases are intact in some form or another,
22 //! with some ergonomic changes, such as idiomatic short names, returning
23 //! new values instead of mutating the receiver, and having separate method
24 //! variants that take a non-default rounding mode (with the suffix `_r`).
25 //! Comments have been preserved where possible, only slightly adapted.
26 //!
27 //! Instead of keeping a pointer to a configuration struct and inspecting it
28 //! dynamically on every operation, types (e.g. `ieee::Double`), traits
29 //! (e.g. `ieee::Semantics`) and associated constants are employed for
30 //! increased type safety and performance.
31 //!
32 //! On-heap bigints are replaced everywhere (except in decimal conversion),
33 //! with short arrays of `type Limb = u128` elements (instead of `u64`),
34 //! This allows fitting the largest supported significands in one integer
35 //! (`ieee::Quad` and `ppc::Fallback` use slightly less than 128 bits).
36 //! All of the functions in the `ieee::sig` module operate on slices.
37 //!
38 //! # Note
39 //!
40 //! This API is completely unstable and subject to change.
41
42 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
43       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
44       html_root_url = "https://doc.rust-lang.org/nightly/")]
45 #![deny(warnings)]
46 #![forbid(unsafe_code)]
47
48 #![feature(i128_type)]
49 #![feature(slice_patterns)]
50 #![feature(try_from)]
51
52 #![cfg_attr(stage0, feature(const_fn))]
53 #![cfg_attr(not(stage0), feature(const_min_value))]
54 #![cfg_attr(not(stage0), feature(const_max_value))]
55
56 #[macro_use]
57 extern crate rustc_bitflags;
58
59 use std::cmp::Ordering;
60 use std::fmt;
61 use std::ops::{Neg, Add, Sub, Mul, Div, Rem};
62 use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign, BitOrAssign};
63 use std::str::FromStr;
64
65 bitflags! {
66     /// IEEE-754R 7: Default exception handling.
67     ///
68     /// UNDERFLOW or OVERFLOW are always returned or-ed with INEXACT.
69     #[must_use]
70     #[derive(Debug)]
71     flags Status: u8 {
72         const OK = 0x00,
73         const INVALID_OP = 0x01,
74         const DIV_BY_ZERO = 0x02,
75         const OVERFLOW = 0x04,
76         const UNDERFLOW = 0x08,
77         const INEXACT = 0x10
78     }
79 }
80
81 impl BitOrAssign for Status {
82     fn bitor_assign(&mut self, rhs: Self) {
83         *self = *self | rhs;
84     }
85 }
86
87 #[must_use]
88 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
89 pub struct StatusAnd<T> {
90     pub status: Status,
91     pub value: T,
92 }
93
94 impl Status {
95     pub fn and<T>(self, value: T) -> StatusAnd<T> {
96         StatusAnd {
97             status: self,
98             value,
99         }
100     }
101 }
102
103 impl<T> StatusAnd<T> {
104     fn map<F: FnOnce(T) -> U, U>(self, f: F) -> StatusAnd<U> {
105         StatusAnd {
106             status: self.status,
107             value: f(self.value),
108         }
109     }
110 }
111
112 #[macro_export]
113 macro_rules! unpack {
114     ($status:ident|=, $e:expr) => {
115         match $e {
116             $crate::StatusAnd { status, value } => {
117                 $status |= status;
118                 value
119             }
120         }
121     };
122     ($status:ident=, $e:expr) => {
123         match $e {
124             $crate::StatusAnd { status, value } => {
125                 $status = status;
126                 value
127             }
128         }
129     }
130 }
131
132 /// Category of internally-represented number.
133 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
134 pub enum Category {
135     Infinity,
136     NaN,
137     Normal,
138     Zero,
139 }
140
141 /// IEEE-754R 4.3: Rounding-direction attributes.
142 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
143 pub enum Round {
144     NearestTiesToEven,
145     TowardPositive,
146     TowardNegative,
147     TowardZero,
148     NearestTiesToAway,
149 }
150
151 impl Neg for Round {
152     type Output = Round;
153     fn neg(self) -> Round {
154         match self {
155             Round::TowardPositive => Round::TowardNegative,
156             Round::TowardNegative => Round::TowardPositive,
157             Round::NearestTiesToEven | Round::TowardZero | Round::NearestTiesToAway => self,
158         }
159     }
160 }
161
162 /// A signed type to represent a floating point number's unbiased exponent.
163 pub type ExpInt = i16;
164
165 // \c ilogb error results.
166 pub const IEK_INF: ExpInt = ExpInt::max_value();
167 pub const IEK_NAN: ExpInt = ExpInt::min_value();
168 pub const IEK_ZERO: ExpInt = ExpInt::min_value() + 1;
169
170 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
171 pub struct ParseError(pub &'static str);
172
173 /// A self-contained host- and target-independent arbitrary-precision
174 /// floating-point software implementation.
175 ///
176 /// `apfloat` uses significand bignum integer arithmetic as provided by functions
177 /// in the `ieee::sig`.
178 ///
179 /// Written for clarity rather than speed, in particular with a view to use in
180 /// the front-end of a cross compiler so that target arithmetic can be correctly
181 /// performed on the host. Performance should nonetheless be reasonable,
182 /// particularly for its intended use. It may be useful as a base
183 /// implementation for a run-time library during development of a faster
184 /// target-specific one.
185 ///
186 /// All 5 rounding modes in the IEEE-754R draft are handled correctly for all
187 /// implemented operations. Currently implemented operations are add, subtract,
188 /// multiply, divide, fused-multiply-add, conversion-to-float,
189 /// conversion-to-integer and conversion-from-integer. New rounding modes
190 /// (e.g. away from zero) can be added with three or four lines of code.
191 ///
192 /// Four formats are built-in: IEEE single precision, double precision,
193 /// quadruple precision, and x87 80-bit extended double (when operating with
194 /// full extended precision). Adding a new format that obeys IEEE semantics
195 /// only requires adding two lines of code: a declaration and definition of the
196 /// format.
197 ///
198 /// All operations return the status of that operation as an exception bit-mask,
199 /// so multiple operations can be done consecutively with their results or-ed
200 /// together. The returned status can be useful for compiler diagnostics; e.g.,
201 /// inexact, underflow and overflow can be easily diagnosed on constant folding,
202 /// and compiler optimizers can determine what exceptions would be raised by
203 /// folding operations and optimize, or perhaps not optimize, accordingly.
204 ///
205 /// At present, underflow tininess is detected after rounding; it should be
206 /// straight forward to add support for the before-rounding case too.
207 ///
208 /// The library reads hexadecimal floating point numbers as per C99, and
209 /// correctly rounds if necessary according to the specified rounding mode.
210 /// Syntax is required to have been validated by the caller.
211 ///
212 /// It also reads decimal floating point numbers and correctly rounds according
213 /// to the specified rounding mode.
214 ///
215 /// Non-zero finite numbers are represented internally as a sign bit, a 16-bit
216 /// signed exponent, and the significand as an array of integer limbs. After
217 /// normalization of a number of precision P the exponent is within the range of
218 /// the format, and if the number is not denormal the P-th bit of the
219 /// significand is set as an explicit integer bit. For denormals the most
220 /// significant bit is shifted right so that the exponent is maintained at the
221 /// format's minimum, so that the smallest denormal has just the least
222 /// significant bit of the significand set. The sign of zeros and infinities
223 /// is significant; the exponent and significand of such numbers is not stored,
224 /// but has a known implicit (deterministic) value: 0 for the significands, 0
225 /// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and
226 /// significand are deterministic, although not really meaningful, and preserved
227 /// in non-conversion operations. The exponent is implicitly all 1 bits.
228 ///
229 /// `apfloat` does not provide any exception handling beyond default exception
230 /// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause
231 /// by encoding Signaling NaNs with the first bit of its trailing significand as
232 /// 0.
233 ///
234 /// Future work
235 /// ===========
236 ///
237 /// Some features that may or may not be worth adding:
238 ///
239 /// Optional ability to detect underflow tininess before rounding.
240 ///
241 /// New formats: x87 in single and double precision mode (IEEE apart from
242 /// extended exponent range) (hard).
243 ///
244 /// New operations: sqrt, nexttoward.
245 ///
246 pub trait Float
247     : Copy
248     + Default
249     + FromStr<Err = ParseError>
250     + PartialOrd
251     + fmt::Display
252     + Neg<Output = Self>
253     + AddAssign
254     + SubAssign
255     + MulAssign
256     + DivAssign
257     + RemAssign
258     + Add<Output = StatusAnd<Self>>
259     + Sub<Output = StatusAnd<Self>>
260     + Mul<Output = StatusAnd<Self>>
261     + Div<Output = StatusAnd<Self>>
262     + Rem<Output = StatusAnd<Self>> {
263     /// Total number of bits in the in-memory format.
264     const BITS: usize;
265
266     /// Number of bits in the significand. This includes the integer bit.
267     const PRECISION: usize;
268
269     /// The largest E such that 2^E is representable; this matches the
270     /// definition of IEEE 754.
271     const MAX_EXP: ExpInt;
272
273     /// The smallest E such that 2^E is a normalized number; this
274     /// matches the definition of IEEE 754.
275     const MIN_EXP: ExpInt;
276
277     /// Positive Zero.
278     const ZERO: Self;
279
280     /// Positive Infinity.
281     const INFINITY: Self;
282
283     /// NaN (Not a Number).
284     // FIXME(eddyb) provide a default when qnan becomes const fn.
285     const NAN: Self;
286
287     /// Factory for QNaN values.
288     // FIXME(eddyb) should be const fn.
289     fn qnan(payload: Option<u128>) -> Self;
290
291     /// Factory for SNaN values.
292     // FIXME(eddyb) should be const fn.
293     fn snan(payload: Option<u128>) -> Self;
294
295     /// Largest finite number.
296     // FIXME(eddyb) should be const (but FloatPair::largest is nontrivial).
297     fn largest() -> Self;
298
299     /// Smallest (by magnitude) finite number.
300     /// Might be denormalized, which implies a relative loss of precision.
301     const SMALLEST: Self;
302
303     /// Smallest (by magnitude) normalized finite number.
304     // FIXME(eddyb) should be const (but FloatPair::smallest_normalized is nontrivial).
305     fn smallest_normalized() -> Self;
306
307     // Arithmetic
308
309     fn add_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
310     fn sub_r(self, rhs: Self, round: Round) -> StatusAnd<Self> {
311         self.add_r(-rhs, round)
312     }
313     fn mul_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
314     fn mul_add_r(self, multiplicand: Self, addend: Self, round: Round) -> StatusAnd<Self>;
315     fn mul_add(self, multiplicand: Self, addend: Self) -> StatusAnd<Self> {
316         self.mul_add_r(multiplicand, addend, Round::NearestTiesToEven)
317     }
318     fn div_r(self, rhs: Self, round: Round) -> StatusAnd<Self>;
319     /// IEEE remainder.
320     // This is not currently correct in all cases.
321     fn ieee_rem(self, rhs: Self) -> StatusAnd<Self> {
322         let mut v = self;
323
324         let status;
325         v = unpack!(status=, v / rhs);
326         if status == Status::DIV_BY_ZERO {
327             return status.and(self);
328         }
329
330         assert!(Self::PRECISION < 128);
331
332         let status;
333         let x = unpack!(status=, v.to_i128_r(128, Round::NearestTiesToEven, &mut false));
334         if status == Status::INVALID_OP {
335             return status.and(self);
336         }
337
338         let status;
339         let mut v = unpack!(status=, Self::from_i128(x));
340         assert_eq!(status, Status::OK); // should always work
341
342         let status;
343         v = unpack!(status=, v * rhs);
344         assert_eq!(status - Status::INEXACT, Status::OK); // should not overflow or underflow
345
346         let status;
347         v = unpack!(status=, self - v);
348         assert_eq!(status - Status::INEXACT, Status::OK); // likewise
349
350         if v.is_zero() {
351             status.and(v.copy_sign(self)) // IEEE754 requires this
352         } else {
353             status.and(v)
354         }
355     }
356     /// C fmod, or llvm frem.
357     fn c_fmod(self, rhs: Self) -> StatusAnd<Self>;
358     fn round_to_integral(self, round: Round) -> StatusAnd<Self>;
359
360     /// IEEE-754R 2008 5.3.1: nextUp.
361     fn next_up(self) -> StatusAnd<Self>;
362
363     /// IEEE-754R 2008 5.3.1: nextDown.
364     ///
365     /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
366     /// appropriate sign switching before/after the computation.
367     fn next_down(self) -> StatusAnd<Self> {
368         (-self).next_up().map(|r| -r)
369     }
370
371     fn abs(self) -> Self {
372         if self.is_negative() { -self } else { self }
373     }
374     fn copy_sign(self, rhs: Self) -> Self {
375         if self.is_negative() != rhs.is_negative() {
376             -self
377         } else {
378             self
379         }
380     }
381
382     // Conversions
383     fn from_bits(input: u128) -> Self;
384     fn from_i128_r(input: i128, round: Round) -> StatusAnd<Self> {
385         if input < 0 {
386             Self::from_u128_r(-input as u128, -round).map(|r| -r)
387         } else {
388             Self::from_u128_r(input as u128, round)
389         }
390     }
391     fn from_i128(input: i128) -> StatusAnd<Self> {
392         Self::from_i128_r(input, Round::NearestTiesToEven)
393     }
394     fn from_u128_r(input: u128, round: Round) -> StatusAnd<Self>;
395     fn from_u128(input: u128) -> StatusAnd<Self> {
396         Self::from_u128_r(input, Round::NearestTiesToEven)
397     }
398     fn from_str_r(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseError>;
399     fn to_bits(self) -> u128;
400
401     /// Convert a floating point number to an integer according to the
402     /// rounding mode. In case of an invalid operation exception,
403     /// deterministic values are returned, namely zero for NaNs and the
404     /// minimal or maximal value respectively for underflow or overflow.
405     /// If the rounded value is in range but the floating point number is
406     /// not the exact integer, the C standard doesn't require an inexact
407     /// exception to be raised. IEEE-854 does require it so we do that.
408     ///
409     /// Note that for conversions to integer type the C standard requires
410     /// round-to-zero to always be used.
411     ///
412     /// The *is_exact output tells whether the result is exact, in the sense
413     /// that converting it back to the original floating point type produces
414     /// the original value. This is almost equivalent to result==Status::OK,
415     /// except for negative zeroes.
416     fn to_i128_r(self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<i128> {
417         let status;
418         if self.is_negative() {
419             if self.is_zero() {
420                 // Negative zero can't be represented as an int.
421                 *is_exact = false;
422             }
423             let r = unpack!(status=, (-self).to_u128_r(width, -round, is_exact));
424
425             // Check for values that don't fit in the signed integer.
426             if r > (1 << (width - 1)) {
427                 // Return the most negative integer for the given width.
428                 *is_exact = false;
429                 Status::INVALID_OP.and(-1 << (width - 1))
430             } else {
431                 status.and(r.wrapping_neg() as i128)
432             }
433         } else {
434             // Positive case is simpler, can pretend it's a smaller unsigned
435             // integer, and `to_u128` will take care of all the edge cases.
436             self.to_u128_r(width - 1, round, is_exact).map(
437                 |r| r as i128,
438             )
439         }
440     }
441     fn to_i128(self, width: usize) -> StatusAnd<i128> {
442         self.to_i128_r(width, Round::TowardZero, &mut true)
443     }
444     fn to_u128_r(self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<u128>;
445     fn to_u128(self, width: usize) -> StatusAnd<u128> {
446         self.to_u128_r(width, Round::TowardZero, &mut true)
447     }
448
449     fn cmp_abs_normal(self, rhs: Self) -> Ordering;
450
451     /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
452     fn bitwise_eq(self, rhs: Self) -> bool;
453
454     // IEEE-754R 5.7.2 General operations.
455
456     /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
457     /// both are not NaN. If either argument is a NaN, returns the other argument.
458     fn min(self, other: Self) -> Self {
459         if self.is_nan() {
460             other
461         } else if other.is_nan() {
462             self
463         } else if other.partial_cmp(&self) == Some(Ordering::Less) {
464             other
465         } else {
466             self
467         }
468     }
469
470     /// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
471     /// both are not NaN. If either argument is a NaN, returns the other argument.
472     fn max(self, other: Self) -> Self {
473         if self.is_nan() {
474             other
475         } else if other.is_nan() {
476             self
477         } else if self.partial_cmp(&other) == Some(Ordering::Less) {
478             other
479         } else {
480             self
481         }
482     }
483
484     /// IEEE-754R isSignMinus: Returns true if and only if the current value is
485     /// negative.
486     ///
487     /// This applies to zeros and NaNs as well.
488     fn is_negative(self) -> bool;
489
490     /// IEEE-754R isNormal: Returns true if and only if the current value is normal.
491     ///
492     /// This implies that the current value of the float is not zero, subnormal,
493     /// infinite, or NaN following the definition of normality from IEEE-754R.
494     fn is_normal(self) -> bool {
495         !self.is_denormal() && self.is_finite_non_zero()
496     }
497
498     /// Returns true if and only if the current value is zero, subnormal, or
499     /// normal.
500     ///
501     /// This means that the value is not infinite or NaN.
502     fn is_finite(self) -> bool {
503         !self.is_nan() && !self.is_infinite()
504     }
505
506     /// Returns true if and only if the float is plus or minus zero.
507     fn is_zero(self) -> bool {
508         self.category() == Category::Zero
509     }
510
511     /// IEEE-754R isSubnormal(): Returns true if and only if the float is a
512     /// denormal.
513     fn is_denormal(self) -> bool;
514
515     /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
516     fn is_infinite(self) -> bool {
517         self.category() == Category::Infinity
518     }
519
520     /// Returns true if and only if the float is a quiet or signaling NaN.
521     fn is_nan(self) -> bool {
522         self.category() == Category::NaN
523     }
524
525     /// Returns true if and only if the float is a signaling NaN.
526     fn is_signaling(self) -> bool;
527
528     // Simple Queries
529
530     fn category(self) -> Category;
531     fn is_non_zero(self) -> bool {
532         !self.is_zero()
533     }
534     fn is_finite_non_zero(self) -> bool {
535         self.is_finite() && !self.is_zero()
536     }
537     fn is_pos_zero(self) -> bool {
538         self.is_zero() && !self.is_negative()
539     }
540     fn is_neg_zero(self) -> bool {
541         self.is_zero() && self.is_negative()
542     }
543
544     /// Returns true if and only if the number has the smallest possible non-zero
545     /// magnitude in the current semantics.
546     fn is_smallest(self) -> bool {
547         Self::SMALLEST.copy_sign(self).bitwise_eq(self)
548     }
549
550     /// Returns true if and only if the number has the largest possible finite
551     /// magnitude in the current semantics.
552     fn is_largest(self) -> bool {
553         Self::largest().copy_sign(self).bitwise_eq(self)
554     }
555
556     /// Returns true if and only if the number is an exact integer.
557     fn is_integer(self) -> bool {
558         // This could be made more efficient; I'm going for obviously correct.
559         if !self.is_finite() {
560             return false;
561         }
562         self.round_to_integral(Round::TowardZero).value.bitwise_eq(
563             self,
564         )
565     }
566
567     /// If this value has an exact multiplicative inverse, return it.
568     fn get_exact_inverse(self) -> Option<Self>;
569
570     /// Returns the exponent of the internal representation of the Float.
571     ///
572     /// Because the radix of Float is 2, this is equivalent to floor(log2(x)).
573     /// For special Float values, this returns special error codes:
574     ///
575     ///   NaN -> \c IEK_NAN
576     ///   0   -> \c IEK_ZERO
577     ///   Inf -> \c IEK_INF
578     ///
579     fn ilogb(self) -> ExpInt;
580
581     /// Returns: self * 2^exp for integral exponents.
582     fn scalbn_r(self, exp: ExpInt, round: Round) -> Self;
583     fn scalbn(self, exp: ExpInt) -> Self {
584         self.scalbn_r(exp, Round::NearestTiesToEven)
585     }
586
587     /// Equivalent of C standard library function.
588     ///
589     /// While the C standard says exp is an unspecified value for infinity and nan,
590     /// this returns INT_MAX for infinities, and INT_MIN for NaNs (see `ilogb`).
591     fn frexp_r(self, exp: &mut ExpInt, round: Round) -> Self;
592     fn frexp(self, exp: &mut ExpInt) -> Self {
593         self.frexp_r(exp, Round::NearestTiesToEven)
594     }
595 }
596
597 pub trait FloatConvert<T: Float>: Float {
598     /// Convert a value of one floating point type to another.
599     /// The return value corresponds to the IEEE754 exceptions. *loses_info
600     /// records whether the transformation lost information, i.e. whether
601     /// converting the result back to the original type will produce the
602     /// original value (this is almost the same as return value==Status::OK,
603     /// but there are edge cases where this is not so).
604     fn convert_r(self, round: Round, loses_info: &mut bool) -> StatusAnd<T>;
605     fn convert(self, loses_info: &mut bool) -> StatusAnd<T> {
606         self.convert_r(Round::NearestTiesToEven, loses_info)
607     }
608 }
609
610 macro_rules! float_common_impls {
611     ($ty:ident<$t:tt>) => {
612         impl<$t> Default for $ty<$t> where Self: Float {
613             fn default() -> Self {
614                 Self::ZERO
615             }
616         }
617
618         impl<$t> ::std::str::FromStr for $ty<$t> where Self: Float {
619             type Err = ParseError;
620             fn from_str(s: &str) -> Result<Self, ParseError> {
621                 Self::from_str_r(s, Round::NearestTiesToEven).map(|x| x.value)
622             }
623         }
624
625         // Rounding ties to the nearest even, by default.
626
627         impl<$t> ::std::ops::Add for $ty<$t> where Self: Float {
628             type Output = StatusAnd<Self>;
629             fn add(self, rhs: Self) -> StatusAnd<Self> {
630                 self.add_r(rhs, Round::NearestTiesToEven)
631             }
632         }
633
634         impl<$t> ::std::ops::Sub for $ty<$t> where Self: Float {
635             type Output = StatusAnd<Self>;
636             fn sub(self, rhs: Self) -> StatusAnd<Self> {
637                 self.sub_r(rhs, Round::NearestTiesToEven)
638             }
639         }
640
641         impl<$t> ::std::ops::Mul for $ty<$t> where Self: Float {
642             type Output = StatusAnd<Self>;
643             fn mul(self, rhs: Self) -> StatusAnd<Self> {
644                 self.mul_r(rhs, Round::NearestTiesToEven)
645             }
646         }
647
648         impl<$t> ::std::ops::Div for $ty<$t> where Self: Float {
649             type Output = StatusAnd<Self>;
650             fn div(self, rhs: Self) -> StatusAnd<Self> {
651                 self.div_r(rhs, Round::NearestTiesToEven)
652             }
653         }
654
655         impl<$t> ::std::ops::Rem for $ty<$t> where Self: Float {
656             type Output = StatusAnd<Self>;
657             fn rem(self, rhs: Self) -> StatusAnd<Self> {
658                 self.c_fmod(rhs)
659             }
660         }
661
662         impl<$t> ::std::ops::AddAssign for $ty<$t> where Self: Float {
663             fn add_assign(&mut self, rhs: Self) {
664                 *self = (*self + rhs).value;
665             }
666         }
667
668         impl<$t> ::std::ops::SubAssign for $ty<$t> where Self: Float {
669             fn sub_assign(&mut self, rhs: Self) {
670                 *self = (*self - rhs).value;
671             }
672         }
673
674         impl<$t> ::std::ops::MulAssign for $ty<$t> where Self: Float {
675             fn mul_assign(&mut self, rhs: Self) {
676                 *self = (*self * rhs).value;
677             }
678         }
679
680         impl<$t> ::std::ops::DivAssign for $ty<$t> where Self: Float {
681             fn div_assign(&mut self, rhs: Self) {
682                 *self = (*self / rhs).value;
683             }
684         }
685
686         impl<$t> ::std::ops::RemAssign for $ty<$t> where Self: Float {
687             fn rem_assign(&mut self, rhs: Self) {
688                 *self = (*self % rhs).value;
689             }
690         }
691     }
692 }
693
694 pub mod ieee;
695 pub mod ppc;