Skip to content

Commit 1ba811e

Browse files
committed
Replace asserts in BitWriter.write(signedNumber:) with precondition that uses min/maxRepresentableNumber
1 parent f0a2d5f commit 1ba811e

1 file changed

Lines changed: 6 additions & 18 deletions

File tree

Sources/BitWriter.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,44 +56,32 @@ extension BitWriter {
5656
}
5757

5858
public func write(signedNumber: Int, bitsCount: Int, representation: SignedNumberRepresentation = .twoComplementNegatives) {
59+
precondition(signedNumber >= representation.minRepresentableNumber(bitsCount: bitsCount) &&
60+
signedNumber <= representation.maxRepresentableNumber(bitsCount: bitsCount),
61+
"\(signedNumber) cannot be represented by \(representation) using \(bitsCount) bits")
62+
5963
var magnitude = signedNumber.magnitude
6064
switch representation {
6165
case .signMagnitude:
62-
assert(magnitude < (1 << (bitsCount - 1)),
63-
"\(signedNumber) will be truncated when represented by Sign-Magnitude using \(bitsCount) bits")
6466
magnitude += signedNumber < 0 ? (1 << (bitsCount - 1)) : 0
6567
self.write(unsignedNumber: magnitude, bitsCount: bitsCount)
6668
case .oneComplementNegatives:
67-
assert(magnitude < (1 << (bitsCount - 1)),
68-
"\(signedNumber) will be truncated when represented by 1-complement using \(bitsCount) bits")
6969
if signedNumber < 0 {
7070
magnitude = ~magnitude
7171
}
7272
self.write(unsignedNumber: magnitude, bitsCount: bitsCount)
7373
case .twoComplementNegatives:
74-
assert((signedNumber >= 0 && magnitude <= (1 << (bitsCount - 1)) - 1) ||
75-
(signedNumber < 0 && magnitude <= 1 << (bitsCount - 1)),
76-
"\(signedNumber) will be truncated when represented by 2-complement using \(bitsCount) bits")
7774
if signedNumber < 0 {
7875
magnitude = ~magnitude &+ 1
7976
}
8077
self.write(unsignedNumber: magnitude, bitsCount: bitsCount)
8178
case .biased(let bias):
82-
assert(bias >= 0, "Bias cannot be less than zero.")
83-
assert(signedNumber >= -bias,
84-
"\(signedNumber) is too small to be encoded by biased representation with \(bias) bias")
85-
let encoded = UInt(bitPattern: signedNumber &+ bias)
86-
let encodedUpperBound = bitsCount == UInt.bitWidth ? UInt.max : (1 << bitsCount) - 1
87-
assert(encoded <= encodedUpperBound,
88-
"\(signedNumber) is too big to be encoded by biased representation with \(bias) bias using \(bitsCount) bits")
89-
self.write(unsignedNumber: encoded, bitsCount: bitsCount)
79+
precondition(bias >= 0, "Bias cannot be less than zero.")
80+
self.write(unsignedNumber: UInt(bitPattern: signedNumber &+ bias), bitsCount: bitsCount)
9081
case .radixNegativeTwo:
9182
let mask = UInt(truncatingIfNeeded: 0xAA_AA_AA_AA_AA_AA_AA_AA as UInt64)
9283
let unsignedBitPattern = UInt(bitPattern: signedNumber)
9384
let encoded = (unsignedBitPattern &+ mask) ^ mask
94-
let encodedBound = bitsCount == UInt.bitWidth ? UInt.max : ((1 << bitsCount) - 1)
95-
assert(encoded <= encodedBound,
96-
"\(signedNumber) will be truncated when represented by -2 radix using \(bitsCount) bits")
9785
self.write(unsignedNumber: encoded, bitsCount: bitsCount)
9886
}
9987
}

0 commit comments

Comments
 (0)