openat: don’t close (-1)
[gnulib.git] / lib / setpayloadl.c
blobd7d0469e386a2544b342b1b0653d8647a981addc
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. */
19 #include <config.h>
21 /* Specification. */
22 #include <math.h>
24 #if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
26 int
27 setpayloadl (long double *result, long double payload)
29 return setpayload ((double *) result, payload);
32 #else
34 # include <float.h>
35 # include <stdint.h>
37 # include "signed-nan.h"
38 # include "snan.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)))
47 int
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
52 if (
53 # if defined __hppa || (defined __mips__ && !MIPS_NAN2008_LONG_DOUBLE) || defined __sh__
54 /* A zero payload is not allowed, because that would denote Infinity.
55 Cf. snan.h. */
56 payload > 0.0L
57 # else
58 payload >= 0.0L
59 # endif
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
63 # endif
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
67 # endif
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)
71 # endif
74 memory_long_double x;
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;
85 # else
86 # error "Please port gnulib setpayloadl.c to your platform!"
87 # endif
88 # endif
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)
95 | pl_hi;
96 x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 1 : -1)] = pl_lo;
97 # else
98 # error "Please port gnulib setpayloadl.c to your platform!"
99 # endif
100 # endif
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)];
113 # else
114 # error "Please port gnulib setpayloadl.c to your platform!"
115 # endif
116 # endif
118 *result = x.value;
119 return 0;
121 else
123 *result = 0.0L;
124 return -1;
126 # else
127 # error "Please port gnulib setpayloadl.c to your platform!"
128 # endif
131 #endif