xref: /freebsd/lib/msun/src/s_nan.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
14b6b5744SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
297cd4a832SDavid Schultz #include <sys/endian.h>
307cd4a832SDavid Schultz #include <ctype.h>
314b6b5744SDavid Schultz #include <float.h>
324b6b5744SDavid Schultz #include <math.h>
337cd4a832SDavid Schultz #include <stdint.h>
347cd4a832SDavid Schultz #include <strings.h>
354b6b5744SDavid Schultz 
364b6b5744SDavid Schultz #include "math_private.h"
374b6b5744SDavid Schultz 
387cd4a832SDavid Schultz /*
397cd4a832SDavid Schultz  * Scan a string of hexadecimal digits (the format nan(3) expects) and
407cd4a832SDavid Schultz  * make a bit array (using the local endianness). We stop when we
417cd4a832SDavid Schultz  * encounter an invalid character, NUL, etc.  If we overflow, we do
427cd4a832SDavid Schultz  * the same as gcc's __builtin_nan(), namely, discard the high order bits.
437cd4a832SDavid Schultz  *
447cd4a832SDavid Schultz  * The format this routine accepts needs to be compatible with what is used
457cd4a832SDavid Schultz  * in contrib/gdtoa/hexnan.c (for strtod/scanf) and what is used in
467cd4a832SDavid Schultz  * __builtin_nan(). In fact, we're only 100% compatible for strings we
477cd4a832SDavid Schultz  * consider valid, so we might be violating the C standard. But it's
487cd4a832SDavid Schultz  * impossible to use nan(3) portably anyway, so this seems good enough.
497cd4a832SDavid Schultz  */
507cd4a832SDavid Schultz void
_scan_nan(uint32_t * words,int num_words,const char * s)517cd4a832SDavid Schultz _scan_nan(uint32_t *words, int num_words, const char *s)
527cd4a832SDavid Schultz {
537cd4a832SDavid Schultz 	int si;		/* index into s */
547cd4a832SDavid Schultz 	int bitpos;	/* index into words (in bits) */
557cd4a832SDavid Schultz 
567cd4a832SDavid Schultz 	bzero(words, num_words * sizeof(uint32_t));
577cd4a832SDavid Schultz 
587cd4a832SDavid Schultz 	/* Allow a leading '0x'. (It's expected, but redundant.) */
597cd4a832SDavid Schultz 	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
607cd4a832SDavid Schultz 		s += 2;
617cd4a832SDavid Schultz 
627cd4a832SDavid Schultz 	/* Scan forwards in the string, looking for the end of the sequence. */
637cd4a832SDavid Schultz 	for (si = 0; isxdigit(s[si]); si++)
647cd4a832SDavid Schultz 		;
657cd4a832SDavid Schultz 
667cd4a832SDavid Schultz 	/* Scan backwards, filling in the bits in words[] as we go. */
677cd4a832SDavid Schultz 	for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
687cd4a832SDavid Schultz 		if (--si < 0)
697cd4a832SDavid Schultz 			break;
70f1343c7fSAlfredo Dal'Ava Junior #if _BYTE_ORDER == _LITTLE_ENDIAN
717cd4a832SDavid Schultz 		words[bitpos / 32] |= digittoint(s[si]) << (bitpos % 32);
72f1343c7fSAlfredo Dal'Ava Junior #else
73f1343c7fSAlfredo Dal'Ava Junior 		words[num_words - 1 - bitpos / 32] |=
74f1343c7fSAlfredo Dal'Ava Junior 		    digittoint(s[si]) << (bitpos % 32);
75f1343c7fSAlfredo Dal'Ava Junior #endif
767cd4a832SDavid Schultz 	}
777cd4a832SDavid Schultz }
787cd4a832SDavid Schultz 
794b6b5744SDavid Schultz double
nan(const char * s)804b6b5744SDavid Schultz nan(const char *s)
814b6b5744SDavid Schultz {
827cd4a832SDavid Schultz 	union {
837cd4a832SDavid Schultz 		double d;
847cd4a832SDavid Schultz 		uint32_t bits[2];
857cd4a832SDavid Schultz 	} u;
864b6b5744SDavid Schultz 
877cd4a832SDavid Schultz 	_scan_nan(u.bits, 2, s);
887cd4a832SDavid Schultz #if _BYTE_ORDER == _LITTLE_ENDIAN
897cd4a832SDavid Schultz 	u.bits[1] |= 0x7ff80000;
907cd4a832SDavid Schultz #else
917cd4a832SDavid Schultz 	u.bits[0] |= 0x7ff80000;
927cd4a832SDavid Schultz #endif
937cd4a832SDavid Schultz 	return (u.d);
944b6b5744SDavid Schultz }
954b6b5744SDavid Schultz 
964b6b5744SDavid Schultz float
nanf(const char * s)974b6b5744SDavid Schultz nanf(const char *s)
984b6b5744SDavid Schultz {
997cd4a832SDavid Schultz 	union {
1007cd4a832SDavid Schultz 		float f;
1017cd4a832SDavid Schultz 		uint32_t bits[1];
1027cd4a832SDavid Schultz 	} u;
1034b6b5744SDavid Schultz 
1047cd4a832SDavid Schultz 	_scan_nan(u.bits, 1, s);
1057cd4a832SDavid Schultz 	u.bits[0] |= 0x7fc00000;
1067cd4a832SDavid Schultz 	return (u.f);
1074b6b5744SDavid Schultz }
1084b6b5744SDavid Schultz 
1094b6b5744SDavid Schultz #if (LDBL_MANT_DIG == 53)
1104b6b5744SDavid Schultz __weak_reference(nan, nanl);
1114b6b5744SDavid Schultz #endif
112