Why does the compiler not use SIMD in my range-expression?

175 Views Asked by At

I have got two implementations of a dot-product: One hand-coded https://godbolt.org/z/48EEnnY4r

int bla2(const std::vector<int>& a, const std::vector<int>& b){

    int res = 0;
    for(size_t i=0; i < a.size(); ++i){
        res += a[i]*b[i];
    }
    return res;
}

and one using C++23's std::views::zip https://godbolt.org/z/TsGW1WYnf

int bla(const std::vector<int>& a, const std::vector<int>& b){

    int res = 0;
    for(const auto& [x,y]  : std::views::zip(a,b)){
        res += x*y;
    }
    return res;
}

In godbolt the hand-coded version uses a lot of SIMD instructions, while the zip-based implementation doesn't. What's going on here? If I implement it using iterators it also gets SIMD. I thought under the hood ranges just use iterators. Are these expression not equivalent?

0

There are 0 best solutions below