etc/services - sync with NetBSD-8
[minix.git] / lib / libm / src / s_nan.c
blob3560da46e00e0a5420c3771dc8ab96f9bc88b012
1 /* $NetBSD: s_nan.c,v 1.2 2013/02/09 20:19:13 christos Exp $ */
3 /*-
4 * Copyright (c) 2007 David Schultz
5 * All rights reserved.
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.
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.
28 * $FreeBSD: src/lib/msun/src/s_nan.c,v 1.2 2007/12/18 23:46:32 das Exp $
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: s_nan.c,v 1.2 2013/02/09 20:19:13 christos Exp $");
33 #include <sys/endian.h>
34 #include <ctype.h>
35 #include <float.h>
36 #include <math.h>
37 #include <stdint.h>
38 #include <string.h>
40 #include "math_private.h"
42 /* XXX: Locale? not here? in <ctype.h>? */
43 static int
44 digittoint(char s) {
45 if (isdigit((unsigned char)s))
46 return s - '0';
47 if (islower((unsigned char)s))
48 return s - 'a' + 10;
49 return s - 'A' + 10;
53 * Scan a string of hexadecimal digits (the format nan(3) expects) and
54 * make a bit array (using the local endianness). We stop when we
55 * encounter an invalid character, NUL, etc. If we overflow, we do
56 * the same as gcc's __builtin_nan(), namely, discard the high order bits.
58 * The format this routine accepts needs to be compatible with what is used
59 * in contrib/gdtoa/hexnan.c (for strtod/scanf) and what is used in
60 * __builtin_nan(). In fact, we're only 100% compatible for strings we
61 * consider valid, so we might be violating the C standard. But it's
62 * impossible to use nan(3) portably anyway, so this seems good enough.
64 static void
65 _scan_nan(uint32_t *words, int num_words, const char *s)
67 int si; /* index into s */
68 int bitpos; /* index into words (in bits) */
70 memset(words, 0, num_words * sizeof(*words));
72 /* Allow a leading '0x'. (It's expected, but redundant.) */
73 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
74 s += 2;
76 /* Scan forwards in the string, looking for the end of the sequence. */
77 for (si = 0; isxdigit((unsigned char)s[si]); si++)
78 continue;
80 /* Scan backwards, filling in the bits in words[] as we go. */
81 #if _BYTE_ORDER == _LITTLE_ENDIAN
82 for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
83 #else
84 for (bitpos = 32 * num_words - 4; bitpos >= 0; bitpos -= 4) {
85 #endif
86 if (--si < 0)
87 break;
88 words[bitpos / 32] |= digittoint(s[si]) << (bitpos % 32);
92 double
93 nan(const char *s)
95 union {
96 double d;
97 uint32_t bits[2];
98 } u;
100 _scan_nan(u.bits, 2, s);
101 #if _BYTE_ORDER == _LITTLE_ENDIAN
102 u.bits[1] |= 0x7ff80000;
103 #else
104 u.bits[0] |= 0x7ff80000;
105 #endif
106 return (u.d);
109 float
110 nanf(const char *s)
112 union {
113 float f;
114 uint32_t bits[1];
115 } u;
117 _scan_nan(u.bits, 1, s);
118 u.bits[0] |= 0x7fc00000;
119 return (u.f);
122 #if (LDBL_MANT_DIG == 53)
123 __weak_reference(nan, nanl);
124 #endif