1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::fmt::Debug;

use crate::ffi::*;
use crate::{Output};

use libc::size_t;

#[macro_export]
macro_rules! apply_kind {
    ($primitive_t:ty, $kind:ident, $func:ident, [ $( $arg:expr ),* ]) => {
        paste::item! {
            match $kind {
                $crate::NumberTypeKind::Primitive => $func::<$primitive_t>($( $arg ),*),
                $crate::NumberTypeKind::Two => $func::<[<$primitive_t 2>]>($( $arg ),*),
                $crate::NumberTypeKind::Three => $func::<[<$primitive_t 3>]>($( $arg ),*),
                $crate::NumberTypeKind::Four => $func::<[<$primitive_t 4>]>($( $arg ),*),
                $crate::NumberTypeKind::Eight => $func::<[<$primitive_t 8>]>($( $arg ),*),
                $crate::NumberTypeKind::Sixteen => $func::<[<$primitive_t 16>]>($( $arg ),*),
            }
        }
    }
}

#[macro_export]
macro_rules! apply_number_type {
    ($num_type:expr, $func:ident, [ $( $arg:expr ),* ]) => {
        match $num_type.number_type() {
            $crate::NumberType::ClBool => $func::<cl_bool>($( $arg ),*),
            $crate::NumberType::ClDouble => $func::<cl_double>($( $arg ),*),
            $crate::NumberType::ClSizeT => $func::<size_t>($( $arg ),*),
            $crate::NumberType::ClHalf => $func::<cl_half>($( $arg ),*),
            $crate::NumberType::ClChar(kind) => apply_kind!(cl_char, kind, $func, $( $arg ),*),
            $crate::NumberType::ClUchar(kind) => apply_kind!(cl_u_char, kind, $func, $( $arg ),*),
            $crate::NumberType::ClShort(kind) => apply_kind!(cl_short, kind, $func, $( $arg ),*),
            $crate::NumberType::ClUshort(kind) => apply_kind!(cl_ushort, kind, $func, $( $arg ),*),
            $crate::NumberType::ClInt(kind) => apply_kind!(cl_int, kind, $func, $( $arg ),*),
            $crate::NumberType::ClUint(kind) => apply_kind!(cl_uint, kind, $func, $( $arg ),*),
            $crate::NumberType::ClLong(kind) => apply_kind!(cl_long, kind, $func, $( $arg ),*),
            $crate::NumberType::ClUlong(kind) => apply_kind!(cl_ulong, kind, $func, $( $arg ),*),
            $crate::NumberType::ClFloat(kind) => apply_kind!(cl_float, kind, $func, $( $arg ),*),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NumberTypeKind {
    Primitive,
    Two,
    Three,
    Four,
    Eight,
    Sixteen,
}

impl NumberTypeKind {
    fn count(&self) -> usize {
        match self {
            Self::Primitive => 1,
            Self::Two => 2,
            Self::Three => 3,
            Self::Four => 4,
            Self::Eight => 5,
            Self::Sixteen => 6,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NumberType {
    ClChar(NumberTypeKind),
    ClUchar(NumberTypeKind),
    ClShort(NumberTypeKind),
    ClUshort(NumberTypeKind),
    ClInt(NumberTypeKind),
    ClUint(NumberTypeKind),
    ClLong(NumberTypeKind),
    ClUlong(NumberTypeKind),
    ClFloat(NumberTypeKind),
    ClHalf,
    ClBool,
    SizeT,
    ClDouble,
}

#[inline]
fn _match_or_panic(t1: NumberType, t2: NumberType) {
    if t1 != t2 {
        panic!("Type mismatch - {:?} vs {:?}", t1, t2);
    }
}


/// An error related to CL types.
#[derive(Debug, Fail, PartialEq, Eq, Clone)]
pub enum TypeError {
    #[fail(display = "TypeMismatchError - expected {:?}, but found {:?}", _0, _1)]
    TypeMismatch(NumberType, NumberType),

    #[fail(display = "InvalidTypeError - the value {:?} is not a valid value for type {}", _0, 1)]
    InvalidValue(NumberType, String),
}

impl NumberType {
    pub fn size_of_t(&self) -> usize {
        match self {
            NumberType::ClChar(kind) => std::mem::size_of::<cl_char>() * kind.count(),
            NumberType::ClUchar(kind) => std::mem::size_of::<cl_uchar>() * kind.count(),
            NumberType::ClShort(kind) => std::mem::size_of::<cl_short>() * kind.count(),
            NumberType::ClUshort(kind) => std::mem::size_of::<cl_ushort>() * kind.count(),
            NumberType::ClInt(kind) => std::mem::size_of::<cl_int>() * kind.count(),
            NumberType::ClUint(kind) => std::mem::size_of::<cl_uint>() * kind.count(),
            NumberType::ClLong(kind) => std::mem::size_of::<cl_long>() * kind.count(),
            NumberType::ClUlong(kind) => std::mem::size_of::<cl_ulong>() * kind.count(),
            NumberType::ClFloat(kind) => std::mem::size_of::<cl_float>() * kind.count(),
            NumberType::ClHalf => std::mem::size_of::<cl_half>(),
            NumberType::ClBool => std::mem::size_of::<cl_bool>(),
            NumberType::SizeT => std::mem::size_of::<size_t>(),
            NumberType::ClDouble => std::mem::size_of::<cl_double>(),
        }
    }


    pub fn matches(&self, other: NumberType) -> bool {
        *self == other
    }

    pub fn match_or_panic(&self, other: NumberType) {
        _match_or_panic(*self, other)
    }

    pub fn type_check(&self, other: NumberType) -> Output<()> {
        if self.matches(other) {
            Ok(())
        } else {
            Err(TypeError::TypeMismatch(*self, other).into())
        }
    }
}

pub trait NumberTypedT {
    fn number_type() -> NumberType;

    fn matches(other: NumberType) -> bool {
        Self::number_type() == other
    }
    
    fn match_or_panic(other: NumberType) {
        _match_or_panic(Self::number_type(), other);
    }
}

pub trait NumberTyped {
    fn number_type(&self) -> NumberType;

    fn matches(&self, other: NumberType) -> bool {
        self.number_type() == other
    }
    
    fn match_or_panic(&self, other: NumberType) {
        _match_or_panic(self.number_type(), other);
    }
}

pub struct NumberTypedVec {
    t: NumberType,
    _ptr: *mut libc::c_void,
    _len: usize,
    _cap: usize,
}

impl NumberTyped for NumberTypedVec {
    fn number_type(&self) -> NumberType {
        self.t
    }
}