numr 0.16.0

Numerical computing and tensor library for Rust, in the role of NumPy: n-dimensional arrays, linear algebra, FFT, statistics and autograd on CPU, CUDA and WebGPU
Documentation
//! Wrapping integer `neg`, `abs`, `sign` and `square`, plus the exact integer
//! rounding family, for the element-wise unary kernels.
//!
//! The generic unary path converts each element to `f64`, applies the operation
//! and converts back. That is wrong for integers twice over:
//!
//! * `T::from_f64` saturates, so `neg(i32::MIN)` answers `i32::MAX` and
//!   `abs(i8::MIN)` answers `i8::MAX`. The convention
//!   [`wide_acc`](crate::runtime::cpu::kernels::wide_acc) documents, and that
//!   [`binary_int`](crate::runtime::cpu::kernels::binary_int) implements, is
//!   **element-wise integer ops wrap, accumulators saturate**. `neg` and `abs`
//!   are element-wise, so both must answer `MIN`.
//! * `f64` has 53 mantissa bits, so an `i64` or `u64` past 2^53 does not
//!   survive the round trip at all. `neg(9007199254740993)` came back one off.
//!
//! CUDA's `neg_i32` / `abs_i32` (`runtime/cuda/kernels/unary.cu`) and WebGPU's
//! `neg_i32` / `abs_i32` (`runtime/wgpu/shaders/unary_i32.wgsl`) both wrap
//! already, WGSL by definition and CUDA by two's-complement hardware, so this
//! file is what brings CPU into line with them rather than the reverse.
//!
//! Rust's `-x`, `x.abs()` and `x * x` panic on overflow in a debug build and
//! wrap in a release build, so none may appear here: a tensor op's answer must
//! not depend on the build profile.
//!
//! `floor`, `ceil`, `round`, `round_ties_even` and `trunc` are the IDENTITY on
//! an integer dtype: an integer is already a whole number, so there is nothing
//! to round. The `f64` round trip below computed the right value and then threw
//! precision away carrying it back, so `floor(9007199254740993i64)` answered
//! `9007199254740992` and `floor(u64::MAX)` saturated. Answering with the input
//! itself is exact for every width.
//!
//! `neg` on an UNSIGNED dtype wraps like every other element-wise integer op:
//! it is `0 - a` in modular arithmetic, so `neg(1u64)` is `u64::MAX`. That is
//! what this crate's unsigned `sub` already answers and what NumPy answers, so
//! rejecting it would make `neg` disagree with the very next operation.

use crate::dtype::{DType, Element};
use crate::ops::UnaryOp;

/// An integer element type with the wrapping unary arithmetic described above.
trait WrappingIntUnary: Element {
    fn w_neg(self) -> Self;
    fn w_abs(self) -> Self;
    fn w_sign(self) -> Self;
    fn w_square(self) -> Self;
}

macro_rules! impl_signed_unary {
    ($($ty:ty),* $(,)?) => {
        $(
            impl WrappingIntUnary for $ty {
                #[inline]
                fn w_neg(self) -> Self {
                    self.wrapping_neg()
                }
                #[inline]
                fn w_abs(self) -> Self {
                    self.wrapping_abs()
                }
                #[inline]
                fn w_sign(self) -> Self {
                    if self > 0 {
                        1
                    } else if self < 0 {
                        -1
                    } else {
                        0
                    }
                }
                #[inline]
                fn w_square(self) -> Self {
                    self.wrapping_mul(self)
                }
            }
        )*
    };
}

macro_rules! impl_unsigned_unary {
    ($($ty:ty),* $(,)?) => {
        $(
            impl WrappingIntUnary for $ty {
                #[inline]
                fn w_neg(self) -> Self {
                    self.wrapping_neg()
                }
                #[inline]
                fn w_abs(self) -> Self {
                    self
                }
                #[inline]
                fn w_sign(self) -> Self {
                    if self > 0 { 1 } else { 0 }
                }
                #[inline]
                fn w_square(self) -> Self {
                    self.wrapping_mul(self)
                }
            }
        )*
    };
}

impl_signed_unary!(i8, i16, i32, i64);
impl_unsigned_unary!(u8, u16, u32, u64);

/// One element of an integer unary operation.
#[inline]
fn elem<T: WrappingIntUnary>(op: UnaryOp, a: T) -> Option<T> {
    match op {
        UnaryOp::Neg => Some(a.w_neg()),
        UnaryOp::Abs => Some(a.w_abs()),
        UnaryOp::Sign => Some(a.w_sign()),
        UnaryOp::Square => Some(a.w_square()),
        // Rounding an integer changes nothing, whatever the tie rule.
        UnaryOp::Floor
        | UnaryOp::Ceil
        | UnaryOp::Round
        | UnaryOp::RoundTiesEven
        | UnaryOp::Trunc => Some(a),
        _ => None,
    }
}

/// Run `neg`, `abs`, `sign`, `square` or one of the rounding ops over `len`
/// contiguous elements when `T` is an integer dtype.
///
/// Returns `false` for every other operation and every other dtype, leaving the
/// caller on its generic `f64` path. The `op` match sits inside the per-dtype
/// branch but outside the loop, so the branch is resolved once.
///
/// # Safety
///
/// `a` and `out` must be valid for `len` elements, and `out` must not overlap
/// `a` unless it is the same pointer.
#[inline]
pub(super) unsafe fn unary_int_kernel<T: Element>(
    op: UnaryOp,
    a: *const T,
    out: *mut T,
    len: usize,
) -> bool {
    if !matches!(
        op,
        UnaryOp::Neg
            | UnaryOp::Abs
            | UnaryOp::Sign
            | UnaryOp::Square
            | UnaryOp::Floor
            | UnaryOp::Ceil
            | UnaryOp::Round
            | UnaryOp::RoundTiesEven
            | UnaryOp::Trunc
    ) {
        return false;
    }

    macro_rules! run {
        ($ty:ty) => {{
            // SAFETY: the `T::DTYPE` arm that selected this branch proves `T`
            // is exactly `$ty`, so both casts are identity casts.
            let a = a as *const $ty;
            let out = out as *mut $ty;
            unsafe {
                for i in 0..len {
                    // `None` only for an op the guard above excluded.
                    if let Some(v) = elem::<$ty>(op, *a.add(i)) {
                        *out.add(i) = v;
                    }
                }
            }
            true
        }};
    }

    match T::DTYPE {
        DType::I8 => run!(i8),
        DType::I16 => run!(i16),
        DType::I32 => run!(i32),
        DType::I64 => run!(i64),
        DType::U8 => run!(u8),
        DType::U16 => run!(u16),
        DType::U32 => run!(u32),
        DType::U64 => run!(u64),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn neg_and_abs_wrap_at_the_signed_minimum() {
        assert_eq!(elem::<i32>(UnaryOp::Neg, i32::MIN), Some(i32::MIN));
        assert_eq!(elem::<i32>(UnaryOp::Abs, i32::MIN), Some(i32::MIN));
        assert_eq!(elem::<i8>(UnaryOp::Neg, i8::MIN), Some(i8::MIN));
        assert_eq!(elem::<i8>(UnaryOp::Abs, i8::MIN), Some(i8::MIN));
        assert_eq!(elem::<i64>(UnaryOp::Neg, i64::MIN), Some(i64::MIN));
        assert_eq!(elem::<i64>(UnaryOp::Abs, i64::MIN), Some(i64::MIN));
    }

    #[test]
    fn wide_integers_survive_exactly() {
        // 2^53 + 1 has no f64 representation, so the old to_f64 round trip
        // answered one less than this.
        assert_eq!(
            elem::<i64>(UnaryOp::Neg, 9007199254740993),
            Some(-9007199254740993)
        );
        assert_eq!(elem::<u64>(UnaryOp::Neg, 1), Some(u64::MAX));
    }

    #[test]
    fn sign_reports_three_values() {
        assert_eq!(elem::<i16>(UnaryOp::Sign, -7), Some(-1));
        assert_eq!(elem::<i16>(UnaryOp::Sign, 0), Some(0));
        assert_eq!(elem::<i16>(UnaryOp::Sign, 7), Some(1));
        assert_eq!(elem::<i16>(UnaryOp::Sign, i16::MIN), Some(-1));
        assert_eq!(elem::<u8>(UnaryOp::Sign, 0), Some(0));
        assert_eq!(elem::<u8>(UnaryOp::Sign, 200), Some(1));
    }

    #[test]
    fn square_wraps_rather_than_saturating() {
        // The f64 round trip answered i8::MAX (127) for 100 * 100 and
        // u8::MAX (255) for 16 * 16. CUDA's square_i32 already wrapped.
        assert_eq!(elem::<i8>(UnaryOp::Square, 100), Some(16));
        assert_eq!(elem::<u8>(UnaryOp::Square, 16), Some(0));
        assert_eq!(elem::<i32>(UnaryOp::Square, 100_000), Some(1_410_065_408));
        assert_eq!(elem::<i16>(UnaryOp::Square, -7), Some(49));
    }

    #[test]
    fn abs_is_the_identity_on_unsigned_dtypes() {
        assert_eq!(elem::<u32>(UnaryOp::Abs, u32::MAX), Some(u32::MAX));
    }

    #[test]
    fn rounding_is_the_exact_identity_past_the_f64_mantissa() {
        // 2^53 + 1 has no f64 representation, so the round trip answered
        // 9007199254740992 here.
        assert_eq!(
            elem::<i64>(UnaryOp::Floor, 9007199254740993),
            Some(9007199254740993)
        );
        assert_eq!(elem::<u64>(UnaryOp::Ceil, u64::MAX), Some(u64::MAX));
        assert_eq!(elem::<i64>(UnaryOp::Round, i64::MIN), Some(i64::MIN));
        assert_eq!(
            elem::<i64>(UnaryOp::RoundTiesEven, i64::MAX),
            Some(i64::MAX)
        );
        assert_eq!(
            elem::<i64>(UnaryOp::Trunc, -9007199254740993),
            Some(-9007199254740993)
        );
    }

    #[test]
    fn rounding_is_claimed_for_every_integer_width() {
        for op in [
            UnaryOp::Floor,
            UnaryOp::Ceil,
            UnaryOp::Round,
            UnaryOp::RoundTiesEven,
            UnaryOp::Trunc,
        ] {
            assert_eq!(elem::<i8>(op, -7), Some(-7));
            assert_eq!(elem::<u8>(op, 200), Some(200));
            assert_eq!(elem::<i16>(op, i16::MIN), Some(i16::MIN));
            assert_eq!(elem::<u16>(op, u16::MAX), Some(u16::MAX));
            assert_eq!(elem::<i32>(op, i32::MIN), Some(i32::MIN));
            assert_eq!(elem::<u32>(op, u32::MAX), Some(u32::MAX));
        }
    }

    #[test]
    fn other_ops_and_dtypes_are_not_claimed() {
        let (a, mut out) = ([1i32], [0i32]);
        let handled = unsafe { unary_int_kernel(UnaryOp::Sqrt, a.as_ptr(), out.as_mut_ptr(), 1) };
        assert!(!handled);

        let (a, mut out) = ([1.0f64], [0.0f64]);
        let handled = unsafe { unary_int_kernel(UnaryOp::Neg, a.as_ptr(), out.as_mut_ptr(), 1) };
        assert!(!handled);
    }
}