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