Skip to content

Commit c7d5008

Browse files
committed
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 0bbcae4 commit c7d5008

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
@@ -793,7 +793,7 @@ extension Unsafe${Mutable}RawBufferPointer {
793793
}
794794

795795
_debugPrecondition(
796-
Int(bitPattern: base) % MemoryLayout<S.Element>.stride == 0,
796+
Int(bitPattern: base) & MemoryLayout<S.Element>.alignment-1 == 0,
797797
"buffer base address must be properly aligned to access S.Element"
798798
)
799799

@@ -853,7 +853,7 @@ extension Unsafe${Mutable}RawBufferPointer {
853853
return .init(start: nil, count: 0)
854854
}
855855
_debugPrecondition(
856-
Int(bitPattern: baseAddress) % MemoryLayout<C.Element>.stride == 0,
856+
Int(bitPattern: baseAddress) & MemoryLayout<C.Element>.alignment-1 == 0,
857857
"buffer base address must be properly aligned to access C.Element"
858858
)
859859
_precondition(
@@ -878,7 +878,7 @@ extension Unsafe${Mutable}RawBufferPointer {
878878
}
879879
_internalInvariant(_end != nil)
880880
_debugPrecondition(
881-
Int(bitPattern: baseAddress) % MemoryLayout<C.Element>.stride == 0,
881+
Int(bitPattern: baseAddress) & MemoryLayout<C.Element>.alignment-1 == 0,
882882
"buffer base address must be properly aligned to access C.Element"
883883
)
884884
var iterator = source.makeIterator()
@@ -940,7 +940,7 @@ extension Unsafe${Mutable}RawBufferPointer {
940940
return .init(start: nil, count: 0)
941941
}
942942
_debugPrecondition(
943-
Int(bitPattern: baseAddress) % MemoryLayout<T>.stride == 0,
943+
Int(bitPattern: baseAddress) & MemoryLayout<T>.alignment-1 == 0,
944944
"buffer base address must be properly aligned to access T"
945945
)
946946
_precondition(

0 commit comments

Comments
 (0)