xref: /netbsd-src/lib/libm/src/s_nan.c (revision ee48b743e752965e204e0086d3d3ab6d3a93c0e3)
1*ee48b743Schristos /*	$NetBSD: s_nan.c,v 1.2 2013/02/09 20:19:13 christos Exp $	*/
2be721c1dSchristos 
3be721c1dSchristos /*-
4be721c1dSchristos  * Copyright (c) 2007 David Schultz
5be721c1dSchristos  * All rights reserved.
6be721c1dSchristos  *
7be721c1dSchristos  * Redistribution and use in source and binary forms, with or without
8be721c1dSchristos  * modification, are permitted provided that the following conditions
9be721c1dSchristos  * are met:
10be721c1dSchristos  * 1. Redistributions of source code must retain the above copyright
11be721c1dSchristos  *    notice, this list of conditions and the following disclaimer.
12be721c1dSchristos  * 2. Redistributions in binary form must reproduce the above copyright
13be721c1dSchristos  *    notice, this list of conditions and the following disclaimer in the
14be721c1dSchristos  *    documentation and/or other materials provided with the distribution.
15be721c1dSchristos  *
16be721c1dSchristos  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17be721c1dSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18be721c1dSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19be721c1dSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20be721c1dSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21be721c1dSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22be721c1dSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23be721c1dSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24be721c1dSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25be721c1dSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26be721c1dSchristos  * SUCH DAMAGE.
27be721c1dSchristos  *
28be721c1dSchristos  * $FreeBSD: src/lib/msun/src/s_nan.c,v 1.2 2007/12/18 23:46:32 das Exp $
29be721c1dSchristos  */
30be721c1dSchristos #include <sys/cdefs.h>
31*ee48b743Schristos __RCSID("$NetBSD: s_nan.c,v 1.2 2013/02/09 20:19:13 christos Exp $");
32be721c1dSchristos 
33be721c1dSchristos #include <sys/endian.h>
34be721c1dSchristos #include <ctype.h>
35be721c1dSchristos #include <float.h>
36be721c1dSchristos #include <math.h>
37be721c1dSchristos #include <stdint.h>
38*ee48b743Schristos #include <string.h>
39be721c1dSchristos 
40be721c1dSchristos #include "math_private.h"
41be721c1dSchristos 
42*ee48b743Schristos /* XXX: Locale? not here? in <ctype.h>? */
43*ee48b743Schristos static int
digittoint(char s)44*ee48b743Schristos digittoint(char s) {
45*ee48b743Schristos 	if (isdigit((unsigned char)s))
46*ee48b743Schristos 		return s - '0';
47*ee48b743Schristos 	if (islower((unsigned char)s))
48*ee48b743Schristos 		return s - 'a' + 10;
49*ee48b743Schristos 	return s - 'A' + 10;
50*ee48b743Schristos }
51*ee48b743Schristos 
52be721c1dSchristos /*
53be721c1dSchristos  * Scan a string of hexadecimal digits (the format nan(3) expects) and
54be721c1dSchristos  * make a bit array (using the local endianness). We stop when we
55be721c1dSchristos  * encounter an invalid character, NUL, etc.  If we overflow, we do
56be721c1dSchristos  * the same as gcc's __builtin_nan(), namely, discard the high order bits.
57be721c1dSchristos  *
58be721c1dSchristos  * The format this routine accepts needs to be compatible with what is used
59be721c1dSchristos  * in contrib/gdtoa/hexnan.c (for strtod/scanf) and what is used in
60be721c1dSchristos  * __builtin_nan(). In fact, we're only 100% compatible for strings we
61be721c1dSchristos  * consider valid, so we might be violating the C standard. But it's
62be721c1dSchristos  * impossible to use nan(3) portably anyway, so this seems good enough.
63be721c1dSchristos  */
64*ee48b743Schristos static void
_scan_nan(uint32_t * words,int num_words,const char * s)65be721c1dSchristos _scan_nan(uint32_t *words, int num_words, const char *s)
66be721c1dSchristos {
67be721c1dSchristos 	int si;		/* index into s */
68be721c1dSchristos 	int bitpos;	/* index into words (in bits) */
69be721c1dSchristos 
70*ee48b743Schristos 	memset(words, 0, num_words * sizeof(*words));
71be721c1dSchristos 
72be721c1dSchristos 	/* Allow a leading '0x'. (It's expected, but redundant.) */
73be721c1dSchristos 	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
74be721c1dSchristos 		s += 2;
75be721c1dSchristos 
76be721c1dSchristos 	/* Scan forwards in the string, looking for the end of the sequence. */
77*ee48b743Schristos 	for (si = 0; isxdigit((unsigned char)s[si]); si++)
78*ee48b743Schristos 		continue;
79be721c1dSchristos 
80be721c1dSchristos 	/* Scan backwards, filling in the bits in words[] as we go. */
81be721c1dSchristos #if _BYTE_ORDER == _LITTLE_ENDIAN
82be721c1dSchristos 	for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
83be721c1dSchristos #else
84be721c1dSchristos 	for (bitpos = 32 * num_words - 4; bitpos >= 0; bitpos -= 4) {
85be721c1dSchristos #endif
86be721c1dSchristos 		if (--si < 0)
87be721c1dSchristos 			break;
88be721c1dSchristos 		words[bitpos / 32] |= digittoint(s[si]) << (bitpos % 32);
89be721c1dSchristos 	}
90be721c1dSchristos }
91be721c1dSchristos 
92be721c1dSchristos double
93be721c1dSchristos nan(const char *s)
94be721c1dSchristos {
95be721c1dSchristos 	union {
96be721c1dSchristos 		double d;
97be721c1dSchristos 		uint32_t bits[2];
98be721c1dSchristos 	} u;
99be721c1dSchristos 
100be721c1dSchristos 	_scan_nan(u.bits, 2, s);
101be721c1dSchristos #if _BYTE_ORDER == _LITTLE_ENDIAN
102be721c1dSchristos 	u.bits[1] |= 0x7ff80000;
103be721c1dSchristos #else
104be721c1dSchristos 	u.bits[0] |= 0x7ff80000;
105be721c1dSchristos #endif
106be721c1dSchristos 	return (u.d);
107be721c1dSchristos }
108be721c1dSchristos 
109be721c1dSchristos float
110be721c1dSchristos nanf(const char *s)
111be721c1dSchristos {
112be721c1dSchristos 	union {
113be721c1dSchristos 		float f;
114be721c1dSchristos 		uint32_t bits[1];
115be721c1dSchristos 	} u;
116be721c1dSchristos 
117be721c1dSchristos 	_scan_nan(u.bits, 1, s);
118be721c1dSchristos 	u.bits[0] |= 0x7fc00000;
119be721c1dSchristos 	return (u.f);
120be721c1dSchristos }
121be721c1dSchristos 
122be721c1dSchristos #if (LDBL_MANT_DIG == 53)
123be721c1dSchristos __weak_reference(nan, nanl);
124be721c1dSchristos #endif
125