lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Handle floating-point rounding mode within libc. |
| 2 | Copyright (C) 2012-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _ROUNDING_MODE_H |
| 20 | #define _ROUNDING_MODE_H 1 |
| 21 | |
| 22 | #include <fenv.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | /* Get the architecture-specific definition of how to determine the |
| 27 | rounding mode in libc. This header must also define the FE_* |
| 28 | macros for any standard rounding modes the architecture does not |
| 29 | have in <fenv.h>, to arbitrary distinct values. */ |
| 30 | #include <get-rounding-mode.h> |
| 31 | |
| 32 | /* Return true if a number should be rounded away from zero in |
| 33 | rounding mode MODE, false otherwise. NEGATIVE is true if the |
| 34 | number is negative, false otherwise. LAST_DIGIT_ODD is true if the |
| 35 | last digit of the truncated value (last bit for binary) is odd, |
| 36 | false otherwise. HALF_BIT is true if the number is at least half |
| 37 | way from the truncated value to the next value with the |
| 38 | least-significant digit in the same place, false otherwise. |
| 39 | MORE_BITS is true if the number is not exactly equal to the |
| 40 | truncated value or the half-way value, false otherwise. */ |
| 41 | |
| 42 | static bool |
| 43 | round_away (bool negative, bool last_digit_odd, bool half_bit, bool more_bits, |
| 44 | int mode) |
| 45 | { |
| 46 | switch (mode) |
| 47 | { |
| 48 | case FE_DOWNWARD: |
| 49 | return negative && (half_bit || more_bits); |
| 50 | |
| 51 | case FE_TONEAREST: |
| 52 | return half_bit && (last_digit_odd || more_bits); |
| 53 | |
| 54 | case FE_TOWARDZERO: |
| 55 | return false; |
| 56 | |
| 57 | case FE_UPWARD: |
| 58 | return !negative && (half_bit || more_bits); |
| 59 | |
| 60 | default: |
| 61 | abort (); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #endif /* rounding-mode.h */ |