关于数学库中的对齐

在各类数学库中,如DirectXMath,XNA,Eigen等,为了使用SSE加速

往往用__declspec(align(16))定义其向量矩阵,因而对编程产生了一些影响

Eigen的说明文档写的比较详细,列举了以下各类情况,并提供了解决方案,使用其他数学库也可以参考

  Explanation of the assertion on unaligned arrays
 
  Fixed-size vectorizable Eigen objects
 
  Structures Having Eigen Members
 
  Using STL Containers with Eigen
 
  Passing Eigen objects by value to functions
 
  Compiler making a wrong assumption on stack alignment

仅就我个人使用而言,主要遇到以下两点问题

  • 字节对齐的对象作为参数,因为形参不一定是对齐的,所以会造成程序中断,部分编译器也可能直接报错--解决方法很简单,参数设为指针或者引用型参数
  • 类或结构体中包含字节对其的对象--重载new和delete
template<size_t Alignment>
class CAlignedAllocation
{
public:
    static void* operator new(size_t size) { return _aligned_malloc(size, Alignment); }
    static void operator delete(void* memory) { _aligned_free(memory); }
};

class CMyClass:
public CAlignedAllocation<16>
{
  DirectX::XMVector...  
}

猜你喜欢

转载自www.cnblogs.com/Foxy-Jo/p/11075005.html
今日推荐