[英]How to implement _mm_and_ps?
我正在嘗試使用浮點值來實現_mm_and_ps
。 文檔說這個函數是 4 個單精度浮點數的按位,但我不確定如何計算 2 個浮點數的按位。 基本上我想實現以下
vector<float> bitwise_and(vector<float>a ,vector<float> b){
assert(a.size()==4);
assert(b.size()==4);
vector<float> res(4);
for(int i=0;i<4;i++)
res[i]=a[i]&b[i]; //here is the problem
return res;
}
您可以通過char*
指針訪問每個字節的數據。
vector<float> bitwise_and(vector<float>a ,vector<float> b){
assert(a.size()==4);
assert(b.size()==4);
vector<float> res(4);
for(int i=0;i<4;i++)
for (size_t j = 0; j < sizeof(float); j++)
reinterpret_cast<char*>(&res[i])[j]=
reinterpret_cast<char*>(&a[i])[j]&
reinterpret_cast<char*>(&b[i])[j];
return res;
}
另一種方法是使用union
以int
訪問float
內存(假設int
與float
具有相同的大小)
vector<float> bitwise_and(vector<float>a ,vector<float> b){
assert(a.size()==4);
assert(b.size()==4);
assert(sizeof(float)==sizeof(int));
vector<float> res(4);
union hoge { float fl; int in; };
for(int i=0;i<4;i++) {
hoge res_h, a_h, b_h;
a_h.fl = a[i];
b_h.fl = b[i];
res_h.in = a_h.in & b_h.in;
res[i] = res_h.fl;
}
return res;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.