Skip to content

Commit 2e16361

Browse files
stephentyroneglessard
authored andcommitted
Check the base address against the element alignment rather than stride
Stride is the gap between elements, not the alignment that each element must satisfy (stride need not even be a power of two).
1 parent 6c75168 commit 2e16361

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ extension Unsafe${Mutable}RawBufferPointer {
781781
}
782782

783783
_debugPrecondition(
784-
Int(bitPattern: base) % MemoryLayout<S.Element>.stride == 0,
784+
Int(bitPattern: base) & MemoryLayout<S.Element>.alignment-1 == 0,
785785
"buffer base address must be properly aligned to access S.Element"
786786
)
787787

@@ -841,7 +841,7 @@ extension Unsafe${Mutable}RawBufferPointer {
841841
return .init(start: nil, count: 0)
842842
}
843843
_debugPrecondition(
844-
Int(bitPattern: baseAddress) % MemoryLayout<C.Element>.stride == 0,
844+
Int(bitPattern: baseAddress) & MemoryLayout<C.Element>.alignment-1 == 0,
845845
"buffer base address must be properly aligned to access C.Element"
846846
)
847847
_precondition(
@@ -866,7 +866,7 @@ extension Unsafe${Mutable}RawBufferPointer {
866866
}
867867
_internalInvariant(_end != nil)
868868
_debugPrecondition(
869-
Int(bitPattern: baseAddress) % MemoryLayout<C.Element>.stride == 0,
869+
Int(bitPattern: baseAddress) & MemoryLayout<C.Element>.alignment-1 == 0,
870870
"buffer base address must be properly aligned to access C.Element"
871871
)
872872
var iterator = source.makeIterator()
@@ -928,7 +928,7 @@ extension Unsafe${Mutable}RawBufferPointer {
928928
return .init(start: nil, count: 0)
929929
}
930930
_debugPrecondition(
931-
Int(bitPattern: baseAddress) % MemoryLayout<T>.stride == 0,
931+
Int(bitPattern: baseAddress) & MemoryLayout<T>.alignment-1 == 0,
932932
"buffer base address must be properly aligned to access T"
933933
)
934934
_precondition(

0 commit comments

Comments
 (0)