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