const_soft_float/soft_f64/
add.rs

1use crate::soft_f64::SoftF64;
2
3type F = SoftF64;
4
5type FInt = u64;
6
7pub(crate) const fn add(a: F, b: F) -> F {
8    let one: FInt = 1;
9    let zero: FInt = 0;
10
11    let bits = F::BITS as FInt;
12    let significand_bits = F::SIGNIFICAND_BITS;
13    let max_exponent = F::EXPONENT_MAX;
14
15    let implicit_bit = F::IMPLICIT_BIT;
16    let significand_mask = F::SIGNIFICAND_MASK;
17    let sign_bit = F::SIGN_MASK as FInt;
18    let abs_mask = sign_bit - one;
19    let exponent_mask = F::EXPONENT_MASK;
20    let inf_rep = exponent_mask;
21    let quiet_bit = implicit_bit >> 1;
22    let qnan_rep = exponent_mask | quiet_bit;
23
24    let mut a_rep = a.repr();
25    let mut b_rep = b.repr();
26    let a_abs = a_rep & abs_mask;
27    let b_abs = b_rep & abs_mask;
28
29    // Detect if a or b is zero, infinity, or NaN.
30    if a_abs.wrapping_sub(one) >= inf_rep - one || b_abs.wrapping_sub(one) >= inf_rep - one {
31        // NaN + anything = qNaN
32        if a_abs > inf_rep {
33            return F::from_repr(a_abs | quiet_bit);
34        }
35        // anything + NaN = qNaN
36        if b_abs > inf_rep {
37            return F::from_repr(b_abs | quiet_bit);
38        }
39
40        if a_abs == inf_rep {
41            // +/-infinity + -/+infinity = qNaN
42            if (a.repr() ^ b.repr()) == sign_bit {
43                return F::from_repr(qnan_rep);
44            } else {
45                // +/-infinity + anything remaining = +/- infinity
46                return a;
47            }
48        }
49
50        // anything remaining + +/-infinity = +/-infinity
51        if b_abs == inf_rep {
52            return b;
53        }
54
55        // zero + anything = anything
56        if a_abs == 0 {
57            // but we need to get the sign right for zero + zero
58            if b_abs == 0 {
59                return F::from_repr(a.repr() & b.repr());
60            } else {
61                return b;
62            }
63        }
64
65        // anything + zero = anything
66        if b_abs == 0 {
67            return a;
68        }
69    }
70
71    // Swap a and b if necessary so that a has the larger absolute value.
72    if b_abs > a_abs {
73        // Don't use mem::swap because it may generate references to memcpy in unoptimized code.
74        let tmp = a_rep;
75        a_rep = b_rep;
76        b_rep = tmp;
77    }
78
79    // Extract the exponent and significand from the (possibly swapped) a and b.
80    let mut a_exponent: i32 = ((a_rep & exponent_mask) >> significand_bits) as _;
81    let mut b_exponent: i32 = ((b_rep & exponent_mask) >> significand_bits) as _;
82    let mut a_significand = a_rep & significand_mask;
83    let mut b_significand = b_rep & significand_mask;
84
85    // normalize any denormals, and adjust the exponent accordingly.
86    if a_exponent == 0 {
87        let (exponent, significand) = F::normalize(a_significand);
88        a_exponent = exponent;
89        a_significand = significand;
90    }
91    if b_exponent == 0 {
92        let (exponent, significand) = F::normalize(b_significand);
93        b_exponent = exponent;
94        b_significand = significand;
95    }
96
97    // The sign of the result is the sign of the larger operand, a.  If they
98    // have opposite signs, we are performing a subtraction; otherwise addition.
99    let result_sign = a_rep & sign_bit;
100    let subtraction = ((a_rep ^ b_rep) & sign_bit) != zero;
101
102    // Shift the significands to give us round, guard and sticky, and or in the
103    // implicit significand bit.  (If we fell through from the denormal path it
104    // was already set by normalize(), but setting it twice won't hurt
105    // anything.)
106    a_significand = (a_significand | implicit_bit) << 3;
107    b_significand = (b_significand | implicit_bit) << 3;
108
109    // Shift the significand of b by the difference in exponents, with a sticky
110    // bottom bit to get rounding correct.
111    let align = a_exponent.wrapping_sub(b_exponent) as _;
112    if align != 0 {
113        if align < bits {
114            let sticky = (b_significand << bits.wrapping_sub(align) != 0) as FInt;
115            b_significand = (b_significand >> align) | sticky;
116        } else {
117            b_significand = one; // sticky; b is known to be non-zero.
118        }
119    }
120    if subtraction {
121        a_significand = a_significand.wrapping_sub(b_significand);
122        // If a == -b, return +zero.
123        if a_significand == 0 {
124            return F::from_repr(0);
125        }
126
127        // If partial cancellation occured, we need to left-shift the result
128        // and adjust the exponent:
129        if a_significand < implicit_bit << 3 {
130            let shift =
131                a_significand.leading_zeros() as i32 - (implicit_bit << 3).leading_zeros() as i32;
132            a_significand <<= shift;
133            a_exponent -= shift;
134        }
135    } else {
136        // addition
137        a_significand += b_significand;
138
139        // If the addition carried up, we need to right-shift the result and
140        // adjust the exponent:
141        if a_significand & implicit_bit << 4 != 0 {
142            let sticky = (a_significand & one != 0) as FInt;
143            a_significand = a_significand >> 1 | sticky;
144            a_exponent += 1;
145        }
146    }
147
148    // If we have overflowed the type, return +/- infinity:
149    if a_exponent >= max_exponent as i32 {
150        return F::from_repr(inf_rep | result_sign);
151    }
152
153    if a_exponent <= 0 {
154        // Result is denormal before rounding; the exponent is zero and we
155        // need to shift the significand.
156        let shift = (1 - a_exponent) as _;
157        let sticky = ((a_significand << bits.wrapping_sub(shift)) != 0) as FInt;
158        a_significand = a_significand >> shift | sticky;
159        a_exponent = 0;
160    }
161
162    // Low three bits are round, guard, and sticky.
163    let a_significand_i32: i32 = a_significand as _;
164    let round_guard_sticky: i32 = a_significand_i32 & 0x7;
165
166    // Shift the significand into place, and mask off the implicit bit.
167    let mut result = a_significand >> 3 & significand_mask;
168
169    // Insert the exponent and sign.
170    result |= (a_exponent as FInt) << significand_bits;
171    result |= result_sign;
172
173    // Final rounding.  The result may overflow to infinity, but that is the
174    // correct result in that case.
175    if round_guard_sticky > 0x4 {
176        result += one;
177    }
178    if round_guard_sticky == 0x4 {
179        result += result & one;
180    }
181
182    F::from_repr(result)
183}
184
185#[cfg(test)]
186mod test {
187    use super::*;
188
189    #[test]
190    fn sanity_check() {
191        assert_eq!(SoftF64(1.0).add(SoftF64(1.0)).0, 2.0)
192    }
193}