Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* Construct a quiet NaN 'long double' with a given payload.
Copyright 2024-2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible. */
#include <config.h>
/* Specification. */
#include <math.h>
#if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
int
setpayloadl (long double *result, long double payload)
{
return setpayload ((double *) result, payload);
}
#else
# include <float.h>
# include <stdint.h>
# include "signed-nan.h"
# include "snan.h"
/* 2^(LDBL_MANT_DIG-1). */
# define TWO_LDBL_MANT_DIG \
((long double) (1U << ((LDBL_MANT_DIG - 1) / 4)) \
* (long double) (1U << ((LDBL_MANT_DIG - 1 + 1) / 4)) \
* (long double) (1U << ((LDBL_MANT_DIG - 1 + 2) / 4)) \
* (long double) (1U << ((LDBL_MANT_DIG - 1 + 3) / 4)))
int
setpayloadl (long double *result, long double payload)
{
# if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 106 || LDBL_MANT_DIG == 113) \
&& defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
if (
# if defined __hppa || (defined __mips__ && !MIPS_NAN2008_LONG_DOUBLE) || defined __sh__
/* A zero payload is not allowed, because that would denote Infinity.
Cf. snan.h. */
payload > 0.0L
# else
payload >= 0.0L
# endif
# if LDBL_MANT_DIG == 64 /* on i386, x86_64, ia64, m68k */
&& payload < 4611686018427387904.0L /* exp2l (LDBL_MANT_DIG - 2) */
&& payload == (long double) (int64_t) payload
# endif
# if LDBL_MANT_DIG == 106 /* on powerpc, powerpc64, powerpc64le */
&& payload < 2251799813685248.0L /* exp2l (DBL_MANT_DIG - 2) */
&& payload == (long double) (int64_t) payload
# endif
# if LDBL_MANT_DIG == 113 /* on alpha, arm64, loongarch64, mips64, riscv64, s390x, sparc64 */
&& payload < 2596148429267413814265248164610048.0L /* exp2l (LDBL_MANT_DIG - 2) */
&& payload == +(+(payload + TWO_LDBL_MANT_DIG) - TWO_LDBL_MANT_DIG)
# endif
)
{
memory_long_double x;
x.value = positive_NaNl ();
# if LDBL_MANT_DIG == 64 /* on i386, x86_64, ia64, m68k */
uint64_t pl = (int64_t) payload;
# if LDBL_EXPBIT0_WORD == 2 && LDBL_EXPBIT0_BIT == 0 /* on i386, x86_64, ia64 */
x.word[1] = (x.word[1] & 0xC0000000U) | (uint32_t) (pl >> 32);
x.word[0] = (uint32_t) pl;
# elif LDBL_EXPBIT0_WORD == 0 && LDBL_EXPBIT0_BIT == 16 /* on m68k */
x.word[1] = (x.word[1] & 0xC0000000U) | (uint32_t) (pl >> 32);
x.word[2] = (uint32_t) pl;
# else
# error "Please port gnulib setpayloadl.c to your platform!"
# endif
# endif
# if LDBL_MANT_DIG == 106 /* on powerpc, powerpc64, powerpc64le */
uint64_t pl = (int64_t) payload;
# if LDBL_EXPBIT0_BIT == 20
uint32_t pl_hi = (uint32_t) (pl >> 32);
uint32_t pl_lo = (uint32_t) pl;
x.word[LDBL_EXPBIT0_WORD] = (x.word[LDBL_EXPBIT0_WORD] & 0xFFF80000U)
| pl_hi;
x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 1 : -1)] = pl_lo;
# else
# error "Please port gnulib setpayloadl.c to your platform!"
# endif
# endif
# if LDBL_MANT_DIG == 113 /* on alpha, arm64, loongarch64, mips64, riscv64, s390x, sparc64 */
# if LDBL_EXPBIT0_BIT == 16
memory_long_double pl;
pl.value = payload + TWO_LDBL_MANT_DIG;
x.word[LDBL_EXPBIT0_WORD] = (x.word[LDBL_EXPBIT0_WORD] & 0xFFFF8000U)
| (pl.word[LDBL_EXPBIT0_WORD] & 0x00007FFFU);
x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 1 : -1)] =
pl.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 1 : -1)];
x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 2 : -2)] =
pl.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 2 : -2)];
x.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 3 : -3)] =
pl.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD == 0 ? 3 : -3)];
# else
# error "Please port gnulib setpayloadl.c to your platform!"
# endif
# endif
*result = x.value;
return 0;
}
else
{
*result = 0.0L;
return -1;
}
# else
# error "Please port gnulib setpayloadl.c to your platform!"
# endif
}
#endif