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
// The SSE operations on binary32 and binary64.
use super::{Computed, Exceptions, 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 architectural exception bits.
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;
/// 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, Exceptions);
/// 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 IE 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 IE 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 OE for overflow, UE for a tiny result and PE for inexact rounding
/// at the format's precision with an unbounded exponent. The instruction
/// caller suppresses the destination write.
fn unmasked_exceptions(value: Unrounded<Self>, rounding: Rounding) -> Exceptions;
}
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 add(a: Self, b: Self, rounding: Rounding) -> Computed<Self> {
a.add::<X86>(b, rounding).map_exceptions(Exceptions::from)
}
#[inline]
fn subtract(a: Self, b: Self, rounding: Rounding) -> Computed<Self> {
a.subtract::<X86>(b, rounding)
.map_exceptions(Exceptions::from)
}
#[inline]
fn multiply(a: Self, b: Self) -> Computed<Self> {
a.multiply::<X86>(b).map_exceptions(Exceptions::from)
}
#[inline]
fn divide(a: Self, b: Self) -> Computed<Self> {
a.divide::<X86>(b).map_exceptions(Exceptions::from)
}
#[inline]
fn sqrt(a: Self) -> Computed<Self> {
a.sqrt::<X86>().map_exceptions(Exceptions::from)
}
#[inline]
fn compare(a: Self, b: Self, signalling: bool) -> (Relation, Exceptions) {
let (relation, flags) = a.compare::<X86>(b, signalling);
(relation, Exceptions::from(flags))
}
#[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).map_exceptions(Exceptions::from)
}
#[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)
.map_exceptions(Exceptions::from)
}
#[inline]
fn unmasked_exceptions(value: Unrounded<Self>, rounding: Rounding) -> Exceptions {
let (negative, exponent, significand, extra) = value.parts();
let (exponent, _, inexact, _) =
round::unbounded(negative, exponent, significand, extra, rounding, $discarded);
Exceptions::OE.when(exponent > $max)
| Exceptions::UE.when(exponent < $min)
| Exceptions::PE.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).map_exceptions(Exceptions::from)
}
}
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), Exceptions::NONE)
}
}