blob: 7f7fbd13bf6f5d31a238737784d16417868d6d0a [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 */
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
Felix Thomasmathibalan03921602023-09-25 18:35:49 +010027#include <cmath>
28#include <cstdint>
29
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010030namespace arm_compute
31{
32namespace helpers
33{
34namespace float_ops
35{
36union RawFloat
37{
38 /** Constructor
39 *
40 * @param[in] val Floating-point value
41 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042 explicit RawFloat(float val) : f32(val)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010043 {
44 }
45 /** Extract sign of floating point number
46 *
47 * @return Sign of floating point number
48 */
49 int32_t sign() const
50 {
51 return i32 >> 31;
52 }
53 /** Extract exponent of floating point number
54 *
55 * @return Exponent of floating point number
56 */
57 int32_t exponent() const
58 {
59 return (i32 >> 23) & 0xFF;
60 }
61 /** Extract mantissa of floating point number
62 *
63 * @return Mantissa of floating point number
64 */
65 int32_t mantissa() const
66 {
67 return i32 & 0x007FFFFF;
68 }
69
70 int32_t i32;
71 float f32;
72};
73
74/** Checks if two floating point numbers are equal given an allowed number of ULPs
75 *
76 * @param[in] a First number to compare
77 * @param[in] b Second number to compare
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010078 * @param[in] max_allowed_ulps (Optional) Number of allowed ULPs
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010079 *
80 * @return True if number is close else false
81 */
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010082inline bool is_equal_ulps(float a, float b, int max_allowed_ulps = 0)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010083{
84 RawFloat ra(a);
85 RawFloat rb(b);
86
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010087 // Check ULP distance
88 const int ulps = std::abs(ra.i32 - rb.i32);
89 return ulps <= max_allowed_ulps;
90}
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +010091
92/** Checks if the input floating point number is 1.0f checking if the difference is within a range defined with epsilon
93 *
94 * @param[in] a Input floating point number
95 * @param[in] epsilon (Optional) Epsilon used to define the error bounds
96 *
97 * @return True if number is close to 1.0f
98 */
99inline bool is_one(float a, float epsilon = 0.00001f)
100{
101 return std::abs(1.0f - a) <= epsilon;
102}
103
104/** Checks if the input floating point number is 0.0f checking if the difference is within a range defined with epsilon
105 *
106 * @param[in] a Input floating point number
107 * @param[in] epsilon (Optional) Epsilon used to define the error bounds
108 *
109 * @return True if number is close to 0.0f
110 */
111inline bool is_zero(float a, float epsilon = 0.00001f)
112{
113 return std::abs(0.0f - a) <= epsilon;
114}
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100115} // namespace float_ops
116} // namespace helpers
117} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000118#endif /* ARM_COMPUTE_UTILS_HELPERS_FLOAT_OPS_H */