custos 0.7.0

A minimal OpenCL, WGPU, CUDA and host CPU array manipulation engine.
Documentation
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Contains traits for generic math.
#![allow(missing_docs)]

use core::{
    cmp::Ordering,
    iter::Sum,
    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign}, fmt::Display,
};

/// A trait that returns the default / zero of a value.
pub trait Zero {
    /// Returns zero or the default.
    /// # Example
    /// ```
    /// use custos::number::Zero;
    ///
    /// assert_eq!(f32::zero(), 0.);
    ///
    /// ```
    #[inline]
    fn zero() -> Self
    where
        Self: Default,
    {
        Self::default()
    }
}

impl<T: Default> Zero for T {}

/// A trait that returns 1 for a number.
pub trait One {
    /// Returns one..
    /// # Example
    /// ```
    /// use custos::number::One;
    ///
    /// fn generic_one<T: One>() -> T {
    ///     T::one()
    /// }
    ///
    /// assert_eq!(generic_one::<f32>(), 1.);
    ///
    /// ```
    fn one() -> Self;
}

/// A trait that returns 2 for a number.
pub trait Two {
    /// Returns two.
    /// # Example
    /// ```
    /// use custos::number::Two;
    ///
    /// fn generic_two<T: Two>() -> T {
    ///     T::two()
    /// }
    ///
    /// assert_eq!(generic_two::<f32>(), 2.);
    ///
    /// ```
    fn two() -> Self;
}

macro_rules! typical_number_impl {
    ($($t:ident),*) => {
        $(
            impl One for $t {
                #[inline]
                fn one() -> $t {
                    1 as $t
                }
            }

            impl Two for $t {
                #[inline]
                fn two() -> $t {
                    2 as $t
                }
            }
        )*

    };
}

typical_number_impl! {
    f32, f64, i8, i16, i32, i64, i128,
    isize, u8, u16, u32, u64, u128, usize
}

/// Numeric is a trait that is implemented for all numeric types.
pub trait Numeric:
    Sized + Default + Copy + PartialOrd + PartialEq + core::fmt::Debug + core::fmt::Display
{
}

impl Numeric for bool {}
impl Numeric for f32 {}
impl Numeric for f64 {}
impl Numeric for i8 {}
impl Numeric for i16 {}
impl Numeric for i32 {}
impl Numeric for i64 {}
impl Numeric for i128 {}
impl Numeric for isize {}
impl Numeric for u8 {}
impl Numeric for u16 {}
impl Numeric for u32 {}
impl Numeric for u64 {}
impl Numeric for u128 {}
impl Numeric for usize {}

/// Implementors of `Number` require some basic math operations.
/// # Example
/// ```
/// use custos::number::Number;
///
/// fn generic_add<T: Number>(a: T, b: T) -> T {
///     a + b
/// }
///
/// assert_eq!(generic_add(1., 2.), 3.);
/// ```
pub trait Number:
    Numeric
    + Add<Self, Output = Self>
    + Sub<Self, Output = Self>
    + Div<Self, Output = Self>
    + Mul<Self, Output = Self>
    + Rem<Self, Output = Self>
    + One
    + Two
    + Zero
    + for<'a> Rem<&'a Self, Output = Self>
    + for<'a> Add<&'a Self, Output = Self>
    + for<'a> Sub<&'a Self, Output = Self>
    + for<'a> Div<&'a Self, Output = Self>
    + for<'a> Mul<&'a Self, Output = Self>
    + RemAssign<Self>
    + AddAssign<Self>
    + SubAssign<Self>
    + MulAssign<Self>
    + DivAssign<Self>
    + Sum<Self>
    + Display
{
    fn from_usize(value: usize) -> Self;
    fn from_u64(value: u64) -> Self;
    fn as_usize(&self) -> usize;
    fn as_f64(&self) -> f64;
    fn max(self, rhs: Self) -> Self;
}

macro_rules! number_apply {
    ($($t:ident),*) => {
        $(
            impl Number for $t {
                #[inline]
                fn from_usize(value: usize) -> $t {
                    value as $t
                }

                #[inline]
                fn from_u64(value: u64) -> $t {
                    value as $t
                }

                #[inline]
                fn as_usize(&self) -> usize {
                    *self as usize
                }

                #[inline]
                fn as_f64(&self) -> f64 {
                    *self as f64
                }

                #[inline]
                fn max(self, rhs: Self) -> Self {
                    if self > rhs {
                        self
                    } else {
                        rhs
                    }
                }
            }
        )*

    };
}

number_apply! {
    f32, f64, i8, i16, i32, i64, i128,
    isize, u8, u16, u32, u64, u128, usize
}

pub trait Float: Neg<Output = Self> + Number {
    #[inline]
    fn squared(lhs: Self) -> Self {
        lhs * lhs
    }
    fn exp(&self) -> Self;
    fn powf(&self, rhs: Self) -> Self;
    fn powi(&self, rhs: i32) -> Self;

    #[inline]
    fn cmp(lhs: Self, rhs: Self) -> Option<Ordering> {
        lhs.partial_cmp(&rhs)
    }
    //fn from_usize(value: usize) -> Self;
    fn tanh(&self) -> Self;
    fn sin(&self) -> Self;
    fn cos(&self) -> Self;
    fn tan(&self) -> Self;
    fn as_generic(value: f64) -> Self;
    fn sqrt(&self) -> Self;
    fn log(&self, base: Self) -> Self;
    fn ln(&self) -> Self;
    fn abs(&self) -> Self;
}

#[cfg(not(feature = "no-std"))]
macro_rules! float_apply {
    ($($t:ident),*) => {
        $(
            impl Float for $t {

                #[inline]
                fn squared(lhs: $t) -> $t {
                    lhs*lhs
                }
                #[inline]
                fn exp(&self) -> $t {
                    $t::exp(*self)
                }
                #[inline]
                fn powf(&self, rhs: $t) -> $t {
                    $t::powf(*self, rhs)
                }
                #[inline]
                fn powi(&self, rhs: i32) -> $t {
                    $t::powi(*self, rhs)
                }

                #[inline]
                fn tanh(&self) -> $t {
                    $t::tanh(*self)
                }
                #[inline]
                fn sin(&self) -> $t {
                    $t::sin(*self)
                }

                #[inline]
                fn cos(&self) -> $t {
                    $t::cos(*self)
                }

                #[inline]
                fn tan(&self) -> $t {
                    $t::tan(*self)
                }

                #[inline]
                fn as_generic(value: f64) -> $t {
                    value as $t
                }
                #[inline]
                fn sqrt(&self) -> $t {
                    $t::sqrt(*self)
                }

                #[inline]
                fn log(&self, base: Self) -> $t {
                    $t::log(*self, base)
                }

                #[inline]
                fn ln(&self) -> $t {
                    $t::ln(*self)
                }
                #[inline]
                fn abs(&self) -> $t {
                    $t::abs(*self)
                }
            }
        )*
    };
}

#[cfg(not(feature = "no-std"))]
float_apply!(f32, f64);

#[cfg(feature = "no-std")]
impl Float for f32 {
    #[inline]
    fn exp(&self) -> Self {
        libm::expf(*self)
    }

    #[inline]
    fn powf(&self, rhs: Self) -> Self {
        libm::powf(*self, rhs)
    }

    #[inline]
    fn powi(&self, rhs: i32) -> Self {
        libm::powf(*self, rhs as f32)
    }

    #[inline]
    fn tanh(&self) -> Self {
        libm::tanhf(*self)
    }

    #[inline]
    fn sin(&self) -> Self {
        libm::sinf(*self)
    }

    #[inline]
    fn as_generic(value: f64) -> Self {
        value as f32
    }

    #[inline]
    fn sqrt(&self) -> Self {
        libm::sqrtf(*self)
    }

    #[inline]
    fn ln(&self) -> Self {
        libm::logf(*self)
    }

    #[inline]
    fn abs(&self) -> Self {
        (*self > 0.) as usize as f32 * *self - (*self < 0.) as usize as f32 * *self
    }

    #[inline]
    fn cos(&self) -> Self {
        libm::cosf(*self)
    }

    #[inline]
    fn tan(&self) -> Self {
        libm::tanf(*self)
    }

    #[inline]
    fn log(&self, base: Self) -> Self {
        libm::log10f(*self) / libm::log10f(base)
    }
}

#[cfg(feature = "no-std")]
impl Float for f64 {
    #[inline]
    fn exp(&self) -> Self {
        libm::exp(*self)
    }

    #[inline]
    fn powf(&self, rhs: Self) -> Self {
        libm::pow(*self, rhs)
    }

    #[inline]
    fn powi(&self, rhs: i32) -> Self {
        libm::pow(*self, rhs as f64)
    }

    #[inline]
    fn tanh(&self) -> Self {
        libm::tanh(*self)
    }

    #[inline]
    fn sin(&self) -> Self {
        libm::sin(*self)
    }

    #[inline]
    fn as_generic(value: f64) -> Self {
        value
    }

    #[inline]
    fn sqrt(&self) -> Self {
        libm::sqrt(*self)
    }

    #[inline]
    fn ln(&self) -> Self {
        libm::log(*self)
    }

    #[inline]
    fn abs(&self) -> Self {
        (*self > 0.) as usize as f64 * *self - (*self < 0.) as usize as f64 * *self
    }

    #[inline]
    fn cos(&self) -> Self {
        libm::cos(*self)
    }
    
    #[inline]
    fn tan(&self) -> Self {
        libm::tan(*self)
    }

    #[inline]
    fn log(&self, base: Self) -> Self {
        libm::log10(*self) / libm::log10(base)
    }
}