| | | 1 | | //======================================================================= |
| | | 2 | | // Fixed64.WideRawRatio.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | namespace FixedMathSharp; |
| | | 12 | | |
| | | 13 | | /// <content> |
| | | 14 | | /// Provides wide-precision (128/192-bit) ratio-to-<see cref="Fixed64"/> conversion helpers, |
| | | 15 | | /// used for computing exact division and unit-interval results without precision loss. |
| | | 16 | | /// </content> |
| | | 17 | | public partial struct Fixed64 |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Converts an exact ratio proven by this method to lie in [0, 1] to Q32.32. |
| | | 21 | | /// </summary> |
| | | 22 | | internal static bool TryGetUnitIntervalRatio( |
| | | 23 | | Signed192 numerator, |
| | | 24 | | Signed192 denominator, |
| | | 25 | | out Fixed64 result) |
| | | 26 | | { |
| | 767 | 27 | | int denominatorSign = denominator.Sign; |
| | 767 | 28 | | int numeratorSign = numerator.Sign; |
| | 767 | 29 | | if (denominatorSign == 0 || (numeratorSign != 0 && numeratorSign != denominatorSign)) |
| | | 30 | | { |
| | 5 | 31 | | result = default; |
| | 5 | 32 | | return false; |
| | | 33 | | } |
| | | 34 | | |
| | 762 | 35 | | if (numeratorSign == 0) |
| | | 36 | | { |
| | 19 | 37 | | result = Zero; |
| | 19 | 38 | | return true; |
| | | 39 | | } |
| | | 40 | | |
| | 743 | 41 | | WideArithmetic.GetMagnitude(numerator, out ulong numeratorHigh, out ulong numeratorMiddle, out ulong numeratorLo |
| | 743 | 42 | | WideArithmetic.GetMagnitude(denominator, out ulong denominatorHigh, out ulong denominatorMiddle, out ulong denom |
| | 743 | 43 | | int comparison = WideArithmetic.CompareUnsigned( |
| | 743 | 44 | | numeratorHigh, |
| | 743 | 45 | | numeratorMiddle, |
| | 743 | 46 | | numeratorLow, |
| | 743 | 47 | | denominatorHigh, |
| | 743 | 48 | | denominatorMiddle, |
| | 743 | 49 | | denominatorLow); |
| | 743 | 50 | | if (comparison > 0) |
| | | 51 | | { |
| | 4 | 52 | | result = default; |
| | 4 | 53 | | return false; |
| | | 54 | | } |
| | | 55 | | |
| | 739 | 56 | | if (comparison == 0) |
| | | 57 | | { |
| | 12 | 58 | | result = One; |
| | 12 | 59 | | return true; |
| | | 60 | | } |
| | | 61 | | |
| | 727 | 62 | | result = GetUnitIntervalRatio( |
| | 727 | 63 | | numeratorHigh, |
| | 727 | 64 | | numeratorMiddle, |
| | 727 | 65 | | numeratorLow, |
| | 727 | 66 | | denominatorHigh, |
| | 727 | 67 | | denominatorMiddle, |
| | 727 | 68 | | denominatorLow); |
| | 727 | 69 | | return true; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Converts an exact five-word ratio proven by this method to lie in [0, 1] |
| | | 74 | | /// to Q32.32 using guard/sticky round-half-to-even state. |
| | | 75 | | /// </summary> |
| | | 76 | | internal static bool TryGetUnitIntervalRatio( |
| | | 77 | | Signed320 numerator, |
| | | 78 | | Signed320 denominator, |
| | | 79 | | out Fixed64 result) |
| | | 80 | | { |
| | 95 | 81 | | if (Signed192.TryNarrowSigned(numerator, out Signed192 narrowNumerator) |
| | 95 | 82 | | && Signed192.TryNarrowSigned(denominator, out Signed192 narrowDenominator)) |
| | | 83 | | { |
| | 74 | 84 | | return TryGetUnitIntervalRatio(narrowNumerator, narrowDenominator, out result); |
| | | 85 | | } |
| | | 86 | | |
| | 21 | 87 | | int denominatorSign = denominator.Sign; |
| | 21 | 88 | | int numeratorSign = numerator.Sign; |
| | 21 | 89 | | if (denominatorSign == 0 || (numeratorSign != 0 && numeratorSign != denominatorSign)) |
| | | 90 | | { |
| | 4 | 91 | | result = default; |
| | 4 | 92 | | return false; |
| | | 93 | | } |
| | | 94 | | |
| | 17 | 95 | | if (numeratorSign == 0) |
| | | 96 | | { |
| | 1 | 97 | | result = Zero; |
| | 1 | 98 | | return true; |
| | | 99 | | } |
| | | 100 | | |
| | 16 | 101 | | WideArithmetic.GetMagnitude( |
| | 16 | 102 | | numerator, |
| | 16 | 103 | | out ulong numeratorWord4, |
| | 16 | 104 | | out ulong numeratorWord3, |
| | 16 | 105 | | out ulong numeratorWord2, |
| | 16 | 106 | | out ulong numeratorWord1, |
| | 16 | 107 | | out ulong numeratorWord0); |
| | 16 | 108 | | WideArithmetic.GetMagnitude( |
| | 16 | 109 | | denominator, |
| | 16 | 110 | | out ulong denominatorWord4, |
| | 16 | 111 | | out ulong denominatorWord3, |
| | 16 | 112 | | out ulong denominatorWord2, |
| | 16 | 113 | | out ulong denominatorWord1, |
| | 16 | 114 | | out ulong denominatorWord0); |
| | 16 | 115 | | int comparison = WideArithmetic.CompareUnsigned( |
| | 16 | 116 | | numeratorWord4, |
| | 16 | 117 | | numeratorWord3, |
| | 16 | 118 | | numeratorWord2, |
| | 16 | 119 | | numeratorWord1, |
| | 16 | 120 | | numeratorWord0, |
| | 16 | 121 | | denominatorWord4, |
| | 16 | 122 | | denominatorWord3, |
| | 16 | 123 | | denominatorWord2, |
| | 16 | 124 | | denominatorWord1, |
| | 16 | 125 | | denominatorWord0); |
| | 16 | 126 | | if (comparison > 0) |
| | | 127 | | { |
| | 1 | 128 | | result = default; |
| | 1 | 129 | | return false; |
| | | 130 | | } |
| | | 131 | | |
| | 15 | 132 | | if (comparison == 0) |
| | | 133 | | { |
| | 1 | 134 | | result = One; |
| | 1 | 135 | | return true; |
| | | 136 | | } |
| | | 137 | | |
| | 14 | 138 | | result = GetUnitIntervalRatio( |
| | 14 | 139 | | numeratorWord4, |
| | 14 | 140 | | numeratorWord3, |
| | 14 | 141 | | numeratorWord2, |
| | 14 | 142 | | numeratorWord1, |
| | 14 | 143 | | numeratorWord0, |
| | 14 | 144 | | denominatorWord4, |
| | 14 | 145 | | denominatorWord3, |
| | 14 | 146 | | denominatorWord2, |
| | 14 | 147 | | denominatorWord1, |
| | 14 | 148 | | denominatorWord0); |
| | 14 | 149 | | return true; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private static Fixed64 GetUnitIntervalRatio( |
| | | 153 | | ulong numeratorWord4, |
| | | 154 | | ulong numeratorWord3, |
| | | 155 | | ulong numeratorWord2, |
| | | 156 | | ulong numeratorWord1, |
| | | 157 | | ulong numeratorWord0, |
| | | 158 | | ulong denominatorWord4, |
| | | 159 | | ulong denominatorWord3, |
| | | 160 | | ulong denominatorWord2, |
| | | 161 | | ulong denominatorWord1, |
| | | 162 | | ulong denominatorWord0) |
| | | 163 | | { |
| | 333 | 164 | | ulong guardedQuotient = 0UL; |
| | 22644 | 165 | | for (int bit = FixedMath.SHIFT_AMOUNT_I; bit >= 0; bit--) |
| | | 166 | | { |
| | 10989 | 167 | | WideArithmetic.ShiftLeftOne( |
| | 10989 | 168 | | ref numeratorWord4, |
| | 10989 | 169 | | ref numeratorWord3, |
| | 10989 | 170 | | ref numeratorWord2, |
| | 10989 | 171 | | ref numeratorWord1, |
| | 10989 | 172 | | ref numeratorWord0); |
| | 10989 | 173 | | guardedQuotient <<= 1; |
| | 10989 | 174 | | if (WideArithmetic.CompareUnsigned( |
| | 10989 | 175 | | numeratorWord4, |
| | 10989 | 176 | | numeratorWord3, |
| | 10989 | 177 | | numeratorWord2, |
| | 10989 | 178 | | numeratorWord1, |
| | 10989 | 179 | | numeratorWord0, |
| | 10989 | 180 | | denominatorWord4, |
| | 10989 | 181 | | denominatorWord3, |
| | 10989 | 182 | | denominatorWord2, |
| | 10989 | 183 | | denominatorWord1, |
| | 10989 | 184 | | denominatorWord0) < 0) |
| | | 185 | | { |
| | | 186 | | continue; |
| | | 187 | | } |
| | | 188 | | |
| | 5243 | 189 | | WideArithmetic.SubtractUnsigned( |
| | 5243 | 190 | | ref numeratorWord4, |
| | 5243 | 191 | | ref numeratorWord3, |
| | 5243 | 192 | | ref numeratorWord2, |
| | 5243 | 193 | | ref numeratorWord1, |
| | 5243 | 194 | | ref numeratorWord0, |
| | 5243 | 195 | | denominatorWord4, |
| | 5243 | 196 | | denominatorWord3, |
| | 5243 | 197 | | denominatorWord2, |
| | 5243 | 198 | | denominatorWord1, |
| | 5243 | 199 | | denominatorWord0); |
| | 5243 | 200 | | guardedQuotient |= 1UL; |
| | | 201 | | } |
| | | 202 | | |
| | 333 | 203 | | ulong rounded = RoundGuardedQuotientToEven( |
| | 333 | 204 | | guardedQuotient, |
| | 333 | 205 | | (numeratorWord4 | numeratorWord3 | numeratorWord2 | numeratorWord1 | numeratorWord0) != 0UL, |
| | 333 | 206 | | out _); |
| | 333 | 207 | | return new Fixed64((long)rounded); |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 211 | | private static Fixed64 GetUnitIntervalRatio( |
| | | 212 | | ulong numeratorHigh, |
| | | 213 | | ulong numeratorMiddle, |
| | | 214 | | ulong numeratorLow, |
| | | 215 | | ulong denominatorHigh, |
| | | 216 | | ulong denominatorMiddle, |
| | | 217 | | ulong denominatorLow) |
| | | 218 | | { |
| | 1186 | 219 | | if ((numeratorHigh | denominatorHigh) == 0UL) |
| | | 220 | | { |
| | 940 | 221 | | return GetUnitIntervalRatio128( |
| | 940 | 222 | | numeratorMiddle, |
| | 940 | 223 | | numeratorLow, |
| | 940 | 224 | | denominatorMiddle, |
| | 940 | 225 | | denominatorLow); |
| | | 226 | | } |
| | | 227 | | |
| | 246 | 228 | | ulong guardedQuotient = 0UL; |
| | 16728 | 229 | | for (int bit = FixedMath.SHIFT_AMOUNT_I; bit >= 0; bit--) |
| | | 230 | | { |
| | 8118 | 231 | | WideArithmetic.ShiftLeftOne(ref numeratorHigh, ref numeratorMiddle, ref numeratorLow); |
| | 8118 | 232 | | guardedQuotient <<= 1; |
| | 8118 | 233 | | if (WideArithmetic.CompareUnsigned( |
| | 8118 | 234 | | numeratorHigh, |
| | 8118 | 235 | | numeratorMiddle, |
| | 8118 | 236 | | numeratorLow, |
| | 8118 | 237 | | denominatorHigh, |
| | 8118 | 238 | | denominatorMiddle, |
| | 8118 | 239 | | denominatorLow) < 0) |
| | | 240 | | { |
| | | 241 | | continue; |
| | | 242 | | } |
| | | 243 | | |
| | 2991 | 244 | | WideArithmetic.SubtractUnsigned( |
| | 2991 | 245 | | ref numeratorHigh, |
| | 2991 | 246 | | ref numeratorMiddle, |
| | 2991 | 247 | | ref numeratorLow, |
| | 2991 | 248 | | denominatorHigh, |
| | 2991 | 249 | | denominatorMiddle, |
| | 2991 | 250 | | denominatorLow); |
| | 2991 | 251 | | guardedQuotient |= 1UL; |
| | | 252 | | } |
| | | 253 | | |
| | 246 | 254 | | ulong rounded = RoundGuardedQuotientToEven( |
| | 246 | 255 | | guardedQuotient, |
| | 246 | 256 | | (numeratorHigh | numeratorMiddle | numeratorLow) != 0UL, |
| | 246 | 257 | | out _); |
| | 246 | 258 | | return new Fixed64((long)rounded); |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 262 | | private static Fixed64 GetUnitIntervalRatio128( |
| | | 263 | | ulong numeratorHigh, |
| | | 264 | | ulong numeratorLow, |
| | | 265 | | ulong denominatorHigh, |
| | | 266 | | ulong denominatorLow) |
| | | 267 | | { |
| | 940 | 268 | | ulong guardedQuotient = 0UL; |
| | 63920 | 269 | | for (int bit = FixedMath.SHIFT_AMOUNT_I; bit >= 0; bit--) |
| | | 270 | | { |
| | 31020 | 271 | | bool overflow = (numeratorHigh & (1UL << 63)) != 0UL; |
| | 31020 | 272 | | numeratorHigh = (numeratorHigh << 1) | (numeratorLow >> 63); |
| | 31020 | 273 | | numeratorLow <<= 1; |
| | 31020 | 274 | | guardedQuotient <<= 1; |
| | | 275 | | |
| | 31020 | 276 | | if (!overflow |
| | 31020 | 277 | | && (numeratorHigh < denominatorHigh |
| | 31020 | 278 | | || (numeratorHigh == denominatorHigh && numeratorLow < denominatorLow))) |
| | | 279 | | { |
| | | 280 | | continue; |
| | | 281 | | } |
| | | 282 | | |
| | 11431 | 283 | | ulong previousLow = numeratorLow; |
| | 11431 | 284 | | numeratorLow = unchecked(numeratorLow - denominatorLow); |
| | 11431 | 285 | | ulong borrow = previousLow < denominatorLow ? 1UL : 0UL; |
| | 11431 | 286 | | numeratorHigh = unchecked(numeratorHigh - denominatorHigh - borrow); |
| | 11431 | 287 | | guardedQuotient |= 1UL; |
| | | 288 | | } |
| | | 289 | | |
| | 940 | 290 | | ulong rounded = RoundGuardedQuotientToEven( |
| | 940 | 291 | | guardedQuotient, |
| | 940 | 292 | | (numeratorHigh | numeratorLow) != 0UL, |
| | 940 | 293 | | out _); |
| | 940 | 294 | | return new Fixed64((long)rounded); |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | /// <summary> |
| | | 298 | | /// Computes the ratio of two non-negative wide integers, returning a Fixed64 value in the unit interval [0, 1]. |
| | | 299 | | /// </summary> |
| | | 300 | | internal static Fixed64 GetUnitIntervalRatio( |
| | | 301 | | ReadOnlySpan<ulong> numerator, |
| | | 302 | | ReadOnlySpan<ulong> denominator) |
| | | 303 | | { |
| | 161 | 304 | | int length = Math.Max(numerator.Length, denominator.Length) + 1; |
| | 161 | 305 | | Span<ulong> numeratorMagnitude = stackalloc ulong[length]; |
| | 161 | 306 | | Span<ulong> remainder = stackalloc ulong[length]; |
| | 161 | 307 | | Span<ulong> denominatorMagnitude = stackalloc ulong[length]; |
| | 161 | 308 | | numeratorMagnitude.Clear(); |
| | 161 | 309 | | remainder.Clear(); |
| | 161 | 310 | | denominatorMagnitude.Clear(); |
| | 161 | 311 | | numerator.CopyTo(numeratorMagnitude); |
| | 161 | 312 | | denominator.CopyTo(denominatorMagnitude); |
| | 161 | 313 | | ShiftLeftMagnitude( |
| | 161 | 314 | | numeratorMagnitude, |
| | 161 | 315 | | FixedMath.SHIFT_AMOUNT_I, |
| | 161 | 316 | | remainder); |
| | 161 | 317 | | _ = TryGetSignedRawRatioCore( |
| | 161 | 318 | | remainder, |
| | 161 | 319 | | denominatorMagnitude, |
| | 161 | 320 | | negative: false, |
| | 161 | 321 | | roundToEven: true, |
| | 161 | 322 | | out Fixed64 result); |
| | 161 | 323 | | return result; |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Converts an arbitrary nonzero signed wide ratio to Q32.32 with |
| | | 328 | | /// round-half-to-even and signed saturation. |
| | | 329 | | /// </summary> |
| | | 330 | | internal static Fixed64 GetSignedRatio(Signed192 numerator, Signed192 denominator) |
| | | 331 | | { |
| | 1694 | 332 | | int numeratorSign = numerator.Sign; |
| | 1694 | 333 | | int denominatorSign = denominator.Sign; |
| | 1694 | 334 | | if (numeratorSign == 0) |
| | 220 | 335 | | return Zero; |
| | 1474 | 336 | | if (denominatorSign == 0) |
| | 2 | 337 | | return numeratorSign < 0 ? MinValue : MaxValue; |
| | | 338 | | |
| | 1472 | 339 | | bool negative = numeratorSign != denominatorSign; |
| | 1472 | 340 | | WideArithmetic.GetMagnitude(numerator, out ulong numeratorHigh, out ulong numeratorMiddle, out ulong numeratorLo |
| | 1472 | 341 | | WideArithmetic.GetMagnitude(denominator, out ulong denominatorHigh, out ulong denominatorMiddle, out ulong denom |
| | | 342 | | |
| | 1472 | 343 | | if (!negative) |
| | | 344 | | { |
| | 1048 | 345 | | int comparison = WideArithmetic.CompareUnsigned( |
| | 1048 | 346 | | numeratorHigh, numeratorMiddle, numeratorLow, |
| | 1048 | 347 | | denominatorHigh, denominatorMiddle, denominatorLow); |
| | 1048 | 348 | | if (comparison <= 0) |
| | | 349 | | { |
| | 691 | 350 | | return comparison == 0 |
| | 691 | 351 | | ? One |
| | 691 | 352 | | : GetUnitIntervalRatio( |
| | 691 | 353 | | numeratorHigh, numeratorMiddle, numeratorLow, |
| | 691 | 354 | | denominatorHigh, denominatorMiddle, denominatorLow); |
| | | 355 | | } |
| | | 356 | | } |
| | | 357 | | |
| | 781 | 358 | | int numeratorBits = WideArithmetic.GetBitLength(numeratorHigh, numeratorMiddle, numeratorLow); |
| | 781 | 359 | | int denominatorBits = WideArithmetic.GetBitLength(denominatorHigh, denominatorMiddle, denominatorLow); |
| | 781 | 360 | | int integerShift = numeratorBits - denominatorBits; |
| | 781 | 361 | | if (integerShift >= 31) |
| | | 362 | | { |
| | 15 | 363 | | if (integerShift > 31) |
| | 8 | 364 | | return negative ? MinValue : MaxValue; |
| | | 365 | | |
| | 7 | 366 | | WideArithmetic.ShiftLeft( |
| | 7 | 367 | | denominatorHigh, |
| | 7 | 368 | | denominatorMiddle, |
| | 7 | 369 | | denominatorLow, |
| | 7 | 370 | | 31, |
| | 7 | 371 | | out ulong limitHigh, |
| | 7 | 372 | | out ulong limitMiddle, |
| | 7 | 373 | | out ulong limitLow); |
| | 7 | 374 | | int limitComparison = WideArithmetic.CompareUnsigned( |
| | 7 | 375 | | numeratorHigh, numeratorMiddle, numeratorLow, |
| | 7 | 376 | | limitHigh, limitMiddle, limitLow); |
| | 7 | 377 | | if (limitComparison > 0 || (limitComparison == 0 && !negative)) |
| | 4 | 378 | | return negative ? MinValue : MaxValue; |
| | 3 | 379 | | if (limitComparison == 0) |
| | 2 | 380 | | return MinValue; |
| | | 381 | | } |
| | | 382 | | |
| | 767 | 383 | | ulong guardedQuotient = 0UL; |
| | 767 | 384 | | if (integerShift >= 0) |
| | | 385 | | { |
| | 616 | 386 | | WideArithmetic.ShiftLeft( |
| | 616 | 387 | | denominatorHigh, |
| | 616 | 388 | | denominatorMiddle, |
| | 616 | 389 | | denominatorLow, |
| | 616 | 390 | | integerShift, |
| | 616 | 391 | | out ulong shiftedHigh, |
| | 616 | 392 | | out ulong shiftedMiddle, |
| | 616 | 393 | | out ulong shiftedLow); |
| | 5812 | 394 | | for (int bit = integerShift; bit >= 0; bit--) |
| | | 395 | | { |
| | 2290 | 396 | | if (WideArithmetic.CompareUnsigned( |
| | 2290 | 397 | | numeratorHigh, numeratorMiddle, numeratorLow, |
| | 2290 | 398 | | shiftedHigh, shiftedMiddle, shiftedLow) >= 0) |
| | | 399 | | { |
| | 1378 | 400 | | WideArithmetic.SubtractUnsigned( |
| | 1378 | 401 | | ref numeratorHigh, |
| | 1378 | 402 | | ref numeratorMiddle, |
| | 1378 | 403 | | ref numeratorLow, |
| | 1378 | 404 | | shiftedHigh, |
| | 1378 | 405 | | shiftedMiddle, |
| | 1378 | 406 | | shiftedLow); |
| | 1378 | 407 | | guardedQuotient |= 1UL << bit; |
| | | 408 | | } |
| | | 409 | | |
| | 2290 | 410 | | WideArithmetic.ShiftRightOne(ref shiftedHigh, ref shiftedMiddle, ref shiftedLow); |
| | | 411 | | } |
| | | 412 | | } |
| | | 413 | | |
| | 52156 | 414 | | for (int bit = FixedMath.SHIFT_AMOUNT_I; bit >= 0; bit--) |
| | | 415 | | { |
| | 25311 | 416 | | WideArithmetic.ShiftLeftOne(ref numeratorHigh, ref numeratorMiddle, ref numeratorLow); |
| | 25311 | 417 | | guardedQuotient <<= 1; |
| | 25311 | 418 | | if (WideArithmetic.CompareUnsigned( |
| | 25311 | 419 | | numeratorHigh, numeratorMiddle, numeratorLow, |
| | 25311 | 420 | | denominatorHigh, denominatorMiddle, denominatorLow) < 0) |
| | | 421 | | { |
| | | 422 | | continue; |
| | | 423 | | } |
| | | 424 | | |
| | 7830 | 425 | | WideArithmetic.SubtractUnsigned( |
| | 7830 | 426 | | ref numeratorHigh, |
| | 7830 | 427 | | ref numeratorMiddle, |
| | 7830 | 428 | | ref numeratorLow, |
| | 7830 | 429 | | denominatorHigh, |
| | 7830 | 430 | | denominatorMiddle, |
| | 7830 | 431 | | denominatorLow); |
| | 7830 | 432 | | guardedQuotient |= 1UL; |
| | | 433 | | } |
| | | 434 | | |
| | 767 | 435 | | ulong magnitude = RoundGuardedQuotientToEven( |
| | 767 | 436 | | guardedQuotient, |
| | 767 | 437 | | (numeratorHigh | numeratorMiddle | numeratorLow) != 0UL, |
| | 767 | 438 | | out bool roundedOverflow); |
| | 767 | 439 | | ulong limit = negative ? 1UL << 63 : (ulong)long.MaxValue; |
| | 767 | 440 | | if (roundedOverflow || magnitude > limit) |
| | 3 | 441 | | return negative ? MinValue : MaxValue; |
| | | 442 | | |
| | 764 | 443 | | long raw = negative ? unchecked(-(long)magnitude) : (long)magnitude; |
| | 764 | 444 | | return new Fixed64(raw); |
| | | 445 | | } |
| | | 446 | | |
| | | 447 | | /// <summary> |
| | | 448 | | /// Converts an arbitrary nonzero signed five-word ratio to Q32.32 with |
| | | 449 | | /// round-half-to-even and signed saturation. |
| | | 450 | | /// </summary> |
| | | 451 | | internal static Fixed64 GetSignedRatio(Signed320 numerator, Signed320 denominator) |
| | | 452 | | { |
| | 1104 | 453 | | if (Signed192.TryNarrowSigned(numerator, out Signed192 narrowNumerator) |
| | 1104 | 454 | | && Signed192.TryNarrowSigned(denominator, out Signed192 narrowDenominator)) |
| | | 455 | | { |
| | 675 | 456 | | return GetSignedRatio(narrowNumerator, narrowDenominator); |
| | | 457 | | } |
| | | 458 | | |
| | 429 | 459 | | int numeratorSign = numerator.Sign; |
| | 429 | 460 | | int denominatorSign = denominator.Sign; |
| | 429 | 461 | | if (numeratorSign == 0) |
| | 47 | 462 | | return Zero; |
| | 382 | 463 | | if (denominatorSign == 0) |
| | 2 | 464 | | return numeratorSign < 0 ? MinValue : MaxValue; |
| | | 465 | | |
| | 380 | 466 | | bool negative = numeratorSign != denominatorSign; |
| | 380 | 467 | | WideArithmetic.GetMagnitude( |
| | 380 | 468 | | numerator, |
| | 380 | 469 | | out ulong numeratorWord4, |
| | 380 | 470 | | out ulong numeratorWord3, |
| | 380 | 471 | | out ulong numeratorWord2, |
| | 380 | 472 | | out ulong numeratorWord1, |
| | 380 | 473 | | out ulong numeratorWord0); |
| | 380 | 474 | | WideArithmetic.GetMagnitude( |
| | 380 | 475 | | denominator, |
| | 380 | 476 | | out ulong denominatorWord4, |
| | 380 | 477 | | out ulong denominatorWord3, |
| | 380 | 478 | | out ulong denominatorWord2, |
| | 380 | 479 | | out ulong denominatorWord1, |
| | 380 | 480 | | out ulong denominatorWord0); |
| | | 481 | | |
| | 380 | 482 | | int comparison = WideArithmetic.CompareUnsigned( |
| | 380 | 483 | | numeratorWord4, |
| | 380 | 484 | | numeratorWord3, |
| | 380 | 485 | | numeratorWord2, |
| | 380 | 486 | | numeratorWord1, |
| | 380 | 487 | | numeratorWord0, |
| | 380 | 488 | | denominatorWord4, |
| | 380 | 489 | | denominatorWord3, |
| | 380 | 490 | | denominatorWord2, |
| | 380 | 491 | | denominatorWord1, |
| | 380 | 492 | | denominatorWord0); |
| | 380 | 493 | | if (comparison <= 0) |
| | | 494 | | { |
| | 366 | 495 | | if (comparison == 0) |
| | 47 | 496 | | return negative ? -One : One; |
| | | 497 | | |
| | 319 | 498 | | Fixed64 unitRatio = GetUnitIntervalRatio( |
| | 319 | 499 | | numeratorWord4, |
| | 319 | 500 | | numeratorWord3, |
| | 319 | 501 | | numeratorWord2, |
| | 319 | 502 | | numeratorWord1, |
| | 319 | 503 | | numeratorWord0, |
| | 319 | 504 | | denominatorWord4, |
| | 319 | 505 | | denominatorWord3, |
| | 319 | 506 | | denominatorWord2, |
| | 319 | 507 | | denominatorWord1, |
| | 319 | 508 | | denominatorWord0); |
| | 319 | 509 | | return negative ? -unitRatio : unitRatio; |
| | | 510 | | } |
| | | 511 | | |
| | 14 | 512 | | int numeratorBits = WideArithmetic.GetBitLength( |
| | 14 | 513 | | numeratorWord4, numeratorWord3, numeratorWord2, numeratorWord1, numeratorWord0); |
| | 14 | 514 | | int denominatorBits = WideArithmetic.GetBitLength( |
| | 14 | 515 | | denominatorWord4, denominatorWord3, denominatorWord2, denominatorWord1, denominatorWord0); |
| | 14 | 516 | | int integerShift = numeratorBits - denominatorBits; |
| | 14 | 517 | | if (integerShift >= 31) |
| | | 518 | | { |
| | 9 | 519 | | if (integerShift > 31) |
| | 2 | 520 | | return negative ? MinValue : MaxValue; |
| | | 521 | | |
| | 7 | 522 | | WideArithmetic.ShiftLeft( |
| | 7 | 523 | | denominatorWord4, |
| | 7 | 524 | | denominatorWord3, |
| | 7 | 525 | | denominatorWord2, |
| | 7 | 526 | | denominatorWord1, |
| | 7 | 527 | | denominatorWord0, |
| | 7 | 528 | | 31, |
| | 7 | 529 | | out ulong limitWord4, |
| | 7 | 530 | | out ulong limitWord3, |
| | 7 | 531 | | out ulong limitWord2, |
| | 7 | 532 | | out ulong limitWord1, |
| | 7 | 533 | | out ulong limitWord0); |
| | 7 | 534 | | int limitComparison = WideArithmetic.CompareUnsigned( |
| | 7 | 535 | | numeratorWord4, |
| | 7 | 536 | | numeratorWord3, |
| | 7 | 537 | | numeratorWord2, |
| | 7 | 538 | | numeratorWord1, |
| | 7 | 539 | | numeratorWord0, |
| | 7 | 540 | | limitWord4, |
| | 7 | 541 | | limitWord3, |
| | 7 | 542 | | limitWord2, |
| | 7 | 543 | | limitWord1, |
| | 7 | 544 | | limitWord0); |
| | 7 | 545 | | if (limitComparison > 0 || (limitComparison == 0 && !negative)) |
| | 4 | 546 | | return negative ? MinValue : MaxValue; |
| | 3 | 547 | | if (limitComparison == 0) |
| | 2 | 548 | | return MinValue; |
| | | 549 | | } |
| | | 550 | | |
| | 6 | 551 | | ulong guardedQuotient = 0UL; |
| | 6 | 552 | | WideArithmetic.ShiftLeft( |
| | 6 | 553 | | denominatorWord4, |
| | 6 | 554 | | denominatorWord3, |
| | 6 | 555 | | denominatorWord2, |
| | 6 | 556 | | denominatorWord1, |
| | 6 | 557 | | denominatorWord0, |
| | 6 | 558 | | integerShift, |
| | 6 | 559 | | out ulong shiftedWord4, |
| | 6 | 560 | | out ulong shiftedWord3, |
| | 6 | 561 | | out ulong shiftedWord2, |
| | 6 | 562 | | out ulong shiftedWord1, |
| | 6 | 563 | | out ulong shiftedWord0); |
| | 210 | 564 | | for (int bit = integerShift; bit >= 0; bit--) |
| | | 565 | | { |
| | 99 | 566 | | if (WideArithmetic.CompareUnsigned( |
| | 99 | 567 | | numeratorWord4, |
| | 99 | 568 | | numeratorWord3, |
| | 99 | 569 | | numeratorWord2, |
| | 99 | 570 | | numeratorWord1, |
| | 99 | 571 | | numeratorWord0, |
| | 99 | 572 | | shiftedWord4, |
| | 99 | 573 | | shiftedWord3, |
| | 99 | 574 | | shiftedWord2, |
| | 99 | 575 | | shiftedWord1, |
| | 99 | 576 | | shiftedWord0) >= 0) |
| | | 577 | | { |
| | 96 | 578 | | WideArithmetic.SubtractUnsigned( |
| | 96 | 579 | | ref numeratorWord4, |
| | 96 | 580 | | ref numeratorWord3, |
| | 96 | 581 | | ref numeratorWord2, |
| | 96 | 582 | | ref numeratorWord1, |
| | 96 | 583 | | ref numeratorWord0, |
| | 96 | 584 | | shiftedWord4, |
| | 96 | 585 | | shiftedWord3, |
| | 96 | 586 | | shiftedWord2, |
| | 96 | 587 | | shiftedWord1, |
| | 96 | 588 | | shiftedWord0); |
| | 96 | 589 | | guardedQuotient |= 1UL << bit; |
| | | 590 | | } |
| | | 591 | | |
| | 99 | 592 | | WideArithmetic.ShiftRightOne( |
| | 99 | 593 | | ref shiftedWord4, |
| | 99 | 594 | | ref shiftedWord3, |
| | 99 | 595 | | ref shiftedWord2, |
| | 99 | 596 | | ref shiftedWord1, |
| | 99 | 597 | | ref shiftedWord0); |
| | | 598 | | } |
| | | 599 | | |
| | 408 | 600 | | for (int bit = FixedMath.SHIFT_AMOUNT_I; bit >= 0; bit--) |
| | | 601 | | { |
| | 198 | 602 | | WideArithmetic.ShiftLeftOne( |
| | 198 | 603 | | ref numeratorWord4, |
| | 198 | 604 | | ref numeratorWord3, |
| | 198 | 605 | | ref numeratorWord2, |
| | 198 | 606 | | ref numeratorWord1, |
| | 198 | 607 | | ref numeratorWord0); |
| | 198 | 608 | | guardedQuotient <<= 1; |
| | 198 | 609 | | if (WideArithmetic.CompareUnsigned( |
| | 198 | 610 | | numeratorWord4, |
| | 198 | 611 | | numeratorWord3, |
| | 198 | 612 | | numeratorWord2, |
| | 198 | 613 | | numeratorWord1, |
| | 198 | 614 | | numeratorWord0, |
| | 198 | 615 | | denominatorWord4, |
| | 198 | 616 | | denominatorWord3, |
| | 198 | 617 | | denominatorWord2, |
| | 198 | 618 | | denominatorWord1, |
| | 198 | 619 | | denominatorWord0) < 0) |
| | | 620 | | { |
| | | 621 | | continue; |
| | | 622 | | } |
| | | 623 | | |
| | 101 | 624 | | WideArithmetic.SubtractUnsigned( |
| | 101 | 625 | | ref numeratorWord4, |
| | 101 | 626 | | ref numeratorWord3, |
| | 101 | 627 | | ref numeratorWord2, |
| | 101 | 628 | | ref numeratorWord1, |
| | 101 | 629 | | ref numeratorWord0, |
| | 101 | 630 | | denominatorWord4, |
| | 101 | 631 | | denominatorWord3, |
| | 101 | 632 | | denominatorWord2, |
| | 101 | 633 | | denominatorWord1, |
| | 101 | 634 | | denominatorWord0); |
| | 101 | 635 | | guardedQuotient |= 1UL; |
| | | 636 | | } |
| | | 637 | | |
| | 6 | 638 | | ulong magnitude = RoundGuardedQuotientToEven( |
| | 6 | 639 | | guardedQuotient, |
| | 6 | 640 | | (numeratorWord4 | numeratorWord3 | numeratorWord2 | numeratorWord1 | numeratorWord0) != 0UL, |
| | 6 | 641 | | out bool roundedOverflow); |
| | 6 | 642 | | ulong limit = negative ? 1UL << 63 : (ulong)long.MaxValue; |
| | 6 | 643 | | if (roundedOverflow || magnitude > limit) |
| | 3 | 644 | | return negative ? MinValue : MaxValue; |
| | | 645 | | |
| | 3 | 646 | | long raw = negative ? unchecked(-(long)magnitude) : (long)magnitude; |
| | 3 | 647 | | return new Fixed64(raw); |
| | | 648 | | } |
| | | 649 | | |
| | | 650 | | /// <summary> |
| | | 651 | | /// Converts an arbitrary signed five-word ratio to Q32.32 when its final |
| | | 652 | | /// round-half-to-even result is representable. |
| | | 653 | | /// </summary> |
| | | 654 | | internal static bool TryGetSignedRatio( |
| | | 655 | | Signed320 numerator, |
| | | 656 | | Signed320 denominator, |
| | | 657 | | out Fixed64 result) |
| | | 658 | | { |
| | 87 | 659 | | int numeratorSign = numerator.Sign; |
| | 87 | 660 | | int denominatorSign = denominator.Sign; |
| | 87 | 661 | | if (denominatorSign == 0) |
| | | 662 | | { |
| | 1 | 663 | | result = default; |
| | 1 | 664 | | return false; |
| | | 665 | | } |
| | 86 | 666 | | if (numeratorSign == 0) |
| | | 667 | | { |
| | 23 | 668 | | result = Zero; |
| | 23 | 669 | | return true; |
| | | 670 | | } |
| | | 671 | | |
| | 63 | 672 | | WideArithmetic.GetMagnitude( |
| | 63 | 673 | | numerator, |
| | 63 | 674 | | out ulong numeratorWord4, |
| | 63 | 675 | | out ulong numeratorWord3, |
| | 63 | 676 | | out ulong numeratorWord2, |
| | 63 | 677 | | out ulong numeratorWord1, |
| | 63 | 678 | | out ulong numeratorWord0); |
| | 63 | 679 | | WideArithmetic.GetMagnitude( |
| | 63 | 680 | | denominator, |
| | 63 | 681 | | out ulong denominatorWord4, |
| | 63 | 682 | | out ulong denominatorWord3, |
| | 63 | 683 | | out ulong denominatorWord2, |
| | 63 | 684 | | out ulong denominatorWord1, |
| | 63 | 685 | | out ulong denominatorWord0); |
| | 63 | 686 | | Signed320 numeratorMagnitude = new( |
| | 63 | 687 | | numeratorWord4, numeratorWord3, numeratorWord2, numeratorWord1, numeratorWord0); |
| | 63 | 688 | | Signed320 denominatorMagnitude = new( |
| | 63 | 689 | | denominatorWord4, denominatorWord3, denominatorWord2, denominatorWord1, denominatorWord0); |
| | 63 | 690 | | Signed320 twiceScale = Signed320.ExtendValue( |
| | 63 | 691 | | Signed192.Signed(FixedMath.ONE_L * 2L)); |
| | 63 | 692 | | bool negative = numeratorSign != denominatorSign; |
| | 63 | 693 | | Signed192 roundedLimit = negative |
| | 63 | 694 | | ? new Signed192(0UL, 1UL, 1UL) // 2 * 2^63 + 1 |
| | 63 | 695 | | : new Signed192(0UL, 0UL, ulong.MaxValue); // 2 * (2^63 - 1) + 1 |
| | 63 | 696 | | Signed576 scaledNumerator = GetNonNegativeProduct(numeratorMagnitude, twiceScale); |
| | 63 | 697 | | Signed576 scaledLimit = GetNonNegativeProduct( |
| | 63 | 698 | | denominatorMagnitude, |
| | 63 | 699 | | Signed320.ExtendValue(roundedLimit)); |
| | 63 | 700 | | int comparison = WideArithmetic.CompareNonNegative(scaledNumerator, scaledLimit); |
| | 63 | 701 | | if (comparison > 0 || (!negative && comparison == 0)) |
| | | 702 | | { |
| | 18 | 703 | | result = default; |
| | 18 | 704 | | return false; |
| | | 705 | | } |
| | | 706 | | |
| | 45 | 707 | | result = GetSignedRatio(numerator, denominator); |
| | 45 | 708 | | return true; |
| | | 709 | | } |
| | | 710 | | |
| | | 711 | | private static Signed576 GetNonNegativeProduct(Signed320 leftMagnitude, Signed320 right) |
| | | 712 | | { |
| | 126 | 713 | | Signed576 product = WideArithmetic.MultiplySigned320(leftMagnitude, right); |
| | 126 | 714 | | return product.Sign < 0 |
| | 126 | 715 | | ? WideArithmetic.SubtractSigned576(default, product) |
| | 126 | 716 | | : product; |
| | | 717 | | } |
| | | 718 | | |
| | | 719 | | /// <summary> |
| | | 720 | | /// Converts an exact signed five-word ratio with a positive single-word |
| | | 721 | | /// denominator to a raw integer with round-half-to-even. |
| | | 722 | | /// </summary> |
| | | 723 | | /// <remarks> |
| | | 724 | | /// The caller owns the invariant that the quotient is representable. |
| | | 725 | | /// </remarks> |
| | | 726 | | internal static Fixed64 GetSignedRawRatio(Signed320 numerator, Signed192 denominator) |
| | | 727 | | { |
| | 2503 | 728 | | int numeratorSign = numerator.Sign; |
| | 2503 | 729 | | if (numeratorSign == 0) |
| | 200 | 730 | | return Zero; |
| | | 731 | | |
| | 2303 | 732 | | WideArithmetic.GetMagnitude( |
| | 2303 | 733 | | denominator, |
| | 2303 | 734 | | out _, |
| | 2303 | 735 | | out _, |
| | 2303 | 736 | | out ulong denominatorLow); |
| | 2303 | 737 | | WideArithmetic.GetMagnitude( |
| | 2303 | 738 | | numerator, |
| | 2303 | 739 | | out _, |
| | 2303 | 740 | | out _, |
| | 2303 | 741 | | out _, |
| | 2303 | 742 | | out ulong word1, |
| | 2303 | 743 | | out ulong word0); |
| | | 744 | | |
| | 2303 | 745 | | ulong quotient = Divide128By64( |
| | 2303 | 746 | | word1, |
| | 2303 | 747 | | word0, |
| | 2303 | 748 | | denominatorLow, |
| | 2303 | 749 | | out ulong remainder); |
| | 2303 | 750 | | int midpointComparison = remainder.CompareTo(denominatorLow - remainder); |
| | 2303 | 751 | | if (midpointComparison > 0 || (midpointComparison == 0 && (quotient & 1UL) != 0UL)) |
| | 629 | 752 | | quotient++; |
| | | 753 | | |
| | 2303 | 754 | | long raw = numeratorSign < 0 ? unchecked(-(long)quotient) : (long)quotient; |
| | 2303 | 755 | | return new Fixed64(raw); |
| | | 756 | | } |
| | | 757 | | |
| | | 758 | | /// <summary> |
| | | 759 | | /// Converts an exact signed nine-word ratio directly to a raw integer with |
| | | 760 | | /// round-half-to-even. Unlike <c>GetSignedRatio</c>, this method does not |
| | | 761 | | /// apply an additional Q32.32 scale. |
| | | 762 | | /// </summary> |
| | | 763 | | internal static bool TryGetSignedRawRatio( |
| | | 764 | | Signed576 numerator, |
| | | 765 | | Signed576 denominator, |
| | | 766 | | out Fixed64 result) |
| | | 767 | | { |
| | 95930 | 768 | | int numeratorSign = numerator.Sign; |
| | 95930 | 769 | | int denominatorSign = denominator.Sign; |
| | 95930 | 770 | | if (denominatorSign == 0) |
| | | 771 | | { |
| | 3 | 772 | | result = default; |
| | 3 | 773 | | return false; |
| | | 774 | | } |
| | 95927 | 775 | | if (numeratorSign == 0) |
| | | 776 | | { |
| | 21708 | 777 | | result = Zero; |
| | 21708 | 778 | | return true; |
| | | 779 | | } |
| | | 780 | | |
| | 74219 | 781 | | bool negative = numeratorSign != denominatorSign; |
| | 74219 | 782 | | if (Signed192.TryNarrowSigned(numerator, out Signed192 numerator192) |
| | 74219 | 783 | | && Signed192.TryNarrowSigned(denominator, out Signed192 denominator192)) |
| | | 784 | | { |
| | 69669 | 785 | | Span<ulong> narrowRemainder = stackalloc ulong[3]; |
| | 69669 | 786 | | Span<ulong> narrowDenominator = stackalloc ulong[3]; |
| | 69669 | 787 | | WideArithmetic.GetMagnitude( |
| | 69669 | 788 | | numerator192, |
| | 69669 | 789 | | out narrowRemainder[2], out narrowRemainder[1], out narrowRemainder[0]); |
| | 69669 | 790 | | WideArithmetic.GetMagnitude( |
| | 69669 | 791 | | denominator192, |
| | 69669 | 792 | | out narrowDenominator[2], out narrowDenominator[1], out narrowDenominator[0]); |
| | 69669 | 793 | | return TryGetSignedRawRatioCore( |
| | 69669 | 794 | | narrowRemainder, |
| | 69669 | 795 | | narrowDenominator, |
| | 69669 | 796 | | negative, |
| | 69669 | 797 | | roundToEven: true, |
| | 69669 | 798 | | out result); |
| | | 799 | | } |
| | 4550 | 800 | | if (Signed320.TryNarrowSigned(numerator, out Signed320 numerator320) |
| | 4550 | 801 | | && Signed320.TryNarrowSigned(denominator, out Signed320 denominator320)) |
| | | 802 | | { |
| | 4421 | 803 | | Span<ulong> mediumRemainder = stackalloc ulong[5]; |
| | 4421 | 804 | | Span<ulong> mediumDenominator = stackalloc ulong[5]; |
| | 4421 | 805 | | WideArithmetic.GetMagnitude( |
| | 4421 | 806 | | numerator320, |
| | 4421 | 807 | | out mediumRemainder[4], out mediumRemainder[3], out mediumRemainder[2], |
| | 4421 | 808 | | out mediumRemainder[1], out mediumRemainder[0]); |
| | 4421 | 809 | | WideArithmetic.GetMagnitude( |
| | 4421 | 810 | | denominator320, |
| | 4421 | 811 | | out mediumDenominator[4], out mediumDenominator[3], out mediumDenominator[2], |
| | 4421 | 812 | | out mediumDenominator[1], out mediumDenominator[0]); |
| | 4421 | 813 | | return TryGetSignedRawRatioCore( |
| | 4421 | 814 | | mediumRemainder, |
| | 4421 | 815 | | mediumDenominator, |
| | 4421 | 816 | | negative, |
| | 4421 | 817 | | roundToEven: true, |
| | 4421 | 818 | | out result); |
| | | 819 | | } |
| | | 820 | | |
| | 129 | 821 | | Span<ulong> remainder = stackalloc ulong[9]; |
| | 129 | 822 | | Span<ulong> denominatorMagnitude = stackalloc ulong[9]; |
| | 129 | 823 | | WideArithmetic.GetMagnitude(numerator, remainder); |
| | 129 | 824 | | WideArithmetic.GetMagnitude(denominator, denominatorMagnitude); |
| | 129 | 825 | | return TryGetSignedRawRatioCore( |
| | 129 | 826 | | remainder, |
| | 129 | 827 | | denominatorMagnitude, |
| | 129 | 828 | | negative, |
| | 129 | 829 | | roundToEven: true, |
| | 129 | 830 | | out result); |
| | | 831 | | } |
| | | 832 | | |
| | | 833 | | /// <summary> |
| | | 834 | | /// Converts an exact signed eleven-word ratio directly to a raw integer |
| | | 835 | | /// with round-half-to-even. |
| | | 836 | | /// </summary> |
| | | 837 | | internal static bool TryGetSignedRawRatio( |
| | | 838 | | Signed704 numerator, |
| | | 839 | | Signed704 denominator, |
| | | 840 | | out Fixed64 result) |
| | | 841 | | { |
| | 34012 | 842 | | int numeratorSign = numerator.Sign; |
| | 34012 | 843 | | int denominatorSign = denominator.Sign; |
| | 34012 | 844 | | if (denominatorSign == 0) |
| | | 845 | | { |
| | 1 | 846 | | result = default; |
| | 1 | 847 | | return false; |
| | | 848 | | } |
| | 34011 | 849 | | if (numeratorSign == 0) |
| | | 850 | | { |
| | 3905 | 851 | | result = Zero; |
| | 3905 | 852 | | return true; |
| | | 853 | | } |
| | | 854 | | |
| | 30106 | 855 | | Span<ulong> remainder = stackalloc ulong[11]; |
| | 30106 | 856 | | Span<ulong> denominatorMagnitude = stackalloc ulong[11]; |
| | 30106 | 857 | | WideArithmetic.GetMagnitude(numerator, remainder); |
| | 30106 | 858 | | WideArithmetic.GetMagnitude(denominator, denominatorMagnitude); |
| | 30106 | 859 | | return TryGetSignedRawRatioCore( |
| | 30106 | 860 | | remainder, |
| | 30106 | 861 | | denominatorMagnitude, |
| | 30106 | 862 | | numeratorSign != denominatorSign, |
| | 30106 | 863 | | roundToEven: true, |
| | 30106 | 864 | | out result); |
| | | 865 | | } |
| | | 866 | | |
| | | 867 | | internal static bool TryGetSignedRawRatio( |
| | | 868 | | Signed832 numerator, |
| | | 869 | | Signed832 denominator, |
| | | 870 | | int numeratorLeftShift, |
| | | 871 | | out Fixed64 result) |
| | | 872 | | { |
| | 592 | 873 | | int numeratorSign = numerator.Sign; |
| | 592 | 874 | | int denominatorSign = denominator.Sign; |
| | 592 | 875 | | if (denominatorSign == 0) |
| | | 876 | | { |
| | 1 | 877 | | result = default; |
| | 1 | 878 | | return false; |
| | | 879 | | } |
| | 591 | 880 | | if (numeratorSign == 0) |
| | | 881 | | { |
| | 10 | 882 | | result = Zero; |
| | 10 | 883 | | return true; |
| | | 884 | | } |
| | | 885 | | |
| | 581 | 886 | | Span<ulong> numeratorMagnitude = stackalloc ulong[14]; |
| | 581 | 887 | | Span<ulong> denominatorMagnitude = stackalloc ulong[14]; |
| | 581 | 888 | | Span<ulong> shiftedNumerator = stackalloc ulong[14]; |
| | 581 | 889 | | numeratorMagnitude.Clear(); |
| | 581 | 890 | | denominatorMagnitude.Clear(); |
| | 581 | 891 | | shiftedNumerator.Clear(); |
| | 581 | 892 | | WideArithmetic.GetMagnitude(numerator, numeratorMagnitude); |
| | 581 | 893 | | WideArithmetic.GetMagnitude(denominator, denominatorMagnitude); |
| | 581 | 894 | | ShiftLeftMagnitude( |
| | 581 | 895 | | numeratorMagnitude, |
| | 581 | 896 | | numeratorLeftShift, |
| | 581 | 897 | | shiftedNumerator); |
| | 581 | 898 | | return TryGetSignedRawRatioCore( |
| | 581 | 899 | | shiftedNumerator, |
| | 581 | 900 | | denominatorMagnitude, |
| | 581 | 901 | | numeratorSign != denominatorSign, |
| | 581 | 902 | | roundToEven: true, |
| | 581 | 903 | | out result); |
| | | 904 | | } |
| | | 905 | | |
| | | 906 | | internal static Fixed64 GetNonNegativeRawRatioFloor( |
| | | 907 | | Signed704 numerator, |
| | | 908 | | Signed704 denominator) |
| | | 909 | | { |
| | 6378 | 910 | | if (numerator.Sign <= 0) |
| | 1679 | 911 | | return Zero; |
| | | 912 | | |
| | 4699 | 913 | | Span<ulong> remainder = stackalloc ulong[11]; |
| | 4699 | 914 | | Span<ulong> denominatorMagnitude = stackalloc ulong[11]; |
| | 4699 | 915 | | WideArithmetic.GetMagnitude(numerator, remainder); |
| | 4699 | 916 | | WideArithmetic.GetMagnitude(denominator, denominatorMagnitude); |
| | 4699 | 917 | | return TryGetSignedRawRatioCore( |
| | 4699 | 918 | | remainder, |
| | 4699 | 919 | | denominatorMagnitude, |
| | 4699 | 920 | | negative: false, |
| | 4699 | 921 | | roundToEven: false, |
| | 4699 | 922 | | out Fixed64 result) |
| | 4699 | 923 | | ? result |
| | 4699 | 924 | | : MaxValue; |
| | | 925 | | } |
| | | 926 | | |
| | | 927 | | internal static bool TryGetSignedRawRatio( |
| | | 928 | | ReadOnlySpan<ulong> numeratorMagnitude, |
| | | 929 | | ReadOnlySpan<ulong> denominatorMagnitude, |
| | | 930 | | bool negative, |
| | | 931 | | out Fixed64 result) |
| | | 932 | | { |
| | 5 | 933 | | int denominatorLength = |
| | 5 | 934 | | GetActiveMagnitudeLength(denominatorMagnitude); |
| | 5 | 935 | | if (denominatorLength == 1 |
| | 5 | 936 | | && denominatorMagnitude[0] == 0UL) |
| | | 937 | | { |
| | 1 | 938 | | result = default; |
| | 1 | 939 | | return false; |
| | | 940 | | } |
| | | 941 | | |
| | 4 | 942 | | int numeratorLength = |
| | 4 | 943 | | GetActiveMagnitudeLength(numeratorMagnitude); |
| | 4 | 944 | | if (numeratorLength == 1 |
| | 4 | 945 | | && numeratorMagnitude[0] == 0UL) |
| | | 946 | | { |
| | 1 | 947 | | result = Zero; |
| | 1 | 948 | | return true; |
| | | 949 | | } |
| | | 950 | | |
| | 3 | 951 | | int length = Math.Max(numeratorLength, denominatorLength); |
| | 3 | 952 | | Span<ulong> remainder = stackalloc ulong[length]; |
| | 3 | 953 | | Span<ulong> denominator = stackalloc ulong[length]; |
| | 3 | 954 | | remainder.Clear(); |
| | 3 | 955 | | denominator.Clear(); |
| | 3 | 956 | | numeratorMagnitude[..numeratorLength].CopyTo(remainder); |
| | 3 | 957 | | denominatorMagnitude[..denominatorLength].CopyTo(denominator); |
| | 3 | 958 | | return TryGetSignedRawRatioCore( |
| | 3 | 959 | | remainder, |
| | 3 | 960 | | denominator, |
| | 3 | 961 | | negative, |
| | 3 | 962 | | roundToEven: true, |
| | 3 | 963 | | out result); |
| | | 964 | | } |
| | | 965 | | |
| | | 966 | | private static bool TryGetSignedRawRatioCore( |
| | | 967 | | Span<ulong> remainder, |
| | | 968 | | Span<ulong> denominatorMagnitude, |
| | | 969 | | bool negative, |
| | | 970 | | bool roundToEven, |
| | | 971 | | out Fixed64 result) |
| | | 972 | | { |
| | 109769 | 973 | | int remainderLength = GetActiveMagnitudeLength(remainder); |
| | 109769 | 974 | | int denominatorLength = GetActiveMagnitudeLength(denominatorMagnitude); |
| | 109769 | 975 | | if (denominatorLength == 1 |
| | 109769 | 976 | | && denominatorMagnitude[0] == 0UL) |
| | | 977 | | { |
| | 2 | 978 | | result = default; |
| | 2 | 979 | | return false; |
| | | 980 | | } |
| | | 981 | | |
| | 109767 | 982 | | int activeLength = Math.Max(remainderLength, denominatorLength); |
| | 109767 | 983 | | Span<ulong> activeRemainder = remainder[..activeLength]; |
| | 109767 | 984 | | Span<ulong> activeDenominator = denominatorMagnitude[..activeLength]; |
| | 109767 | 985 | | int quotientBit = GetMagnitudeBitLength(remainder[..remainderLength]) |
| | 109767 | 986 | | - GetMagnitudeBitLength(denominatorMagnitude[..denominatorLength]); |
| | 109767 | 987 | | if (quotientBit > 63) |
| | | 988 | | { |
| | 153 | 989 | | result = default; |
| | 153 | 990 | | return false; |
| | | 991 | | } |
| | | 992 | | |
| | 109614 | 993 | | if (denominatorLength == 1) |
| | | 994 | | { |
| | 49629 | 995 | | ulong denominator = denominatorMagnitude[0]; |
| | 49629 | 996 | | ulong numeratorHigh = remainderLength == 2 ? remainder[1] : 0UL; |
| | 49629 | 997 | | ulong singleWordQuotient = Divide128By64( |
| | 49629 | 998 | | numeratorHigh, |
| | 49629 | 999 | | remainder[0], |
| | 49629 | 1000 | | denominator, |
| | 49629 | 1001 | | out ulong singleWordRemainder); |
| | 49629 | 1002 | | int singleWordMidpointComparison = roundToEven |
| | 49629 | 1003 | | ? singleWordRemainder.CompareTo(denominator - singleWordRemainder) |
| | 49629 | 1004 | | : -1; |
| | 49629 | 1005 | | return TryCreateRawRatioResult( |
| | 49629 | 1006 | | singleWordQuotient, |
| | 49629 | 1007 | | singleWordMidpointComparison, |
| | 49629 | 1008 | | negative, |
| | 49629 | 1009 | | out result); |
| | | 1010 | | } |
| | | 1011 | | |
| | 59985 | 1012 | | ulong quotient = 0UL; |
| | 59985 | 1013 | | if (quotientBit >= 0) |
| | | 1014 | | { |
| | 59897 | 1015 | | Span<ulong> shiftedDenominatorStorage = |
| | 59897 | 1016 | | stackalloc ulong[denominatorMagnitude.Length]; |
| | 59897 | 1017 | | Span<ulong> shiftedDenominator = shiftedDenominatorStorage[..activeLength]; |
| | 59897 | 1018 | | ShiftLeftMagnitude(activeDenominator, quotientBit, shiftedDenominator); |
| | 4040504 | 1019 | | for (int bit = quotientBit; bit >= 0; bit--) |
| | | 1020 | | { |
| | 1960355 | 1021 | | if (WideArithmetic.CompareMagnitudeEqualLength( |
| | 1960355 | 1022 | | activeRemainder, |
| | 1960355 | 1023 | | shiftedDenominator) >= 0) |
| | | 1024 | | { |
| | 519108 | 1025 | | WideArithmetic.SubtractEqualMagnitudes( |
| | 519108 | 1026 | | activeRemainder, |
| | 519108 | 1027 | | shiftedDenominator, |
| | 519108 | 1028 | | activeRemainder); |
| | 519108 | 1029 | | quotient |= 1UL << bit; |
| | | 1030 | | } |
| | | 1031 | | |
| | 1960355 | 1032 | | ShiftRightOne(shiftedDenominator); |
| | | 1033 | | } |
| | | 1034 | | } |
| | | 1035 | | |
| | 59985 | 1036 | | int midpointComparison = -1; |
| | 59985 | 1037 | | if (roundToEven) |
| | | 1038 | | { |
| | 57716 | 1039 | | Span<ulong> denominatorMinusRemainderStorage = |
| | 57716 | 1040 | | stackalloc ulong[denominatorMagnitude.Length]; |
| | 57716 | 1041 | | Span<ulong> denominatorMinusRemainder = |
| | 57716 | 1042 | | denominatorMinusRemainderStorage[..activeLength]; |
| | 57716 | 1043 | | activeDenominator.CopyTo(denominatorMinusRemainder); |
| | 57716 | 1044 | | WideArithmetic.SubtractEqualMagnitudes( |
| | 57716 | 1045 | | denominatorMinusRemainder, |
| | 57716 | 1046 | | activeRemainder, |
| | 57716 | 1047 | | denominatorMinusRemainder); |
| | 57716 | 1048 | | midpointComparison = WideArithmetic.CompareMagnitudeEqualLength( |
| | 57716 | 1049 | | activeRemainder, |
| | 57716 | 1050 | | denominatorMinusRemainder); |
| | | 1051 | | } |
| | 59985 | 1052 | | return TryCreateRawRatioResult( |
| | 59985 | 1053 | | quotient, |
| | 59985 | 1054 | | midpointComparison, |
| | 59985 | 1055 | | negative, |
| | 59985 | 1056 | | out result); |
| | | 1057 | | } |
| | | 1058 | | |
| | | 1059 | | private static bool TryCreateRawRatioResult( |
| | | 1060 | | ulong quotient, |
| | | 1061 | | int midpointComparison, |
| | | 1062 | | bool negative, |
| | | 1063 | | out Fixed64 result) |
| | | 1064 | | { |
| | 109614 | 1065 | | if (midpointComparison > 0 || (midpointComparison == 0 && (quotient & 1UL) != 0UL)) |
| | | 1066 | | { |
| | 33684 | 1067 | | quotient++; |
| | 33684 | 1068 | | if (quotient == 0UL) |
| | | 1069 | | { |
| | 1 | 1070 | | result = default; |
| | 1 | 1071 | | return false; |
| | | 1072 | | } |
| | | 1073 | | } |
| | | 1074 | | |
| | 109613 | 1075 | | ulong limit = negative ? 1UL << 63 : (ulong)long.MaxValue; |
| | 109613 | 1076 | | if (quotient > limit) |
| | | 1077 | | { |
| | 176 | 1078 | | result = default; |
| | 176 | 1079 | | return false; |
| | | 1080 | | } |
| | | 1081 | | |
| | 109437 | 1082 | | long raw = negative ? unchecked(-(long)quotient) : (long)quotient; |
| | 109437 | 1083 | | result = new Fixed64(raw); |
| | 109437 | 1084 | | return true; |
| | | 1085 | | } |
| | | 1086 | | |
| | | 1087 | | private static int GetMagnitudeBitLength(ReadOnlySpan<ulong> value) |
| | | 1088 | | { |
| | 219534 | 1089 | | int index = value.Length - 1; |
| | 219534 | 1090 | | return (index * 64) + 64 - CountLeadingZeroes(value[index]); |
| | | 1091 | | } |
| | | 1092 | | |
| | | 1093 | | private static int GetActiveMagnitudeLength(ReadOnlySpan<ulong> value) |
| | | 1094 | | { |
| | 219547 | 1095 | | int length = value.Length; |
| | 792845 | 1096 | | while (length > 1 && value[length - 1] == 0UL) |
| | 573298 | 1097 | | length--; |
| | 219547 | 1098 | | return length; |
| | | 1099 | | } |
| | | 1100 | | |
| | | 1101 | | private static void ShiftLeftMagnitude( |
| | | 1102 | | ReadOnlySpan<ulong> source, |
| | | 1103 | | int bits, |
| | | 1104 | | Span<ulong> destination) |
| | | 1105 | | { |
| | 60639 | 1106 | | if (bits == 0) |
| | | 1107 | | { |
| | 420 | 1108 | | source.CopyTo(destination); |
| | 420 | 1109 | | return; |
| | | 1110 | | } |
| | | 1111 | | |
| | 60219 | 1112 | | destination[0] = source[0] << bits; |
| | 604308 | 1113 | | for (int index = 1; index < destination.Length; index++) |
| | 241935 | 1114 | | destination[index] = (source[index] << bits) | (source[index - 1] >> (64 - bits)); |
| | 60219 | 1115 | | } |
| | | 1116 | | |
| | | 1117 | | private static void ShiftRightOne(Span<ulong> value) |
| | | 1118 | | { |
| | 1960355 | 1119 | | ulong carry = 0UL; |
| | 22947812 | 1120 | | for (int index = value.Length - 1; index >= 0; index--) |
| | | 1121 | | { |
| | 9513551 | 1122 | | ulong nextCarry = value[index] << 63; |
| | 9513551 | 1123 | | value[index] = (value[index] >> 1) | carry; |
| | 9513551 | 1124 | | carry = nextCarry; |
| | | 1125 | | } |
| | 1960355 | 1126 | | } |
| | | 1127 | | } |