arduano / simdeez

easy simd
MIT License
332 stars 25 forks source link

inconsistent overflow behavior between scalar and simd #27

Closed lovasoa closed 4 years ago

lovasoa commented 4 years ago

The following code :

use simdeez::*;
use simdeez::scalar::*;
use simdeez::avx2::*;

#[inline(always)]
fn add<S: Simd>() -> i32 {
    unsafe {
      (S::set1_epi32(std::i32::MAX) + S::set1_epi32(1))[0]
    }
}

fn main() {
    println!("add avx2 = {}", add::<Avx2>());
    println!("add scalar = {}", add::<Scalar>());
}

gives the following output :

add avx2 = -2147483648
thread 'main' panicked at 'attempt to add with overflow', /home/ophir/.cargo/registry/src/github.com-1ecc6299db9ec823/simdeez-1.0.0/src/overloads/add.rs:17:15
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This is because the simd variant implements wrapping addition (which is expected), but the scalar variant uses the default rust + semantics for i32: it panics on overflow unless compiled in release mode.

I think the semantics of the arithmetic operators should be the same for all variants, preferably wrapping addition.