1 /* Construct a quiet NaN 'long double' with a given payload.
2 Copyright 2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible. */
24 #if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
27 setpayloadl (long double *result
, long double payload
)
29 return setpayload ((double *) result
, payload
);
37 # include "signed-nan.h"
40 /* 2^(LDBL_MANT_DIG-1). */
41 # define TWO_LDBL_MANT_DIG \
42 ((long double) (1U << ((LDBL_MANT_DIG - 1) / 4)) \
43 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 1) / 4)) \
44 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 2) / 4)) \
45 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 3) / 4)))
48 setpayloadl (long double *result
, long double payload
)
50 # if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 106 || LDBL_MANT_DIG == 113) \
51 && defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
53 # if defined __hppa || (defined __mips__ && !MIPS_NAN2008_LONG_DOUBLE) || defined __sh__
54 /* A zero payload is not allowed, because that would denote Infinity.
60 # if LDBL_MANT_DIG == 64 /* on i386, x86_64, ia64, m68k */
61 && payload
< 4611686018427387904.0L /* exp2l (LDBL_MANT_DIG - 2) */
62 && payload
== (long double) (int64_t) payload
64 # if LDBL_MANT_DIG == 106 /* on powerpc, powerpc64, powerpc64le */
65 && payload
< 2251799813685248.0L /* exp2l (DBL_MANT_DIG - 2) */
66 && payload
== (long double) (int64_t) payload
68 # if LDBL_MANT_DIG == 113 /* on alpha, arm64, loongarch64, mips64, riscv64, s390x, sparc64 */
69 && payload
< 2596148429267413814265248164610048.0L /* exp2l (LDBL_MANT_DIG - 2) */
70 && payload
== +(+(payload
+ TWO_LDBL_MANT_DIG
) - TWO_LDBL_MANT_DIG
)
75 x
.value
= positive_NaNl ();
77 # if LDBL_MANT_DIG == 64 /* on i386, x86_64, ia64, m68k */
78 uint64_t pl
= (int64_t) payload
;
79 # if LDBL_EXPBIT0_WORD == 2 && LDBL_EXPBIT0_BIT == 0 /* on i386, x86_64, ia64 */
80 x
.word
[1] = (x
.word
[1] & 0xC0000000U
) | (uint32_t) (pl
>> 32);
81 x
.word
[0] = (uint32_t) pl
;
82 # elif LDBL_EXPBIT0_WORD == 0 && LDBL_EXPBIT0_BIT == 16 /* on m68k */
83 x
.word
[1] = (x
.word
[1] & 0xC0000000U
) | (uint32_t) (pl
>> 32);
84 x
.word
[2] = (uint32_t) pl
;
86 # error "Please port gnulib setpayloadl.c to your platform!"
89 # if LDBL_MANT_DIG == 106 /* on powerpc, powerpc64, powerpc64le */
90 uint64_t pl
= (int64_t) payload
;
91 # if LDBL_EXPBIT0_BIT == 20
92 uint32_t pl_hi
= (uint32_t) (pl
>> 32);
93 uint32_t pl_lo
= (uint32_t) pl
;
94 x
.word
[LDBL_EXPBIT0_WORD
] = (x
.word
[LDBL_EXPBIT0_WORD
] & 0xFFF80000U
)
96 x
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
== 0 ? 1 : -1)] = pl_lo
;
98 # error "Please port gnulib setpayloadl.c to your platform!"
101 # if LDBL_MANT_DIG == 113 /* on alpha, arm64, loongarch64, mips64, riscv64, s390x, sparc64 */
102 # if LDBL_EXPBIT0_BIT == 16
103 memory_long_double pl
;
104 pl
.value
= payload
+ TWO_LDBL_MANT_DIG
;
105 x
.word
[LDBL_EXPBIT0_WORD
] = (x
.word
[LDBL_EXPBIT0_WORD
] & 0xFFFF8000U
)
106 | (pl
.word
[LDBL_EXPBIT0_WORD
] & 0x00007FFFU
);
107 x
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
== 0 ? 1 : -1)] =
108 pl
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
== 0 ? 1 : -1)];
109 x
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
== 0 ? 2 : -2)] =
110 pl
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
== 0 ? 2 : -2)];
111 x
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
== 0 ? 3 : -3)] =
112 pl
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
== 0 ? 3 : -3)];
114 # error "Please port gnulib setpayloadl.c to your platform!"
127 # error "Please port gnulib setpayloadl.c to your platform!"