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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [machine/] [powerpc/] [strtosfix32.c] - Blame information for rev 158

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
#ifdef __SPE__
2
 
3
#include <_ansi.h>
4
#include <limits.h>
5
#include <errno.h>
6
#include <stdlib.h>
7
#include <reent.h>
8
#include "vfieeefp.h"
9
 
10
/*
11
 * Convert a string to a fixed-point (sign + 31-bits) value.
12
 *
13
 * Ignores `locale' stuff.
14
 */
15
__int32_t
16
_DEFUN (_strtosfix32_r, (rptr, nptr, endptr),
17
        struct _reent *rptr _AND
18
        _CONST char *nptr _AND
19
        char **endptr)
20
{
21
  union double_union dbl;
22
  int exp, negexp, sign;
23
  unsigned long tmp, tmp2;
24
  long result = 0;
25
 
26
  dbl.d = _strtod_r (rptr, nptr, endptr);
27
 
28
  /* treat NAN as domain error, +/- infinity as saturation */
29
  if (!finite(dbl.d))
30
    {
31
      if (isnan (dbl.d))
32
        {
33
          rptr->_errno = EDOM;
34
          return 0;
35
        }
36
      rptr->_errno = ERANGE;
37
      if (word0(dbl) & Sign_bit)
38
        return LONG_MIN;
39
      return LONG_MAX;
40
    }
41
 
42
  /* check for normal saturation */
43
  if (dbl.d >= 1.0)
44
    {
45
      rptr->_errno = ERANGE;
46
      return LONG_MAX;
47
    }
48
  else if (dbl.d < -1.0)
49
    {
50
      rptr->_errno = ERANGE;
51
      return LONG_MIN;
52
    }
53
 
54
  /* otherwise we have normal number in range */
55
 
56
  /* strip off sign and exponent */
57
  sign = word0(dbl) & Sign_bit;
58
  exp = ((word0(dbl) & Exp_mask) >> Exp_shift) - Bias;
59
  negexp = -exp;
60
  if (negexp > 31)
61
    return 0;
62
  word0(dbl) &= ~(Exp_mask | Sign_bit);
63
  /* add in implicit normalized bit */
64
  word0(dbl) |= Exp_msk1;
65
  /* shift so result is contained in single word */
66
  tmp = word0(dbl) << Ebits;
67
  tmp |= ((unsigned long)word1(dbl) >> (32 - Ebits));
68
  if (negexp != 0)
69
    {
70
      /* perform rounding */
71
      tmp2 = tmp + (1 << (negexp - 1));
72
      result = (long)(tmp2 >> negexp);
73
      /* check if rounding caused carry bit which must be added into result */
74
      if (tmp2 < tmp)
75
        result |= (1 << (32 - negexp));
76
      /* check if positive saturation has occurred because of rounding */
77
      if (!sign && result < 0)
78
        {
79
          rptr->_errno = ERANGE;
80
          return LONG_MAX;
81
        }
82
    }
83
  else
84
    {
85
      /* we have -1.0, no rounding necessary */
86
      return LONG_MIN;
87
    }
88
 
89
  return sign ? -result : result;
90
}
91
 
92
#ifndef _REENT_ONLY
93
 
94
__int32_t
95
_DEFUN (strtosfix32, (s, ptr, base),
96
        _CONST char *s _AND
97
        char **ptr)
98
{
99
  return _strtosfix32_r (_REENT, s, ptr);
100
}
101
 
102
#endif
103
 
104
#endif /* __SPE__ */

powered by: WebSVN 2.1.0

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