Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 1 | /* |
Felix Thomasmathibalan | 0392160 | 2023-09-25 18:35:49 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, 2023 Arm Limited. |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 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 | */ |
Gunes Bayir | b6718c8 | 2023-10-09 19:32:20 +0100 | [diff] [blame] | 24 | #ifndef ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H |
| 25 | #define ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 26 | |
Felix Thomasmathibalan | 0392160 | 2023-09-25 18:35:49 +0100 | [diff] [blame] | 27 | #include <cmath> |
| 28 | #include <cstdint> |
Gunes Bayir | b6718c8 | 2023-10-09 19:32:20 +0100 | [diff] [blame] | 29 | #include <cstdlib> |
Felix Thomasmathibalan | 0392160 | 2023-09-25 18:35:49 +0100 | [diff] [blame] | 30 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 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 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 43 | explicit RawFloat(float val) : f32(val) |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 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 |
Gian Marco Iodice | 82d9dd1 | 2019-06-10 16:45:40 +0100 | [diff] [blame] | 79 | * @param[in] max_allowed_ulps (Optional) Number of allowed ULPs |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 80 | * |
| 81 | * @return True if number is close else false |
| 82 | */ |
Gian Marco Iodice | 82d9dd1 | 2019-06-10 16:45:40 +0100 | [diff] [blame] | 83 | inline bool is_equal_ulps(float a, float b, int max_allowed_ulps = 0) |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 84 | { |
| 85 | RawFloat ra(a); |
| 86 | RawFloat rb(b); |
| 87 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 88 | // Check ULP distance |
| 89 | const int ulps = std::abs(ra.i32 - rb.i32); |
| 90 | return ulps <= max_allowed_ulps; |
| 91 | } |
Gian Marco Iodice | 82d9dd1 | 2019-06-10 16:45:40 +0100 | [diff] [blame] | 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 | } |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 116 | } // namespace float_ops |
| 117 | } // namespace helpers |
| 118 | } // namespace arm_compute |
Gunes Bayir | b6718c8 | 2023-10-09 19:32:20 +0100 | [diff] [blame] | 119 | #endif // ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H |