xref: /freebsd/lib/msun/src/s_nan.c (revision f1343c7f6721b6a6de43813b57aa5e09cb4fd5f5)
14b6b5744SDavid Schultz /*-
25e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
35e53a4f9SPedro F. Giffuni  *
44b6b5744SDavid Schultz  * Copyright (c) 2007 David Schultz
54b6b5744SDavid Schultz  * All rights reserved.
64b6b5744SDavid Schultz  *
74b6b5744SDavid Schultz  * Redistribution and use in source and binary forms, with or without
84b6b5744SDavid Schultz  * modification, are permitted provided that the following conditions
94b6b5744SDavid Schultz  * are met:
104b6b5744SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
114b6b5744SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
124b6b5744SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
134b6b5744SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
144b6b5744SDavid Schultz  *    documentation and/or other materials provided with the distribution.
154b6b5744SDavid Schultz  *
164b6b5744SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174b6b5744SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184b6b5744SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194b6b5744SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
204b6b5744SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214b6b5744SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224b6b5744SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234b6b5744SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244b6b5744SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254b6b5744SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264b6b5744SDavid Schultz  * SUCH DAMAGE.
274b6b5744SDavid Schultz  *
284b6b5744SDavid Schultz  * $FreeBSD$
294b6b5744SDavid Schultz  */
304b6b5744SDavid Schultz 
317cd4a832SDavid Schultz #include <sys/endian.h>
327cd4a832SDavid Schultz #include <ctype.h>
334b6b5744SDavid Schultz #include <float.h>
344b6b5744SDavid Schultz #include <math.h>
357cd4a832SDavid Schultz #include <stdint.h>
367cd4a832SDavid Schultz #include <strings.h>
374b6b5744SDavid Schultz 
384b6b5744SDavid Schultz #include "math_private.h"
394b6b5744SDavid Schultz 
407cd4a832SDavid Schultz /*
417cd4a832SDavid Schultz  * Scan a string of hexadecimal digits (the format nan(3) expects) and
427cd4a832SDavid Schultz  * make a bit array (using the local endianness). We stop when we
437cd4a832SDavid Schultz  * encounter an invalid character, NUL, etc.  If we overflow, we do
447cd4a832SDavid Schultz  * the same as gcc's __builtin_nan(), namely, discard the high order bits.
457cd4a832SDavid Schultz  *
467cd4a832SDavid Schultz  * The format this routine accepts needs to be compatible with what is used
477cd4a832SDavid Schultz  * in contrib/gdtoa/hexnan.c (for strtod/scanf) and what is used in
487cd4a832SDavid Schultz  * __builtin_nan(). In fact, we're only 100% compatible for strings we
497cd4a832SDavid Schultz  * consider valid, so we might be violating the C standard. But it's
507cd4a832SDavid Schultz  * impossible to use nan(3) portably anyway, so this seems good enough.
517cd4a832SDavid Schultz  */
527cd4a832SDavid Schultz void
537cd4a832SDavid Schultz _scan_nan(uint32_t *words, int num_words, const char *s)
547cd4a832SDavid Schultz {
557cd4a832SDavid Schultz 	int si;		/* index into s */
567cd4a832SDavid Schultz 	int bitpos;	/* index into words (in bits) */
577cd4a832SDavid Schultz 
587cd4a832SDavid Schultz 	bzero(words, num_words * sizeof(uint32_t));
597cd4a832SDavid Schultz 
607cd4a832SDavid Schultz 	/* Allow a leading '0x'. (It's expected, but redundant.) */
617cd4a832SDavid Schultz 	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
627cd4a832SDavid Schultz 		s += 2;
637cd4a832SDavid Schultz 
647cd4a832SDavid Schultz 	/* Scan forwards in the string, looking for the end of the sequence. */
657cd4a832SDavid Schultz 	for (si = 0; isxdigit(s[si]); si++)
667cd4a832SDavid Schultz 		;
677cd4a832SDavid Schultz 
687cd4a832SDavid Schultz 	/* Scan backwards, filling in the bits in words[] as we go. */
697cd4a832SDavid Schultz 	for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
707cd4a832SDavid Schultz 		if (--si < 0)
717cd4a832SDavid Schultz 			break;
72*f1343c7fSAlfredo Dal'Ava Junior #if _BYTE_ORDER == _LITTLE_ENDIAN
737cd4a832SDavid Schultz 		words[bitpos / 32] |= digittoint(s[si]) << (bitpos % 32);
74*f1343c7fSAlfredo Dal'Ava Junior #else
75*f1343c7fSAlfredo Dal'Ava Junior 		words[num_words - 1 - bitpos / 32] |=
76*f1343c7fSAlfredo Dal'Ava Junior 		    digittoint(s[si]) << (bitpos % 32);
77*f1343c7fSAlfredo Dal'Ava Junior #endif
787cd4a832SDavid Schultz 	}
797cd4a832SDavid Schultz }
807cd4a832SDavid Schultz 
814b6b5744SDavid Schultz double
824b6b5744SDavid Schultz nan(const char *s)
834b6b5744SDavid Schultz {
847cd4a832SDavid Schultz 	union {
857cd4a832SDavid Schultz 		double d;
867cd4a832SDavid Schultz 		uint32_t bits[2];
877cd4a832SDavid Schultz 	} u;
884b6b5744SDavid Schultz 
897cd4a832SDavid Schultz 	_scan_nan(u.bits, 2, s);
907cd4a832SDavid Schultz #if _BYTE_ORDER == _LITTLE_ENDIAN
917cd4a832SDavid Schultz 	u.bits[1] |= 0x7ff80000;
927cd4a832SDavid Schultz #else
937cd4a832SDavid Schultz 	u.bits[0] |= 0x7ff80000;
947cd4a832SDavid Schultz #endif
957cd4a832SDavid Schultz 	return (u.d);
964b6b5744SDavid Schultz }
974b6b5744SDavid Schultz 
984b6b5744SDavid Schultz float
994b6b5744SDavid Schultz nanf(const char *s)
1004b6b5744SDavid Schultz {
1017cd4a832SDavid Schultz 	union {
1027cd4a832SDavid Schultz 		float f;
1037cd4a832SDavid Schultz 		uint32_t bits[1];
1047cd4a832SDavid Schultz 	} u;
1054b6b5744SDavid Schultz 
1067cd4a832SDavid Schultz 	_scan_nan(u.bits, 1, s);
1077cd4a832SDavid Schultz 	u.bits[0] |= 0x7fc00000;
1087cd4a832SDavid Schultz 	return (u.f);
1094b6b5744SDavid Schultz }
1104b6b5744SDavid Schultz 
1114b6b5744SDavid Schultz #if (LDBL_MANT_DIG == 53)
1124b6b5744SDavid Schultz __weak_reference(nan, nanl);
1134b6b5744SDavid Schultz #endif
114