|
| 1 | +#[cfg(test)] |
| 2 | +mod tests; |
| 3 | + |
| 4 | +use crate::f128::u256::U256; |
| 5 | + |
| 6 | +/// Software implementation of `f128::div_euclid`. |
| 7 | +#[allow(dead_code)] |
| 8 | +pub(crate) fn div_euclid(a: f128, b: f128) -> f128 { |
| 9 | + if let Some((a_neg, a_exp, a_m)) = normal_form(a) |
| 10 | + && let Some((b_neg, b_exp, b_m)) = normal_form(b) |
| 11 | + { |
| 12 | + let exp = a_exp - b_exp; |
| 13 | + match (a_neg, b_neg) { |
| 14 | + (false, false) => div_floor(exp, a_m, b_m), |
| 15 | + (true, false) => -div_ceil(exp, a_m, b_m), |
| 16 | + (false, true) => -div_floor(exp, a_m, b_m), |
| 17 | + (true, true) => div_ceil(exp, a_m, b_m), |
| 18 | + } |
| 19 | + } else { |
| 20 | + // `a` or `b` are +-0.0 or infinity or NaN. |
| 21 | + // `a / b` is also +-0.0 or infinity or NaN. |
| 22 | + // There is no need to round to an integer. |
| 23 | + a / b |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// Returns `floor((a << exp) / b)`. |
| 28 | +/// |
| 29 | +/// Requires `2^112 <= a, b < 2^113`. |
| 30 | +fn div_floor(exp: i32, a: u128, b: u128) -> f128 { |
| 31 | + if exp < 0 { |
| 32 | + 0.0 |
| 33 | + } else if exp <= 15 { |
| 34 | + // aa < (2^113 << 15) = 2^128 |
| 35 | + let aa = a << exp; |
| 36 | + // q < 2^128 / 2^112 = 2^16 |
| 37 | + let q = (aa / b) as u32; |
| 38 | + // We have to use `as` because `From<u32> for f128` is not yet implemented. |
| 39 | + q as f128 |
| 40 | + } else if exp <= 127 { |
| 41 | + // aa = a << exp |
| 42 | + // aa < (2^113 << 127) = 2^240 |
| 43 | + let aa = U256::shl_u128(a, exp as u32); |
| 44 | + // q < 2^240 / 2^112 = 2^128 |
| 45 | + let (q, _) = aa.div_rem(b); |
| 46 | + q as f128 |
| 47 | + } else { |
| 48 | + // aa >= (2^112 << 127) = 2^239 |
| 49 | + // aa < (2^113 << 127) = 2^240 |
| 50 | + let aa = U256::shl_u128(a, 127); |
| 51 | + // e > 0 |
| 52 | + // The result is floor((aa << e) / b). |
| 53 | + let e = (exp - 127) as u32; |
| 54 | + |
| 55 | + // aa = q * b + r |
| 56 | + // q >= 2^239 / 2^113 = 2^126 |
| 57 | + // q < 2^239 / 2^112 = 2^128 |
| 58 | + // 0 <= r < b |
| 59 | + let (q, r) = aa.div_rem(b); |
| 60 | + |
| 61 | + // result = floor((aa << e) / b) = (q << e) + floor((r << e) / b) |
| 62 | + // 0 <= (r << e) / b < 2^e |
| 63 | + // |
| 64 | + // There are two cases: |
| 65 | + // 1. floor((r << e) / b) = 0 |
| 66 | + // 2. 0 < floor((r << e) / b) < 2^e |
| 67 | + // |
| 68 | + // In case 1: |
| 69 | + // The result is q << e. |
| 70 | + // |
| 71 | + // In case 2: |
| 72 | + // The result is (q << e) + non-zero low e bits. |
| 73 | + // This rounds the same way as (q | 1) << e because rounding beyond |
| 74 | + // the 25 most significant bits of q depends only on whether the low-order |
| 75 | + // bits are non-zero. |
| 76 | + // |
| 77 | + // Case 1 happens when: |
| 78 | + // (r << e) / b < 1 |
| 79 | + // (r << e) <= b - 1 |
| 80 | + // r <= ((b - 1) >> e) |
| 81 | + let case_1_bound = if e < 128 { (b - 1) >> e } else { 0 }; |
| 82 | + let q_adj = if r <= case_1_bound { |
| 83 | + // Case 1. |
| 84 | + q |
| 85 | + } else { |
| 86 | + // Case 2. |
| 87 | + q | 1 |
| 88 | + }; |
| 89 | + q_adj as f128 * pow2(e) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// Returns `ceil((a << exp) / b)`. |
| 94 | +/// |
| 95 | +/// Requires `2^112 <= a, b < 2^113`. |
| 96 | +fn div_ceil(exp: i32, a: u128, b: u128) -> f128 { |
| 97 | + if exp < 0 { |
| 98 | + 1.0 |
| 99 | + } else if exp <= 15 { |
| 100 | + // aa < (2^113 << 15) = 2^128 |
| 101 | + let aa = a << exp; |
| 102 | + // q < 2^128 / 2^112 + 1 = 2^16 + 1 |
| 103 | + let q = ((aa - 1) / b) as u32 + 1; |
| 104 | + // We have to use `as` because `From<u32> for f128` is not yet implemented. |
| 105 | + q as f128 |
| 106 | + } else if exp <= 127 { |
| 107 | + // aa = a << exp |
| 108 | + // aa <= ((2^113 - 1) << 127) = 2^240 - 2^127 |
| 109 | + let aa = U256::shl_u128(a, exp as u32); |
| 110 | + // q <= (2^240 - 2^127) / 2^112 + 1 = 2^128 - 2^15 + 1 |
| 111 | + let (q, _) = (aa - U256::ONE).div_rem(b); |
| 112 | + (q + 1) as f128 |
| 113 | + } else { |
| 114 | + // aa >= (2^112 << 127) = 2^239 |
| 115 | + // aa <= ((2^113 - 1) << 127) = 2^240 - 2^127 |
| 116 | + let aa = U256::shl_u128(a, 127); |
| 117 | + // e > 0 |
| 118 | + // The result is ceil((aa << e) / b). |
| 119 | + let e = (exp - 127) as u32; |
| 120 | + |
| 121 | + // aa = q * b + r |
| 122 | + // q >= 2^239 / 2^112 = 2^126 |
| 123 | + // q <= (2^240 - 2^127) / 2^112 = 2^128 - 2^15 |
| 124 | + // 0 <= r < b |
| 125 | + let (q, r) = aa.div_rem(b); |
| 126 | + |
| 127 | + // result = ceil((aa << e) / b) = (q << e) + ceil((r << e) / b) |
| 128 | + // 0 <= (r << e) / b < 2^e |
| 129 | + // |
| 130 | + // There are three cases: |
| 131 | + // 1. ceil((r << e) / b) = 0 |
| 132 | + // 2. 0 < ceil((r << e) / b) < 2^e |
| 133 | + // 3. ceil((r << e) / b) = 2^e |
| 134 | + // |
| 135 | + // In case 1: |
| 136 | + // The result is q << e. |
| 137 | + // |
| 138 | + // In case 2: |
| 139 | + // The result is (q << e) + non-zero low e bits. |
| 140 | + // This rounds the same way as (q | 1) << e because rounding beyond |
| 141 | + // the 54 most significant bits of q depends only on whether the low-order |
| 142 | + // bits are non-zero. |
| 143 | + // |
| 144 | + // In case 3: |
| 145 | + // The result is (q + 1) << e. |
| 146 | + // |
| 147 | + // Case 1 happens when r = 0. |
| 148 | + // Case 3 happens when: |
| 149 | + // (r << e) / b > (1 << e) - 1 |
| 150 | + // (r << e) > (b << e) - b |
| 151 | + // ((b - r) << e) <= b - 1 |
| 152 | + // b - r <= (b - 1) >> e |
| 153 | + // r >= b - ((b - 1) >> e) |
| 154 | + let case_3_bound = b - if e < 128 { (b - 1) >> e } else { 0 }; |
| 155 | + let q_adj = if r == 0 { |
| 156 | + // Case 1. |
| 157 | + q |
| 158 | + } else if r < case_3_bound { |
| 159 | + // Case 2. |
| 160 | + q | 1 |
| 161 | + } else { |
| 162 | + // Case 3. |
| 163 | + q + 1 |
| 164 | + }; |
| 165 | + q_adj as f128 * pow2(e) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/// For finite, non-zero numbers returns (sign, exponent, mantissa). |
| 170 | +/// |
| 171 | +/// `x = (-1)^sign * 2^exp * mantissa` |
| 172 | +/// |
| 173 | +/// `2^112 <= mantissa < 2^113` |
| 174 | +fn normal_form(x: f128) -> Option<(bool, i32, u128)> { |
| 175 | + let bits = x.to_bits(); |
| 176 | + let sign = bits >> 127 != 0; |
| 177 | + let biased_exponent = (bits >> 112 & 0x7fff) as i32; |
| 178 | + let significand = bits & ((1 << 112) - 1); |
| 179 | + match biased_exponent { |
| 180 | + 0 if significand == 0 => { |
| 181 | + // 0.0 |
| 182 | + None |
| 183 | + } |
| 184 | + 0 => { |
| 185 | + // Subnormal number: 2^(-16382-112) * significand. |
| 186 | + // We want mantissa to have exactly 15 leading zeros. |
| 187 | + let shift = significand.leading_zeros() - 15; |
| 188 | + Some((sign, -16382 - 112 - shift as i32, significand << shift)) |
| 189 | + } |
| 190 | + 0x7fff => { |
| 191 | + // Infinity or NaN. |
| 192 | + None |
| 193 | + } |
| 194 | + _ => { |
| 195 | + // Normal number: 2^(biased_exponent-16383-112) * (2^112 + significand) |
| 196 | + Some((sign, biased_exponent - 16383 - 112, 1 << 112 | significand)) |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +/// Returns `2^exp`. |
| 202 | +fn pow2(exp: u32) -> f128 { |
| 203 | + if exp <= 16383 { f128::from_bits(u128::from(exp + 16383) << 112) } else { f128::INFINITY } |
| 204 | +} |
0 commit comments