Skip to content

Commit fb036e9

Browse files
committed
stdlib: more efficient pointer overflow checks in the UnsafePointer.pointer(to:) functions
Saves the optional nil check for unwrapping the pointer and one condition
1 parent d9fc62e commit fb036e9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

stdlib/public/core/UnsafePointer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ extension UnsafePointer {
453453
guard let o = property._storedInlineOffset else { return nil }
454454
_internalInvariant(o >= 0)
455455
_debugPrecondition(
456-
o == 0 || UnsafeRawPointer(self) < UnsafeRawPointer(bitPattern: 0 &- o)!,
456+
!UInt(bitPattern: self).addingReportingOverflow(UInt(bitPattern: o)).overflow,
457457
"Overflow in pointer arithmetic"
458458
)
459459
return .init(Builtin.gepRaw_Word(_rawValue, o._builtinWordValue))
@@ -1341,7 +1341,7 @@ extension UnsafeMutablePointer {
13411341
guard let o = property._storedInlineOffset else { return nil }
13421342
_internalInvariant(o >= 0)
13431343
_debugPrecondition(
1344-
o == 0 || UnsafeRawPointer(self) < UnsafeRawPointer(bitPattern: 0 &- o)!,
1344+
!UInt(bitPattern: self).addingReportingOverflow(UInt(bitPattern: o)).overflow,
13451345
"Overflow in pointer arithmetic"
13461346
)
13471347
return .init(Builtin.gepRaw_Word(_rawValue, o._builtinWordValue))
@@ -1365,7 +1365,7 @@ extension UnsafeMutablePointer {
13651365
guard let o = property._storedInlineOffset else { return nil }
13661366
_internalInvariant(o >= 0)
13671367
_debugPrecondition(
1368-
o == 0 || UnsafeRawPointer(self) < UnsafeRawPointer(bitPattern: 0 &- o)!,
1368+
!UInt(bitPattern: self).addingReportingOverflow(UInt(bitPattern: o)).overflow,
13691369
"Overflow in pointer arithmetic"
13701370
)
13711371
return .init(Builtin.gepRaw_Word(_rawValue, o._builtinWordValue))

0 commit comments

Comments
 (0)