16字节的alligment问题 [英] 16 byte alligment issue

查看:261
本文介绍了16字节的alligment问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DirectXMath ,创建 XMMatrix XMVector 在类中。

I am using DirectXMath, creating XMMatrix and XMVector in classes.

当我调用 XMMatrixMultiply 时,会抛出未处理的异常。

When I call XMMatrixMultiply it throws unhandled exception on it.

我在网上发现这是byte alligment的问题,因为 DirectXMath 使用 SIMD

I have found online that it is an issue with byte alligment, since DirectXMath uses SIMD instructions set which results in missaligned heap allocation.

建议的解决方案之一是使用 XMFLOAT4X4 变量,然后在需要时将它们更改为临时 XMMatrix ,但它不是最好和最快的解决方案imo。

One of the proposed solution was to use XMFLOAT4X4 variables and then change them to temporary XMMatrix whenever needed, but it isn't the nicest and fastest solution imo.

是使用 _aligned_malloc ,但我不知道如何使用它。

Another one was to use _aligned_malloc, yet I have no idea whatsoever how to use it. I have never had to do any memory allocations and it is black magic for me.

另一个,是重载 new operator ,但他们没有提供任何信息如何做。

Another one, was to overload new operator, yet they did not provide any information how to do it.

对于重载方法,我不使用 new 创建 XMMatrix 对象,因为我不使用它们作为指针。

And regarding the overloading method, I'm not using new to create XMMatrix objects since I don't use them as pointers.

我认为 _alligned_malloc 解决方案最好在这里,但我不知道如何使用它,在哪里和什么时候调用它。

I think _alligned_malloc solution would be best here, but I have no idea how to use it, where and when to call it.

我真的很感激任何帮助。

I would really appreciate any help.

推荐答案

不假装是一个完整的答案。

Not pretend to be a complete answer.

有些方法没有提及:


  • #define _XM_NO_INTRINSICS _ 。简单。慢。现在工作,只是一行代码。 ;)

  • 不要在堆上存储 XMVECTOR XMMATRIX 存储 XMFLOAT4 XMFLOAT4X4 并仅在需要时才转换为SIMD类型(因此它们将存储在堆栈中)。慢点。

  • 不要存储 XMVECTOR XMMATRIX 在堆,第2部分。只是存储你的类在堆栈。快速。挺难的。许多代码需要更改(可能)。

  • 使用aligned allocator。快速。硬。许多小时到google,许多代码写和调试。

  • 不要使用DirectXMath(以前的XMMath)库。选择任何其他(有很多)或写自己的。快速。

  • #define _XM_NO_INTRINSICS_. Simple. Slow. Works right now, just one line of code. ;)
  • Don't store XMVECTOR and XMMATRIX on a heap. Store XMFLOAT4 or XMFLOAT4X4 and convert to SIMD types only when needed (so they will be stored on stack). Slower. Many code to change (probably).
  • Don't store XMVECTOR and XMMATRIX on a heap, part 2. Just store your classes on stack. Fast. Pretty hard. Many code to change (probably).
  • Use aligned allocator. Fast. Hard. Many hours to google, many code to write and debug.
  • Don't use DirectXMath (previously XMMath) library. Choose any other (there are plenty) or write your own. Fast. Many code to change (probably).

如果你想要对齐分配器,它没有DirectX或DirectXMath。这是高级话题。没有人能给你完整的解决方案。但是,以下是一些Google搜索结果:

If you want aligned allocator, it has nothing to DirectX or DirectXMath. It is advanced topic. No one can give you complete solution. But, here are some results of googling:

  • returning aligned memory with new?
  • Harder to C++: Aligned Memory Allocation
  • many more

非常注意。使用不良内存分配器,您可以引入更多的问题,而不是解决。

Be very attentive. With bad memory allocator you can introduce much more problems than solve.

希望它有所帮助。快乐编码! :)

Hope it helps somehow. Happy Coding! :)

这篇关于16字节的alligment问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆