OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc2/] [newlib/] [libc/] [stdlib/] [gdtoa-gethex.c] - Diff between revs 207 and 520

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 520
/****************************************************************
/****************************************************************
 
 
The author of this software is David M. Gay.
The author of this software is David M. Gay.
 
 
Copyright (C) 1998 by Lucent Technologies
Copyright (C) 1998 by Lucent Technologies
All Rights Reserved
All Rights Reserved
 
 
Permission to use, copy, modify, and distribute this software and
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of Lucent or any of its entities
documentation, and that the name of Lucent or any of its entities
not be used in advertising or publicity pertaining to
not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
distribution of the software without specific, written prior
permission.
permission.
 
 
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
THIS SOFTWARE.
 
 
****************************************************************/
****************************************************************/
 
 
/* Please send bug reports to David M. Gay (dmg at acm dot org,
/* Please send bug reports to David M. Gay (dmg at acm dot org,
 * with " at " changed at "@" and " dot " changed to ".").      */
 * with " at " changed at "@" and " dot " changed to ".").      */
 
 
#include <_ansi.h>
#include <_ansi.h>
#include <reent.h>
#include <reent.h>
#include <string.h>
#include <string.h>
#include "mprec.h"
#include "mprec.h"
#include "gdtoa.h"
#include "gdtoa.h"
#include "gd_qnan.h"
#include "gd_qnan.h"
#include "locale.h"
#include "locale.h"
 
 
unsigned char hexdig[256];
unsigned char hexdig[256];
 
 
static void
static void
_DEFUN (htinit, (h, s, inc),
_DEFUN (htinit, (h, s, inc),
        unsigned char *h _AND
        unsigned char *h _AND
        unsigned char *s _AND
        unsigned char *s _AND
        int inc)
        int inc)
{
{
        int i, j;
        int i, j;
        for(i = 0; (j = s[i]) !=0; i++)
        for(i = 0; (j = s[i]) !=0; i++)
                h[j] = i + inc;
                h[j] = i + inc;
}
}
 
 
void
void
_DEFUN_VOID (hexdig_init)
_DEFUN_VOID (hexdig_init)
{
{
#define USC (unsigned char *)
#define USC (unsigned char *)
        htinit(hexdig, USC "0123456789", 0x10);
        htinit(hexdig, USC "0123456789", 0x10);
        htinit(hexdig, USC "abcdef", 0x10 + 10);
        htinit(hexdig, USC "abcdef", 0x10 + 10);
        htinit(hexdig, USC "ABCDEF", 0x10 + 10);
        htinit(hexdig, USC "ABCDEF", 0x10 + 10);
}
}
 
 
static void
static void
_DEFUN(rshift, (b, k),
_DEFUN(rshift, (b, k),
        _Bigint *b _AND
        _Bigint *b _AND
        int k)
        int k)
{
{
        __ULong *x, *x1, *xe, y;
        __ULong *x, *x1, *xe, y;
        int n;
        int n;
 
 
        x = x1 = b->_x;
        x = x1 = b->_x;
        n = k >> kshift;
        n = k >> kshift;
        if (n < b->_wds) {
        if (n < b->_wds) {
                xe = x + b->_wds;
                xe = x + b->_wds;
                x += n;
                x += n;
                if (k &= kmask) {
                if (k &= kmask) {
                        n = ULbits - k;
                        n = ULbits - k;
                        y = *x++ >> k;
                        y = *x++ >> k;
                        while(x < xe) {
                        while(x < xe) {
                                *x1++ = (y | (*x << n)) & ALL_ON;
                                *x1++ = (y | (*x << n)) & ALL_ON;
                                y = *x++ >> k;
                                y = *x++ >> k;
                                }
                                }
                        if ((*x1 = y) !=0)
                        if ((*x1 = y) !=0)
                                x1++;
                                x1++;
                        }
                        }
                else
                else
                        while(x < xe)
                        while(x < xe)
                                *x1++ = *x++;
                                *x1++ = *x++;
                }
                }
        if ((b->_wds = x1 - b->_x) == 0)
        if ((b->_wds = x1 - b->_x) == 0)
                b->_x[0] = 0;
                b->_x[0] = 0;
}
}
 
 
static _Bigint *
static _Bigint *
_DEFUN (increment, (ptr, b),
_DEFUN (increment, (ptr, b),
        struct _reent *ptr _AND
        struct _reent *ptr _AND
        _Bigint *b)
        _Bigint *b)
{
{
        __ULong *x, *xe;
        __ULong *x, *xe;
        _Bigint *b1;
        _Bigint *b1;
#ifdef Pack_16
#ifdef Pack_16
        __ULong carry = 1, y;
        __ULong carry = 1, y;
#endif
#endif
 
 
        x = b->_x;
        x = b->_x;
        xe = x + b->_wds;
        xe = x + b->_wds;
#ifdef Pack_32
#ifdef Pack_32
        do {
        do {
                if (*x < (__ULong)0xffffffffL) {
                if (*x < (__ULong)0xffffffffL) {
                        ++*x;
                        ++*x;
                        return b;
                        return b;
                        }
                        }
                *x++ = 0;
                *x++ = 0;
                } while(x < xe);
                } while(x < xe);
#else
#else
        do {
        do {
                y = *x + carry;
                y = *x + carry;
                carry = y >> 16;
                carry = y >> 16;
                *x++ = y & 0xffff;
                *x++ = y & 0xffff;
                if (!carry)
                if (!carry)
                        return b;
                        return b;
                } while(x < xe);
                } while(x < xe);
        if (carry)
        if (carry)
#endif
#endif
        {
        {
                if (b->_wds >= b->_maxwds) {
                if (b->_wds >= b->_maxwds) {
                        b1 = Balloc(ptr, b->_k+1);
                        b1 = Balloc(ptr, b->_k+1);
                        Bcopy(b1, b);
                        Bcopy(b1, b);
                        Bfree(ptr, b);
                        Bfree(ptr, b);
                        b = b1;
                        b = b1;
                        }
                        }
                b->_x[b->_wds++] = 1;
                b->_x[b->_wds++] = 1;
                }
                }
        return b;
        return b;
}
}
 
 
 
 
int
int
_DEFUN(gethex, (ptr, sp, fpi, exp, bp, sign),
_DEFUN(gethex, (ptr, sp, fpi, exp, bp, sign),
        struct _reent *ptr _AND
        struct _reent *ptr _AND
        _CONST char **sp _AND
        _CONST char **sp _AND
        FPI *fpi _AND
        FPI *fpi _AND
        Long *exp _AND
        Long *exp _AND
        _Bigint **bp _AND
        _Bigint **bp _AND
        int sign)
        int sign)
{
{
        _Bigint *b;
        _Bigint *b;
        _CONST unsigned char *decpt, *s0, *s, *s1;
        _CONST unsigned char *decpt, *s0, *s, *s1;
        int esign, havedig, irv, k, n, nbits, up, zret;
        int esign, havedig, irv, k, n, nbits, up, zret;
        __ULong L, lostbits, *x;
        __ULong L, lostbits, *x;
        Long e, e1;
        Long e, e1;
        unsigned char *decimalpoint = (unsigned char *)
        unsigned char *decimalpoint = (unsigned char *)
                                      _localeconv_r (ptr)->decimal_point;
                                      _localeconv_r (ptr)->decimal_point;
        size_t decp_len = strlen ((const char *) decimalpoint);
        size_t decp_len = strlen ((const char *) decimalpoint);
        unsigned char decp_end = decimalpoint[decp_len - 1];
        unsigned char decp_end = decimalpoint[decp_len - 1];
 
 
        if (!hexdig['0'])
        if (!hexdig['0'])
                hexdig_init();
                hexdig_init();
        havedig = 0;
        havedig = 0;
        s0 = *(_CONST unsigned char **)sp + 2;
        s0 = *(_CONST unsigned char **)sp + 2;
        while(s0[havedig] == '0')
        while(s0[havedig] == '0')
                havedig++;
                havedig++;
        s0 += havedig;
        s0 += havedig;
        s = s0;
        s = s0;
        decpt = 0;
        decpt = 0;
        zret = 0;
        zret = 0;
        e = 0;
        e = 0;
        if (!hexdig[*s]) {
        if (!hexdig[*s]) {
                zret = 1;
                zret = 1;
                if (strncmp ((const char *) s, (const char *) decimalpoint,
                if (strncmp ((const char *) s, (const char *) decimalpoint,
                             decp_len) != 0)
                             decp_len) != 0)
                        goto pcheck;
                        goto pcheck;
                decpt = (s += decp_len);
                decpt = (s += decp_len);
                if (!hexdig[*s])
                if (!hexdig[*s])
                        goto pcheck;
                        goto pcheck;
                while(*s == '0')
                while(*s == '0')
                        s++;
                        s++;
                if (hexdig[*s])
                if (hexdig[*s])
                        zret = 0;
                        zret = 0;
                havedig = 1;
                havedig = 1;
                s0 = s;
                s0 = s;
                }
                }
        while(hexdig[*s])
        while(hexdig[*s])
                s++;
                s++;
        if (strncmp ((const char *) s, (const char *) decimalpoint,
        if (strncmp ((const char *) s, (const char *) decimalpoint,
                     decp_len) == 0
                     decp_len) == 0
            && !decpt) {
            && !decpt) {
                decpt = (s += decp_len);
                decpt = (s += decp_len);
                while(hexdig[*s])
                while(hexdig[*s])
                        s++;
                        s++;
                }
                }
        if (decpt)
        if (decpt)
                e = -(((Long)(s-decpt)) << 2);
                e = -(((Long)(s-decpt)) << 2);
 pcheck:
 pcheck:
        s1 = s;
        s1 = s;
        switch(*s) {
        switch(*s) {
          case 'p':
          case 'p':
          case 'P':
          case 'P':
                esign = 0;
                esign = 0;
                switch(*++s) {
                switch(*++s) {
                  case '-':
                  case '-':
                        esign = 1;
                        esign = 1;
                        /* no break */
                        /* no break */
                  case '+':
                  case '+':
                        s++;
                        s++;
                  }
                  }
                if ((n = hexdig[*s]) == 0 || n > 0x19) {
                if ((n = hexdig[*s]) == 0 || n > 0x19) {
                        s = s1;
                        s = s1;
                        break;
                        break;
                        }
                        }
                e1 = n - 0x10;
                e1 = n - 0x10;
                while((n = hexdig[*++s]) !=0 && n <= 0x19)
                while((n = hexdig[*++s]) !=0 && n <= 0x19)
                        e1 = 10*e1 + n - 0x10;
                        e1 = 10*e1 + n - 0x10;
                if (esign)
                if (esign)
                        e1 = -e1;
                        e1 = -e1;
                e += e1;
                e += e1;
          }
          }
        *sp = (char*)s;
        *sp = (char*)s;
        if (zret)
        if (zret)
                return havedig ? STRTOG_Zero : STRTOG_NoNumber;
                return havedig ? STRTOG_Zero : STRTOG_NoNumber;
        n = s1 - s0 - 1;
        n = s1 - s0 - 1;
        for(k = 0; n > 7; n >>= 1)
        for(k = 0; n > 7; n >>= 1)
                k++;
                k++;
        b = Balloc(ptr, k);
        b = Balloc(ptr, k);
        x = b->_x;
        x = b->_x;
        n = 0;
        n = 0;
        L = 0;
        L = 0;
        while(s1 > s0) {
        while(s1 > s0) {
                if (*--s1 == decp_end && s1 - decp_len + 1 >= s0
                if (*--s1 == decp_end && s1 - decp_len + 1 >= s0
                    && strncmp ((const char *) s1 - decp_len + 1,
                    && strncmp ((const char *) s1 - decp_len + 1,
                                (const char *) decimalpoint, decp_len) == 0) {
                                (const char *) decimalpoint, decp_len) == 0) {
                        s1 -= decp_len - 1; /* Note the --s1 above! */
                        s1 -= decp_len - 1; /* Note the --s1 above! */
                        continue;
                        continue;
                }
                }
                if (n == 32) {
                if (n == 32) {
                        *x++ = L;
                        *x++ = L;
                        L = 0;
                        L = 0;
                        n = 0;
                        n = 0;
                        }
                        }
                L |= (hexdig[*s1] & 0x0f) << n;
                L |= (hexdig[*s1] & 0x0f) << n;
                n += 4;
                n += 4;
                }
                }
        *x++ = L;
        *x++ = L;
        b->_wds = n = x - b->_x;
        b->_wds = n = x - b->_x;
        n = 32*n - hi0bits(L);
        n = 32*n - hi0bits(L);
        nbits = fpi->nbits;
        nbits = fpi->nbits;
        lostbits = 0;
        lostbits = 0;
        x = b->_x;
        x = b->_x;
        if (n > nbits) {
        if (n > nbits) {
                n -= nbits;
                n -= nbits;
                if (any_on(b,n)) {
                if (any_on(b,n)) {
                        lostbits = 1;
                        lostbits = 1;
                        k = n - 1;
                        k = n - 1;
                        if (x[k>>kshift] & 1 << (k & kmask)) {
                        if (x[k>>kshift] & 1 << (k & kmask)) {
                                lostbits = 2;
                                lostbits = 2;
                                if (k > 1 && any_on(b,k-1))
                                if (k > 1 && any_on(b,k-1))
                                        lostbits = 3;
                                        lostbits = 3;
                                }
                                }
                        }
                        }
                rshift(b, n);
                rshift(b, n);
                e += n;
                e += n;
                }
                }
        else if (n < nbits) {
        else if (n < nbits) {
                n = nbits - n;
                n = nbits - n;
                b = lshift(ptr, b, n);
                b = lshift(ptr, b, n);
                e -= n;
                e -= n;
                x = b->_x;
                x = b->_x;
                }
                }
        if (e > fpi->emax) {
        if (e > fpi->emax) {
 ovfl:
 ovfl:
                Bfree(ptr, b);
                Bfree(ptr, b);
                *bp = 0;
                *bp = 0;
                return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi;
                return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi;
                }
                }
        irv = STRTOG_Normal;
        irv = STRTOG_Normal;
        if (e < fpi->emin) {
        if (e < fpi->emin) {
                irv = STRTOG_Denormal;
                irv = STRTOG_Denormal;
                n = fpi->emin - e;
                n = fpi->emin - e;
                if (n >= nbits) {
                if (n >= nbits) {
                        switch (fpi->rounding) {
                        switch (fpi->rounding) {
                          case FPI_Round_near:
                          case FPI_Round_near:
                                if (n == nbits && (n < 2 || any_on(b,n-1)))
                                if (n == nbits && (n < 2 || any_on(b,n-1)))
                                        goto one_bit;
                                        goto one_bit;
                                break;
                                break;
                          case FPI_Round_up:
                          case FPI_Round_up:
                                if (!sign)
                                if (!sign)
                                        goto one_bit;
                                        goto one_bit;
                                break;
                                break;
                          case FPI_Round_down:
                          case FPI_Round_down:
                                if (sign) {
                                if (sign) {
 one_bit:
 one_bit:
                                        *exp = fpi->emin;
                                        *exp = fpi->emin;
                                        x[0] = b->_wds = 1;
                                        x[0] = b->_wds = 1;
                                        *bp = b;
                                        *bp = b;
                                        return STRTOG_Denormal | STRTOG_Inexhi
                                        return STRTOG_Denormal | STRTOG_Inexhi
                                                | STRTOG_Underflow;
                                                | STRTOG_Underflow;
                                        }
                                        }
                          }
                          }
                        Bfree(ptr, b);
                        Bfree(ptr, b);
                        *bp = 0;
                        *bp = 0;
                        return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow;
                        return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow;
                        }
                        }
                k = n - 1;
                k = n - 1;
                if (lostbits)
                if (lostbits)
                        lostbits = 1;
                        lostbits = 1;
                else if (k > 0)
                else if (k > 0)
                        lostbits = any_on(b,k);
                        lostbits = any_on(b,k);
                if (x[k>>kshift] & 1 << (k & kmask))
                if (x[k>>kshift] & 1 << (k & kmask))
                        lostbits |= 2;
                        lostbits |= 2;
                nbits -= n;
                nbits -= n;
                rshift(b,n);
                rshift(b,n);
                e = fpi->emin;
                e = fpi->emin;
                }
                }
        if (lostbits) {
        if (lostbits) {
                up = 0;
                up = 0;
                switch(fpi->rounding) {
                switch(fpi->rounding) {
                  case FPI_Round_zero:
                  case FPI_Round_zero:
                        break;
                        break;
                  case FPI_Round_near:
                  case FPI_Round_near:
                    if ((lostbits & 2)
                    if ((lostbits & 2)
                            && ((lostbits & 1) | (x[0] & 1)))
                            && ((lostbits & 1) | (x[0] & 1)))
                                up = 1;
                                up = 1;
                        break;
                        break;
                  case FPI_Round_up:
                  case FPI_Round_up:
                        up = 1 - sign;
                        up = 1 - sign;
                        break;
                        break;
                  case FPI_Round_down:
                  case FPI_Round_down:
                        up = sign;
                        up = sign;
                  }
                  }
                if (up) {
                if (up) {
                        k = b->_wds;
                        k = b->_wds;
                        b = increment(ptr, b);
                        b = increment(ptr, b);
                        x = b->_x;
                        x = b->_x;
                        if (irv == STRTOG_Denormal) {
                        if (irv == STRTOG_Denormal) {
                                if (nbits == fpi->nbits - 1
                                if (nbits == fpi->nbits - 1
                                 && x[nbits >> kshift] & 1 << (nbits & kmask))
                                 && x[nbits >> kshift] & 1 << (nbits & kmask))
                                        irv =  STRTOG_Normal;
                                        irv =  STRTOG_Normal;
                                }
                                }
                        else if ((b->_wds > k)
                        else if ((b->_wds > k)
                         || ((n = nbits & kmask) !=0
                         || ((n = nbits & kmask) !=0
                             && (hi0bits(x[k-1]) < 32-n))) {
                             && (hi0bits(x[k-1]) < 32-n))) {
                                rshift(b,1);
                                rshift(b,1);
                                if (++e > fpi->emax)
                                if (++e > fpi->emax)
                                        goto ovfl;
                                        goto ovfl;
                                }
                                }
                        irv |= STRTOG_Inexhi;
                        irv |= STRTOG_Inexhi;
                        }
                        }
                else
                else
                        irv |= STRTOG_Inexlo;
                        irv |= STRTOG_Inexlo;
                }
                }
        *bp = b;
        *bp = b;
        *exp = e;
        *exp = e;
        return irv;
        return irv;
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.