Skip to content

Commit 6eddc2e

Browse files
committed
[stdlib] Don’t do full integer conversions in _roundUp
This code is exercised on pretty much every access to ManagedBuffer — let’s keep it as simple as possible. The overflow case is not expected to ever occur here; this code is used to align offsets within objects, it isn’t expected to get inputs anywhere near Int.max.
1 parent 73ace80 commit 6eddc2e

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

stdlib/public/core/Builtin.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ internal func _roundUp(_ offset: UInt, toAlignment alignment: Int) -> UInt {
3939
@inlinable
4040
internal func _roundUp(_ offset: Int, toAlignment alignment: Int) -> Int {
4141
_internalInvariant(offset >= 0)
42-
return Int(_roundUpImpl(UInt(bitPattern: offset), toAlignment: alignment))
42+
let offset = UInt(bitPattern: offset)
43+
let result = Int(bitPattern: _roundUpImpl(offset, toAlignment: alignment))
44+
_internalInvariant(result >= 0)
45+
return result
4346
}
4447

4548
/// Returns a tri-state of 0 = no, 1 = yes, 2 = maybe.

0 commit comments

Comments
 (0)