|
9 | 9 | //
|
10 | 10 | //===----------------------------------------------------------------------===//
|
11 | 11 |
|
| 12 | +/// A namespace for "augmented arithmetic" operations for types conforming to |
| 13 | +/// `Real`. |
| 14 | +/// |
| 15 | +/// Augmented arithmetic refers to a family of algorithms that represent |
| 16 | +/// the results of floating-point computations using multiple values such that |
| 17 | +/// either the error is minimized or the result is exact. |
12 | 18 | public enum Augmented { }
|
13 | 19 |
|
14 | 20 | extension Augmented {
|
| 21 | + /// The product `a * b` represented as an implicit sum `head + tail`. |
| 22 | + /// |
| 23 | + /// `head` is the correctly rounded value of `a*b`. If no overflow or |
| 24 | + /// underflow occurs, `tail` represents the rounding error incurred in |
| 25 | + /// computing `head`, such that the exact product is the sum of `head` |
| 26 | + /// and `tail` computed without rounding. |
| 27 | + /// |
| 28 | + /// This operation is sometimes called "twoProd" or "twoProduct". |
| 29 | + /// |
| 30 | + /// Edge Cases: |
| 31 | + /// - |
| 32 | + /// - `head` is always the IEEE 754 product `a * b`. |
| 33 | + /// - If `head` is not finite, `tail` is unspecified and should not be |
| 34 | + /// interpreted as having any meaning (it may be `NaN` or `infinity`). |
| 35 | + /// - When `head` is close to the underflow boundary, the rounding error |
| 36 | + /// may not be representable due to underflow, and `tail` will be rounded. |
| 37 | + /// If `head` is very small, `tail` may even be zero, even though the |
| 38 | + /// product is not exact. |
| 39 | + /// - If `head` is zero, `tail` is also a zero with unspecified sign. |
| 40 | + /// |
| 41 | + /// Postconditions: |
| 42 | + /// - |
| 43 | + /// - If `head` is normal, then `abs(tail) < abs(head.ulp)`. |
| 44 | + /// Assuming IEEE 754 default rounding, `abs(tail) <= abs(head.ulp)/2`. |
| 45 | + /// - If both `head` and `tail` are normal, then `a * b` is exactly |
| 46 | + /// equal to `head + tail` when computed as real numbers. |
15 | 47 | @_transparent
|
16 |
| - public static func twoProdFMA<T:Real>(_ a: T, _ b: T) -> (head: T, tail: T) { |
| 48 | + public static func product<T:Real>(_ a: T, _ b: T) -> (head: T, tail: T) { |
17 | 49 | let head = a*b
|
| 50 | + // TODO: consider providing an FMA-less implementation for use when |
| 51 | + // targeting platforms without hardware FMA support. This works everywhere, |
| 52 | + // falling back on the C math.h fma funcions, but may be slow on older x86. |
18 | 53 | let tail = (-head).addingProduct(a, b)
|
19 | 54 | return (head, tail)
|
20 | 55 | }
|
21 | 56 |
|
| 57 | + /// The sum `a + b` represented as an implicit sum `head + tail`. |
| 58 | + /// |
| 59 | + /// `head` is the correctly rounded value of `a + b`. `tail` is the |
| 60 | + /// error from that computation rounded to the closest representable |
| 61 | + /// value. |
| 62 | + /// |
| 63 | + /// Unlike `Augmented.product(a, b)`, the rounding error of a sum can |
| 64 | + /// never underflow. However, it may not be exactly representable when |
| 65 | + /// `a` and `b` differ widely in magnitude. |
| 66 | + /// |
| 67 | + /// This operation is sometimes called "fastTwoSum". |
| 68 | + /// |
| 69 | + /// - Parameters: |
| 70 | + /// - a: The summand with larger magnitude. |
| 71 | + /// - b: The summand with smaller magnitude. |
| 72 | + /// |
| 73 | + /// Preconditions: |
| 74 | + /// - |
| 75 | + /// - `large.magnitude` must not be smaller than `small.magnitude`. |
| 76 | + /// They may be equal, or one or both may be `NaN`. |
| 77 | + /// This precondition is only enforced in debug builds. |
| 78 | + /// |
| 79 | + /// Edge Cases: |
| 80 | + /// - |
| 81 | + /// - `head` is always the IEEE 754 sum `a + b`. |
| 82 | + /// - If `head` is not finite, `tail` is unspecified and should not be |
| 83 | + /// interpreted as having any meaning (it may be `NaN` or `infinity`). |
| 84 | + /// |
| 85 | + /// Postconditions: |
| 86 | + /// - |
| 87 | + /// - If `head` is normal, then `abs(tail) < abs(head.ulp)`. |
| 88 | + /// Assuming IEEE 754 default rounding, `abs(tail) <= abs(head.ulp)/2`. |
22 | 89 | @_transparent
|
23 |
| - public static func fastTwoSum<T:Real>(_ a: T, _ b: T) -> (head: T, tail: T) { |
| 90 | + public static func sum<T:Real>(large a: T, small b: T) -> (head: T, tail: T) { |
24 | 91 | assert(!(b.magnitude > a.magnitude))
|
25 | 92 | let head = a + b
|
26 | 93 | let tail = a - head + b
|
|
0 commit comments