Home | History | Annotate | Line # | Download | only in gdtoa
hexnan.c revision 1.3
      1 /* $NetBSD: hexnan.c,v 1.3 2006/03/11 18:38:14 kleink 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 	while((c = *(CONST unsigned char*)++s) != 0) {
     77 		if (!(h = hexdig[c])) {
     78 			if (c <= ' ') {
     79 				if (hd0 < havedig) {
     80 					if (x < x1 && i < 8)
     81 						L_shift(x, x1, i);
     82 					if (x <= x0) {
     83 						i = 8;
     84 						continue;
     85 						}
     86 					hd0 = havedig;
     87 					*--x = 0;
     88 					x1 = x;
     89 					i = 0;
     90 					}
     91 				continue;
     92 				}
     93 			if (/*(*/ c == ')' && havedig) {
     94 				*sp = s + 1;
     95 				break;
     96 				}
     97 			return STRTOG_NaN;
     98 			}
     99 		havedig++;
    100 		if (++i > 8) {
    101 			if (x <= x0)
    102 				continue;
    103 			i = 1;
    104 			*--x = 0;
    105 			}
    106 		*x = (*x << 4) | (h & 0xf);
    107 		}
    108 	if (!havedig)
    109 		return STRTOG_NaN;
    110 	if (x < x1 && i < 8)
    111 		L_shift(x, x1, i);
    112 	if (x > x0) {
    113 		x1 = x0;
    114 		do *x1++ = *x++;
    115 			while(x <= xe);
    116 		do *x1++ = 0;
    117 			while(x1 <= xe);
    118 		}
    119 	else {
    120 		/* truncate high-order word if necessary */
    121 		if ( (i = nbits & (ULbits-1)) !=0)
    122 			*xe &= ((ULong)0xffffffff) >> (ULbits - i);
    123 		}
    124 	for(x1 = xe;; --x1) {
    125 		if (*x1 != 0)
    126 			break;
    127 		if (x1 == x0) {
    128 			*x1 = 1;
    129 			break;
    130 			}
    131 		}
    132 	return STRTOG_NaNbits;
    133 	}
    134