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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// The SSE operations on binary32 and binary64.
use super::{Computed, Flags, Rounded, X86, indefinite};
use crate::nan::NanPolicy;
use crate::{Double, Relation, Rounding, Single, Tininess, Unrounded, round};
/// SSE arithmetic shared by binary32 and binary64. These associated functions
/// use [`super::X86`] and report [`Flags`].
pub trait Real: Copy {
/// The default NaN. It is negative and quiet, with a zero payload.
const DEFAULT_NAN: Self;
/// Tests whether the operand is a signalling NaN.
fn is_signalling_nan(value: Self) -> bool;
/// Returns the sign bit, including for zero and NaN.
fn negative(value: Self) -> bool;
/// Tests whether the operand is a NaN.
fn is_nan(value: Self) -> bool;
/// Tests whether the operand is denormal: a zero exponent field and a
/// nonzero fraction.
fn is_denormal(value: Self) -> bool;
/// Encodes zero with the given sign.
fn zero(negative: bool) -> Self;
/// Adds two lanes. See [`crate::Single::add`] for the operand cases.
/// NaNs and exceptions follow the [x86 rules](super).
fn add(a: Self, b: Self, rounding: Rounding) -> Computed<Self>;
/// Subtracts the second lane from the first. See [`crate::Single::subtract`] for the operand cases.
/// NaNs and exceptions follow the [x86 rules](super).
fn subtract(a: Self, b: Self, rounding: Rounding) -> Computed<Self>;
/// Multiplies two lanes. See [`crate::Single::multiply`] for the operand cases.
/// NaNs and exceptions follow the [x86 rules](super).
fn multiply(a: Self, b: Self) -> Computed<Self>;
/// Divides the first lane by the second. See [`crate::Single::divide`] for the operand cases.
/// NaNs and exceptions follow the [x86 rules](super).
fn divide(a: Self, b: Self) -> Computed<Self>;
/// Takes the square root of a lane. See [`crate::Single::sqrt`] for the operand cases.
/// NaNs and exceptions follow the [x86 rules](super).
fn sqrt(a: Self) -> Computed<Self>;
/// Compares two lanes. See [`crate::Single::compare`] for the operand cases.
/// NaNs and exceptions follow the [x86 rules](super).
fn compare(a: Self, b: Self, signalling: bool) -> (Relation, Flags);
/// Converts a signed 32-bit integer. Binary64 finishes exactly. A nonzero
/// binary32 result goes through the destination rounding.
fn from_i32(value: i32) -> Computed<Self>;
/// Rounds to the destination integer width. NaN, infinity or an out-of-range
/// rounded value returns the most negative integer with invalid alone.
fn to_i32(value: Self, rounding: Rounding) -> Rounded<i32>;
/// Converts a signed 64-bit integer. A nonzero result goes through the
/// destination rounding.
fn from_i64(value: i64) -> Computed<Self>;
/// Rounds to a signed 64-bit integer. NaN, infinity or an out-of-range
/// rounded value returns the most negative integer with invalid alone.
fn to_i64(value: Self, rounding: Rounding) -> Rounded<i64>;
/// Applies the masked response. It rounds on the format's grid, overflows
/// to infinity or the largest finite value, and underflows onto the
/// denormal grid.
fn round(value: Unrounded<Self>, rounding: Rounding) -> Rounded<Self>;
/// Returns the flags of the unmasked response: overflow for a result above
/// the format's range, underflow for a tiny result and inexact for inexact
/// rounding at the format's precision with an unbounded exponent. The instruction
/// caller suppresses the destination write.
fn unmasked_flags(value: Unrounded<Self>, rounding: Rounding) -> Flags;
}
macro_rules! real_methods {
(discarded: $discarded:literal, normal_exponents: $min:literal ..= $max:literal) => {
const DEFAULT_NAN: Self = <X86 as NanPolicy<Self>>::DEFAULT;
#[inline]
fn is_signalling_nan(value: Self) -> bool {
<X86 as NanPolicy<Self>>::is_signalling(value)
}
#[inline]
fn negative(value: Self) -> bool {
value.negative()
}
#[inline]
fn is_nan(value: Self) -> bool {
value.is_nan()
}
#[inline]
fn is_denormal(value: Self) -> bool {
value.is_denormal()
}
#[inline]
fn zero(negative: bool) -> Self {
Self::zero(negative)
}
#[inline]
fn add(a: Self, b: Self, rounding: Rounding) -> Computed<Self> {
a.add::<X86>(b, rounding)
}
#[inline]
fn subtract(a: Self, b: Self, rounding: Rounding) -> Computed<Self> {
a.subtract::<X86>(b, rounding)
}
#[inline]
fn multiply(a: Self, b: Self) -> Computed<Self> {
a.multiply::<X86>(b)
}
#[inline]
fn divide(a: Self, b: Self) -> Computed<Self> {
a.divide::<X86>(b)
}
#[inline]
fn sqrt(a: Self) -> Computed<Self> {
a.sqrt::<X86>()
}
#[inline]
fn compare(a: Self, b: Self, signalling: bool) -> (Relation, Flags) {
a.compare::<X86>(b, signalling)
}
#[inline]
fn to_i32(value: Self, rounding: Rounding) -> Rounded<i32> {
indefinite(value.to_i32(rounding), i32::MIN)
}
#[inline]
fn from_i64(value: i64) -> Computed<Self> {
Self::from_i64(value)
}
#[inline]
fn to_i64(value: Self, rounding: Rounding) -> Rounded<i64> {
indefinite(value.to_i64(rounding), i64::MIN)
}
#[inline]
fn round(value: Unrounded<Self>, rounding: Rounding) -> Rounded<Self> {
value.round(rounding, Tininess::AfterRounding)
}
#[inline]
fn unmasked_flags(value: Unrounded<Self>, rounding: Rounding) -> Flags {
let (negative, exponent, significand, extra) = value.parts();
let (exponent, _, inexact, _) =
round::unbounded(negative, exponent, significand, extra, rounding, $discarded);
Flags::OVERFLOW.when(exponent > $max)
| Flags::UNDERFLOW.when(exponent < $min)
| Flags::INEXACT.when(inexact)
}
};
}
impl Real for Single {
real_methods!(discarded: 40, normal_exponents: -126..=127);
#[inline]
fn from_i32(value: i32) -> Computed<Single> {
Single::from_i32(value)
}
}
impl Real for Double {
real_methods!(discarded: 11, normal_exponents: -1022..=1023);
// The conversion is exact, so the result is always Finished.
#[inline]
fn from_i32(value: i32) -> Computed<Double> {
Computed::Finished(Double::from_i32(value), Flags::NONE)
}
}