Skip to content

Commit c6f4bcd

Browse files
committed
[SE-0101] MemoryLayout: Migrate testsuite and benchmarks
1 parent 0915063 commit c6f4bcd

25 files changed

+163
-163
lines changed

benchmark/single-source/BitCount.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Foundation
1717
import TestsUtils
1818

1919
func countBitSet(_ num: Int) -> Int {
20-
let bits = sizeof(Int.self) * 8
20+
let bits = MemoryLayout<Int>.size * 8
2121
var cnt: Int = 0
2222
var mask: Int = 1
2323
for _ in 0...bits {

test/1_stdlib/Character.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ CharacterTests.test("literal") {
136136

137137
CharacterTests.test("sizeof") {
138138
// FIXME: should be 8.
139-
// <rdar://problem/16754935> sizeof(Character.self) is 9, should be 8
139+
// <rdar://problem/16754935> MemoryLayout<Character>.size is 9, should be 8
140140

141-
let size1 = sizeof(Character.self)
141+
let size1 = MemoryLayout<Character>.size
142142
expectTrue(size1 == 8 || size1 == 9)
143143

144144
var a: Character = "a"
145-
let size2 = sizeofValue(a)
145+
let size2 = MemoryLayout._ofInstance(a).size
146146
expectTrue(size2 == 8 || size2 == 9)
147147

148148
expectEqual(size1, size2)

test/1_stdlib/Runtime.swift.gyb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1483,9 +1483,9 @@ func computeCountLeadingZeroes(_ x: Int64) -> Int64 {
14831483

14841484
BitTwiddlingTestSuite.test("_pointerSize") {
14851485
#if arch(i386) || arch(arm)
1486-
expectEqual(4, sizeof(Optional<AnyObject>.self))
1486+
expectEqual(4, MemoryLayout<Optional<AnyObject>>.size)
14871487
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le)
1488-
expectEqual(8, sizeof(Optional<AnyObject>.self))
1488+
expectEqual(8, MemoryLayout<Optional<AnyObject>>.size)
14891489
#else
14901490
fatalError("implement")
14911491
#endif

test/1_stdlib/TestData.swift

+16-16
Original file line numberDiff line numberDiff line change
@@ -458,19 +458,19 @@ class TestData : TestDataSuper {
458458

459459
func testCopyBytes() {
460460
let c = 10
461-
let underlyingBuffer = malloc(c * strideof(UInt16.self))!
461+
let underlyingBuffer = malloc(c * MemoryLayout<UInt16>.stride)!
462462
let u16Ptr = underlyingBuffer.bindMemory(to: UInt16.self, capacity: c)
463463
let buffer = UnsafeMutableBufferPointer<UInt16>(start: u16Ptr, count: c)
464464

465465
buffer[0] = 0
466466
buffer[1] = 0
467467

468-
var data = Data(capacity: c * strideof(UInt16.self))
469-
data.resetBytes(in: 0..<c * strideof(UInt16.self))
468+
var data = Data(capacity: c * MemoryLayout<UInt16>.stride)
469+
data.resetBytes(in: 0..<c * MemoryLayout<UInt16>.stride)
470470
data[0] = 0xFF
471471
data[1] = 0xFF
472472
let copiedCount = data.copyBytes(to: buffer)
473-
expectEqual(copiedCount, c * strideof(UInt16.self))
473+
expectEqual(copiedCount, c * MemoryLayout<UInt16>.stride)
474474

475475
expectEqual(buffer[0], 0xFFFF)
476476
free(underlyingBuffer)
@@ -481,7 +481,7 @@ class TestData : TestDataSuper {
481481
var data = a.withUnsafeBufferPointer {
482482
return Data(buffer: $0)
483483
}
484-
let expectedSize = strideof(UInt8.self) * a.count
484+
let expectedSize = MemoryLayout<UInt8>.stride * a.count
485485
expectEqual(expectedSize, data.count)
486486

487487
let underlyingBuffer = unsafeBitCast(malloc(expectedSize - 1)!, to: UnsafeMutablePointer<UInt8>.self)
@@ -505,7 +505,7 @@ class TestData : TestDataSuper {
505505
var data = a.withUnsafeBufferPointer {
506506
return Data(buffer: $0)
507507
}
508-
let expectedSize = strideof(Int32.self) * a.count
508+
let expectedSize = MemoryLayout<Int32>.stride * a.count
509509
expectEqual(expectedSize, data.count)
510510

511511
let underlyingBuffer = unsafeBitCast(malloc(expectedSize + 1)!, to: UnsafeMutablePointer<UInt8>.self)
@@ -700,14 +700,14 @@ class TestData : TestDataSuper {
700700
return Data(buffer: $0)
701701
}
702702

703-
var expectedSize = strideof(Int32.self) * a.count
703+
var expectedSize = MemoryLayout<Int32>.stride * a.count
704704
expectEqual(expectedSize, data.count)
705705

706706
[false, true].withUnsafeBufferPointer {
707707
data.append($0)
708708
}
709709

710-
expectedSize += strideof(Bool.self) * 2
710+
expectedSize += MemoryLayout<Bool>.stride * 2
711711
expectEqual(expectedSize, data.count)
712712

713713
let underlyingBuffer = unsafeBitCast(malloc(expectedSize)!, to: UnsafeMutablePointer<UInt8>.self)
@@ -810,51 +810,51 @@ class TestData : TestDataSuper {
810810
return Data(buffer: $0)
811811
}
812812

813-
expectEqual(data.count, strideof(MyStruct.self) * 3)
813+
expectEqual(data.count, MemoryLayout<MyStruct>.stride * 3)
814814

815815

816816
// append
817817
stuff.withUnsafeBufferPointer {
818818
data.append($0)
819819
}
820820

821-
expectEqual(data.count, strideof(MyStruct.self) * 6)
821+
expectEqual(data.count, MemoryLayout<MyStruct>.stride * 6)
822822

823823
// copyBytes
824824
do {
825825
// equal size
826-
let underlyingBuffer = malloc(6 * strideof(MyStruct.self))!
826+
let underlyingBuffer = malloc(6 * MemoryLayout<MyStruct>.stride)!
827827
defer { free(underlyingBuffer) }
828828

829829
let ptr = underlyingBuffer.bindMemory(to: MyStruct.self, capacity: 6)
830830
let buffer = UnsafeMutableBufferPointer<MyStruct>(start: ptr, count: 6)
831831

832832
let byteCount = data.copyBytes(to: buffer)
833-
expectEqual(6 * strideof(MyStruct.self), byteCount)
833+
expectEqual(6 * MemoryLayout<MyStruct>.stride, byteCount)
834834
}
835835

836836
do {
837837
// undersized
838-
let underlyingBuffer = malloc(3 * strideof(MyStruct.self))!
838+
let underlyingBuffer = malloc(3 * MemoryLayout<MyStruct>.stride)!
839839
defer { free(underlyingBuffer) }
840840

841841
let ptr = underlyingBuffer.bindMemory(to: MyStruct.self, capacity: 3)
842842
let buffer = UnsafeMutableBufferPointer<MyStruct>(start: ptr, count: 3)
843843

844844
let byteCount = data.copyBytes(to: buffer)
845-
expectEqual(3 * strideof(MyStruct.self), byteCount)
845+
expectEqual(3 * MemoryLayout<MyStruct>.stride, byteCount)
846846
}
847847

848848
do {
849849
// oversized
850-
let underlyingBuffer = malloc(12 * strideof(MyStruct.self))!
850+
let underlyingBuffer = malloc(12 * MemoryLayout<MyStruct>.stride)!
851851
defer { free(underlyingBuffer) }
852852

853853
let ptr = underlyingBuffer.bindMemory(to: MyStruct.self, capacity: 6)
854854
let buffer = UnsafeMutableBufferPointer<MyStruct>(start: ptr, count: 6)
855855

856856
let byteCount = data.copyBytes(to: buffer)
857-
expectEqual(6 * strideof(MyStruct.self), byteCount)
857+
expectEqual(6 * MemoryLayout<MyStruct>.stride, byteCount)
858858
}
859859
}
860860

test/1_stdlib/UnsafeRawPointer.swift

+26-26
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class Missile {
1616
UnsafeMutableRawPointerExtraTestSuite.test("initializeMemory") {
1717
Missile.missilesLaunched = 0
1818
do {
19-
let sizeInBytes = 3 * strideof(Missile.self)
19+
let sizeInBytes = 3 * MemoryLayout<Missile>.stride
2020
var p1 = UnsafeMutableRawPointer.allocate(
21-
bytes: sizeInBytes, alignedTo: alignof(Missile.self))
21+
bytes: sizeInBytes, alignedTo: MemoryLayout<Missile>.alignment)
2222
defer {
23-
p1.deallocate(bytes: sizeInBytes, alignedTo: alignof(Missile.self))
23+
p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Missile>.alignment)
2424
}
2525
var ptrM = p1.initializeMemory(as: Missile.self, to: Missile(1))
2626
p1.initializeMemory(as: Missile.self, at: 1, count: 2, to: Missile(2))
@@ -29,9 +29,9 @@ UnsafeMutableRawPointerExtraTestSuite.test("initializeMemory") {
2929
expectEqual(2, ptrM[2].number)
3030

3131
var p2 = UnsafeMutableRawPointer.allocate(
32-
bytes: sizeInBytes, alignedTo: alignof(Missile.self))
32+
bytes: sizeInBytes, alignedTo: MemoryLayout<Missile>.alignment)
3333
defer {
34-
p2.deallocate(bytes: sizeInBytes, alignedTo: alignof(Missile.self))
34+
p2.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Missile>.alignment)
3535
}
3636
let ptrM2 = p2.moveInitializeMemory(as: Missile.self, from: ptrM, count: 3)
3737
defer {
@@ -55,11 +55,11 @@ UnsafeMutableRawPointerExtraTestSuite.test("initializeMemory") {
5555
}
5656

5757
UnsafeMutableRawPointerExtraTestSuite.test("bindMemory") {
58-
let sizeInBytes = 3 * strideof(Int.self)
58+
let sizeInBytes = 3 * MemoryLayout<Int>.stride
5959
var p1 = UnsafeMutableRawPointer.allocate(
60-
bytes: sizeInBytes, alignedTo: alignof(Int.self))
60+
bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
6161
defer {
62-
p1.deallocate(bytes: sizeInBytes, alignedTo: alignof(Int.self))
62+
p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
6363
}
6464
let ptrI = p1.bindMemory(to: Int.self, capacity: 3)
6565
ptrI.initialize(from: 1...3)
@@ -72,33 +72,33 @@ UnsafeMutableRawPointerExtraTestSuite.test("bindMemory") {
7272
}
7373

7474
UnsafeMutableRawPointerExtraTestSuite.test("load/store") {
75-
let sizeInBytes = 3 * strideof(Int.self)
75+
let sizeInBytes = 3 * MemoryLayout<Int>.stride
7676
var p1 = UnsafeMutableRawPointer.allocate(
77-
bytes: sizeInBytes, alignedTo: alignof(Int.self))
77+
bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
7878
defer {
79-
p1.deallocate(bytes: sizeInBytes, alignedTo: alignof(Int.self))
79+
p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
8080
}
8181
let ptrI = p1.initializeMemory(as: Int.self, from: 1...3)
8282
defer {
8383
ptrI.deinitialize(count: 3)
8484
}
8585
expectEqual(1, p1.load(as: Int.self))
86-
expectEqual(2, p1.load(fromByteOffset: strideof(Int.self), as: Int.self))
87-
expectEqual(3, p1.load(fromByteOffset: 2*strideof(Int.self), as: Int.self))
86+
expectEqual(2, p1.load(fromByteOffset: MemoryLayout<Int>.stride, as: Int.self))
87+
expectEqual(3, p1.load(fromByteOffset: 2*MemoryLayout<Int>.stride, as: Int.self))
8888
p1.storeBytes(of: 4, as: Int.self)
89-
p1.storeBytes(of: 5, toByteOffset: strideof(Int.self), as: Int.self)
90-
p1.storeBytes(of: 6, toByteOffset: 2 * strideof(Int.self), as: Int.self)
89+
p1.storeBytes(of: 5, toByteOffset: MemoryLayout<Int>.stride, as: Int.self)
90+
p1.storeBytes(of: 6, toByteOffset: 2 * MemoryLayout<Int>.stride, as: Int.self)
9191
expectEqual(4, p1.load(as: Int.self))
92-
expectEqual(5, p1.load(fromByteOffset: strideof(Int.self), as: Int.self))
93-
expectEqual(6, p1.load(fromByteOffset: 2 * strideof(Int.self), as: Int.self))
92+
expectEqual(5, p1.load(fromByteOffset: MemoryLayout<Int>.stride, as: Int.self))
93+
expectEqual(6, p1.load(fromByteOffset: 2 * MemoryLayout<Int>.stride, as: Int.self))
9494
}
9595

9696
UnsafeMutableRawPointerExtraTestSuite.test("copyBytes") {
97-
let sizeInBytes = 4 * strideof(Int.self)
97+
let sizeInBytes = 4 * MemoryLayout<Int>.stride
9898
var rawPtr = UnsafeMutableRawPointer.allocate(
99-
bytes: sizeInBytes, alignedTo: alignof(Int.self))
99+
bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
100100
defer {
101-
rawPtr.deallocate(bytes: sizeInBytes, alignedTo: alignof(Int.self))
101+
rawPtr.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
102102
}
103103
let ptrI = rawPtr.initializeMemory(as: Int.self, count: 4, to: 42)
104104
defer {
@@ -108,32 +108,32 @@ UnsafeMutableRawPointerExtraTestSuite.test("copyBytes") {
108108
// Right overlap
109109
ptrI[0] = 1
110110
ptrI[1] = 2
111-
(rawPtr + strideof(Int.self)).copyBytes(
112-
from: roPtr, count: 2 * strideof(Int.self))
111+
(rawPtr + MemoryLayout<Int>.stride).copyBytes(
112+
from: roPtr, count: 2 * MemoryLayout<Int>.stride)
113113
expectEqual(1, ptrI[1])
114114
expectEqual(2, ptrI[2])
115115

116116
// Left overlap
117117
ptrI[1] = 2
118118
ptrI[2] = 3
119119
rawPtr.copyBytes(
120-
from: roPtr + strideof(Int.self), count: 2 * strideof(Int.self))
120+
from: roPtr + MemoryLayout<Int>.stride, count: 2 * MemoryLayout<Int>.stride)
121121
expectEqual(2, ptrI[0])
122122
expectEqual(3, ptrI[1])
123123

124124
// Disjoint:
125125
ptrI[2] = 2
126126
ptrI[3] = 3
127127
rawPtr.copyBytes(
128-
from: roPtr + 2 * strideof(Int.self), count: 2 * strideof(Int.self))
128+
from: roPtr + 2 * MemoryLayout<Int>.stride, count: 2 * MemoryLayout<Int>.stride)
129129
expectEqual(2, ptrI[0])
130130
expectEqual(3, ptrI[1])
131131

132132
// Backwards
133133
ptrI[0] = 0
134134
ptrI[1] = 1
135-
(rawPtr + 2 * strideof(Int.self)).copyBytes(
136-
from: roPtr, count: 2 * strideof(Int.self))
135+
(rawPtr + 2 * MemoryLayout<Int>.stride).copyBytes(
136+
from: roPtr, count: 2 * MemoryLayout<Int>.stride)
137137
expectEqual(0, ptrI[2])
138138
expectEqual(1, ptrI[3])
139139
}

test/1_stdlib/simd.swift.gyb

+29-29
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,35 @@ var simdTestSuite = TestSuite("simd")
5959

6060
simdTestSuite.test("sizes") {
6161
// C interop requires that vector be the right size.
62-
expectEqual(8, sizeof(float2.self))
63-
expectEqual(16, sizeof(float3.self))
64-
expectEqual(16, sizeof(float4.self))
65-
expectEqual(8, sizeof(int2.self))
66-
expectEqual(16, sizeof(int3.self))
67-
expectEqual(16, sizeof(int4.self))
68-
expectEqual(16, sizeof(double2.self))
69-
expectEqual(32, sizeof(double3.self))
70-
expectEqual(32, sizeof(double4.self))
71-
72-
expectEqual(16, sizeof(float2x2.self))
73-
expectEqual(32, sizeof(float2x3.self))
74-
expectEqual(32, sizeof(float2x4.self))
75-
expectEqual(24, sizeof(float3x2.self))
76-
expectEqual(48, sizeof(float3x3.self))
77-
expectEqual(48, sizeof(float3x4.self))
78-
expectEqual(32, sizeof(float4x2.self))
79-
expectEqual(64, sizeof(float4x3.self))
80-
expectEqual(64, sizeof(float4x4.self))
81-
82-
expectEqual(32, sizeof(double2x2.self))
83-
expectEqual(64, sizeof(double2x3.self))
84-
expectEqual(64, sizeof(double2x4.self))
85-
expectEqual(48, sizeof(double3x2.self))
86-
expectEqual(96, sizeof(double3x3.self))
87-
expectEqual(96, sizeof(double3x4.self))
88-
expectEqual(64, sizeof(double4x2.self))
89-
expectEqual(128, sizeof(double4x3.self))
90-
expectEqual(128, sizeof(double4x4.self))
62+
expectEqual(8, MemoryLayout<float2>.size)
63+
expectEqual(16, MemoryLayout<float3>.size)
64+
expectEqual(16, MemoryLayout<float4>.size)
65+
expectEqual(8, MemoryLayout<int2>.size)
66+
expectEqual(16, MemoryLayout<int3>.size)
67+
expectEqual(16, MemoryLayout<int4>.size)
68+
expectEqual(16, MemoryLayout<double2>.size)
69+
expectEqual(32, MemoryLayout<double3>.size)
70+
expectEqual(32, MemoryLayout<double4>.size)
71+
72+
expectEqual(16, MemoryLayout<float2x2>.size)
73+
expectEqual(32, MemoryLayout<float2x3>.size)
74+
expectEqual(32, MemoryLayout<float2x4>.size)
75+
expectEqual(24, MemoryLayout<float3x2>.size)
76+
expectEqual(48, MemoryLayout<float3x3>.size)
77+
expectEqual(48, MemoryLayout<float3x4>.size)
78+
expectEqual(32, MemoryLayout<float4x2>.size)
79+
expectEqual(64, MemoryLayout<float4x3>.size)
80+
expectEqual(64, MemoryLayout<float4x4>.size)
81+
82+
expectEqual(32, MemoryLayout<double2x2>.size)
83+
expectEqual(64, MemoryLayout<double2x3>.size)
84+
expectEqual(64, MemoryLayout<double2x4>.size)
85+
expectEqual(48, MemoryLayout<double3x2>.size)
86+
expectEqual(96, MemoryLayout<double3x3>.size)
87+
expectEqual(96, MemoryLayout<double3x4>.size)
88+
expectEqual(64, MemoryLayout<double4x2>.size)
89+
expectEqual(128, MemoryLayout<double4x3>.size)
90+
expectEqual(128, MemoryLayout<double4x4>.size)
9191
}
9292

9393
simdTestSuite.test("vector init") {

test/Constraints/diagnostics.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ func read2(_ p: UnsafeMutableRawPointer, maxLength: Int) {}
834834
func read<T : Integer>() -> T? {
835835
var buffer : T
836836
let n = withUnsafePointer(to: &buffer) { (p) in
837-
read2(UnsafePointer(p), maxLength: sizeof(T)) // expected-error {{cannot convert value of type 'UnsafePointer<_>' to expected argument type 'UnsafeMutableRawPointer'}}
837+
read2(UnsafePointer(p), maxLength: MemoryLayout<T>.size) // expected-error {{cannot convert value of type 'UnsafePointer<_>' to expected argument type 'UnsafeMutableRawPointer'}}
838838
}
839839
}
840840

test/Interpreter/SDK/Accelerate.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extension vU1024: ExpressibleByIntegerLiteral, CustomStringConvertible, Equatabl
1010
public init(integerLiteral: Int) {
1111
var integerLiteral = integerLiteral
1212
self.init()
13-
memcpy(&self, &integerLiteral, sizeof(Int.self))
13+
memcpy(&self, &integerLiteral, MemoryLayout<Int>.size)
1414
}
1515

1616
init(_ int: Int) {
@@ -37,7 +37,7 @@ extension Int {
3737
var u1024 = u1024
3838
// NB: Doesn't overflow check
3939
self.init()
40-
memcpy(&self, &u1024, sizeof(Int.self))
40+
memcpy(&self, &u1024, MemoryLayout<Int>.size)
4141
}
4242
}
4343

@@ -61,7 +61,7 @@ func quorem(_ x: vU1024, _ y: vU1024) -> (vU1024, vU1024) {
6161
public func ==(x: vU1024, y: vU1024) -> Bool {
6262
var x = x
6363
var y = y
64-
return memcmp(&x, &y, sizeof(vU1024.self)) == 0
64+
return memcmp(&x, &y, MemoryLayout<vU1024>.size) == 0
6565
}
6666

6767
func factorial(_ x: Int) -> vU1024 {

0 commit comments

Comments
 (0)