Skip to content

Mask operations are not optimized well #472

@DaniPopes

Description

@DaniPopes

I tried this code (godbolt):

#![feature(portable_simd)]

use std::simd::prelude::*;

type Simd = u8x16;

#[no_mangle]
pub fn check_hex(chunk: Simd) -> bool {
    let valid_digit = chunk.simd_ge(Simd::splat(b'0')) & chunk.simd_le(Simd::splat(b'9'));
    let valid_upper = chunk.simd_ge(Simd::splat(b'A')) & chunk.simd_le(Simd::splat(b'F'));
    let valid_lower = chunk.simd_ge(Simd::splat(b'a')) & chunk.simd_le(Simd::splat(b'f'));
    let valid = valid_digit | valid_upper | valid_lower;
    valid.all()
}

I expected to see this happen: optimizes as well as the hand-rolled x86 version

Instead, this happened: doesn't, see godbolt

Meta

rustc --version --verbose:

rustc 1.91.0-nightly (1ebbd87a6 2025-08-11)
binary: rustc
commit-hash: 1ebbd87a62ce96a72b22da61b7c2c43893534842
commit-date: 2025-08-11
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions