use crate::{error::Result, kernels::macros::ops};
use core::ffi::c_void;
ops!(
add,
sub,
mul,
div,
pow,
maximum,
minimum,
logical_and,
logical_or,
logical_xor,
eq,
ne,
lt,
le,
gt,
ge
);
pub fn call_ops_binary(
kernel_name: crate::kernels::macros::Kernel,
lhs: *const c_void,
rhs: *const c_void,
output: *mut c_void,
metadata: &[usize],
) -> Result<()> {
unsafe {
dispatch_binary(kernel_name.0, lhs, rhs, output, metadata.as_ptr());
}
Ok(())
}
macro_rules! declare_and_dispatch_binary {
($($op:ident),* $(,)?) => {
paste::paste! {
extern "C" {
$(
fn [<$op _bool>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _f8e4m3>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _f8e5m2>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _bf16>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _f16>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _f32>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _f64>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _u8>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _u16>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _u32>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _u64>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _i8>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _i16>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _i32>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
fn [<$op _i64>](lhs: *const c_void, rhs: *const c_void, output: *mut c_void, metadata: *const usize);
)*
}
unsafe fn dispatch_binary(
name: &str,
lhs: *const c_void,
rhs: *const c_void,
output: *mut c_void,
metadata: *const usize,
) {
match name {
$(
concat!(stringify!($op), "_bool") => [<$op _bool>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_f8e4m3") => [<$op _f8e4m3>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_f8e5m2") => [<$op _f8e5m2>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_bf16") => [<$op _bf16>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_f16") => [<$op _f16>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_f32") => [<$op _f32>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_f64") => [<$op _f64>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_u8") => [<$op _u8>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_u16") => [<$op _u16>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_u32") => [<$op _u32>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_u64") => [<$op _u64>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_i8") => [<$op _i8>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_i16") => [<$op _i16>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_i32") => [<$op _i32>](lhs, rhs, output, metadata),
concat!(stringify!($op), "_i64") => [<$op _i64>](lhs, rhs, output, metadata),
)*
_ => panic!("Unsupported binary kernel: {}", name),
}
}
}
};
}
declare_and_dispatch_binary!(
add,
sub,
mul,
div,
pow,
maximum,
minimum,
logical_and,
logical_or,
logical_xor,
eq,
ne,
lt,
le,
gt,
ge
);