| 1 | /* |
| 2 | Copyright (C) 2020-2023 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "grn.h" |
| 20 | #include "grn_float.h" |
| 21 | |
| 22 | #include <math.h> |
| 23 | #include <float.h> |
| 24 | |
| 25 | #ifdef GRN_HAVE_BFLOAT16 |
| 26 | float |
| 27 | grn_bfloat16_to_float32(grn_bfloat16 value) |
| 28 | { |
| 29 | float value_float = 0; |
| 30 | memcpy(((char *)(&value_float)) + sizeof(float) - sizeof(grn_bfloat16), &value, sizeof(grn_bfloat16)); |
| 31 | return value_float; |
| 32 | } |
| 33 | |
| 34 | grn_bfloat16 |
| 35 | grn_float32_to_bfloat16(float value) |
| 36 | { |
| 37 | grn_bfloat16 value_bfloat16 = 0; |
| 38 | memcpy(&value_bfloat16, |
| 39 | ((char *)&value) + sizeof(float) - sizeof(grn_bfloat16), |
| 40 | sizeof(grn_bfloat16)); |
| 41 | return value_bfloat16; |
| 42 | } |
| 43 | |
| 44 | bool |
| 45 | grn_bfloat16_is_zero(grn_bfloat16 value) |
| 46 | { |
| 47 | return grn_float32_is_zero(grn_bfloat16_to_float32(value)); |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | bool |
| 52 | grn_float32_is_zero(float value) |
| 53 | { |
| 54 | return fabsf(value) < FLT_EPSILON; |
| 55 | } |
| 56 | |
| 57 | bool |
| 58 | grn_float_is_zero(double value) |
| 59 | { |
| 60 | return fabs(value) < DBL_EPSILON; |
| 61 | } |