xref: /freebsd/contrib/gdtoa/strtoIg.c (revision a35d88931c87cfe6bd38f01d7bad22140b3b38f3)
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
30 	David M. Gay
31 	Bell Laboratories, Room 2C-463
32 	600 Mountain Avenue
33 	Murray Hill, NJ 07974-0636
34 	U.S.A.
35 	dmg@bell-labs.com
36  */
37 
38 #include "gdtoaimp.h"
39 
40  int
41 #ifdef KR_headers
42 strtoIg(s00, se, fpi, exp, B, rvp) CONST char *s00; char **se; FPI *fpi; Long *exp; Bigint **B; int *rvp;
43 #else
44 strtoIg(CONST char *s00, char **se, FPI *fpi, Long *exp, Bigint **B, int *rvp)
45 #endif
46 {
47 	Bigint *b, *b1;
48 	int i, nb, nw, nw1, rv, rv1, swap;
49 	unsigned int nb1, nb11;
50 	Long e1;
51 
52 	b = *B;
53 	rv = strtodg(s00, se, fpi, exp, b->x);
54 	if (!(rv & STRTOG_Inexact)) {
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 		swap = 0;
69 		b1 = increment(b1);
70 		if (fpi->sudden_underflow
71 		 && (rv & STRTOG_Retmask) == STRTOG_Zero) {
72 			b1->x[0] = 0;
73 			b1->x[nw1] = 1L << nb11;
74 			rv1 += STRTOG_Normal - STRTOG_Zero;
75 			rv1 &= ~STRTOG_Underflow;
76 			goto swapcheck;
77 			}
78 		if (b1->wds > nw
79 		 || nb1 && b1->x[nw1] & 1L << nb1) {
80 			if (++e1 > fpi->emax)
81 				rv1 = STRTOG_Infinite | STRTOG_Inexhi;
82 			rshift(b1, 1);
83 			}
84 		else if ((rv & STRTOG_Retmask) == STRTOG_Denormal) {
85 			if (b1->x[nw1] & 1L << nb11) {
86 				rv1 += STRTOG_Normal - STRTOG_Denormal;
87 				rv1 &= ~STRTOG_Underflow;
88 				}
89 			}
90 		}
91 	else {
92 		swap = STRTOG_Neg;
93 		if ((rv & STRTOG_Retmask) == STRTOG_Infinite) {
94 			b1 = set_ones(b1, nb);
95 			e1 = fpi->emax;
96 			rv1 = STRTOG_Normal | STRTOG_Inexlo;
97 			goto swapcheck;
98 			}
99 		decrement(b1);
100 		if ((rv & STRTOG_Retmask) == STRTOG_Denormal) {
101 			for(i = nw1; !b1->x[i]; --i)
102 				if (!i) {
103 					rv1 = STRTOG_Zero | STRTOG_Inexlo;
104 					break;
105 					}
106 			goto swapcheck;
107 			}
108 		if (!(b1->x[nw1] & 1L << nb11)) {
109 			if (e1 == fpi->emin) {
110 				if (fpi->sudden_underflow)
111 					rv1 += STRTOG_Zero - STRTOG_Normal;
112 				else
113 					rv1 += STRTOG_Denormal - STRTOG_Normal;
114 				rv1 |= STRTOG_Underflow;
115 				}
116 			else {
117 				b1 = lshift(b1, 1);
118 				b1->x[0] |= 1;
119 				--e1;
120 				}
121 			}
122 		}
123  swapcheck:
124 	if (swap ^ (rv & STRTOG_Neg)) {
125 		rvp[0] = rv1;
126 		rvp[1] = rv;
127 		B[0] = b1;
128 		B[1] = b;
129 		exp[1] = exp[0];
130 		exp[0] = e1;
131 		}
132 	else {
133 		rvp[0] = rv;
134 		rvp[1] = rv1;
135 		B[1] = b1;
136 		exp[1] = e1;
137 		}
138 	return rv;
139 	}
140