Fix last ChangeLog entry.
[gnulib.git] / lib / setpayloadsigl.c
blob919873049018fd0ae8e854ffad0219d6cc88aecc
1 /* Construct a signalling NaN 'long double' with a given payload.
2 Copyright 2024-2025 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 setpayloadsigl (long double *result, long double payload)
29 return setpayloadsig ((double *) result, payload);
32 #else
34 # include <float.h>
35 # include <stdint.h>
37 # include "signed-snan.h"
39 /* 2^(LDBL_MANT_DIG-1). */
40 # define TWO_LDBL_MANT_DIG \
41 ((long double) (1U << ((LDBL_MANT_DIG - 1) / 4)) \
42 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 1) / 4)) \
43 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 2) / 4)) \
44 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 3) / 4)))
46 int
47 setpayloadsigl (long double *result, long double payload)
49 # if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 106 || LDBL_MANT_DIG == 113) \
50 && defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
51 if (
52 # if defined __hppa || (defined __mips__ && !MIPS_NAN2008_LONG_DOUBLE) || defined __sh__
53 payload >= 0.0L
54 # else
55 /* A zero payload is not allowed, because that would denote Infinity.
56 Cf. snan.h. */
57 payload > 0.0L
58 # endif
59 # if LDBL_MANT_DIG == 64 /* on i386, x86_64, ia64, m68k */
60 && payload < 4611686018427387904.0L /* exp2l (LDBL_MANT_DIG - 2) */
61 && payload == (long double) (int64_t) payload
62 # endif
63 # if LDBL_MANT_DIG == 106 /* on powerpc, powerpc64, powerpc64le */
64 && payload < 2251799813685248.0L /* exp2l (DBL_MANT_DIG - 2) */
65 && payload == (long double) (int64_t) payload
66 # endif
67 # if LDBL_MANT_DIG == 113 /* on alpha, arm64, loongarch64, mips64, riscv64, s390x, sparc64 */
68 && payload < 2596148429267413814265248164610048.0L /* exp2l (LDBL_MANT_DIG - 2) */
69 && payload == +(+(payload + TWO_LDBL_MANT_DIG) - TWO_LDBL_MANT_DIG)
70 # endif
73 memory_long_double x = memory_positive_SNaNl ();
75 # if LDBL_MANT_DIG == 64 /* on i386, x86_64, ia64, m68k */
76 uint64_t pl = (int64_t) payload;
77 # if LDBL_EXPBIT0_WORD == 2 && LDBL_EXPBIT0_BIT == 0 /* on i386, x86_64, ia64 */
78 x.word[1] = (x.word[1] & 0xC0000000U) | (uint32_t) (pl >> 32);
79 x.word[0] = (uint32_t) pl;
80 # elif LDBL_EXPBIT0_WORD == 0 && LDBL_EXPBIT0_BIT == 16 /* on m68k */
81 x.word[1] = (x.word[1] & 0xC0000000U) | (uint32_t) (pl >> 32);
82 x.word[2] = (uint32_t) pl;
83 # else
84 # error "Please port gnulib setpayloadsigl.c to your platform!"
85 # endif
86 # endif
87 # if LDBL_MANT_DIG == 106 /* on powerpc, powerpc64, powerpc64le */
88 uint64_t pl = (int64_t) payload;
89 # if LDBL_EXPBIT0_BIT == 20
90 uint32_t pl_hi = (uint32_t) (pl >> 32);
91 uint32_t pl_lo = (uint32_t) pl;
92 x.word[LDBL_EXPBIT0_WORD] = (x.word[LDBL_EXPBIT0_WORD] & 0xFFF80000U)
93 | pl_hi;
94 x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 1 : -1)] = pl_lo;
95 # else
96 # error "Please port gnulib setpayloadsigl.c to your platform!"
97 # endif
98 # endif
99 # if LDBL_MANT_DIG == 113 /* on alpha, arm64, loongarch64, mips64, riscv64, s390x, sparc64 */
100 # if LDBL_EXPBIT0_BIT == 16
101 memory_long_double pl;
102 pl.value = payload + TWO_LDBL_MANT_DIG;
103 x.word[LDBL_EXPBIT0_WORD] = (x.word[LDBL_EXPBIT0_WORD] & 0xFFFF8000U)
104 | (pl.word[LDBL_EXPBIT0_WORD] & 0x00007FFFU);
105 x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 1 : -1)] =
106 pl.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 1 : -1)];
107 x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 2 : -2)] =
108 pl.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 2 : -2)];
109 x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 3 : -3)] =
110 pl.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 3 : -3)];
111 # else
112 # error "Please port gnulib setpayloadsigl.c to your platform!"
113 # endif
114 # endif
116 *result = x.value;
117 return 0;
119 else
121 *result = 0.0L;
122 return -1;
124 # else
125 # error "Please port gnulib setpayloadsigl.c to your platform!"
126 # endif
129 #endif