Skip to content

Commit cbd8d89

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

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

Tests/BinaryParsingTests/FloatingPointTests.swift

Lines changed: 31 additions & 4 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,32 @@ 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(Float80.init(parsingLittleEndian:))
208+
let beValue = try Interesting.float80OneBE.withParserSpan(Float80.init(parsingBigEndian:))
209+
210+
print(leValue,
211+
String(leValue.exponentBitPattern, radix: 16),
212+
String(leValue.significandBitPattern, radix: 16))
213+
print(beValue,
214+
String(beValue.exponentBitPattern, radix: 16),
215+
String(beValue.significandBitPattern, radix: 16))
216+
217+
#expect(leValue == 1.0)
218+
#expect(beValue == 1.0)
219+
}
220+
197221
@Test(arguments: Interesting.float80s)
198222
func testFloat80RoundTrip(_ value: Float80) throws {
199223
let bytesLE = Array(littleEndian: value)
200224
let bytesBE = Array(bigEndian: value)
201225

226+
let hexBE = bytesBE.map { String($0, radix: 16) }.joined(separator: "_")
227+
202228
do {
203-
let value1 = try bytesLE.withParserSpan(Float80.init(parsingLittleEndian:))
229+
let value1 = try bytesLE.withParserSpan(
230+
Float80.init(parsingLittleEndian:))
204231
let value2 = try bytesLE.withParserSpan { input in
205232
try Float80(parsing: &input, endianness: .little)
206233
}
@@ -225,14 +252,14 @@ struct FloatingPointTests {
225252
}
226253

227254
if value.isNaN {
228-
#expect(value1.isNaN)
255+
#expect(value1.isNaN, "big: \(hexBE)")
229256
#expect(value2.isNaN)
230257
if value.isSignalingNaN {
231258
#expect(value1.isSignalingNaN)
232259
#expect(value2.isSignalingNaN)
233260
}
234261
} else {
235-
#expect(value1 == value)
262+
#expect(value1 == value, "big: \(hexBE)")
236263
#expect(value2 == value)
237264
}
238265
}

Tests/BinaryParsingTests/TestingSupport.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ extension Array where Element == UInt8 {
146146
self.append(contentsOf: bytes)
147147
}
148148
let signAndExponent =
149-
value.exponentBitPattern | (value.sign == .minus ? 1 : 0 << 31)
149+
value.exponentBitPattern | ((value.sign == .minus ? 1 : 0) << 15)
150150
Swift.withUnsafeBytes(of: signAndExponent.littleEndian) { bytes in
151151
self.append(contentsOf: bytes)
152152
}
@@ -155,7 +155,7 @@ extension Array where Element == UInt8 {
155155
init(bigEndian value: Float80) {
156156
self = []
157157
let signAndExponent =
158-
value.exponentBitPattern | (value.sign == .minus ? 1 : 0 << 31)
158+
value.exponentBitPattern | ((value.sign == .minus ? 1 : 0) << 15)
159159
Swift.withUnsafeBytes(of: signAndExponent.bigEndian) { bytes in
160160
self.append(contentsOf: bytes)
161161
}

0 commit comments

Comments
 (0)