#simd #ndarray #argminmax


ArgMinMax (argmin & argmax in 1 function) with SIMD for floats and integers

10 releases (5 breaking)

0.6.1 Apr 14, 2023
0.6.0 Apr 10, 2023
0.5.0 Mar 23, 2023
0.4.0 Feb 13, 2023
0.1.1 Oct 2, 2022

#3 in #sse

Download history 33/week @ 2023-02-09 29/week @ 2023-02-16 1/week @ 2023-02-23 89/week @ 2023-03-09 128/week @ 2023-03-16 24/week @ 2023-03-23 15/week @ 2023-03-30 1076/week @ 2023-04-06 1807/week @ 2023-04-13 1761/week @ 2023-04-20 1998/week @ 2023-04-27 3405/week @ 2023-05-04 4127/week @ 2023-05-11 4269/week @ 2023-05-18 5294/week @ 2023-05-25

17,376 downloads per month
Used in 25 crates (via polars-ops)

MIT license



Efficient argmin & argmax (in 1 function) with SIMD (SSE, AVX(2), AVX5121, NEON1) for f16, f32, f64, i8, i16, i32, i64, u8, u16, u32, u64.

🚀 The function is generic over the type of the array, so it can be used on &[T] or Vec<T> where T can be f162, f322, f643, i8, i16, i32, i64, u8, u16, u32, u64.

🤝 The trait is implemented for slice, Vec, 1D ndarray::ArrayBase4, apache arrow::PrimitiveArray5 and arrow2::PrimitiveArray6.

Runtime CPU feature detection is used to select the most efficient implementation for the current CPU. This means that the same binary can be used on different CPUs without recompilation.

👀 The SIMD implementation contains no if checks, ensuring that the runtime of the function is independent of the input data its order (best-case = worst-case = average-case).

🪄 Efficient support for f16 and uints: through (bijective aka symmetric) bitwise operations, f16 (optional1) and uints are converted to ordered integers, allowing to use integer SIMD instructions.

1 for AVX512 and most of NEON you should enable the (default) "nightly_simd" feature (requires nightly Rust).
2 for f16 you should enable the "half" feature.
3 for f32 and f64 you should enable the (default) "float" feature.
4 for ndarray::ArrayBase you should enable the "ndarray" feature.
5 for arrow::PrimitiveArray you should enable the "arrow" feature.
6 for arrow2::PrimitiveArray you should enable the "arrow2" feature.


Add the following to your Cargo.toml:

argminmax = "0.6.1"

Example usage

use argminmax::ArgMinMax;  // import trait

let arr: Vec<i32> = (0..200_000).collect();  // create a vector

let (min, max) = arr.argminmax();  // apply extension

println!("min: {}, max: {}", min, max);
println!("arr[min]: {}, arr[max]: {}", arr[min], arr[max]);



Implemented for ints, uints, and floats (if "float" feature enabled).

Provides the following functions:

  • argminmax: returns the index of the minimum and maximum element in the array.

When dealing with NaNs, ArgMinMax its functions ignore NaNs. For more info see Limitations.


Implemented for floats (if "float" feature enabled).

Provides the following functions:

  • nanargminmax: returns the index of the minimum and maximum element in the array.

When dealing with NaNs, NaNArgMinMax its functions return the first NaN its index. For more info see Limitations.

Tip 💡: if you know that there are no NaNs in your the array, we advise you to use ArgMinMax as this should be 5-30% faster than NaNArgMinMax.


  • [default] "nightly_simd": enables the use of non-stable SIMD intrinsics (AVX512 and most of NEON), which are only available on nightly Rust.
  • [default] "float": support f32 and f64 argminmax (uses NaN-handling - see below).
  • "half": support f16 argminmax (through using the half crate).
  • "ndarray": add ArgMinMax trait to ndarray its Array1 & ArrayView1.
  • "arrow": add ArgMinMax trait to arrow its PrimitiveArray.


Benchmarks on my laptop (AMD Ryzen 7 4800U, 1.8 GHz, 16GB RAM) using criterion show that the function is 3-20x faster than the scalar implementation (depending of data type).

See /benches/results.

Run the benchmarks yourself with the following command:

cargo bench --quiet --message-format=short --features half | grep "time:"


To run the tests use the following command:

cargo test --message-format=short --all-features


The library handles NaNs! 🚀

Some (minor) limitations:

  • ArgMinMax its functions ignores NaN values.
    • ❗ When the array contains exclusively NaNs and/or infinities unexpected behaviour can occur (index 0 is returned).
  • NaNArgMinMax its functions returns the first NaN its index (if any present).
    • ❗ When multiple bit-representations for NaNs are used, no guarantee is made that the first NaN is returned.


Some parts of this library are inspired by the great work of minimalrust's argmm project.


~467K SLoC