vex/
common.rs

1#[inline]
2pub fn is_valid(x: f32) -> bool {
3    !(x.is_nan() || x.is_infinite())
4}
5
6/// Gets the next power of two for a given value
7///
8/// # Examples
9/// ```
10/// use vex::next_power_of_two;
11/// let n = next_power_of_two(1);
12/// assert_eq!(n, 2);
13/// ```
14/// ```
15/// use vex::next_power_of_two;
16/// let n = next_power_of_two(2);
17/// assert_eq!(n, 4);
18/// ```
19#[inline]
20pub fn next_power_of_two(x: i32) -> i32 {
21    let mut r = x;
22    r |= r >> 1;
23    r |= r >> 2;
24    r |= r >> 4;
25    r |= r >> 8;
26    r |= r >> 16;
27    r + 1
28}
29
30/// Determines whether or not a given value is a power of two
31///
32/// # Examples
33/// ```
34/// use vex::is_power_of_two;
35/// let n = is_power_of_two(1);
36/// assert_eq!(n, true);
37/// ```
38/// ```
39/// use vex::is_power_of_two;
40/// let n = is_power_of_two(2);
41/// assert_eq!(n, true);
42/// ```
43/// ```
44/// use vex::is_power_of_two;
45/// let n = is_power_of_two(3);
46/// assert_eq!(n, false);
47/// ```
48#[inline]
49pub fn is_power_of_two(x: i32) -> bool {
50    x > 0 && (x & (x - 1)) == 0
51}
52
53/// Returns 1 or -1 depending on the sign of the input value
54///
55/// # Examples
56/// ```
57/// use vex::sign;
58/// let mul = sign(1234.0);
59/// assert_eq!(mul, 1.0);
60/// ```
61/// ```
62/// use vex::sign;
63/// let mul = sign(-1234.0);
64/// assert_eq!(mul, -1.0);
65/// ```
66#[inline]
67pub fn sign(x: f32) -> f32 {
68    if x >= 0.0 {
69        1.0
70    } else {
71        -1.0
72    }
73}
74
75pub trait Matrix<T> {
76    fn transform_point(&self, point: &T) -> T;
77}