Avatar
あれ、もしかして、withMemoryReboundってアライメント治すの?
2:41 AM
@swift-5.1.5 import Foundation let array: [UInt8] = [ 0x10, 0x20, 0x30, 0x40, 0x11, 0x21, 0x31, 0x41, 0x99] func reboundToInt(pointer: UnsafePointer<UInt8>) -> Int { return pointer.withMemoryRebound(to: Int.self, capacity: 1) { (ip) in ip.pointee } } func assumeToInt(pointer: UnsafePointer<UInt8>) -> Int { return UnsafeRawPointer(pointer).assumingMemoryBound(to: Int.self).pointee } let a = array.withUnsafeBufferPointer { (buf) -> Int in reboundToInt(pointer: buf.baseAddress!) } print(String(format: "%016llx", a), a == Int(bitPattern: 0x4131211140302010)) let b = array.withUnsafeBufferPointer { (buf) -> Int in reboundToInt(pointer: buf.baseAddress!.advanced(by: 1)) } print(String(format: "%016llx", b), b == Int(bitPattern: 0x9941312111403020)) let c = array.withUnsafeBufferPointer { (buf) -> Int in assumeToInt(pointer: buf.baseAddress!) } print(String(format: "%016llx", c), c == Int(bitPattern: 0x4131211140302010)) let d = array.withUnsafeBufferPointer { (buf) -> Int in assumeToInt(pointer: buf.baseAddress!.advanced(by: 1)) } print(String(format: "%016llx", d), d == Int(bitPattern: 0x9941312111403020))