伙计们,想象我有一个模板功能:
template <typename T> Vector<T>* Vector<T>::overwrite(const Vector<T>* copy) {
this->_normalized = copy->_normalized;
this->_data[0] = copy->_data[0];
this->_data[1] = copy->_data[1];
this->_data[2] = copy->_data[2];
this->_data[3] = copy->_data[3];
return this;
}
及其规格:
template <> Vector<float>* Vector<float>::overwrite(const Vector<float>* copy) {
__m128 data = _mm_load_ps(copy->_data);
_mm_store_ps(this->_data, data);
return this;
}
现在我想确保处理器支持 SSE,特别是处理器具有 XMM 寄存器,可以使用 1 条指令复制 4 个浮点数。然后我想对双精度有相同的功能,所以我需要 YMM 寄存器。
所以我想知道是否有办法在运行时确定 XMM 和 YMM 的可用性。
另一个更可取的选择是在预处理器工作期间以某种方式知道。即这样我就写了类似的东西:
template <typename T> Vector<T>* Vector<T>::overwrite(const Vector<T>* copy) {
this->_normalized = copy->_normalized;
this->_data[0] = copy->_data[0];
this->_data[1] = copy->_data[1];
this->_data[2] = copy->_data[2];
this->_data[3] = copy->_data[3];
return this;
}
#ifdef XMM_ARE_AVAILABLE
template <> Vector<float>* Vector<float>::overwrite(const Vector<float>* copy) {
__m128 data = _mm_load_ps(copy->_data);
_mm_store_ps(this->_data, data);
return this;
}
#endif
#ifdef YMM_ARE_AVAILABLE
template <> Vector<double>* Vector<double>::overwrite(const Vector<double>* copy) {
/* code that moves four doubles */
return this;
}
#endif
谢谢!