ruxla-kernels 0.1.0

Internal crate of ruxla (use the `ruxla` crate): semantics of ruxla's operations, shared by the native backend and the IR interpreter
Documentation
//! Buffers of any element type, for code that handles element types at run
//! time (the IR interpreter).

use crate::complex::{c64, c128};
use crate::dtype::{DType, Element};
use half::{bf16, f16};

/// The elements of an array of any element type.
#[derive(Clone, PartialEq, Debug)]
pub enum Buffer {
    /// `bool` elements.
    Bool(Vec<bool>),
    /// `i8` elements.
    I8(Vec<i8>),
    /// `i16` elements.
    I16(Vec<i16>),
    /// `i32` elements.
    I32(Vec<i32>),
    /// `i64` elements.
    I64(Vec<i64>),
    /// `u8` elements.
    U8(Vec<u8>),
    /// `u16` elements.
    U16(Vec<u16>),
    /// `u32` elements.
    U32(Vec<u32>),
    /// `u64` elements.
    U64(Vec<u64>),
    /// `f16` elements.
    F16(Vec<f16>),
    /// `bf16` elements.
    BF16(Vec<bf16>),
    /// `f32` elements.
    F32(Vec<f32>),
    /// `f64` elements.
    F64(Vec<f64>),
    /// `c64` elements.
    C64(Vec<c64>),
    /// `c128` elements.
    C128(Vec<c128>),
}

/// Evaluates an expression with the typed elements of a [`Buffer`]: binds
/// `$v` to the `Vec` of the buffer's element type in each arm, so the body
/// is instantiated for every element type.
#[macro_export]
macro_rules! with_buffer {
    ($buf:expr, |$v:ident| $body:expr) => {
        match $buf {
            $crate::Buffer::Bool($v) => $body,
            $crate::Buffer::I8($v) => $body,
            $crate::Buffer::I16($v) => $body,
            $crate::Buffer::I32($v) => $body,
            $crate::Buffer::I64($v) => $body,
            $crate::Buffer::U8($v) => $body,
            $crate::Buffer::U16($v) => $body,
            $crate::Buffer::U32($v) => $body,
            $crate::Buffer::U64($v) => $body,
            $crate::Buffer::F16($v) => $body,
            $crate::Buffer::BF16($v) => $body,
            $crate::Buffer::F32($v) => $body,
            $crate::Buffer::F64($v) => $body,
            $crate::Buffer::C64($v) => $body,
            $crate::Buffer::C128($v) => $body,
        }
    };
}

/// Evaluates an expression with a type parameter `$t` set to the Rust type
/// of an element type.
#[macro_export]
macro_rules! with_dtype {
    ($dtype:expr, |$t:ident| $body:expr) => {
        match $dtype {
            $crate::DType::Bool => {
                type $t = bool;
                $body
            }
            $crate::DType::I8 => {
                type $t = i8;
                $body
            }
            $crate::DType::I16 => {
                type $t = i16;
                $body
            }
            $crate::DType::I32 => {
                type $t = i32;
                $body
            }
            $crate::DType::I64 => {
                type $t = i64;
                $body
            }
            $crate::DType::U8 => {
                type $t = u8;
                $body
            }
            $crate::DType::U16 => {
                type $t = u16;
                $body
            }
            $crate::DType::U32 => {
                type $t = u32;
                $body
            }
            $crate::DType::U64 => {
                type $t = u64;
                $body
            }
            $crate::DType::F16 => {
                type $t = $crate::f16;
                $body
            }
            $crate::DType::BF16 => {
                type $t = $crate::bf16;
                $body
            }
            $crate::DType::F32 => {
                type $t = f32;
                $body
            }
            $crate::DType::F64 => {
                type $t = f64;
                $body
            }
            $crate::DType::C64 => {
                type $t = $crate::c64;
                $body
            }
            $crate::DType::C128 => {
                type $t = $crate::c128;
                $body
            }
        }
    };
}

impl Buffer {
    /// Returns the element type.
    pub fn dtype(&self) -> DType {
        with_buffer!(self, |v| element_dtype(v))
    }

    /// Returns the number of elements.
    pub fn len(&self) -> usize {
        with_buffer!(self, |v| v.len())
    }

    /// Returns whether the buffer has no elements.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the elements converted to another type (see [`cast`](crate::cast)).
    pub fn convert(&self, dtype: DType) -> Buffer {
        with_buffer!(self, |v| with_dtype!(dtype, |U| Buffer::from_vec(
            v.iter()
                .map(|&x| crate::cast::<_, U>(x))
                .collect::<Vec<U>>()
        )))
    }

    /// Wraps a vector.
    pub fn from_vec<T: Element>(v: Vec<T>) -> Buffer {
        T::wrap(v)
    }

    /// Views the elements as `T`, or `None` if they have another type.
    pub fn as_slice<T: Element>(&self) -> Option<&[T]> {
        T::view(self)
    }
}

fn element_dtype<T: Element>(_: &[T]) -> DType {
    T::DTYPE
}

/// Wrapping and viewing typed vectors in [`Buffer`]s.
pub trait BufferElement: Sized {
    /// Wraps a vector.
    fn wrap(v: Vec<Self>) -> Buffer;
    /// Views a buffer's elements, if they have this type.
    fn view(b: &Buffer) -> Option<&[Self]>;
}

macro_rules! impl_buffer_element {
    ($($t:ty => $v:ident;)*) => {$(
        impl BufferElement for $t {
            fn wrap(v: Vec<Self>) -> Buffer {
                Buffer::$v(v)
            }
            fn view(b: &Buffer) -> Option<&[Self]> {
                match b {
                    Buffer::$v(v) => Some(v),
                    _ => None,
                }
            }
        }
    )*};
}

impl_buffer_element! {
    bool => Bool; i8 => I8; i16 => I16; i32 => I32; i64 => I64;
    u8 => U8; u16 => U16; u32 => U32; u64 => U64;
    f16 => F16; bf16 => BF16; f32 => F32; f64 => F64; c64 => C64; c128 => C128;
}