1use crate::support::{
5 CastFrom, CastInto, DInt, Float, FpResult, HInt, Int, IntTy, MinInt, Round, Status,
6};
7
8#[inline]
11pub fn fma_round<F>(x: F, y: F, z: F, _round: Round) -> FpResult<F>
12where
13 F: Float,
14 F: CastFrom<F::SignedInt>,
15 F: CastFrom<i8>,
16 F::Int: HInt,
17 u32: CastInto<F::Int>,
18{
19 let one = IntTy::<F>::ONE;
20 let zero = IntTy::<F>::ZERO;
21
22 let nx = Norm::from_float(x);
24 let ny = Norm::from_float(y);
25 let nz = Norm::from_float(z);
26
27 if nx.is_zero_nan_inf() || ny.is_zero_nan_inf() {
28 return FpResult::ok(x * y + z);
30 }
31
32 if nz.is_zero_nan_inf() {
33 if nz.is_zero() {
34 return FpResult::ok(x * y);
36 }
37 return FpResult::ok(z);
39 }
40
41 let zhi: F::Int;
43 let zlo: F::Int;
44 let (mut rlo, mut rhi) = nx.m.widen_mul(ny.m).lo_hi();
45
46 let mut e: i32 = nx.e + ny.e;
48 let mut d: i32 = nz.e - e;
50 let sbits = F::BITS as i32;
51
52 if d > 0 {
54 if d < sbits {
56 zlo = nz.m << d;
59 zhi = nz.m >> (sbits - d);
60 } else {
61 zlo = zero;
64 zhi = nz.m;
65 d -= sbits;
66
67 e = nz.e - sbits;
69
70 if d == 0 {
71 } else if d < sbits {
73 rlo = (rhi << (sbits - d)) | (rlo >> d);
75 rlo |= IntTy::<F>::from((rlo << (sbits - d)) != zero);
77 rhi = rhi >> d;
78 } else {
79 rlo = one;
82 rhi = zero;
83 }
84 }
85 } else {
86 zhi = zero;
88 d = -d;
89 if d == 0 {
90 zlo = nz.m;
92 } else if d < sbits {
93 let sticky = IntTy::<F>::from((nz.m << (sbits - d)) != zero);
95 zlo = (nz.m >> d) | sticky;
96 } else {
97 zlo = one;
99 }
100 }
101
102 let mut neg = nx.neg ^ ny.neg;
105 let samesign: bool = !neg ^ nz.neg;
106 let mut rhi_nonzero = true;
107
108 if samesign {
109 rlo = rlo.wrapping_add(zlo);
111 rhi += zhi + IntTy::<F>::from(rlo < zlo);
112 } else {
113 let (res, borrow) = rlo.overflowing_sub(zlo);
115 rlo = res;
116 rhi = rhi.wrapping_sub(zhi.wrapping_add(IntTy::<F>::from(borrow)));
117 if (rhi >> (F::BITS - 1)) != zero {
118 rlo = rlo.signed().wrapping_neg().unsigned();
119 rhi = rhi.signed().wrapping_neg().unsigned() - IntTy::<F>::from(rlo != zero);
120 neg = !neg;
121 }
122 rhi_nonzero = rhi != zero;
123 }
124
125 if rhi_nonzero {
129 e += sbits;
131 d = rhi.leading_zeros() as i32 - 1;
132 rhi = (rhi << d) | (rlo >> (sbits - d));
133 rhi |= IntTy::<F>::from((rlo << d) != zero);
135 } else if rlo != zero {
136 d = rlo.leading_zeros() as i32 - 1;
138 if d < 0 {
139 rhi = (rlo >> 1) | (rlo & one);
141 } else {
142 rhi = rlo << d;
143 }
144 } else {
145 return FpResult::ok(x * y + z);
147 }
148
149 e -= d;
150
151 let mut i: F::SignedInt = rhi.signed();
154
155 if neg {
156 i = -i;
157 }
158
159 let mut r: F = F::cast_from_lossy(i);
161
162 let max_pow = F::BITS - 1 + F::EXP_BIAS;
166
167 let mut status = Status::OK;
168
169 if e < -(max_pow as i32 - 2) {
170 if e == -(max_pow as i32 - 1) {
172 let mut c = F::from_parts(false, max_pow, zero);
173 if neg {
174 c = -c;
175 }
176
177 if r == c {
178 status.set_underflow(true);
180 r = F::MIN_POSITIVE_NORMAL.copysign(r);
181 return FpResult::new(r, status);
182 }
183
184 if (rhi << (F::SIG_BITS + 1)) != zero {
185 let iu: F::Int = (rhi >> 1) | (rhi & one) | (one << (F::BITS - 2));
188 i = iu.signed();
189
190 if neg {
191 i = -i;
192 }
193
194 r = F::cast_from_lossy(i);
195
196 r = F::cast_from(2i8) * r - c;
198 status.set_underflow(true);
199 }
200 } else {
201 d = F::EXP_BITS as i32 - 1;
203 let sticky = IntTy::<F>::from(rhi << (F::BITS as i32 - d) != zero);
204 i = (((rhi >> d) | sticky) << d).signed();
205
206 if neg {
207 i = -i;
208 }
209
210 r = F::cast_from_lossy(i);
211 }
212 }
213
214 FpResult::new(super::scalbn(r, e), status)
216}
217
218#[derive(#[automatically_derived]
impl<F: ::core::clone::Clone + Float> ::core::clone::Clone for Norm<F> where
F::Int: ::core::clone::Clone {
#[inline]
fn clone(&self) -> Norm<F> {
Norm {
m: ::core::clone::Clone::clone(&self.m),
e: ::core::clone::Clone::clone(&self.e),
neg: ::core::clone::Clone::clone(&self.neg),
}
}
}Clone, #[automatically_derived]
impl<F: ::core::marker::Copy + Float> ::core::marker::Copy for Norm<F> where
F::Int: ::core::marker::Copy {
}Copy, #[automatically_derived]
impl<F: ::core::fmt::Debug + Float> ::core::fmt::Debug for Norm<F> where
F::Int: ::core::fmt::Debug {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "Norm", "m",
&self.m, "e", &self.e, "neg", &&self.neg)
}
}Debug)]
220struct Norm<F: Float> {
221 m: F::Int,
223 e: i32,
226 neg: bool,
227}
228
229impl<F: Float> Norm<F> {
230 const EXP_UNBIAS: u32 = F::EXP_BIAS + F::SIG_BITS + 1;
232
233 const ZERO_INF_NAN: u32 = F::EXP_SAT - Self::EXP_UNBIAS;
236
237 fn from_float(x: F) -> Self {
238 let mut ix = x.to_bits();
239 let mut e = x.ex() as i32;
240 let neg = x.is_sign_negative();
241 if e == 0 {
242 let scale_i = F::BITS - 1;
244 let scale_f = F::from_parts(false, scale_i + F::EXP_BIAS, F::Int::ZERO);
245 let scaled = x * scale_f;
246 ix = scaled.to_bits();
247 e = scaled.ex() as i32;
248 e = if e == 0 {
249 1 << F::EXP_BITS
252 } else {
253 e - scale_i as i32
255 };
256 }
257
258 e -= Self::EXP_UNBIAS as i32;
259
260 ix &= F::SIG_MASK;
262 ix |= F::IMPLICIT_BIT;
263 ix <<= 1;
264
265 Self { m: ix, e, neg }
266 }
267
268 fn is_zero_nan_inf(self) -> bool {
270 self.e >= Self::ZERO_INF_NAN as i32
271 }
272
273 fn is_zero(self) -> bool {
275 self.e > Self::ZERO_INF_NAN as i32
277 }
278}