Home | History | Annotate | Download | only in gdtoa
      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 #include  <LibConfig.h>
     34 
     35 #include "gdtoaimp.h"
     36 
     37  static void
     38 #ifdef KR_headers
     39 L_shift(x, x1, i) ULong *x; ULong *x1; int i;
     40 #else
     41 L_shift(ULong *x, ULong *x1, int i)
     42 #endif
     43 {
     44   int j;
     45 
     46   i = 8 - i;
     47   i <<= 2;
     48   j = ULbits - i;
     49   do {
     50     *x |= x[1] << j;
     51     x[1] >>= i;
     52     } while(++x < x1);
     53   }
     54 
     55  int
     56 #ifdef KR_headers
     57 hexnan(sp, fpi, x0)
     58   CONST char **sp; CONST FPI *fpi; ULong *x0;
     59 #else
     60 hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0)
     61 #endif
     62 {
     63   ULong c, h, *x, *x1, *xe;
     64   CONST char *s;
     65   int havedig, hd0, i, nbits;
     66 
     67   if (!hexdig['0'])
     68     hexdig_init_D2A();
     69   nbits = fpi->nbits;
     70   x = x0 + ((unsigned int)nbits >> kshift);
     71   if (nbits & kmask)
     72     x++;
     73   *--x = 0;
     74   x1 = xe = x;
     75   havedig = hd0 = i = 0;
     76   s = *sp;
     77   while((c = *(CONST unsigned char*)++s) != 0) {
     78     if ((h = hexdig[c]) == 0) {
     79       if (c <= ' ') {
     80         if (hd0 < havedig) {
     81           if (x < x1 && i < 8)
     82             L_shift(x, x1, i);
     83           if (x <= x0) {
     84             i = 8;
     85             continue;
     86             }
     87           hd0 = havedig;
     88           *--x = 0;
     89           x1 = x;
     90           i = 0;
     91           }
     92         continue;
     93         }
     94       if (/*(*/ c == ')' && havedig) {
     95         *sp = s + 1;
     96         break;
     97         }
     98       return STRTOG_NaN;
     99       }
    100     havedig++;
    101     if (++i > 8) {
    102       if (x <= x0)
    103         continue;
    104       i = 1;
    105       *--x = 0;
    106       }
    107     *x = (*x << 4) | (h & 0xf);
    108     }
    109   if (!havedig)
    110     return STRTOG_NaN;
    111   if (x < x1 && i < 8)
    112     L_shift(x, x1, i);
    113   if (x > x0) {
    114     x1 = x0;
    115     do *x1++ = *x++;
    116       while(x <= xe);
    117     do *x1++ = 0;
    118       while(x1 <= xe);
    119     }
    120   else {
    121     /* truncate high-order word if necessary */
    122     if ( (i = nbits & (ULbits-1)) !=0)
    123       *xe &= ((ULong)0xffffffff) >> (ULbits - i);
    124     }
    125   for(x1 = xe;; --x1) {
    126     if (*x1 != 0)
    127       break;
    128     if (x1 == x0) {
    129       *x1 = 1;
    130       break;
    131       }
    132     }
    133   return STRTOG_NaNbits;
    134   }
    135