xref: /freebsd/lib/msun/src/s_nan.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
14b6b5744SDavid Schultz /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro 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 #if _BYTE_ORDER == _LITTLE_ENDIAN
707cd4a832SDavid Schultz 	for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
717cd4a832SDavid Schultz #else
727cd4a832SDavid Schultz 	for (bitpos = 32 * num_words - 4; bitpos >= 0; bitpos -= 4) {
737cd4a832SDavid Schultz #endif
747cd4a832SDavid Schultz 		if (--si < 0)
757cd4a832SDavid Schultz 			break;
767cd4a832SDavid Schultz 		words[bitpos / 32] |= digittoint(s[si]) << (bitpos % 32);
777cd4a832SDavid Schultz 	}
787cd4a832SDavid Schultz }
797cd4a832SDavid Schultz 
804b6b5744SDavid Schultz double
814b6b5744SDavid Schultz nan(const char *s)
824b6b5744SDavid Schultz {
837cd4a832SDavid Schultz 	union {
847cd4a832SDavid Schultz 		double d;
857cd4a832SDavid Schultz 		uint32_t bits[2];
867cd4a832SDavid Schultz 	} u;
874b6b5744SDavid Schultz 
887cd4a832SDavid Schultz 	_scan_nan(u.bits, 2, s);
897cd4a832SDavid Schultz #if _BYTE_ORDER == _LITTLE_ENDIAN
907cd4a832SDavid Schultz 	u.bits[1] |= 0x7ff80000;
917cd4a832SDavid Schultz #else
927cd4a832SDavid Schultz 	u.bits[0] |= 0x7ff80000;
937cd4a832SDavid Schultz #endif
947cd4a832SDavid Schultz 	return (u.d);
954b6b5744SDavid Schultz }
964b6b5744SDavid Schultz 
974b6b5744SDavid Schultz float
984b6b5744SDavid Schultz nanf(const char *s)
994b6b5744SDavid Schultz {
1007cd4a832SDavid Schultz 	union {
1017cd4a832SDavid Schultz 		float f;
1027cd4a832SDavid Schultz 		uint32_t bits[1];
1037cd4a832SDavid Schultz 	} u;
1044b6b5744SDavid Schultz 
1057cd4a832SDavid Schultz 	_scan_nan(u.bits, 1, s);
1067cd4a832SDavid Schultz 	u.bits[0] |= 0x7fc00000;
1077cd4a832SDavid Schultz 	return (u.f);
1084b6b5744SDavid Schultz }
1094b6b5744SDavid Schultz 
1104b6b5744SDavid Schultz #if (LDBL_MANT_DIG == 53)
1114b6b5744SDavid Schultz __weak_reference(nan, nanl);
1124b6b5744SDavid Schultz #endif
113