Home | History | Annotate | Line # | Download | only in gdtoa
      1 /* $NetBSD: hexnan.c,v 1.5 2011/03/21 04:52:09 christos Exp $ */
      2 
      3 /****************************************************************
      4 
      5 The author of this software is David M. Gay.
      6 
      7 Copyright (C) 2000 by Lucent Technologies
      8 All Rights Reserved
      9 
     10 Permission to use, copy, modify, and distribute this software and
     11 its documentation for any purpose and without fee is hereby
     12 granted, provided that the above copyright notice appear in all
     13 copies and that both that the copyright notice and this
     14 permission notice and warranty disclaimer appear in supporting
     15 documentation, and that the name of Lucent or any of its entities
     16 not be used in advertising or publicity pertaining to
     17 distribution of the software without specific, written prior
     18 permission.
     19 
     20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
     22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
     23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
     26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
     27 THIS SOFTWARE.
     28 
     29 ****************************************************************/
     30 
     31 /* Please send bug reports to David M. Gay (dmg at acm dot org,
     32  * with " at " changed at "@" and " dot " changed to ".").	*/
     33 
     34 #include "gdtoaimp.h"
     35 
     36  static void
     37 #ifdef KR_headers
     38 L_shift(x, x1, i) ULong *x; ULong *x1; int i;
     39 #else
     40 L_shift(ULong *x, ULong *x1, int i)
     41 #endif
     42 {
     43 	int j;
     44 
     45 	i = 8 - i;
     46 	i <<= 2;
     47 	j = ULbits - i;
     48 	do {
     49 		*x |= x[1] << j;
     50 		x[1] >>= i;
     51 		} while(++x < x1);
     52 	}
     53 
     54  int
     55 #ifdef KR_headers
     56 hexnan(sp, fpi, x0)
     57 	CONST char **sp; CONST FPI *fpi; ULong *x0;
     58 #else
     59 hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0)
     60 #endif
     61 {
     62 	ULong c, h, *x, *x1, *xe;
     63 	CONST char *s;
     64 	int havedig, hd0, i, nbits;
     65 
     66 	if (!hexdig['0'])
     67 		hexdig_init_D2A();
     68 	nbits = fpi->nbits;
     69 	x = x0 + ((unsigned int)nbits >> kshift);
     70 	if (nbits & kmask)
     71 		x++;
     72 	*--x = 0;
     73 	x1 = xe = x;
     74 	havedig = hd0 = i = 0;
     75 	s = *sp;
     76 	/* allow optional initial 0x or 0X */
     77 	while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
     78 		++s;
     79 	if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
     80 	 && *(CONST unsigned char*)(s+3) > ' ')
     81 		s += 2;
     82 	while((c = *(CONST unsigned char*)++s) != '\0') {
     83 		if (!(h = hexdig[c])) {
     84 			if (c <= ' ') {
     85 				if (hd0 < havedig) {
     86 					if (x < x1 && i < 8)
     87 						L_shift(x, x1, i);
     88 					if (x <= x0) {
     89 						i = 8;
     90 						continue;
     91 						}
     92 					hd0 = havedig;
     93 					*--x = 0;
     94 					x1 = x;
     95 					i = 0;
     96 					}
     97 				while(*(CONST unsigned char*)(s+1) <= ' ')
     98 					++s;
     99 				if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
    100 				 && *(CONST unsigned char*)(s+3) > ' ')
    101 					s += 2;
    102 				continue;
    103 				}
    104 			if (/*(*/ c == ')' && havedig) {
    105 				*sp = s + 1;
    106 				break;
    107 				}
    108 #ifndef GDTOA_NON_PEDANTIC_NANCHECK
    109 			do {
    110 				if (/*(*/ c == ')') {
    111 					*sp = s + 1;
    112 					break;
    113 					}
    114 				} while((c = *++s) != '\0');
    115 #endif
    116 			return STRTOG_NaN;
    117 			}
    118 		havedig++;
    119 		if (++i > 8) {
    120 			if (x <= x0)
    121 				continue;
    122 			i = 1;
    123 			*--x = 0;
    124 			}
    125 		*x = (*x << 4) | (h & 0xf);
    126 		}
    127 	if (!havedig)
    128 		return STRTOG_NaN;
    129 	if (x < x1 && i < 8)
    130 		L_shift(x, x1, i);
    131 	if (x > x0) {
    132 		x1 = x0;
    133 		do *x1++ = *x++;
    134 			while(x <= xe);
    135 		do *x1++ = 0;
    136 			while(x1 <= xe);
    137 		}
    138 	else {
    139 		/* truncate high-order word if necessary */
    140 		if ( (i = nbits & (ULbits-1)) !=0)
    141 			*xe &= ((ULong)0xffffffff) >> (ULbits - i);
    142 		}
    143 	for(x1 = xe;; --x1) {
    144 		if (*x1 != 0)
    145 			break;
    146 		if (x1 == x0) {
    147 			*x1 = 1;
    148 			break;
    149 			}
    150 		}
    151 	return STRTOG_NaNbits;
    152 	}
    153