| 1 | /* Round to integer type. ldbl-128 version. |
| 2 | Copyright (C) 2016-2020 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 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fenv.h> |
| 21 | #include <math.h> |
| 22 | #include <math_private.h> |
| 23 | #include <libm-alias-ldouble.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #define BIAS 0x3fff |
| 28 | #define MANT_DIG 113 |
| 29 | |
| 30 | #if UNSIGNED |
| 31 | # define RET_TYPE uintmax_t |
| 32 | #else |
| 33 | # define RET_TYPE intmax_t |
| 34 | #endif |
| 35 | |
| 36 | #include <fromfp.h> |
| 37 | |
| 38 | RET_TYPE |
| 39 | FUNC (_Float128 x, int round, unsigned int width) |
| 40 | { |
| 41 | if (width > INTMAX_WIDTH) |
| 42 | width = INTMAX_WIDTH; |
| 43 | uint64_t hx, lx; |
| 44 | GET_LDOUBLE_WORDS64 (hx, lx, x); |
| 45 | bool negative = (hx & 0x8000000000000000ULL) != 0; |
| 46 | if (width == 0) |
| 47 | return fromfp_domain_error (negative, width); |
| 48 | hx &= 0x7fffffffffffffffULL; |
| 49 | if ((hx | lx) == 0) |
| 50 | return 0; |
| 51 | int exponent = hx >> (MANT_DIG - 1 - 64); |
| 52 | exponent -= BIAS; |
| 53 | int max_exponent = fromfp_max_exponent (negative, width); |
| 54 | if (exponent > max_exponent) |
| 55 | return fromfp_domain_error (negative, width); |
| 56 | |
| 57 | hx &= ((1ULL << (MANT_DIG - 1 - 64)) - 1); |
| 58 | hx |= 1ULL << (MANT_DIG - 1 - 64); |
| 59 | uintmax_t uret; |
| 60 | bool half_bit, more_bits; |
| 61 | /* The exponent is at most 63, so we are shifting right by at least |
| 62 | 49 bits. */ |
| 63 | if (exponent >= -1) |
| 64 | { |
| 65 | int shift = MANT_DIG - 1 - exponent; |
| 66 | if (shift <= 64) |
| 67 | { |
| 68 | uint64_t h = 1ULL << (shift - 1); |
| 69 | half_bit = (lx & h) != 0; |
| 70 | more_bits = (lx & (h - 1)) != 0; |
| 71 | uret = hx << (64 - shift); |
| 72 | if (shift != 64) |
| 73 | uret |= lx >> shift; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | uint64_t h = 1ULL << (shift - 1 - 64); |
| 78 | half_bit = (hx & h) != 0; |
| 79 | more_bits = ((hx & (h - 1)) | lx) != 0; |
| 80 | uret = hx >> (shift - 64); |
| 81 | } |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | uret = 0; |
| 86 | half_bit = false; |
| 87 | more_bits = true; |
| 88 | } |
| 89 | return fromfp_round_and_return (negative, uret, half_bit, more_bits, round, |
| 90 | exponent, max_exponent, width); |
| 91 | } |
| 92 | |