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