1 /* Round to integer type. dbl-64 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>
23 #include <libm-alias-double.h>
31 # define RET_TYPE uintmax_t
33 # define RET_TYPE intmax_t
39 FUNC (double x
, int round
, unsigned int width
)
41 if (width
> INTMAX_WIDTH
)
44 EXTRACT_WORDS64 (ix
, x
);
45 bool negative
= (ix
& 0x8000000000000000ULL
) != 0;
47 return fromfp_domain_error (negative
, width
);
48 ix
&= 0x7fffffffffffffffULL
;
51 int exponent
= ix
>> (MANT_DIG
- 1);
53 int max_exponent
= fromfp_max_exponent (negative
, width
);
54 if (exponent
> max_exponent
)
55 return fromfp_domain_error (negative
, width
);
57 ix
&= ((1ULL << (MANT_DIG
- 1)) - 1);
58 ix
|= 1ULL << (MANT_DIG
- 1);
60 bool half_bit
, more_bits
;
61 if (exponent
>= MANT_DIG
- 1)
64 uret
<<= exponent
- (MANT_DIG
- 1);
68 else if (exponent
>= -1)
70 uint64_t h
= 1ULL << (MANT_DIG
- 2 - exponent
);
71 half_bit
= (ix
& h
) != 0;
72 more_bits
= (ix
& (h
- 1)) != 0;
73 uret
= ix
>> (MANT_DIG
- 1 - exponent
);
81 return fromfp_round_and_return (negative
, uret
, half_bit
, more_bits
, round
,
82 exponent
, max_exponent
, width
);