关于数学库中的对齐
在各类数学库中,如DirectXMath,XNA,Eigen等,为了使用SSE加速
往往用__declspec(align(16))定义其向量矩阵,因而对编程产生了一些影响
Eigen的说明文档写的比较详细,列举了以下各类情况,并提供了解决方案,使用其他数学库也可以参考
仅就我个人使用而言,主要遇到以下两点问题
- 字节对齐的对象作为参数,因为形参不一定是对齐的,所以会造成程序中断,部分编译器也可能直接报错--解决方法很简单,参数设为指针或者引用型参数
- 类或结构体中包含字节对其的对象--重载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... }