简体   繁体   中英

How to implement _mm_and_ps?

I am trying to implement _mm_and_ps using float values. The documentation says this function is the bitwise of 4 single precision floating point numbers, but I am not sure how to calculate the bitwise of 2 floating point numbers. Basically I want to implement the following

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;

}

You can access each bytes of data via char* pointer.

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;

}

Another way is to use union to access memory for float as int (assuming int has same size as 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM