blob: 487496915a6fa35df864445c16bc7b9b33f1fd72 [file] [log] [blame]
Georgios Pinitas4b3fba12019-06-04 17:31:46 +01001/*
Felix Thomasmathibalan03921602023-09-25 18:35:49 +01002 * Copyright (c) 2019-2020, 2023 Arm Limited.
Georgios Pinitas4b3fba12019-06-04 17:31:46 +01003 *
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 */
Gunes Bayirb6718c82023-10-09 19:32:20 +010024#ifndef ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H
25#define ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010026
Felix Thomasmathibalan03921602023-09-25 18:35:49 +010027#include <cmath>
28#include <cstdint>
Gunes Bayirb6718c82023-10-09 19:32:20 +010029#include <cstdlib>
Felix Thomasmathibalan03921602023-09-25 18:35:49 +010030
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010031namespace arm_compute
32{
33namespace helpers
34{
35namespace float_ops
36{
37union RawFloat
38{
39 /** Constructor
40 *
41 * @param[in] val Floating-point value
42 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043 explicit RawFloat(float val) : f32(val)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010044 {
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
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010079 * @param[in] max_allowed_ulps (Optional) Number of allowed ULPs
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010080 *
81 * @return True if number is close else false
82 */
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010083inline bool is_equal_ulps(float a, float b, int max_allowed_ulps = 0)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010084{
85 RawFloat ra(a);
86 RawFloat rb(b);
87
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010088 // Check ULP distance
89 const int ulps = std::abs(ra.i32 - rb.i32);
90 return ulps <= max_allowed_ulps;
91}
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010092
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 */
100inline 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 */
112inline bool is_zero(float a, float epsilon = 0.00001f)
113{
114 return std::abs(0.0f - a) <= epsilon;
115}
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100116} // namespace float_ops
117} // namespace helpers
118} // namespace arm_compute
Gunes Bayirb6718c82023-10-09 19:32:20 +0100119#endif // ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H