1 /* Round to integer type. ldbl-128ibm version.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
22 #include <math_private.h>
30 # define RET_TYPE uintmax_t
32 # define RET_TYPE intmax_t
38 FUNC (long double x
, int round
, unsigned int width
)
41 if (width
> INTMAX_WIDTH
)
44 ldbl_unpack (x
, &hi
, &lo
);
45 EXTRACT_WORDS64 (hx
, hi
);
46 EXTRACT_WORDS64 (lx
, lo
);
47 bool negative
= (hx
& 0x8000000000000000ULL
) != 0;
48 bool lo_negative
= (lx
& 0x8000000000000000ULL
) != 0;
50 return fromfp_domain_error (negative
, width
);
51 hx
&= 0x7fffffffffffffffULL
;
52 lx
&= 0x7fffffffffffffffULL
;
55 int hi_exponent
= hx
>> (MANT_DIG
- 1);
57 int exponent
= hi_exponent
;
58 hx
&= ((1ULL << (MANT_DIG
- 1)) - 1);
59 if (hx
== 0 && lx
!= 0 && lo_negative
!= negative
)
61 int max_exponent
= fromfp_max_exponent (negative
, width
);
62 if (exponent
> max_exponent
)
63 return fromfp_domain_error (negative
, width
);
64 int lo_exponent
= lx
>> (MANT_DIG
- 1);
67 /* Convert the high part to integer. */
68 hx
|= 1ULL << (MANT_DIG
- 1);
70 bool half_bit
, more_bits
;
71 if (hi_exponent
>= MANT_DIG
- 1)
74 uret
<<= hi_exponent
- (MANT_DIG
- 1);
78 else if (hi_exponent
>= -1)
80 uint64_t h
= 1ULL << (MANT_DIG
- 2 - hi_exponent
);
81 half_bit
= (hx
& h
) != 0;
82 more_bits
= (hx
& (h
- 1)) != 0;
83 uret
= hx
>> (MANT_DIG
- 1 - hi_exponent
);
92 /* Likewise, the low part. */
96 bool lo_half_bit
, lo_more_bits
;
97 lx
&= ((1ULL << (MANT_DIG
- 1)) - 1);
98 lx
|= 1ULL << (MANT_DIG
- 1);
99 /* The high part exponent is at most 64, so the low part
100 exponent is at most 11. */
101 if (lo_exponent
>= -1)
103 uint64_t h
= 1ULL << (MANT_DIG
- 2 - lo_exponent
);
104 lo_half_bit
= (lx
& h
) != 0;
105 lo_more_bits
= (lx
& (h
- 1)) != 0;
106 lo_uret
= lx
>> (MANT_DIG
- 1 - lo_exponent
);
114 if (lo_negative
== negative
)
117 half_bit
|= lo_half_bit
;
118 more_bits
|= lo_more_bits
;
128 if (lo_more_bits
&& !more_bits
)
145 return fromfp_round_and_return (negative
, uret
, half_bit
, more_bits
, round
,
146 exponent
, max_exponent
, width
);