Skip to content

Commit 710ca08

Browse files
committed
Renamed shifted(right:) to shifted(rightBy:).
1 parent 501f582 commit 710ca08

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

Sources/IntegerUtilities/Divide.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ extension BinaryInteger {
7777
case .toNearestOrAwayFromZero:
7878
// For round to nearest or away, the condition we want to satisfy is
7979
// |r| <= |other/2|, with sign(q) != sign(r) when equality holds.
80-
if r.magnitude < other.magnitude.shifted(right: 1, rounding: .up) {
80+
if r.magnitude < other.magnitude.shifted(rightBy: 1, rounding: .up) {
8181
return q
8282
}
8383
// The (q,r) we have does not satisfy the to nearest or away condition;
@@ -87,7 +87,7 @@ extension BinaryInteger {
8787
case .toNearestOrEven:
8888
// For round to nearest or away, the condition we want to satisfy is
8989
// |r| <= |other/2|, with q even when equality holds.
90-
if r.magnitude > other.magnitude.shifted(right: 1, rounding: .down) ||
90+
if r.magnitude > other.magnitude.shifted(rightBy: 1, rounding: .down) ||
9191
2*r.magnitude == other.magnitude && q._lowWord & 1 == 1 {
9292
if (other > 0) != (r > 0) { return q-1 }
9393
return q+1
@@ -218,7 +218,7 @@ extension SignedInteger {
218218
case .toNearestOrAwayFromZero:
219219
// For round to nearest or away, the condition we want to satisfy is
220220
// |r| <= |other/2|, with sign(q) != sign(r) when equality holds.
221-
if r.magnitude < other.magnitude.shifted(right: 1, rounding: .up) {
221+
if r.magnitude < other.magnitude.shifted(rightBy: 1, rounding: .up) {
222222
return (q, r)
223223
}
224224
// The (q,r) we have does not satisfy the to nearest or away condition;
@@ -228,7 +228,7 @@ extension SignedInteger {
228228
case .toNearestOrEven:
229229
// For round to nearest or away, the condition we want to satisfy is
230230
// |r| <= |other/2|, with q even when equality holds.
231-
if r.magnitude > other.magnitude.shifted(right: 1, rounding: .down) ||
231+
if r.magnitude > other.magnitude.shifted(rightBy: 1, rounding: .down) ||
232232
2*r.magnitude == other.magnitude && q._lowWord & 1 == 1 {
233233
if (other > 0) != (r > 0) { return (q-1, r+other) }
234234
return (q+1, r-other)

Sources/IntegerUtilities/Rounding.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public enum RoundingRule {
5858
case toNearestOrEven
5959

6060
/// Adds a uniform random value from [0, d) to the value being rounded,
61-
/// then returns the floor of the resulting value, where d is the
62-
/// distance between the two closest representable values.
61+
/// where d is the distance between the two closest representable values,
62+
/// then rounds the sum downwards.
6363
case stochastically
6464

6565
/// If the value being rounded is representable, that value is returned.

Sources/IntegerUtilities/Shift.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ extension BinaryInteger {
1818
/// Some examples of different rounding rules:
1919
///
2020
/// // 3/2 is 1.5, which rounds (down by default) to 1.
21-
/// 3.shifted(right: 1)
21+
/// 3.shifted(rightBy: 1)
2222
///
2323
/// // 1.5 rounds up to 2.
24-
/// 3.shifted(right: 1, rounding: .up)
24+
/// 3.shifted(rightBy: 1, rounding: .up)
2525
///
2626
/// // The two closest values are 1 and 2, 1 is returned because it
2727
/// // is odd.
28-
/// 3.shifted(right: 1, rounding: .toOdd)
28+
/// 3.shifted(rightBy: 1, rounding: .toOdd)
2929
///
3030
/// // 7/2^2 = 1.75, so the result is 1 with probability 1/4, and 2
3131
/// // with probability 3/4.
32-
/// 7.shifted(right: 2, rounding: .stochastically)
32+
/// 7.shifted(rightBy: 2, rounding: .stochastically)
3333
///
3434
/// // 4/2^2 = 4/4 = 1, exactly.
35-
/// 4.shifted(right: 2, rounding: .trap)
35+
/// 4.shifted(rightBy: 2, rounding: .trap)
3636
///
3737
/// // 5/2 is 2.5, which is not exact, so this traps.
38-
/// 5.shifted(right: 1, rounding: .trap)
38+
/// 5.shifted(rightBy: 1, rounding: .trap)
3939
@inlinable
4040
public func shifted<Count: BinaryInteger>(
41-
right count: Count,
41+
rightBy count: Count,
4242
rounding rule: RoundingRule = .down
4343
) -> Self {
4444
// Easiest case: count is zero or negative, so shift is always exact;
@@ -50,7 +50,7 @@ extension BinaryInteger {
5050
// that we encounter such a case, we promote to Int8, do the shift, and
5151
// then convert back to the appropriate result type.
5252
if bitWidth <= 1 {
53-
return Self(Int8(self).shifted(right: count, rounding: rule))
53+
return Self(Int8(self).shifted(rightBy: count, rounding: rule))
5454
}
5555
// That pathological case taken care of, we can now handle over-wide
5656
// shifts by first shifting all but bitWidth - 1 bits with sticky
@@ -60,7 +60,7 @@ extension BinaryInteger {
6060
var floor = self >> count
6161
let lost = self - (floor << count)
6262
if lost != 0 { floor |= 1 } // insert sticky bit
63-
return floor.shifted(right: bitWidth - 1, rounding: rule)
63+
return floor.shifted(rightBy: bitWidth - 1, rounding: rule)
6464
}
6565
// Now we are in the happy case: 0 < count < bitWidth, which makes all
6666
// the math to handle rounding simpler.

Tests/IntegerUtilitiesTests/ShiftTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class IntegerUtilitiesShiftTests: XCTestCase {
3737
case .toNearestOrAwayFromZero:
3838
if exact { expected = floor }
3939
else {
40-
let step = value.shifted(right: count - 2, rounding: .toOdd)
40+
let step = value.shifted(rightBy: count - 2, rounding: .toOdd)
4141
switch step & 0b11 {
4242
case 0b01: expected = floor
4343
case 0b10: expected = value > 0 ? ceiling : floor
@@ -48,7 +48,7 @@ final class IntegerUtilitiesShiftTests: XCTestCase {
4848
case .toNearestOrEven:
4949
if exact { expected = floor }
5050
else {
51-
let step = value.shifted(right: count - 2, rounding: .toOdd)
51+
let step = value.shifted(rightBy: count - 2, rounding: .toOdd)
5252
switch step & 0b11 {
5353
case 0b01: expected = floor
5454
case 0b10: expected = floor & 1 == 0 ? floor : ceiling
@@ -61,9 +61,9 @@ final class IntegerUtilitiesShiftTests: XCTestCase {
6161
// or ceiling.
6262
if exact { expected = floor }
6363
else {
64-
let observed = value.shifted(right: count, rounding: rule)
64+
let observed = value.shifted(rightBy: count, rounding: rule)
6565
if observed != floor && observed != ceiling {
66-
print("Error found in \(T.self).shifted(right: \(count), rounding: \(rule)).")
66+
print("Error found in \(T.self).shifted(rightBy: \(count), rounding: \(rule)).")
6767
print(" Value: \(String(value, radix: 16))")
6868
print("Expected: \(String(floor, radix: 16)) or \(String(ceiling, radix: 16))")
6969
print("Observed: \(String(observed, radix: 16))")
@@ -74,9 +74,9 @@ final class IntegerUtilitiesShiftTests: XCTestCase {
7474
case .requireExact:
7575
preconditionFailure()
7676
}
77-
let observed = value.shifted(right: count, rounding: rule)
77+
let observed = value.shifted(rightBy: count, rounding: rule)
7878
if observed != expected {
79-
print("Error found in \(T.self).shifted(right: \(count), rounding: \(rule)).")
79+
print("Error found in \(T.self).shifted(rightBy: \(count), rounding: \(rule)).")
8080
print(" Value: \(String(value, radix: 16))")
8181
print("Expected: \(String(expected, radix: 16))")
8282
print("Observed: \(String(observed, radix: 16))")
@@ -89,7 +89,7 @@ final class IntegerUtilitiesShiftTests: XCTestCase {
8989
) {
9090
for count in -2*T.bitWidth ... 2*T.bitWidth {
9191
// zero shifted by anything is always zero
92-
XCTAssertEqual(0, (0 as T).shifted(right: count, rounding: rule))
92+
XCTAssertEqual(0, (0 as T).shifted(rightBy: count, rounding: rule))
9393
for _ in 0 ..< 100 {
9494
testRoundingShift(T.random(in: .min ... .max), count, rounding: rule)
9595
}
@@ -147,7 +147,7 @@ final class IntegerUtilitiesShiftTests: XCTestCase {
147147
var fails = 0
148148
for count in 0 ... 64 {
149149
let sum = (0..<1024).reduce(into: 0.0) { sum, _ in
150-
let rounded = value.shifted(right: count, rounding: .stochastically)
150+
let rounded = value.shifted(rightBy: count, rounding: .stochastically)
151151
sum += Double(rounded)
152152
}
153153
let expected = Double(sign: .plus, exponent: -count, significand: 1024*Double(value))
@@ -164,15 +164,15 @@ final class IntegerUtilitiesShiftTests: XCTestCase {
164164
// but if you see a repeated failure for a given shift count, that's
165165
// almost surely a real bug.
166166
XCTAssertLessThanOrEqual(difference, 64,
167-
"Accumulated error (\(difference)) was unexpectedly large in \(value).shifted(right: \(count))"
167+
"Accumulated error (\(difference)) was unexpectedly large in \(value).shifted(rightBy: \(count))"
168168
)
169169
}
170170
// Threshold chosen so that this is expected to _almost always_ pass, but
171171
// it may fail sporadically. This is not a great fit for CI workflows,
172172
// sorry. Basically ignore one-off failures, but a repeated failure here
173173
// is an indication that a bug exists.
174174
XCTAssertLessThanOrEqual(fails, 16,
175-
"Accumulated error was large more often than expected for \(value).shifted(right:)"
175+
"Accumulated error was large more often than expected for \(value).shifted(rightBy:)"
176176
)
177177
}
178178

0 commit comments

Comments
 (0)