blob: fceee2e3fedb5c8fa7430f47311a6e5a2ae3e981 [file] [log] [blame]
Georgios Pinitas4b3fba12019-06-04 17:31:46 +01001/*
2 * Copyright (c) 2019 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_UTILS_HELPERS_FLOAT_OPS_H
25#define ARM_COMPUTE_UTILS_HELPERS_FLOAT_OPS_H
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010026
27namespace arm_compute
28{
29namespace helpers
30{
31namespace float_ops
32{
33union RawFloat
34{
35 /** Constructor
36 *
37 * @param[in] val Floating-point value
38 */
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 */
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 */
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 */
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
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010076 * @param[in] max_allowed_ulps (Optional) Number of allowed ULPs
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010077 *
78 * @return True if number is close else false
79 */
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010080inline bool is_equal_ulps(float a, float b, int max_allowed_ulps = 0)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010081{
82 RawFloat ra(a);
83 RawFloat rb(b);
84
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010085 // Check ULP distance
86 const int ulps = std::abs(ra.i32 - rb.i32);
87 return ulps <= max_allowed_ulps;
88}
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010089
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 */
97inline 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 */
109inline bool is_zero(float a, float epsilon = 0.00001f)
110{
111 return std::abs(0.0f - a) <= epsilon;
112}
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100113} // namespace float_ops
114} // namespace helpers
115} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000116#endif /* ARM_COMPUTE_UTILS_HELPERS_FLOAT_OPS_H */