Skip to content

Commit 1382651

Browse files
committed
Fix sign bit shift
1 parent df393de commit 1382651

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

Tests/BinaryParsingTests/FloatingPointTests.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ enum Interesting {
3434
.nan, .signalingNaN,
3535
]
3636

37+
static let float80OneLE: [UInt8] = [
38+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0x3f,
39+
]
40+
41+
static let float80OneBE: [UInt8] = [
42+
0x3f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43+
]
44+
3745
#if !(os(Windows) || os(Android) || ($Embedded && !os(Linux) && !(os(macOS) || os(iOS) || os(watchOS) || os(tvOS)))) && (arch(i386) || arch(x86_64))
3846
static let float80s: [Float80] = [
3947
0.0, 1.0, 1000,
@@ -182,7 +190,7 @@ struct FloatingPointTests {
182190
#expect(value2.isSignalingNaN)
183191
}
184192
} else {
185-
#expect(value1 == value)
193+
#expect(value1 == value, "\(1)")
186194
#expect(value2 == value)
187195
}
188196
}
@@ -194,13 +202,25 @@ struct FloatingPointTests {
194202
}
195203

196204
#if !(os(Windows) || os(Android) || ($Embedded && !os(Linux) && !(os(macOS) || os(iOS) || os(watchOS) || os(tvOS)))) && (arch(i386) || arch(x86_64))
205+
@Test
206+
func staticFloat80() throws {
207+
let leValue = try Interesting.float80OneLE.withParserSpan(
208+
Float80.init(parsingLittleEndian:))
209+
let beValue = try Interesting.float80OneBE.withParserSpan(
210+
Float80.init(parsingBigEndian:))
211+
212+
#expect(leValue == 1.0)
213+
#expect(beValue == 1.0)
214+
}
215+
197216
@Test(arguments: Interesting.float80s)
198217
func testFloat80RoundTrip(_ value: Float80) throws {
199218
let bytesLE = Array(littleEndian: value)
200219
let bytesBE = Array(bigEndian: value)
201220

202221
do {
203-
let value1 = try bytesLE.withParserSpan(Float80.init(parsingLittleEndian:))
222+
let value1 = try bytesLE.withParserSpan(
223+
Float80.init(parsingLittleEndian:))
204224
let value2 = try bytesLE.withParserSpan { input in
205225
try Float80(parsing: &input, endianness: .little)
206226
}

Tests/BinaryParsingTests/TestingSupport.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,19 @@ extension Array where Element == UInt8 {
145145
bytes in
146146
self.append(contentsOf: bytes)
147147
}
148-
let signAndExponent =
149-
value.exponentBitPattern | (value.sign == .minus ? 1 : 0 << 31)
148+
let signAndExponent = UInt16(
149+
truncatingIfNeeded:
150+
value.exponentBitPattern | ((value.sign == .minus ? 1 : 0) << 15))
150151
Swift.withUnsafeBytes(of: signAndExponent.littleEndian) { bytes in
151152
self.append(contentsOf: bytes)
152153
}
153154
}
154155

155156
init(bigEndian value: Float80) {
156157
self = []
157-
let signAndExponent =
158-
value.exponentBitPattern | (value.sign == .minus ? 1 : 0 << 31)
158+
let signAndExponent = UInt16(
159+
truncatingIfNeeded:
160+
value.exponentBitPattern | ((value.sign == .minus ? 1 : 0) << 15))
159161
Swift.withUnsafeBytes(of: signAndExponent.bigEndian) { bytes in
160162
self.append(contentsOf: bytes)
161163
}

0 commit comments

Comments
 (0)