use cubecl_ir::ExpandValue;
use half::{bf16, f16};
use super::{CubePrimitive, Vector};
use crate::prelude::*;
use crate::{
ir::{Scope, attributes::IndexAttr, dialect::plane::*},
unexpanded,
};
pub trait PlaneNumeric {
fn __expand_native_sum(scope: &Scope, value: ExpandValue) -> ExpandValue;
fn __expand_native_inclusive_sum(scope: &Scope, value: ExpandValue) -> ExpandValue;
fn __expand_native_exclusive_sum(scope: &Scope, value: ExpandValue) -> ExpandValue;
fn __expand_native_prod(scope: &Scope, value: ExpandValue) -> ExpandValue;
fn __expand_native_inclusive_prod(scope: &Scope, value: ExpandValue) -> ExpandValue;
fn __expand_native_exclusive_prod(scope: &Scope, value: ExpandValue) -> ExpandValue;
fn __expand_native_plane_min(scope: &Scope, value: ExpandValue) -> ExpandValue;
fn __expand_native_plane_max(scope: &Scope, value: ExpandValue) -> ExpandValue;
}
macro_rules! plane_numeric {
($($ty: ty),*; $sum: ty, $inc_sum: ty, $exc_sum: ty, $prod: ty, $inc_prod: ty, $exc_prod: ty, $min: ty, $max: ty) => {
$(impl PlaneNumeric for $ty {
fn __expand_native_sum(scope: &Scope, value: ExpandValue) -> ExpandValue {
unary_expand(scope, value, <$sum>::new)
}
fn __expand_native_inclusive_sum(scope: &Scope, value: ExpandValue) -> ExpandValue {
unary_expand(scope, value, <$inc_sum>::new)
}
fn __expand_native_exclusive_sum(scope: &Scope, value: ExpandValue) -> ExpandValue {
unary_expand(scope, value, <$exc_sum>::new)
}
fn __expand_native_prod(scope: &Scope, value: ExpandValue) -> ExpandValue {
unary_expand(scope, value, <$prod>::new)
}
fn __expand_native_inclusive_prod(scope: &Scope, value: ExpandValue) -> ExpandValue {
unary_expand(scope, value, <$inc_prod>::new)
}
fn __expand_native_exclusive_prod(scope: &Scope, value: ExpandValue) -> ExpandValue {
unary_expand(scope, value, <$exc_prod>::new)
}
fn __expand_native_plane_min(scope: &Scope, value: ExpandValue) -> ExpandValue {
unary_expand(scope, value, <$min>::new)
}
fn __expand_native_plane_max(scope: &Scope, value: ExpandValue) -> ExpandValue {
unary_expand(scope, value, <$max>::new)
}
})*
};
}
plane_numeric!(i8, i16, i32, i64, isize; ISumOp, InclusiveISumOp, ExclusiveISumOp, IProdOp, InclusiveIProdOp, ExclusiveIProdOp, SMinOp, SMaxOp);
plane_numeric!(u8, u16, u32, u64, usize; ISumOp, InclusiveISumOp, ExclusiveISumOp, IProdOp, InclusiveIProdOp, ExclusiveIProdOp, UMinOp, UMaxOp);
plane_numeric!(f16, bf16, f32, flex32, tf32, f64; FSumOp, InclusiveFSumOp, ExclusiveFSumOp, FProdOp, InclusiveFProdOp, ExclusiveFProdOp, FMinOp, FMaxOp);
pub fn plane_elect() -> bool {
unexpanded!()
}
pub mod plane_elect {
use super::*;
pub fn expand(scope: &Scope) -> NativeExpand<bool> {
let op = ElectOp::new(scope.ctx_mut());
scope.register_with_result(&op).into()
}
}
#[allow(unused_variables)]
pub fn plane_broadcast<E: CubePrimitive>(value: E, index: u32) -> E {
unexpanded!()
}
pub mod plane_broadcast {
use super::*;
pub fn expand<E: CubePrimitive>(
scope: &Scope,
value: NativeExpand<E>,
id: u32,
) -> NativeExpand<E> {
let value = value.read_value(scope);
let op = BroadcastOp::new(scope.ctx_mut(), value, IndexAttr::new(id as usize));
scope.register_with_result(&op).into()
}
}
#[allow(unused_variables)]
pub fn plane_shuffle<E: CubePrimitive>(value: E, src_lane: u32) -> E {
unexpanded!()
}
pub mod plane_shuffle {
use super::*;
pub fn expand<E: CubePrimitive>(
scope: &Scope,
value: NativeExpand<E>,
src_lane: NativeExpand<u32>,
) -> NativeExpand<E> {
let value = value.read_value(scope);
let src_lane = src_lane.read_value(scope);
let op = ShuffleOp::new(scope.ctx_mut(), value, src_lane);
scope.register_with_result(&op).into()
}
}
#[allow(unused_variables)]
pub fn plane_shuffle_xor<E: CubePrimitive>(value: E, mask: u32) -> E {
unexpanded!()
}
pub mod plane_shuffle_xor {
use super::*;
pub fn expand<E: CubePrimitive>(
scope: &Scope,
value: NativeExpand<E>,
mask: NativeExpand<u32>,
) -> NativeExpand<E> {
let value = value.read_value(scope);
let mask = mask.read_value(scope);
let op = ShuffleXorOp::new(scope.ctx_mut(), value, mask);
scope.register_with_result(&op).into()
}
}
#[allow(unused_variables)]
pub fn plane_shuffle_up<E: CubePrimitive>(value: E, delta: u32) -> E {
unexpanded!()
}
pub mod plane_shuffle_up {
use super::*;
pub fn expand<E: CubePrimitive>(
scope: &Scope,
value: NativeExpand<E>,
delta: NativeExpand<u32>,
) -> NativeExpand<E> {
let value = value.read_value(scope);
let delta = delta.read_value(scope);
let op = ShuffleUpOp::new(scope.ctx_mut(), value, delta);
scope.register_with_result(&op).into()
}
}
#[allow(unused_variables)]
pub fn plane_shuffle_down<E: CubePrimitive>(value: E, delta: u32) -> E {
unexpanded!()
}
pub mod plane_shuffle_down {
use super::*;
pub fn expand<E: CubePrimitive>(
scope: &Scope,
value: NativeExpand<E>,
delta: NativeExpand<u32>,
) -> NativeExpand<E> {
let value = value.read_value(scope);
let delta = delta.read_value(scope);
let op = ShuffleDownOp::new(scope.ctx_mut(), value, delta);
scope.register_with_result(&op).into()
}
}
#[allow(unused_variables)]
pub fn plane_sum<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
unexpanded!()
}
pub mod plane_sum {
use super::*;
pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
scope: &Scope,
elem: NativeExpand<E>,
) -> NativeExpand<E> {
E::Scalar::__expand_native_sum(scope, elem.into()).into()
}
}
#[allow(unused_variables)]
pub fn plane_inclusive_sum<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
unexpanded!()
}
pub mod plane_inclusive_sum {
use super::*;
pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
scope: &Scope,
elem: NativeExpand<E>,
) -> NativeExpand<E> {
E::Scalar::__expand_native_inclusive_sum(scope, elem.into()).into()
}
}
#[allow(unused_variables)]
pub fn plane_exclusive_sum<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
unexpanded!()
}
pub mod plane_exclusive_sum {
use super::*;
pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
scope: &Scope,
elem: NativeExpand<E>,
) -> NativeExpand<E> {
E::Scalar::__expand_native_exclusive_sum(scope, elem.into()).into()
}
}
pub fn plane_prod<E: CubePrimitive<Scalar: PlaneNumeric>>(_elem: E) -> E {
unexpanded!()
}
pub mod plane_prod {
use super::*;
pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
scope: &Scope,
elem: NativeExpand<E>,
) -> NativeExpand<E> {
E::Scalar::__expand_native_prod(scope, elem.into()).into()
}
}
#[allow(unused_variables)]
pub fn plane_inclusive_prod<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
unexpanded!()
}
pub mod plane_inclusive_prod {
use super::*;
pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
scope: &Scope,
elem: NativeExpand<E>,
) -> NativeExpand<E> {
E::Scalar::__expand_native_inclusive_prod(scope, elem.into()).into()
}
}
#[allow(unused_variables)]
pub fn plane_exclusive_prod<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
unexpanded!()
}
pub mod plane_exclusive_prod {
use super::*;
pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
scope: &Scope,
elem: NativeExpand<E>,
) -> NativeExpand<E> {
E::Scalar::__expand_native_exclusive_prod(scope, elem.into()).into()
}
}
pub fn plane_max<E: CubePrimitive<Scalar: PlaneNumeric>>(_elem: E) -> E {
unexpanded!()
}
pub mod plane_max {
use super::*;
pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
scope: &Scope,
elem: NativeExpand<E>,
) -> NativeExpand<E> {
E::Scalar::__expand_native_plane_max(scope, elem.into()).into()
}
}
pub fn plane_min<E: CubePrimitive<Scalar: PlaneNumeric>>(_elem: E) -> E {
unexpanded!()
}
pub mod plane_min {
use super::*;
pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
scope: &Scope,
elem: NativeExpand<E>,
) -> NativeExpand<E> {
E::Scalar::__expand_native_plane_min(scope, elem.into()).into()
}
}
pub fn plane_all(_elem: bool) -> bool {
unexpanded!()
}
pub mod plane_all {
use super::*;
pub fn expand(scope: &Scope, elem: NativeExpand<bool>) -> NativeExpand<bool> {
let value = elem.read_value(scope);
let op = AllOp::new(scope.ctx_mut(), value);
scope.register_with_result(&op).into()
}
}
pub fn plane_any(_elem: bool) -> bool {
unexpanded!()
}
pub mod plane_any {
use super::*;
pub fn expand(scope: &Scope, elem: NativeExpand<bool>) -> NativeExpand<bool> {
let value = elem.read_value(scope);
let op = AnyOp::new(scope.ctx_mut(), value);
scope.register_with_result(&op).into()
}
}
pub fn plane_ballot(_elem: bool) -> Vector<u32, Const<4>> {
unexpanded!()
}
pub mod plane_ballot {
use super::*;
pub fn expand(scope: &Scope, elem: NativeExpand<bool>) -> NativeExpand<Vector<u32, Const<4>>> {
let value = elem.read_value(scope);
let op = BallotOp::new(scope.ctx_mut(), value);
scope.register_with_result(&op).into()
}
}