gpt4 book ai didi

c++ - 在 C++ 中重载位移运算符

转载 作者:太空狗 更新时间:2023-10-29 23:43:44 25 4
gpt4 key购买 nike

我想为 arm_neon.h 中 ARM 系统上定义的 uint32x4_t 重载位移运算符。

struct uint32x4_t {
uint32_t val[4];
};

这应该通过调用 SIMD 函数来完成,该函数期望值会发生变化并且立即数不变:

uint32x4_t simdShift(uint32x4_t, constant_immediate);

移位.h

#ifndef SHIFT_H
#define SHIFT_H

namespace A {
namespace B {
/*uint32x4_t simdLoad(uint32_t*) {
...
}*/

template<int N>
uint32x4_t shiftRight(uint32x4_t vec) {
return vshrq_n_u32(vec,N);
}
}
}
uint32x4_t operator>>(uint32x4_t const & vec, const int v) {
return A::B::shiftRight<v>(vec);
}
#endif

主要.cpp

#include "shift.h"

int main() {
uint32_t* data = new uint32_t[4];
data[0] = 1;
data[1] = 2;
data[2] = 3;
data[3] = 4;
uint32x4_t reg;// = simdLoad(data);
reg = reg>>3;
return 0;
}

此代码产生错误:

‘uint32x4_t operator>>(const uint32x4_t&, int)’ must have an argument of class or enumerated type uint32x4_t operator>>(uint32x4_t const & vec, const int v) {

是否有一种变通方法可以为 uint32x4_t 等“原生”类型重载 operator>>

编辑:我采用了建议的解决方法,但错误仍然存​​在 :(

最佳答案

对 ErmIg 答案的增量改进:

template<int N>
constexpr std::integral_constant<int, N> i_{};

template<int N>
uint32x4_t operator >>(uint32x4_t value, std::integral_constant<int, N>) noexcept {
return _mm_slli_si128(value, N);
}

int main() {
std::uint32_t data[4] = {1, 2, 3, 4};
uint32x4_t reg;// = simdLoad(&data);
reg = reg >> i_<3>;
}

注意我已将 operator>> 放在全局 namespace 中;如果您想将它放在不同的命名空间中,则需要在使用它之前将运算符放入作用域中。

关于c++ - 在 C++ 中重载位移运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42385752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com