From ddc8ce302bd8e2420660046db4334627981263de Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Thu, 15 Aug 2024 11:58:38 -0400 Subject: [PATCH] Make all rounding rules (from IntegerUtilities) available on FloatingPoint IntegerUtilities has RoundingRule that is a superset of FloatingPointRoundingRule; many of these are also useful when working with floating-point. This change provides a `rounding(_:)` function similar to the standard libraries `rounded(_:)` that makes all of them available. It has a different name instead of being a shadow because otherwise existing use sites like `rounded(.down)` would become ambiguous. --- Sources/IntegerUtilities/RoundingRule.swift | 64 ++++- .../IntegerUtilitiesTests/RoundingTests.swift | 243 ++++++++++++++++++ 2 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 Tests/IntegerUtilitiesTests/RoundingTests.swift diff --git a/Sources/IntegerUtilities/RoundingRule.swift b/Sources/IntegerUtilities/RoundingRule.swift index 0dc9d83..317b8b1 100644 --- a/Sources/IntegerUtilities/RoundingRule.swift +++ b/Sources/IntegerUtilities/RoundingRule.swift @@ -256,5 +256,67 @@ extension RoundingRule { /// > Deprecated: Use `.toNearestOrAway` instead. @inlinable @available(*, deprecated, renamed: "toNearestOrAway") - static var toNearestOrAwayFromZero: Self { .toNearestOrAway } + public static var toNearestOrAwayFromZero: Self { .toNearestOrAway } +} + +extension FloatingPoint { + /// `self` rounded to integer according to `rule`. + /// + /// This mirrors the standard library `rounded` API, providing access to + /// the expanded set of rounding rules defined in IntegerUtilities. It is + /// not just a shadow because that would lead to ambiguity errors in + /// existing code that uses the shortened `rounded(.down)` form. + @inlinable @inline(__always) + public func rounding(_ rule: RoundingRule) -> Self { + switch rule { + case .down: + return rounded(.down) + case .up: + return rounded(.up) + case .towardZero: + return rounded(.towardZero) + case .awayFromZero: + return rounded(.awayFromZero) + case .toNearestOrDown: + // FP doesn't have toNearestOrDown, so round toNearestOrEven and fixup + // any exact-halfway cases. + let nearest = rounded(.toNearestOrEven) + return nearest - self == 1/2 ? rounded(.down) : nearest + case .toNearestOrUp: + // FP doesn't have toNearestOrUp, so round toNearestOrEven and fixup + // any exact-halfway cases. + let nearest = rounded(.toNearestOrEven) + return self - nearest == 1/2 ? rounded(.up) : nearest + case .toNearestOrZero: + // FP doesn't have toNearestOrZero, so round toNearestOrEven and fixup + // any exact-halfway cases. + let nearest = rounded(.toNearestOrEven) + return (self - nearest).magnitude == 1/2 ? rounded(.towardZero) : nearest + case .toNearestOrAway: + return self.rounded(.toNearestOrAwayFromZero) + case .toNearestOrEven: + return self.rounded(.toNearestOrEven) + case .toOdd: + let trunc = rounded(.towardZero) + if trunc == self { return trunc } + let one = Self(signOf: self, magnitudeOf: 1) + // We have eliminated all large values at this point; add ±0.5, and see + // which way that rounds, then select the other value. + let even = (trunc + one/2).rounded(.toNearestOrEven) + return trunc == even ? trunc + one : trunc + case .stochastically: + let trunc = rounded(.towardZero) + if trunc == self { return trunc } + // We have eliminated all large values at this point; add dither in + // ±[0,1) and then truncate. + let bits = Swift.min(-Self.ulpOfOne.exponent, 32) + let random = Self(UInt32.random(in: 0 ... (1 << bits &- 1))) + let dither = Self(sign: sign, exponent: -bits, significand: random) + return (self + dither).rounded(.towardZero) + case .requireExact: + let trunc = rounded(.towardZero) + precondition(isInfinite || trunc == self, "\(self) is not an exact integer.") + return self + } + } } diff --git a/Tests/IntegerUtilitiesTests/RoundingTests.swift b/Tests/IntegerUtilitiesTests/RoundingTests.swift new file mode 100644 index 0000000..2d75d51 --- /dev/null +++ b/Tests/IntegerUtilitiesTests/RoundingTests.swift @@ -0,0 +1,243 @@ +//===--- RoundingTests.swift ----------------------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import IntegerUtilities +import _TestSupport +import XCTest + +final class IntegerUtilitiesRoundingTests: XCTestCase { + func testRoundingDirected(_ type: T.Type) { + let inf = T.infinity + let gfm = T.greatestFiniteMagnitude + let big: T = 1 / .ulpOfOne + let two: T = 2 + let threeHalves: T = 1.5 + let one: T = 1 + let half: T = 0.5 + let nrm = T.leastNormalMagnitude + let lnm = T.leastNonzeroMagnitude + let vectors: [(input: T, down: T, up: T, zero: T, away: T)] = [ + (-inf, -inf, -inf, -inf, -inf), + (-gfm, -gfm, -gfm, -gfm, -gfm), + (-big.nextUp, -big-1, -big-1, -big-1, -big-1), + (-big, -big, -big, -big, -big), + (-big.nextDown, -big, -big+1, -big+1, -big), + (-two, -two, -two, -two, -two), + (-two.nextDown, -two, -one, -one, -two), + (-threeHalves.nextUp, -two, -one, -one, -two), + (-threeHalves, -two, -one, -one, -two), + (-threeHalves.nextDown, -two, -one, -one, -two), + (-one.nextUp, -two, -one, -one, -two), + (-one, -one, -one, -one, -one), + (-one.nextDown, -one, 0, 0, -one), + (-half.nextUp, -one, 0, 0, -one), + (-half, -one, 0, 0, -one), + (-half.nextDown, -one, 0, 0, -one), + (-nrm, -one, 0, 0, -one), + (-lnm, -one, 0, 0, -one), + (-.zero, 0, 0, 0, 0), + ( .zero, 0, 0, 0, 0), + ( lnm, 0, one, 0, one), + ( nrm, 0, one, 0, one), + ( half.nextDown, 0, one, 0, one), + ( half, 0, one, 0, one), + ( half.nextUp, 0, one, 0, one), + ( one.nextDown, 0, one, 0, one), + ( one, one, one, one, one), + ( one.nextUp, one, two, one, two), + ( threeHalves.nextDown, one, two, one, two), + ( threeHalves, one, two, one, two), + ( threeHalves.nextUp, one, two, one, two), + ( two.nextDown, one, two, one, two), + ( two, two, two, two, two), + ( big.nextDown, big-1, big, big-1, big), + ( big, big, big, big, big), + ( big.nextUp, big+1, big+1, big+1, big+1), + ( gfm, gfm, gfm, gfm, gfm), + ( inf, inf, inf, inf, inf), + ] + for vector in vectors { + + XCTAssertEqual(vector.input.rounding(.down), vector.down) + if vector.down == 0 { + XCTAssertEqual(vector.input.rounding(.down).sign, vector.input.sign) + } + + XCTAssertEqual(vector.input.rounding(.up), vector.up) + if vector.up == 0 { + XCTAssertEqual(vector.input.rounding(.up).sign, vector.input.sign) + } + + XCTAssertEqual(vector.input.rounding(.towardZero), vector.zero) + if vector.zero == 0 { + XCTAssertEqual(vector.input.rounding(.towardZero).sign, vector.input.sign) + } + + XCTAssertEqual(vector.input.rounding(.awayFromZero), vector.away) + if vector.away == 0 { + XCTAssertEqual(vector.input.rounding(.awayFromZero).sign, vector.input.sign) + } + } + } + + func testRoundingDirected() { + testRoundingDirected(Float.self) + testRoundingDirected(Double.self) + } + + func testRoundingNearest(_ type: T.Type) { + let inf = T.infinity + let gfm = T.greatestFiniteMagnitude + let big: T = 1 / .ulpOfOne + let two: T = 2 + let threeHalves: T = 1.5 + let one: T = 1 + let half: T = 0.5 + let nrm = T.leastNormalMagnitude + let lnm = T.leastNonzeroMagnitude + let vectors: [(input: T, down: T, up: T, zero: T, away: T, even: T)] = [ + (-inf, -inf, -inf, -inf, -inf, -inf), + (-gfm, -gfm, -gfm, -gfm, -gfm, -gfm), + (-big.nextUp, -big-1, -big-1, -big-1, -big-1, -big-1), + (-big, -big, -big, -big, -big, -big), + (-big.nextDown, -big, -big+1, -big+1, -big, -big), + (-two, -two, -two, -two, -two, -two), + (-two.nextDown, -two, -two, -two, -two, -two), + (-threeHalves.nextUp, -two, -two, -two, -two, -two), + (-threeHalves, -two, -one, -one, -two, -two), + (-threeHalves.nextDown, -one, -one, -one, -one, -one), + (-one.nextUp, -one, -one, -one, -one, -one), + (-one, -one, -one, -one, -one, -one), + (-one.nextDown, -one, -one, -one, -one, -one), + (-half.nextUp, -one, -one, -one, -one, -one), + (-half, -one, 0, 0, -one, 0), + (-half.nextDown, 0, 0, 0, 0, 0), + (-nrm, 0, 0, 0, 0, 0), + (-lnm, 0, 0, 0, 0, 0), + (-.zero, 0, 0, 0, 0, 0), + ( .zero, 0, 0, 0, 0, 0), + ( lnm, 0, 0, 0, 0, 0), + ( nrm, 0, 0, 0, 0, 0), + ( half.nextDown, 0, 0, 0, 0, 0), + ( half, 0, one, 0, one, 0), + ( half.nextUp, one, one, one, one, one), + ( one.nextDown, one, one, one, one, one), + ( one, one, one, one, one, one), + ( one.nextUp, one, one, one, one, one), + ( threeHalves.nextDown, one, one, one, one, one), + ( threeHalves, one, two, one, two, two), + ( threeHalves.nextUp, two, two, two, two, two), + ( two.nextDown, two, two, two, two, two), + ( two, two, two, two, two, two), + ( big.nextDown, big-1, big, big-1, big, big), + ( big, big, big, big, big, big), + ( big.nextUp, big+1, big+1, big+1, big+1, big+1), + ( gfm, gfm, gfm, gfm, gfm, gfm), + ( inf, inf, inf, inf, inf, inf), + ] + for vector in vectors { + + XCTAssertEqual(vector.input.rounding(.toNearestOrDown), vector.down, "\(vector.input).rounding(.toNearestOrDown)") + if vector.down == 0 { + XCTAssertEqual(vector.input.rounding(.toNearestOrDown).sign, vector.input.sign, "\(vector.input).rounding(.toNearestOrDown)") + } + + XCTAssertEqual(vector.input.rounding(.toNearestOrUp), vector.up, "\(vector.input).rounding(.toNearestOrUp)") + if vector.up == 0 { + XCTAssertEqual(vector.input.rounding(.toNearestOrUp).sign, vector.input.sign, "\(vector.input).rounding(.toNearestOrUp)") + } + + XCTAssertEqual(vector.input.rounding(.toNearestOrZero), vector.zero, "\(vector.input).rounding(.toNearestOrZero)") + if vector.zero == 0 { + XCTAssertEqual(vector.input.rounding(.toNearestOrZero).sign, vector.input.sign, "\(vector.input).rounding(.toNearestOrZero)") + } + + XCTAssertEqual(vector.input.rounding(.toNearestOrAway), vector.away, "\(vector.input).rounding(.toNearestOrAway)") + if vector.away == 0 { + XCTAssertEqual(vector.input.rounding(.toNearestOrAway).sign, vector.input.sign, "\(vector.input).rounding(.toNearestOrAway)") + } + + XCTAssertEqual(vector.input.rounding(.toNearestOrEven), vector.even, "\(vector.input).rounding(.toNearestOrEven)") + if vector.even == 0 { + XCTAssertEqual(vector.input.rounding(.toNearestOrEven).sign, vector.input.sign, "\(vector.input).rounding(.toNearestOrEven)") + } + } + } + + func testRoundingNearest() { + testRoundingNearest(Float.self) + testRoundingNearest(Double.self) + } + + func testRoundingOdd(_ type: T.Type) { + let inf = T.infinity + let gfm = T.greatestFiniteMagnitude + let big: T = 1 / .ulpOfOne + let two: T = 2 + let threeHalves: T = 1.5 + let one: T = 1 + let half: T = 0.5 + let nrm = T.leastNormalMagnitude + let lnm = T.leastNonzeroMagnitude + let vectors: [(input: T, odd: T)] = [ + (-inf, -inf), + (-gfm, -gfm), + (-big.nextUp, -big-1), + (-big, -big), + (-big.nextDown, -big+1), + (-two, -two), + (-two.nextDown, -one), + (-threeHalves.nextUp, -one), + (-threeHalves, -one), + (-threeHalves.nextDown, -one), + (-one.nextUp, -one), + (-one, -one), + (-one.nextDown, -one), + (-half.nextUp, -one), + (-half, -one), + (-half.nextDown, -one), + (-nrm, -one), + (-lnm, -one), + (-.zero, 0), + ( .zero, 0), + ( lnm, one), + ( nrm, one), + ( half.nextDown, one), + ( half, one), + ( half.nextUp, one), + ( one.nextDown, one), + ( one, one), + ( one.nextUp, one), + ( threeHalves.nextDown, one), + ( threeHalves, one), + ( threeHalves.nextUp, one), + ( two.nextDown, one), + ( two, two), + ( big.nextDown, big-1), + ( big, big), + ( big.nextUp, big+1), + ( gfm, gfm), + ( inf, inf) + ] + for vector in vectors { + XCTAssertEqual(vector.input.rounding(.toOdd), vector.odd, "\(vector.input).rounding(.toOdd)") + if vector.odd == 0 { + XCTAssertEqual(vector.input.rounding(.toOdd).sign, vector.input.sign, "\(vector.input).rounding(.toOdd)") + } + } + } + + func testRoundingOdd() { + testRoundingOdd(Float.self) + testRoundingOdd(Double.self) + } +}