Skip to content

Surprising behavior of portable SIMD integer vector max/min #94682

Closed
@RalfJung

Description

@RalfJung

The following code does not print what I would expect:

#![feature(portable_simd)]
use std::simd::*;

fn main() {
    let a = i32x4::splat(10);
    let b = i32x4::from_array([1, 2, 12, -4]);
    println!("{:?} {:?}", a.max(b), a.min(b));
}

I expected element-wise max/min, but I got:

[10, 10, 10, 10] [1, 2, 12, -4]

The funny thing is that portable-simd does not even define max/min on its integer SIMD types (only on the float ones). What happens is that it falls back to Ord::max, and it looks like vectors implement Ord by comparing them as arrays, which probably uses lexicographic comparison.

However this means integer and float vectors max/min methods behave very differently, so this is probably not intended?

Cc @workingjubilee

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions