1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Exception flags, rounded results and computed results.
use core::ops::{BitOr, BitOrAssign};
use crate::Unrounded;
/// Architecture-independent floating-point exception flags.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Flags(u8);
impl Flags {
/// No exceptions.
pub const NONE: Flags = Flags(0);
/// Invalid operation.
pub const INVALID: Flags = Flags(1);
/// Division of a finite nonzero value by zero.
pub const DIVIDE_BY_ZERO: Flags = Flags(2);
/// Overflow of the destination exponent range.
pub const OVERFLOW: Flags = Flags(4);
/// Underflow under the selected range response.
pub const UNDERFLOW: Flags = Flags(8);
/// Inexact result.
pub const INEXACT: Flags = Flags(16);
// Returns the five flags as bits 0 to 4, in the order of the constants
// above.
pub(crate) fn bits(self) -> u8 {
self.0
}
pub(crate) fn from_bits(bits: u8) -> Flags {
Flags(bits)
}
/// Returns whether every flag in `other` is set in self.
#[inline]
pub fn contains(self, other: Flags) -> bool {
self.0 & other.0 == other.0
}
/// Returns self if `condition` holds, and no flags otherwise.
#[inline]
pub fn when(self, condition: bool) -> Flags {
Flags(self.0 & u8::from(condition).wrapping_neg())
}
}
impl BitOr for Flags {
type Output = Flags;
#[inline]
fn bitor(self, other: Flags) -> Flags {
Flags(self.0 | other.0)
}
}
impl BitOrAssign for Flags {
#[inline]
fn bitor_assign(&mut self, other: Flags) {
self.0 |= other.0;
}
}
/// A delivered value, its exception flags and rounding indicators.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rounded<T, F = Flags> {
/// Value delivered by the operation or range response.
pub value: T,
/// Flags reported by the operation and its range response.
pub flags: F,
/// Whether rounding increased the magnitude after tie correction and carry.
/// An overflow replacement with infinity sets this bit. Replacement with zero
/// clears it. For a negative operand, increasing the magnitude moves away from zero.
pub rounded_up: bool,
/// Whether a nonzero magnitude fell below the smallest positive normal
/// under the selected [`crate::Tininess`] rule.
/// An exact tiny result can leave the masked underflow flag clear. A finished
/// result settled without rounding has this bit clear.
pub tiny: bool,
}
impl<T, F> Rounded<T, F> {
/// Builds a result that needed no rounding. `rounded_up` and `tiny` are
/// clear.
pub fn exact(value: T, flags: F) -> Rounded<T, F> {
Rounded {
value,
flags,
rounded_up: false,
tiny: false,
}
}
/// Applies `f` to the value and preserves the flags and rounding indicators.
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Rounded<U, F> {
Rounded {
value: f(self.value),
flags: self.flags,
rounded_up: self.rounded_up,
tiny: self.tiny,
}
}
/// Applies `f` to the flags and preserves the value and rounding
/// indicators.
pub fn map_flags<G>(self, f: impl FnOnce(F) -> G) -> Rounded<T, G> {
Rounded {
value: self.value,
flags: f(self.flags),
rounded_up: self.rounded_up,
tiny: self.tiny,
}
}
}
/// An operation result before the caller chooses a range response.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Computed<T, F = Flags> {
/// Final value and flags. No further rounding or range response can change it.
Finished(T, F),
/// Intermediate to round for the selected precision and range response.
/// Even an exact denormal can need this step.
Unfinished(Unrounded<T>),
}
impl<T, F> Computed<T, F> {
/// Returns a finished value with its flags and both rounding indicators clear.
/// For an unfinished result, invokes `round` once and returns its result.
// Without the hint, its callers leave it out of line.
#[inline]
pub fn settle(self, round: impl FnOnce(Unrounded<T>) -> Rounded<T, F>) -> Rounded<T, F> {
match self {
Computed::Finished(value, flags) => Rounded::exact(value, flags),
Computed::Unfinished(intermediate) => round(intermediate),
}
}
/// Applies `f` to a finished result's flags. An unfinished result carries
/// no flags and is unchanged.
pub fn map_flags<G>(self, f: impl FnOnce(F) -> G) -> Computed<T, G> {
match self {
Computed::Finished(value, flags) => Computed::Finished(value, f(flags)),
Computed::Unfinished(intermediate) => Computed::Unfinished(intermediate),
}
}
}
// Builds an unfinished result. Its intermediate is
// (significand + extra * 2^-64) * 2^(exponent - 63) with its sign.
pub(crate) fn unfinished<T>(
negative: bool,
exponent: i32,
significand: u64,
extra: u64,
) -> Computed<T> {
Computed::Unfinished(Unrounded::from_words(
negative,
exponent,
significand,
extra,
))
}