const_soft_float/soft_f64/
add.rs1use 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 if a_abs.wrapping_sub(one) >= inf_rep - one || b_abs.wrapping_sub(one) >= inf_rep - one {
31 if a_abs > inf_rep {
33 return F::from_repr(a_abs | quiet_bit);
34 }
35 if b_abs > inf_rep {
37 return F::from_repr(b_abs | quiet_bit);
38 }
39
40 if a_abs == inf_rep {
41 if (a.repr() ^ b.repr()) == sign_bit {
43 return F::from_repr(qnan_rep);
44 } else {
45 return a;
47 }
48 }
49
50 if b_abs == inf_rep {
52 return b;
53 }
54
55 if a_abs == 0 {
57 if b_abs == 0 {
59 return F::from_repr(a.repr() & b.repr());
60 } else {
61 return b;
62 }
63 }
64
65 if b_abs == 0 {
67 return a;
68 }
69 }
70
71 if b_abs > a_abs {
73 let tmp = a_rep;
75 a_rep = b_rep;
76 b_rep = tmp;
77 }
78
79 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 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 let result_sign = a_rep & sign_bit;
100 let subtraction = ((a_rep ^ b_rep) & sign_bit) != zero;
101
102 a_significand = (a_significand | implicit_bit) << 3;
107 b_significand = (b_significand | implicit_bit) << 3;
108
109 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; }
119 }
120 if subtraction {
121 a_significand = a_significand.wrapping_sub(b_significand);
122 if a_significand == 0 {
124 return F::from_repr(0);
125 }
126
127 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 a_significand += b_significand;
138
139 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 a_exponent >= max_exponent as i32 {
150 return F::from_repr(inf_rep | result_sign);
151 }
152
153 if a_exponent <= 0 {
154 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 let a_significand_i32: i32 = a_significand as _;
164 let round_guard_sticky: i32 = a_significand_i32 & 0x7;
165
166 let mut result = a_significand >> 3 & significand_mask;
168
169 result |= (a_exponent as FInt) << significand_bits;
171 result |= result_sign;
172
173 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}