ruisa_path/
scalar.rs

1// Copyright 2006 The Android Open Source Project
2// Copyright 2020 Yevhenii Reizner
3//
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6
7use crate::floating_point::f32_as_2s_compliment;
8
9#[allow(missing_docs)]
10pub const SCALAR_MAX: f32 = 3.402823466e+38;
11#[allow(missing_docs)]
12pub const SCALAR_NEARLY_ZERO: f32 = 1.0 / (1 << 12) as f32;
13#[allow(missing_docs)]
14pub const SCALAR_ROOT_2_OVER_2: f32 = 0.707106781;
15
16/// Float number extension methods.
17///
18/// Mainly for internal use. Do not rely on it!
19#[allow(missing_docs)]
20pub trait Scalar {
21    fn half(self) -> Self;
22    fn ave(self, other: Self) -> Self;
23    fn sqr(self) -> Self;
24    fn invert(self) -> Self;
25    fn bound(self, min: Self, max: Self) -> Self;
26    fn is_nearly_equal(self, other: Self) -> bool;
27    fn is_nearly_zero(self) -> bool;
28    fn is_nearly_zero_within_tolerance(self, tolerance: Self) -> bool;
29    fn almost_dequal_ulps(self, other: Self) -> bool;
30}
31
32impl Scalar for f32 {
33    fn half(self) -> f32 {
34        self * 0.5
35    }
36
37    fn ave(self, other: Self) -> f32 {
38        (self + other) * 0.5
39    }
40
41    fn sqr(self) -> f32 {
42        self * self
43    }
44
45    fn invert(self) -> f32 {
46        1.0 / self
47    }
48
49    // Works just like SkTPin, returning `max` for NaN/inf
50    /// A non-panicking clamp.
51    fn bound(self, min: Self, max: Self) -> Self {
52        max.min(self).max(min)
53    }
54
55    fn is_nearly_equal(self, other: Self) -> bool {
56        (self - other).abs() <= SCALAR_NEARLY_ZERO
57    }
58
59    fn is_nearly_zero(self) -> bool {
60        self.is_nearly_zero_within_tolerance(SCALAR_NEARLY_ZERO)
61    }
62
63    fn is_nearly_zero_within_tolerance(self, tolerance: Self) -> bool {
64        debug_assert!(tolerance >= 0.0);
65        self.abs() <= tolerance
66    }
67
68    // From SkPathOpsTypes.
69    fn almost_dequal_ulps(self, other: Self) -> bool {
70        const ULPS_EPSILON: i32 = 16;
71        let a_bits = f32_as_2s_compliment(self);
72        let b_bits = f32_as_2s_compliment(other);
73        // Find the difference in ULPs.
74        a_bits < b_bits + ULPS_EPSILON && b_bits < a_bits + ULPS_EPSILON
75    }
76}
77
78#[allow(missing_docs)]
79#[cfg(all(not(feature = "std"), feature = "no-std-float"))]
80pub trait NoStdFloat {
81    fn trunc(self) -> Self;
82    fn sqrt(self) -> Self;
83    fn abs(self) -> Self;
84    fn sin(self) -> Self;
85    fn cos(self) -> Self;
86    fn ceil(self) -> Self;
87    fn floor(self) -> Self;
88    fn powf(self, y: Self) -> Self;
89    fn acos(self) -> Self;
90}
91
92#[cfg(all(not(feature = "std"), feature = "no-std-float"))]
93impl NoStdFloat for f32 {
94    fn trunc(self) -> Self {
95        libm::truncf(self)
96    }
97    fn sqrt(self) -> Self {
98        libm::sqrtf(self)
99    }
100    fn abs(self) -> Self {
101        libm::fabsf(self)
102    }
103    fn sin(self) -> Self {
104        libm::sinf(self)
105    }
106    fn cos(self) -> Self {
107        libm::cosf(self)
108    }
109    fn ceil(self) -> Self {
110        libm::ceilf(self)
111    }
112    fn floor(self) -> Self {
113        libm::floorf(self)
114    }
115    fn powf(self, y: Self) -> Self {
116        libm::powf(self, y)
117    }
118    fn acos(self) -> Self {
119        libm::acosf(self)
120    }
121}
122
123#[cfg(all(not(feature = "std"), feature = "no-std-float"))]
124impl NoStdFloat for f64 {
125    fn trunc(self) -> Self {
126        libm::trunc(self)
127    }
128    fn sqrt(self) -> Self {
129        libm::sqrt(self)
130    }
131    fn abs(self) -> Self {
132        libm::fabs(self)
133    }
134    fn sin(self) -> Self {
135        libm::sin(self)
136    }
137    fn cos(self) -> Self {
138        libm::cos(self)
139    }
140    fn ceil(self) -> Self {
141        libm::ceil(self)
142    }
143    fn floor(self) -> Self {
144        libm::floor(self)
145    }
146    fn powf(self, y: Self) -> Self {
147        libm::pow(self, y)
148    }
149    fn acos(self) -> Self {
150        libm::acos(self)
151    }
152}
153
154#[cfg(test)]
155mod tests {
156    use super::*;
157
158    #[test]
159    fn bound() {
160        assert_eq!(core::f32::NAN.bound(0.0, 1.0), 1.0);
161        assert_eq!(core::f32::INFINITY.bound(0.0, 1.0), 1.0);
162        assert_eq!(core::f32::NEG_INFINITY.bound(0.0, 1.0), 0.0);
163        assert_eq!(core::f32::EPSILON.bound(0.0, 1.0), core::f32::EPSILON);
164        assert_eq!(0.5.bound(0.0, 1.0), 0.5);
165        assert_eq!((-1.0).bound(0.0, 1.0), 0.0);
166        assert_eq!(2.0.bound(0.0, 1.0), 1.0);
167    }
168}