在许多官方 Apple 示例和 Xcode 模板中,使用 将两个float4x4矩阵相乘simd_mul,例如:
simd_mul(viewMatrix, modelMatrix)
Run Code Online (Sandbox Code Playgroud)
现在, simd.h 为其*类型提供了一个运算符,因此上面的行可以写为
viewMatrix * modelMatrix
Run Code Online (Sandbox Code Playgroud)
以上两种方法在使用或性能上有什么区别吗?我只能调查,simd.h所以我不知道该方法是如何实际实现的,但我想这只是对simd_mul.
没有区别。Swift 是开源的,在 simd.swift.gyb 中你会发现
% for k in [2,3,4]:
/// Matrix multiplication (the "usual" matrix product, not the elementwise
/// product).
% restype = ctype[type] + str(k) + 'x' + str(rows)
% rhstype = ctype[type] + str(k) + 'x' + str(cols)
@_transparent
public static func *(lhs: ${mattype}, rhs: ${rhstype}) -> ${restype} {
return simd_mul(lhs, rhs)
}
% end # for k in [2,3,4]
Run Code Online (Sandbox Code Playgroud)
.gyb 文件(“生成你的样板”)由一个特殊的 Swift 预处理器进行预处理(比较https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20151207/000226.html)。上面的代码嵌套在循环中
%for type in floating_types:
% for rows in [2,3,4]:
% for cols in [2,3,4]:
% mattype = 'simd_' + ctype[type] + str(cols) + 'x' + str(rows)
// ....
% end # for cols in [2,3,4]
% end # for rows in [2,3,4]
%end # for type in floating_types
Run Code Online (Sandbox Code Playgroud)
以便它最终扩展到*所有可能的矩阵操作数组合的运算符定义。
| 归档时间: |
|
| 查看次数: |
417 次 |
| 最近记录: |