|
1 | 1 | #![allow(unused)]
|
2 | 2 |
|
| 3 | +use core::ops::{Shl, Shr}; |
| 4 | + |
3 | 5 | use super::super::fenv::{
|
4 | 6 | FE_INEXACT, FE_TONEAREST, FE_UNDERFLOW, feclearexcept, fegetround, feraiseexcept, fetestexcept,
|
5 | 7 | };
|
| 8 | +use super::super::support::{DInt, HInt, Int}; |
6 | 9 | use super::super::{CastFrom, CastInto, Float, IntTy, MinInt};
|
7 | 10 |
|
| 11 | +const ZEROINFNAN: i32 = 0x7ff - 0x3ff - 52 - 1; |
| 12 | + |
8 | 13 | /// Fused multiply add.
|
9 |
| -pub fn fma<F: Float>(x: F, y: F, z: F) -> F { |
10 |
| - todo!() |
| 14 | +pub fn fma<F: Float>(x: F, y: F, z: F, scbn: impl FnOnce(F, i32) -> F) -> F |
| 15 | +where |
| 16 | + F::Int: CastFrom<u32>, |
| 17 | + F::Int: HInt, |
| 18 | + F::Int: Shr<i32, Output = F::Int>, |
| 19 | + F::Int: Shl<i32, Output = F::Int>, |
| 20 | + F::SignedInt: CastInto<F>, |
| 21 | + u32: CastInto<F::Int>, |
| 22 | + bool: CastInto<F::Int>, |
| 23 | +{ |
| 24 | + let one = F::Int::ONE; |
| 25 | + let zero = F::Int::ZERO; |
| 26 | + |
| 27 | + let nx = Norm::from_float(x); |
| 28 | + let ny = Norm::from_float(y); |
| 29 | + let nz = Norm::from_float(z); |
| 30 | + |
| 31 | + if nx.e >= ZEROINFNAN || ny.e >= ZEROINFNAN { |
| 32 | + return x * y + z; |
| 33 | + } |
| 34 | + if nz.e >= ZEROINFNAN { |
| 35 | + if nz.e > ZEROINFNAN { |
| 36 | + /* z==0 */ |
| 37 | + return x * y + z; |
| 38 | + } |
| 39 | + return z; |
| 40 | + } |
| 41 | + |
| 42 | + let zhi: F::Int; |
| 43 | + let zlo: F::Int; |
| 44 | + |
| 45 | + let (mut rlo, mut rhi) = nx.m.widen_mul(ny.m).lo_hi(); |
| 46 | + |
| 47 | + let mut e: i32 = nx.e + ny.e; |
| 48 | + let mut d: i32 = nz.e - e; |
| 49 | + |
| 50 | + let fbits = F::BITS as i32; |
| 51 | + |
| 52 | + if d > 0 { |
| 53 | + if d < fbits { |
| 54 | + zlo = nz.m << d; |
| 55 | + zhi = nz.m >> (fbits - d); |
| 56 | + } else { |
| 57 | + zlo = zero; |
| 58 | + zhi = nz.m; |
| 59 | + e = nz.e - fbits; |
| 60 | + d -= fbits; |
| 61 | + if d == 0 { |
| 62 | + } else if d < fbits { |
| 63 | + rlo = (rhi << (fbits - d)) | (rlo >> d) | ((rlo << (fbits - d)) != zero).cast(); |
| 64 | + rhi = rhi >> d; |
| 65 | + } else { |
| 66 | + rlo = one; |
| 67 | + rhi = zero; |
| 68 | + } |
| 69 | + } |
| 70 | + } else { |
| 71 | + zhi = zero; |
| 72 | + d = -d; |
| 73 | + if d == 0 { |
| 74 | + zlo = nz.m; |
| 75 | + } else if d < fbits { |
| 76 | + zlo = (nz.m >> d) | ((nz.m << (fbits - d)) != zero).cast(); |
| 77 | + } else { |
| 78 | + zlo = one; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /* add */ |
| 83 | + let mut neg: bool = nx.neg ^ ny.neg; |
| 84 | + let samesign: bool = neg ^ nz.neg; |
| 85 | + let mut nonzero: i32 = 1; |
| 86 | + if samesign { |
| 87 | + /* r += z */ |
| 88 | + rlo = rlo.wrapping_add(zlo); |
| 89 | + rhi += zhi + (rlo < zlo).cast(); |
| 90 | + } else { |
| 91 | + /* r -= z */ |
| 92 | + let (res, borrow) = rlo.overflowing_sub(zlo); |
| 93 | + rlo = res; |
| 94 | + rhi = rhi.wrapping_sub(zhi.wrapping_add(borrow.cast())); |
| 95 | + if (rhi >> (F::BITS - 1)) != zero { |
| 96 | + rlo = (rlo.signed()).wrapping_neg().unsigned(); |
| 97 | + rhi = (rhi.signed()).wrapping_neg().unsigned() - (rlo != zero).cast(); |
| 98 | + neg = !neg; |
| 99 | + } |
| 100 | + nonzero = (rhi != zero) as i32; |
| 101 | + } |
| 102 | + |
| 103 | + /* set rhi to top 63bit of the result (last bit is sticky) */ |
| 104 | + if nonzero != 0 { |
| 105 | + e += fbits; |
| 106 | + d = rhi.leading_zeros() as i32 - 1; |
| 107 | + /* note: d > 0 */ |
| 108 | + rhi = (rhi << d) | (rlo >> (fbits - d)) | ((rlo << d) != zero).cast(); |
| 109 | + } else if rlo != zero { |
| 110 | + d = rlo.leading_zeros() as i32 - 1; |
| 111 | + if d < 0 { |
| 112 | + rhi = (rlo >> 1) | (rlo & one); |
| 113 | + } else { |
| 114 | + rhi = rlo << d; |
| 115 | + } |
| 116 | + } else { |
| 117 | + /* exact +-0 */ |
| 118 | + return x * y + z; |
| 119 | + } |
| 120 | + e -= d; |
| 121 | + |
| 122 | + /* convert to double */ |
| 123 | + let mut i: F::SignedInt = rhi.signed(); /* i is in [1<<62,(1<<63)-1] */ |
| 124 | + if neg { |
| 125 | + i = -i; |
| 126 | + } |
| 127 | + let mut r: F = i.cast(); /* |r| is in [0x1p62,0x1p63] */ |
| 128 | + |
| 129 | + if e < -1022 - 62 { |
| 130 | + /* result is subnormal before rounding */ |
| 131 | + if e == -1022 - 63 { |
| 132 | + let mut c: F = foo::<F>(); |
| 133 | + if neg { |
| 134 | + c = -c; |
| 135 | + } |
| 136 | + if r == c { |
| 137 | + /* min normal after rounding, underflow depends |
| 138 | + on arch behaviour which can be imitated by |
| 139 | + a double to float conversion */ |
| 140 | + // let fltmin: f32 = (x0_ffffff8p_63 * f32::MIN_POSITIVE as f64 * r) as f32; |
| 141 | + // return f64::MIN_POSITIVE / f32::MIN_POSITIVE as f64 * fltmin as f64; |
| 142 | + todo!() |
| 143 | + } |
| 144 | + /* one bit is lost when scaled, add another top bit to |
| 145 | + only round once at conversion if it is inexact */ |
| 146 | + if (rhi << (F::SIG_BITS + 1)) != zero { |
| 147 | + let tmp: F::Int = (rhi >> 1) | (rhi & one) | (one << (F::BITS - 2)); |
| 148 | + i = tmp.signed(); |
| 149 | + if neg { |
| 150 | + i = -i; |
| 151 | + } |
| 152 | + r = i.cast(); |
| 153 | + r = (F::ONE + F::ONE) * r - c; /* remove top bit */ |
| 154 | + |
| 155 | + /* raise underflow portably, such that it |
| 156 | + cannot be optimized away */ |
| 157 | + { |
| 158 | + // let tiny: f64 = f64::MIN_POSITIVE / f32::MIN_POSITIVE as f64 * r; |
| 159 | + // r += (tiny * tiny) * (r - r); |
| 160 | + todo!() |
| 161 | + } |
| 162 | + } |
| 163 | + } else { |
| 164 | + /* only round once when scaled */ |
| 165 | + d = 10; |
| 166 | + i = (((rhi >> d) | ((rhi << (fbits - d)) != zero).cast()) << d).signed(); |
| 167 | + if neg { |
| 168 | + i = -i; |
| 169 | + } |
| 170 | + r = i.cast(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // todo!() |
| 175 | + // |
| 176 | + scbn(r, e) |
| 177 | +} |
| 178 | + |
| 179 | +struct Norm<F: Float> { |
| 180 | + m: F::Int, |
| 181 | + e: i32, |
| 182 | + neg: bool, |
| 183 | +} |
| 184 | + |
| 185 | +impl<F: Float> Norm<F> { |
| 186 | + fn from_float(x: F) -> Self |
| 187 | + where |
| 188 | + F::Int: CastFrom<u32>, |
| 189 | + u32: CastInto<F::Int>, |
| 190 | + { |
| 191 | + let mut ix = x.to_bits(); |
| 192 | + let mut e = x.exp(); |
| 193 | + let neg = x.is_sign_negative(); |
| 194 | + if e.is_zero() { |
| 195 | + ix = (x * foo::<F>()).to_bits(); |
| 196 | + e = x.exp(); |
| 197 | + e = if e != 0 { e - (F::BITS as i32) } else { 0x800 }; |
| 198 | + } |
| 199 | + ix &= F::SIG_MASK; |
| 200 | + ix |= F::IMPLICIT_BIT; |
| 201 | + ix <<= 1; |
| 202 | + e -= 0x3ff + 52 + 1; |
| 203 | + |
| 204 | + Self { m: ix, e, neg } |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +// 1p63 magic number |
| 209 | +fn foo<F: Float>() -> F |
| 210 | +where |
| 211 | + u32: CastInto<F::Int>, |
| 212 | +{ |
| 213 | + F::from_parts(false, (F::BITS - 1).cast(), F::Int::ZERO) |
11 | 214 | }
|
12 | 215 |
|
13 | 216 | /// FMA implementation when there is a larger float type available.
|
|
0 commit comments