Home | History | Annotate | Download | only in src
      1 /*-
      2  * Copyright (c) 2007 David Schultz
      3  * All rights reserved.
      4  *
      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.
     13  *
     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.
     25  *
     26  * $FreeBSD$
     27  */
     28 
     29 #include <sys/endian.h>
     30 #include <ctype.h>
     31 #include <float.h>
     32 #include <math.h>
     33 #include <stdint.h>
     34 #include <strings.h>
     35 
     36 #include "math_private.h"
     37 
     38 /* digittoint is in the FreeBSD C library, but not Bionic at this point */
     39 static int
     40 digittoint(char ch)
     41 {
     42     int d;
     43 
     44     d = ch - '0';
     45     if ((unsigned)d < 10) {
     46         return d;
     47     }
     48     d = ch - 'a';
     49     if ((unsigned)d < 6) {
     50         return d + 10;
     51     }
     52     d = ch - 'A';
     53     if ((unsigned)d < 6) {
     54         return d + 10;
     55     }
     56     return -1;
     57 }
     58 
     59 /*
     60  * Scan a string of hexadecimal digits (the format nan(3) expects) and
     61  * make a bit array (using the local endianness). We stop when we
     62  * encounter an invalid character, NUL, etc.  If we overflow, we do
     63  * the same as gcc's __builtin_nan(), namely, discard the high order bits.
     64  *
     65  * The format this routine accepts needs to be compatible with what is used
     66  * in contrib/gdtoa/hexnan.c (for strtod/scanf) and what is used in
     67  * __builtin_nan(). In fact, we're only 100% compatible for strings we
     68  * consider valid, so we might be violating the C standard. But it's
     69  * impossible to use nan(3) portably anyway, so this seems good enough.
     70  */
     71 void
     72 _scan_nan(uint32_t *words, int num_words, const char *s)
     73 {
     74 	int si;		/* index into s */
     75 	int bitpos;	/* index into words (in bits) */
     76 
     77 	bzero(words, num_words * sizeof(uint32_t));
     78 
     79 	/* Allow a leading '0x'. (It's expected, but redundant.) */
     80 	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
     81 		s += 2;
     82 
     83 	/* Scan forwards in the string, looking for the end of the sequence. */
     84 	for (si = 0; isxdigit(s[si]); si++)
     85 		;
     86 
     87 	/* Scan backwards, filling in the bits in words[] as we go. */
     88 #if _BYTE_ORDER == _LITTLE_ENDIAN
     89 	for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
     90 #else
     91 	for (bitpos = 32 * num_words - 4; bitpos >= 0; bitpos -= 4) {
     92 #endif
     93 		if (--si < 0)
     94 			break;
     95 		words[bitpos / 32] |= digittoint(s[si]) << (bitpos % 32);
     96 	}
     97 }
     98 
     99 double
    100 nan(const char *s)
    101 {
    102 	union {
    103 		double d;
    104 		uint32_t bits[2];
    105 	} u;
    106 
    107 	_scan_nan(u.bits, 2, s);
    108 #if _BYTE_ORDER == _LITTLE_ENDIAN
    109 	u.bits[1] |= 0x7ff80000;
    110 #else
    111 	u.bits[0] |= 0x7ff80000;
    112 #endif
    113 	return (u.d);
    114 }
    115 
    116 float
    117 nanf(const char *s)
    118 {
    119 	union {
    120 		float f;
    121 		uint32_t bits[1];
    122 	} u;
    123 
    124 	_scan_nan(u.bits, 1, s);
    125 	u.bits[0] |= 0x7fc00000;
    126 	return (u.f);
    127 }
    128 
    129 #if (LDBL_MANT_DIG == 53)
    130 __weak_alias(nanl, nan);
    131 #endif
    132