blob: d129a5538f0cb92625b26b25f7dc633975c43bde [file] [log] [blame]
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +01001/*-
Elliott Hughesc1e46b62023-07-19 14:06:31 -07002 * SPDX-License-Identifier: BSD-2-Clause
Elliott Hughes8da8ca42018-05-08 13:35:33 -07003 *
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +01004 * Copyright (c) 2007 David Schultz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +010027 */
28
29#include <sys/endian.h>
30#include <ctype.h>
31#include <float.h>
32#include <math.h>
33#include <stdint.h>
34#include <strings.h>
35
36#include "math_private.h"
37
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +010038/*
39 * Scan a string of hexadecimal digits (the format nan(3) expects) and
40 * make a bit array (using the local endianness). We stop when we
41 * encounter an invalid character, NUL, etc. If we overflow, we do
42 * the same as gcc's __builtin_nan(), namely, discard the high order bits.
43 *
44 * The format this routine accepts needs to be compatible with what is used
45 * in contrib/gdtoa/hexnan.c (for strtod/scanf) and what is used in
46 * __builtin_nan(). In fact, we're only 100% compatible for strings we
47 * consider valid, so we might be violating the C standard. But it's
48 * impossible to use nan(3) portably anyway, so this seems good enough.
49 */
50void
51_scan_nan(uint32_t *words, int num_words, const char *s)
52{
53 int si; /* index into s */
54 int bitpos; /* index into words (in bits) */
55
56 bzero(words, num_words * sizeof(uint32_t));
57
58 /* Allow a leading '0x'. (It's expected, but redundant.) */
59 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
60 s += 2;
61
62 /* Scan forwards in the string, looking for the end of the sequence. */
63 for (si = 0; isxdigit(s[si]); si++)
64 ;
65
66 /* Scan backwards, filling in the bits in words[] as we go. */
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +010067 for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +010068 if (--si < 0)
69 break;
Elliott Hughesbac0ebb2021-01-26 14:17:20 -080070#if _BYTE_ORDER == _LITTLE_ENDIAN
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +010071 words[bitpos / 32] |= digittoint(s[si]) << (bitpos % 32);
Elliott Hughesbac0ebb2021-01-26 14:17:20 -080072#else
73 words[num_words - 1 - bitpos / 32] |=
74 digittoint(s[si]) << (bitpos % 32);
75#endif
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +010076 }
77}
78
79double
80nan(const char *s)
81{
82 union {
83 double d;
84 uint32_t bits[2];
85 } u;
86
87 _scan_nan(u.bits, 2, s);
88#if _BYTE_ORDER == _LITTLE_ENDIAN
89 u.bits[1] |= 0x7ff80000;
90#else
91 u.bits[0] |= 0x7ff80000;
92#endif
93 return (u.d);
94}
95
96float
97nanf(const char *s)
98{
99 union {
100 float f;
101 uint32_t bits[1];
102 } u;
103
104 _scan_nan(u.bits, 1, s);
105 u.bits[0] |= 0x7fc00000;
106 return (u.f);
107}
108
109#if (LDBL_MANT_DIG == 53)
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800110__weak_reference(nan, nanl);
David 'Digit' Turner8e2ff162011-01-25 17:05:50 +0100111#endif