cfavml/math/mod.rs
1mod default;
2#[cfg(feature = "nightly")]
3mod fast_math;
4
5pub use default::StdMath;
6#[cfg(feature = "nightly")]
7pub use fast_math::FastMath;
8
9#[cfg(not(feature = "nightly"))]
10pub type AutoMath = StdMath;
11#[cfg(feature = "nightly")]
12pub type AutoMath = FastMath;
13
14/// Core simple math operations that can be adjusted for certain features
15/// or architectures.
16pub trait Math<T> {
17 /// Returns the equivalent zero value.
18 fn zero() -> T;
19
20 /// Returns the equivalent 1.0 value.
21 fn one() -> T;
22
23 /// The maximum value that the value can hold.
24 fn max() -> T;
25
26 /// The minimum value that the value can hold.
27 fn min() -> T;
28
29 /// Returns the equivalent 1.0 value.
30 fn sqrt(a: T) -> T;
31
32 /// Returns the abs of the value.
33 fn abs(a: T) -> T;
34
35 /// Returns if the two values are equal.
36 fn cmp_eq(a: T, b: T) -> bool;
37
38 /// Returns if `a` is _less than_ `b`.
39 fn cmp_lt(a: T, b: T) -> bool;
40
41 /// Returns if `a` is _less than or equal to_ `b`.
42 fn cmp_lte(a: T, b: T) -> bool;
43
44 /// Returns if `a` is greater than_ `b`.
45 fn cmp_gt(a: T, b: T) -> bool;
46
47 /// Returns if `a` is _greater than or equal to_ `b`.
48 fn cmp_gte(a: T, b: T) -> bool;
49
50 /// Returns the minimum of the two values
51 fn cmp_min(a: T, b: T) -> T;
52
53 /// Returns the maximum of the two values
54 fn cmp_max(a: T, b: T) -> T;
55
56 /// `a + b`
57 fn add(a: T, b: T) -> T;
58
59 /// `a - b`
60 fn sub(a: T, b: T) -> T;
61
62 /// `a * b`
63 fn mul(a: T, b: T) -> T;
64
65 /// `a / b`
66 fn div(a: T, b: T) -> T;
67
68 // No officer, nothing scuffed about this, no sir.
69 #[cfg(test)]
70 fn is_close(a: T, b: T) -> bool;
71
72 #[inline]
73 /// Casts a boolean to their `1` and `0` equivalent types
74 fn cast_bool(v: bool) -> T {
75 if v {
76 Self::one()
77 } else {
78 Self::zero()
79 }
80 }
81}