Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strtoIg.c
Go to the documentation of this file.
1 /****************************************************************
2 
3 The author of this software is David M. Gay.
4 
5 Copyright (C) 1998 by Lucent Technologies
6 All Rights Reserved
7 
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
17 
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
26 
27 ****************************************************************/
28 
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30  * with " at " changed at "@" and " dot " changed to "."). */
31 
32 #include "gdtoaimp.h"
33 
34 int
35 #ifdef KR_headers
36  strtoIg(s00, se, fpi, exp, B, rvp) CONST char* s00;
37 char** se;
38 FPI* fpi;
39 Long* exp;
40 Bigint** B;
41 int* rvp;
42 #else
43 strtoIg(CONST char *s00, char **se, FPI *fpi, Long *exp, Bigint **B, int *rvp)
44 #endif
45 {
46  Bigint *b, *b1;
47  int i, nb, nw, nw1, rv, rv1, swap;
48  unsigned int nb1, nb11;
49  Long e1;
50 
51  b = *B;
52  rv = strtodg(s00, se, fpi, exp, b->x);
53  if(!(rv & STRTOG_Inexact))
54  {
55  B[1] = 0;
56  return *rvp = rv;
57  }
58  e1 = exp[0];
59  rv1 = rv ^ STRTOG_Inexact;
60  b1 = Balloc(b->k);
61  Bcopy(b1, b);
62  nb = fpi->nbits;
63  nb1 = nb & 31;
64  nb11 = (nb1 - 1) & 31;
65  nw = b->wds;
66  nw1 = nw - 1;
67  if(rv & STRTOG_Inexlo)
68  {
69  swap = 0;
70  b1 = increment(b1);
71  if(fpi->sudden_underflow && (rv & STRTOG_Retmask) == STRTOG_Zero)
72  {
73  b1->x[0] = 0;
74  b1->x[nw1] = (ULong)(1L << nb11);
75  rv1 += STRTOG_Normal - STRTOG_Zero;
76  rv1 &= ~STRTOG_Underflow;
77  goto swapcheck;
78  }
79  if(b1->wds > nw || (nb1 && b1->x[nw1] & 1L << nb1))
80  {
81  if(++e1 > fpi->emax)
82  {
83  {
85  }
86  }
87  rshift(b1, 1);
88  }
89  else if((rv & STRTOG_Retmask) == STRTOG_Denormal)
90  {
91  if(b1->x[nw1] & 1L << nb11)
92  {
94  rv1 &= ~STRTOG_Underflow;
95  }
96  }
97  }
98  else
99  {
100  swap = STRTOG_Neg;
101  if((rv & STRTOG_Retmask) == STRTOG_Infinite)
102  {
103  b1 = set_ones(b1, nb);
104  e1 = fpi->emax;
106  goto swapcheck;
107  }
108  decrement(b1);
109  if((rv & STRTOG_Retmask) == STRTOG_Denormal)
110  {
111  for(i = nw1; !b1->x[i]; --i)
112  {
113  {
114  if(!i)
115  {
116  rv1 = STRTOG_Zero | STRTOG_Inexlo;
117  break;
118  }
119  }
120  }
121  goto swapcheck;
122  }
123  if(!(b1->x[nw1] & 1L << nb11))
124  {
125  if(e1 == fpi->emin)
126  {
127  if(fpi->sudden_underflow)
128  {
129  {
130  rv1 += STRTOG_Zero - STRTOG_Normal;
131  }
132  }
133  else
134  {
135  {
137  }
138  }
139  rv1 |= STRTOG_Underflow;
140  }
141  else
142  {
143  b1 = lshift(b1, 1);
144  b1->x[0] |= 1;
145  --e1;
146  }
147  }
148  }
149 swapcheck:
150  if(swap ^ (rv & STRTOG_Neg))
151  {
152  rvp[0] = rv1;
153  rvp[1] = rv;
154  B[0] = b1;
155  B[1] = b;
156  exp[1] = exp[0];
157  exp[0] = e1;
158  }
159  else
160  {
161  rvp[0] = rv;
162  rvp[1] = rv1;
163  B[1] = b1;
164  exp[1] = e1;
165  }
166  return rv;
167 }
int emax
Definition: gdtoa.h:90
ULong x[1]
Definition: gdtoaimp.h:488
#define swap(a, b)
Definition: qsort.c:83
int wds
Definition: gdtoaimp.h:487
int sudden_underflow
Definition: gdtoa.h:92
#define CONST
Definition: gdtoa.h:61
Bigint * lshift(Bigint *b, int k)
Definition: misc.c:495
#define Bcopy(x, y)
Definition: gdtoaimp.h:501
int nbits
Definition: gdtoa.h:88
Bigint * increment(Bigint *b)
Definition: strtodg.c:73
int decrement(Bigint *b)
Definition: strtodg.c:122
unsigned Long ULong
Definition: gdtoa.h:41
int strtoIg(CONST char *s00, char **se, FPI *fpi, Long *exp, Bigint **B, int *rvp)
Definition: strtoIg.c:43
int emin
Definition: gdtoa.h:89
void rshift(Bigint *b, int k)
Definition: gmisc.c:39
Bigint * set_ones(Bigint *b, int n)
Definition: strtodg.c:190
Definition: gdtoa.h:86
Bigint * Balloc(int k)
Definition: misc.c:47
#define Long
Definition: gdtoa.h:38
int k
Definition: gdtoaimp.h:487
int strtodg(CONST char *s00, char **se, FPI *fpi, Long *exp, ULong *bits)
Definition: strtodg.c:443