1#![allow(clippy::bool_comparison)]
2#![allow(clippy::unnecessary_cast)]
3
4mod comparison;
5mod ite;
6pub use comparison::Comp;
7pub use ite::IfThenElse;
8
9use ndarray::*;
10
11use crate::broadcast::multi_broadcast;
12use crate::internal::*;
13
14bin_to_super_type!(and, And,
15 [bool, u8, u16, u32, u64, i8, i16, i32, i64] => |c, &a, &b| *c = (a as i64 != 0 && b as i64 != 0) as _);
16bin_to_super_type!(or, Or,
17 [bool, u8, u16, u32, u64, i8, i16, i32, i64] => |c, &a, &b| *c = (a as i64 != 0 || b as i64 != 0) as _);
18bin_to_super_type!(xor, Xor, [bool] => |c, &a, &b| *c = a ^ b);
19
20element_wise!(not, Not, [bool] => |_, vs| {
21 vs.iter_mut().for_each(|a| *a = !*a);
22 Ok(())
23});
24
25#[derive(Debug, Clone, new, Default, Hash)]
26pub struct Iff;
27
28impl Iff {
29 pub unsafe fn eval_t<T: Datum>(
30 cond: &ArrayViewD<bool>,
31 out: &mut Tensor,
32 t: &Tensor,
33 f: &Tensor,
34 ) {
35 unsafe {
36 Zip::from(out.to_array_view_mut_unchecked::<T>())
37 .and_broadcast(cond)
38 .and_broadcast(t.to_array_view_unchecked::<T>())
39 .and_broadcast(f.to_array_view_unchecked::<T>())
40 .for_each(|r, c, t, f| *r = if *c { t.clone() } else { f.clone() })
41 }
42 }
43}
44
45impl Op for Iff {
46 fn name(&self) -> StaticName {
47 "Iff".into()
48 }
49 op_as_typed_op!();
50}
51
52impl EvalOp for Iff {
53 fn is_stateless(&self) -> bool {
54 true
55 }
56
57 fn eval(&self, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
58 let (cond, t, f) = args_3!(inputs);
59 anyhow::ensure!(t.datum_type() == f.datum_type());
60 let shape: TVec<usize> = multi_broadcast(&[cond.shape(), t.shape(), f.shape()])?;
61 unsafe {
62 let mut result = Tensor::uninitialized_dt(t.datum_type(), &shape)?;
63 let cond = cond.to_array_view::<bool>()?;
64 dispatch_datum_by_size!(Self::eval_t(t.datum_type())(&cond, &mut result, &t, &f));
65 Ok(tvec!(result.into_tvalue()))
66 }
67 }
68}
69
70impl TypedOp for Iff {
71 as_op!();
72
73 fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
74 anyhow::ensure!(inputs.len() == 3, "Iff expects 3 intputs.");
75 if inputs[1].datum_type != inputs[2].datum_type {
76 bail!("Then and else tensors type mismatch ({:?} and {:?}).", inputs[1], inputs[2]);
77 }
78 if inputs[0].rank() != inputs[1].rank() || inputs[0].rank() != inputs[2].rank() {
79 bail!("Inconsistent ranks, {:?}", inputs);
80 }
81 let shape = multi_broadcast(&[
82 inputs[0].shape.to_tvec(),
83 inputs[1].shape.to_tvec(),
84 inputs[2].shape.to_tvec(),
85 ])
86 .unwrap();
87 Ok(tvec!(inputs[1].datum_type.fact(shape)))
88 }
89
90 fn axes_mapping(
91 &self,
92 inputs: &[&TypedFact],
93 outputs: &[&TypedFact],
94 ) -> TractResult<AxesMapping> {
95 AxesMapping::natural(inputs, outputs)
96 }
97}
98
99bin_to_super_type!(bitand, BitAnd,
100 [bool, u8, u16, u32, u64, i8, i16, i32, i64] => |c, &a, &b| *c = a & b);
101bin_to_super_type!(bitor, BitOr,
102 [bool, u8, u16, u32, u64, i8, i16, i32, i64] => |c, &a, &b| *c = a | b);
103bin_to_super_type!(bitxor, BitXor,
104 [bool, u8, u16, u32, u64, i8, i16, i32, i64] => |c, &a, &b| *c = a ^ b);
105
106element_wise!(bitnot, BitNot, [bool, u8, u16, u32, u64, i8, i16, i32, i64] => |_, xs| {
107 xs.iter_mut().for_each(|x| *x = !*x);
108 Ok(())
109});