Skip to content

u64x4::clamp producing unexpected and incorrect results #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jacherr opened this issue Mar 4, 2022 · 2 comments · Fixed by #262
Closed

u64x4::clamp producing unexpected and incorrect results #253

Jacherr opened this issue Mar 4, 2022 · 2 comments · Fixed by #262
Labels
C-bug Category: Bug

Comments

@Jacherr
Copy link

Jacherr commented Mar 4, 2022

It has been observed that, at least on x86 systems, the usage of std::simd::u64x4::clamp may cause undefined behaviour under seemingly random conditions, or at least may not function properly.

The issue can be reproduced in the Rust Playground (Edit: in both debug and release modes):

#![feature(portable_simd)]
use std::simd::*;
fn main() {
    let x = u64x4::from_array([10,1,1,1]);
    let y = x.clamp(u64x4::splat(0), u64x4::splat(9));
    let z = y.as_array();
    dbg!(z);
}

I expected this code to produce a result of [9, 1, 1, 1].
The code, in my testing, instead produced a result of [9, 9, 9, 9].

#![feature(portable_simd)]
use std::simd::*;
fn main() {
    let x = u64x4::from_array([1,1,1,10]);
    let y = x.clamp(u64x4::splat(0), u64x4::splat(9));
    let z = y.as_array();
    dbg!(z);
}

This example should have produced a result of [1, 1, 1, 9].
It instead produced a result of [1, 1, 1, 10].

Meta

rustc --version --verbose:

rustc 1.61.0-nightly (10913c000 2022-03-03)
binary: rustc
commit-hash: 10913c00018c76103b2fd4260d8c02ec728fd244
commit-date: 2022-03-03
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

@Jacherr Jacherr added the C-bug Category: Bug label Mar 4, 2022
@programmerjake
Copy link
Member

that's because clamp is currently only implemented for floats. you're getting Ord::clamp instead, which is giving the correct results (since it lexicographically compares the vectors and returns one of the input vectors, rather than the element-wise clamp your expecting).

@programmerjake
Copy link
Member

this is nearly identical to what's happening in #247

we need to add clamp (and min/max) functions for integer vectors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants