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