https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125648

Ulf Iversen <ulf.iversen at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ulf.iversen at gmail dot com

--- Comment #1 from Ulf Iversen <ulf.iversen at gmail dot com> ---
I've also encountered this bug.

It "works on my machine" meaning with `-march=native` and `-march=znver5` it
works as expected.
But it fails when I have `-march=x86-64`, `-march=x86-64-v3`, `-march=znver2`
or `-mavx2`/no `-march` at all.

This minimal repro only produces the error with -O0 (print 0 without
optimizations, 1 with).
g++-16 -std=c++26 -march=x86-64 -O0 repro_minimal.cpp

```
#include <simd>
#include <cstdio>

int main()
{
    const char* cstr = "abcd";
    const auto v = std::simd::partial_load<std::simd::vec<char>>(cstr, 4);
    printf("%i\n", v[2] == 'c');
}
```

This larger repro is independent of optimization level (-O0/-Os/-O2/-O3)
g++-16 -std=c++26 -march=x86-64 -O3 repro2.cpp
```
#include <simd>

#include <bit>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <string>
#include <vector>

int main()
{
    using vec_c = std::simd::vec<char>;
    using bits_t = std::uint64_t;
    using vec_bits = std::simd::rebind_t<bits_t, vec_c>;
    using mask_bits = typename vec_bits::mask_type;
    const std::size_t lanes = vec_c::size();

    std::string text(lanes + 4, 'x');
    text[lanes + 0] = ',';
    text[lanes + 1] = ',';
    text[lanes + 2] = ',';
    text[lanes + 3] = ',';

    const vec_bits bit_weight(
        [](auto j) { return bits_t(1) << static_cast<unsigned>(j); });

    std::vector<std::size_t> found;

    for (std::size_t i = 0; i < text.size(); i += lanes) {
        const std::size_t remaining = std::min(lanes, text.size() - i);
        const auto v = std::simd::partial_load<vec_c>(text.data() + i,
                                                      
static_cast<std::ptrdiff_t>(remaining));
        const auto hit = (v == vec_c(','));
        if (!std::simd::any_of(hit)) {
            continue;
        }

        const mask_bits hit_wide(hit);
        const auto masked = std::simd::select(hit_wide, bit_weight,
vec_bits(0));
        bits_t bits = std::simd::reduce(masked, std::bit_or<>());
        if (remaining < lanes) {
            bits &= (bits_t(1) << remaining) - 1;
        }

        while (bits != 0) {
            const auto lane = static_cast<std::size_t>(std::countr_zero(bits));
            found.push_back(i + lane);
            bits &= bits - 1;
        }
    }

    std::printf("lanes = %zu\n", lanes);
    std::printf("expected 4 positions: %zu %zu %zu %zu\n", lanes + 0, lanes +
1, lanes + 2,
                lanes + 3);
    std::printf("found %zu position(s): ", found.size());
    for (auto p : found) {
        std::printf("%zu ", p);
    }
    std::printf("\n");

    const bool ok = (found.size() == 4);
    std::printf("\n%s\n", ok ? "PASS" : "FAIL");
    return ok ? 0 : 1;
}
```

g++-16 --version
g++-16 (Ubuntu 16-20260322-1ubuntu1) 16.0.1 20260322 (experimental) [trunk
r16-8246-g569ace1fa50]
Copyright (C) 2026 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Reply via email to