Compute Library
 23.11
float_ops.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020, 2023 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 ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H
25 #define ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H
26 
27 #include <cmath>
28 #include <cstdint>
29 #include <cstdlib>
30 
31 namespace arm_compute
32 {
33 namespace helpers
34 {
35 namespace float_ops
36 {
37 union RawFloat
38 {
39  /** Constructor
40  *
41  * @param[in] val Floating-point value
42  */
43  explicit RawFloat(float val) : f32(val)
44  {
45  }
46  /** Extract sign of floating point number
47  *
48  * @return Sign of floating point number
49  */
50  int32_t sign() const
51  {
52  return i32 >> 31;
53  }
54  /** Extract exponent of floating point number
55  *
56  * @return Exponent of floating point number
57  */
58  int32_t exponent() const
59  {
60  return (i32 >> 23) & 0xFF;
61  }
62  /** Extract mantissa of floating point number
63  *
64  * @return Mantissa of floating point number
65  */
66  int32_t mantissa() const
67  {
68  return i32 & 0x007FFFFF;
69  }
70 
71  int32_t i32;
72  float f32;
73 };
74 
75 /** Checks if two floating point numbers are equal given an allowed number of ULPs
76  *
77  * @param[in] a First number to compare
78  * @param[in] b Second number to compare
79  * @param[in] max_allowed_ulps (Optional) Number of allowed ULPs
80  *
81  * @return True if number is close else false
82  */
83 inline bool is_equal_ulps(float a, float b, int max_allowed_ulps = 0)
84 {
85  RawFloat ra(a);
86  RawFloat rb(b);
87 
88  // Check ULP distance
89  const int ulps = std::abs(ra.i32 - rb.i32);
90  return ulps <= max_allowed_ulps;
91 }
92 
93 /** Checks if the input floating point number is 1.0f checking if the difference is within a range defined with epsilon
94  *
95  * @param[in] a Input floating point number
96  * @param[in] epsilon (Optional) Epsilon used to define the error bounds
97  *
98  * @return True if number is close to 1.0f
99  */
100 inline bool is_one(float a, float epsilon = 0.00001f)
101 {
102  return std::abs(1.0f - a) <= epsilon;
103 }
104 
105 /** Checks if the input floating point number is 0.0f checking if the difference is within a range defined with epsilon
106  *
107  * @param[in] a Input floating point number
108  * @param[in] epsilon (Optional) Epsilon used to define the error bounds
109  *
110  * @return True if number is close to 0.0f
111  */
112 inline bool is_zero(float a, float epsilon = 0.00001f)
113 {
114  return std::abs(0.0f - a) <= epsilon;
115 }
116 } // namespace float_ops
117 } // namespace helpers
118 } // namespace arm_compute
119 #endif // ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H
arm_compute::helpers::float_ops::RawFloat::sign
int32_t sign() const
Extract sign of floating point number.
Definition: float_ops.h:50
arm_compute::helpers::float_ops::RawFloat::exponent
int32_t exponent() const
Extract exponent of floating point number.
Definition: float_ops.h:58
arm_compute::helpers::float_ops::is_zero
bool is_zero(float a, float epsilon=0.00001f)
Checks if the input floating point number is 0.0f checking if the difference is within a range define...
Definition: float_ops.h:112
arm_compute::helpers::float_ops::is_equal_ulps
bool is_equal_ulps(float a, float b, int max_allowed_ulps=0)
Checks if two floating point numbers are equal given an allowed number of ULPs.
Definition: float_ops.h:83
arm_compute::helpers::float_ops::RawFloat::i32
int32_t i32
Definition: float_ops.h:71
arm_compute::helpers::float_ops::RawFloat
Definition: float_ops.h:37
arm_compute::helpers::float_ops::RawFloat::mantissa
int32_t mantissa() const
Extract mantissa of floating point number.
Definition: float_ops.h:66
arm_compute::test::validation::b
SimpleTensor< float > b
Definition: DFT.cpp:157
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::helpers::float_ops::RawFloat::RawFloat
RawFloat(float val)
Constructor.
Definition: float_ops.h:43
arm_compute::helpers::float_ops::RawFloat::f32
float f32
Definition: float_ops.h:72
arm_compute::helpers::float_ops::is_one
bool is_one(float a, float epsilon=0.00001f)
Checks if the input floating point number is 1.0f checking if the difference is within a range define...
Definition: float_ops.h:100
arm_compute::quantization::epsilon
constexpr float epsilon
Definition: AsymmHelpers.cpp:41