Skip to main content

ruxla_kernels/
buffer.rs

1//! Buffers of any element type, for code that handles element types at run
2//! time (the IR interpreter).
3
4use crate::complex::{c64, c128};
5use crate::dtype::{DType, Element};
6use half::{bf16, f16};
7
8/// The elements of an array of any element type.
9#[derive(Clone, PartialEq, Debug)]
10pub enum Buffer {
11    /// `bool` elements.
12    Bool(Vec<bool>),
13    /// `i8` elements.
14    I8(Vec<i8>),
15    /// `i16` elements.
16    I16(Vec<i16>),
17    /// `i32` elements.
18    I32(Vec<i32>),
19    /// `i64` elements.
20    I64(Vec<i64>),
21    /// `u8` elements.
22    U8(Vec<u8>),
23    /// `u16` elements.
24    U16(Vec<u16>),
25    /// `u32` elements.
26    U32(Vec<u32>),
27    /// `u64` elements.
28    U64(Vec<u64>),
29    /// `f16` elements.
30    F16(Vec<f16>),
31    /// `bf16` elements.
32    BF16(Vec<bf16>),
33    /// `f32` elements.
34    F32(Vec<f32>),
35    /// `f64` elements.
36    F64(Vec<f64>),
37    /// `c64` elements.
38    C64(Vec<c64>),
39    /// `c128` elements.
40    C128(Vec<c128>),
41}
42
43/// Evaluates an expression with the typed elements of a [`Buffer`]: binds
44/// `$v` to the `Vec` of the buffer's element type in each arm, so the body
45/// is instantiated for every element type.
46#[macro_export]
47macro_rules! with_buffer {
48    ($buf:expr, |$v:ident| $body:expr) => {
49        match $buf {
50            $crate::Buffer::Bool($v) => $body,
51            $crate::Buffer::I8($v) => $body,
52            $crate::Buffer::I16($v) => $body,
53            $crate::Buffer::I32($v) => $body,
54            $crate::Buffer::I64($v) => $body,
55            $crate::Buffer::U8($v) => $body,
56            $crate::Buffer::U16($v) => $body,
57            $crate::Buffer::U32($v) => $body,
58            $crate::Buffer::U64($v) => $body,
59            $crate::Buffer::F16($v) => $body,
60            $crate::Buffer::BF16($v) => $body,
61            $crate::Buffer::F32($v) => $body,
62            $crate::Buffer::F64($v) => $body,
63            $crate::Buffer::C64($v) => $body,
64            $crate::Buffer::C128($v) => $body,
65        }
66    };
67}
68
69/// Evaluates an expression with a type parameter `$t` set to the Rust type
70/// of an element type.
71#[macro_export]
72macro_rules! with_dtype {
73    ($dtype:expr, |$t:ident| $body:expr) => {
74        match $dtype {
75            $crate::DType::Bool => {
76                type $t = bool;
77                $body
78            }
79            $crate::DType::I8 => {
80                type $t = i8;
81                $body
82            }
83            $crate::DType::I16 => {
84                type $t = i16;
85                $body
86            }
87            $crate::DType::I32 => {
88                type $t = i32;
89                $body
90            }
91            $crate::DType::I64 => {
92                type $t = i64;
93                $body
94            }
95            $crate::DType::U8 => {
96                type $t = u8;
97                $body
98            }
99            $crate::DType::U16 => {
100                type $t = u16;
101                $body
102            }
103            $crate::DType::U32 => {
104                type $t = u32;
105                $body
106            }
107            $crate::DType::U64 => {
108                type $t = u64;
109                $body
110            }
111            $crate::DType::F16 => {
112                type $t = $crate::f16;
113                $body
114            }
115            $crate::DType::BF16 => {
116                type $t = $crate::bf16;
117                $body
118            }
119            $crate::DType::F32 => {
120                type $t = f32;
121                $body
122            }
123            $crate::DType::F64 => {
124                type $t = f64;
125                $body
126            }
127            $crate::DType::C64 => {
128                type $t = $crate::c64;
129                $body
130            }
131            $crate::DType::C128 => {
132                type $t = $crate::c128;
133                $body
134            }
135        }
136    };
137}
138
139impl Buffer {
140    /// Returns the element type.
141    pub fn dtype(&self) -> DType {
142        with_buffer!(self, |v| element_dtype(v))
143    }
144
145    /// Returns the number of elements.
146    pub fn len(&self) -> usize {
147        with_buffer!(self, |v| v.len())
148    }
149
150    /// Returns whether the buffer has no elements.
151    pub fn is_empty(&self) -> bool {
152        self.len() == 0
153    }
154
155    /// Returns the elements converted to another type (see [`cast`](crate::cast)).
156    pub fn convert(&self, dtype: DType) -> Buffer {
157        with_buffer!(self, |v| with_dtype!(dtype, |U| Buffer::from_vec(
158            v.iter()
159                .map(|&x| crate::cast::<_, U>(x))
160                .collect::<Vec<U>>()
161        )))
162    }
163
164    /// Wraps a vector.
165    pub fn from_vec<T: Element>(v: Vec<T>) -> Buffer {
166        T::wrap(v)
167    }
168
169    /// Views the elements as `T`, or `None` if they have another type.
170    pub fn as_slice<T: Element>(&self) -> Option<&[T]> {
171        T::view(self)
172    }
173}
174
175fn element_dtype<T: Element>(_: &[T]) -> DType {
176    T::DTYPE
177}
178
179/// Wrapping and viewing typed vectors in [`Buffer`]s.
180pub trait BufferElement: Sized {
181    /// Wraps a vector.
182    fn wrap(v: Vec<Self>) -> Buffer;
183    /// Views a buffer's elements, if they have this type.
184    fn view(b: &Buffer) -> Option<&[Self]>;
185}
186
187macro_rules! impl_buffer_element {
188    ($($t:ty => $v:ident;)*) => {$(
189        impl BufferElement for $t {
190            fn wrap(v: Vec<Self>) -> Buffer {
191                Buffer::$v(v)
192            }
193            fn view(b: &Buffer) -> Option<&[Self]> {
194                match b {
195                    Buffer::$v(v) => Some(v),
196                    _ => None,
197                }
198            }
199        }
200    )*};
201}
202
203impl_buffer_element! {
204    bool => Bool; i8 => I8; i16 => I16; i32 => I32; i64 => I64;
205    u8 => U8; u16 => U16; u32 => U32; u64 => U64;
206    f16 => F16; bf16 => BF16; f32 => F32; f64 => F64; c64 => C64; c128 => C128;
207}