xref: /aosp_15_r20/external/ComputeLibrary/src/core/utils/helpers/float_ops.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_UTILS_HELPERS_FLOAT_OPS_H
25 #define ARM_COMPUTE_UTILS_HELPERS_FLOAT_OPS_H
26 
27 namespace arm_compute
28 {
29 namespace helpers
30 {
31 namespace float_ops
32 {
33 union RawFloat
34 {
35     /** Constructor
36      *
37      * @param[in] val Floating-point value
38      */
RawFloat(float val)39     explicit RawFloat(float val)
40         : f32(val)
41     {
42     }
43     /** Extract sign of floating point number
44      *
45      * @return Sign of floating point number
46      */
sign()47     int32_t sign() const
48     {
49         return i32 >> 31;
50     }
51     /** Extract exponent of floating point number
52      *
53      * @return Exponent of floating point number
54      */
exponent()55     int32_t exponent() const
56     {
57         return (i32 >> 23) & 0xFF;
58     }
59     /** Extract mantissa of floating point number
60      *
61      * @return Mantissa of floating point number
62      */
mantissa()63     int32_t mantissa() const
64     {
65         return i32 & 0x007FFFFF;
66     }
67 
68     int32_t i32;
69     float   f32;
70 };
71 
72 /** Checks if two floating point numbers are equal given an allowed number of ULPs
73  *
74  * @param[in] a                First number to compare
75  * @param[in] b                Second number to compare
76  * @param[in] max_allowed_ulps (Optional) Number of allowed ULPs
77  *
78  * @return True if number is close else false
79  */
80 inline bool is_equal_ulps(float a, float b, int max_allowed_ulps = 0)
81 {
82     RawFloat ra(a);
83     RawFloat rb(b);
84 
85     // Check ULP distance
86     const int ulps = std::abs(ra.i32 - rb.i32);
87     return ulps <= max_allowed_ulps;
88 }
89 
90 /** Checks if the input floating point number is 1.0f checking if the difference is within a range defined with epsilon
91  *
92  * @param[in] a       Input floating point number
93  * @param[in] epsilon (Optional) Epsilon used to define the error bounds
94  *
95  * @return True if number is close to 1.0f
96  */
97 inline bool is_one(float a, float epsilon = 0.00001f)
98 {
99     return std::abs(1.0f - a) <= epsilon;
100 }
101 
102 /** Checks if the input floating point number is 0.0f checking if the difference is within a range defined with epsilon
103  *
104  * @param[in] a       Input floating point number
105  * @param[in] epsilon (Optional) Epsilon used to define the error bounds
106  *
107  * @return True if number is close to 0.0f
108  */
109 inline bool is_zero(float a, float epsilon = 0.00001f)
110 {
111     return std::abs(0.0f - a) <= epsilon;
112 }
113 } // namespace float_ops
114 } // namespace helpers
115 } // namespace arm_compute
116 #endif /* ARM_COMPUTE_UTILS_HELPERS_FLOAT_OPS_H */
117