gpt4 book ai didi

Swift 结构外部填充

转载 作者:可可西里 更新时间:2023-11-01 01:09:03 26 4
gpt4 key购买 nike

所以我在 swift 中有以下结构

typealias float = Float32
typealias Point = float2
typealias Int = UInt32

//This is a struct that is meant to be the elements in an array that is immensly acessable to c
//CAREFUL this is LIKELY mirrored in a different thing
public struct Info {
var position:Point = Point(x: 0, y: 0)
var strength:float = 1
var altitude:float = 3.141592 / 2.0
var azmuth:Point = Point(x: 0, y: 0)
var state:Int = 0
}

此结构存储在一个数组中,该数组由 Array(repeating: ...

每一帧我都将指向该数组的指针传递到我的 C++ 代码中,在这里我有一个镜像结构

struct Info {
float positionX;
float positionY;
float strength;
float altitude;
float azmuthX;
float azmuthY;
int state;
float pad; //WHy???
};

现在注意它在那里的额外 float “pad”,因为当我去读取第一个元素以外的元素时,在结构声明中没有它,数据被一个 float 的大小偏移(事情会移动一个字段结束)。

为什么 c++ 结构上的填充字段是必需的? Swift 中额外的 float 或额外填充的原因是什么?

最佳答案

float2是 C 类型的 Swift 映射 simd_float2 , 这是在 <simd/vector_types.h 中定义作为

/*! @abstract A vector of two 32-bit floating-point numbers.
* @description In C++ and Metal, this type is also available as
* simd::float2. The alignment of this type is greater than the alignment
* of float; if you need to operate on data buffers that may not be
* suitably aligned, you should access them using simd_packed_float2
* instead. */
typedef __attribute__((__ext_vector_type__(2))) float simd_float2;

重点是

The alignment of this type is greater than the alignment of float

你可以用

验证
print(MemoryLayout<float>.alignment)  // 4
print(MemoryLayout<float2>.alignment) // 8

这会导致 Swift 类型的对齐 Info到 8,及其步幅(即偏移量连续 Info 之间的字节数存储在数组中的元素)为 32。

print(MemoryLayout<Info>.alignment) // 8
print(MemoryLayout<Info>.stride) // 32

另一方面,C 类型 struct Info只有floatint成员,它们都具有 4 个字节的对齐方式。没有决赛 float pad;成员,数组中这种类型的连续元素之间的偏移量是 28 字节,而不是 32。

这解释了差异。你实际上应该做的是定义仅 C 中的类型,并将该定义导入 Swift 中。这是保证保留内存布局的唯一方法,正如 Apple 的 Joe Groff 所写 Mapping C semantics to Swift :

If you depend on a specific layout, you should define the struct in C and import it into Swift for now.

关于Swift 结构外部填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49716569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com