1 /* Test whether long double value is canonical. ldbl-128ibm version.
2 Copyright (C) 2016-2022 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 <math.h>
20 #include <math_private.h>
21 #include <stdint.h>
22
23 int
__iscanonicall(long double x)24 __iscanonicall (long double x)
25 {
26 double xhi, xlo;
27 uint64_t hx, lx;
28
29 ldbl_unpack (x, &xhi, &xlo);
30 EXTRACT_WORDS64 (hx, xhi);
31 EXTRACT_WORDS64 (lx, xlo);
32 int64_t ix = hx & 0x7fffffffffffffffULL;
33 int64_t iy = lx & 0x7fffffffffffffffULL;
34 int hexp = (ix & 0x7ff0000000000000LL) >> 52;
35 int lexp = (iy & 0x7ff0000000000000LL) >> 52;
36
37 if (iy == 0)
38 /* Low part 0 is always OK. */
39 return 1;
40
41 if (hexp == 0x7ff)
42 /* If a NaN, the low part does not matter. If an infinity, the
43 low part must be 0, in which case we have already returned. */
44 return ix != 0x7ff0000000000000LL;
45
46 /* The high part is finite and the low part is nonzero. There must
47 be sufficient difference between the exponents. */
48 bool low_p2;
49 if (lexp == 0)
50 {
51 /* Adjust the exponent for subnormal low part. */
52 lexp = 12 - __builtin_clzll (iy);
53 low_p2 = iy == (1LL << (51 + lexp));
54 }
55 else
56 low_p2 = (iy & 0xfffffffffffffLL) == 0;
57 int expdiff = hexp - lexp;
58 return expdiff > 53 || (expdiff == 53 && low_p2 && (ix & 1) == 0);
59 }
60 libm_hidden_def (__iscanonicall)
61