xref: /netbsd-src/lib/libc/gdtoa/strtoIg.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /* $NetBSD: strtoIg.c,v 1.1.1.1 2006/01/25 15:18:50 kleink Exp $ */
2 
3 /****************************************************************
4 
5 The author of this software is David M. Gay.
6 
7 Copyright (C) 1998 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 
34 #include "gdtoaimp.h"
35 
36  int
37 #ifdef KR_headers
38 strtoIg(s00, se, fpi, exp, B, rvp) CONST char *s00; char **se; FPI *fpi; Long *exp; Bigint **B; int *rvp;
39 #else
40 strtoIg(CONST char *s00, char **se, FPI *fpi, Long *exp, Bigint **B, int *rvp)
41 #endif
42 {
43 	Bigint *b, *b1;
44 	int i, nb, nw, nw1, rv, rv1, swap;
45 	unsigned int nb1, nb11;
46 	Long e1;
47 
48 	b = *B;
49 	rv = strtodg(s00, se, fpi, exp, b->x);
50 	if (!(rv & STRTOG_Inexact)) {
51 		B[1] = 0;
52 		return *rvp = rv;
53 		}
54 	e1 = exp[0];
55 	rv1 = rv ^ STRTOG_Inexact;
56 	b1 = Balloc(b->k);
57 	Bcopy(b1, b);
58 	nb = fpi->nbits;
59 	nb1 = nb & 31;
60 	nb11 = (nb1 - 1) & 31;
61 	nw = b->wds;
62 	nw1 = nw - 1;
63 	if (rv & STRTOG_Inexlo) {
64 		swap = 0;
65 		b1 = increment(b1);
66 		if (fpi->sudden_underflow
67 		 && (rv & STRTOG_Retmask) == STRTOG_Zero) {
68 			b1->x[0] = 0;
69 			b1->x[nw1] = 1L << nb11;
70 			rv1 += STRTOG_Normal - STRTOG_Zero;
71 			rv1 &= ~STRTOG_Underflow;
72 			goto swapcheck;
73 			}
74 		if (b1->wds > nw
75 		 || nb1 && b1->x[nw1] & 1L << nb1) {
76 			if (++e1 > fpi->emax)
77 				rv1 = STRTOG_Infinite | STRTOG_Inexhi;
78 			rshift(b1, 1);
79 			}
80 		else if ((rv & STRTOG_Retmask) == STRTOG_Denormal) {
81 			if (b1->x[nw1] & 1L << nb11) {
82 				rv1 += STRTOG_Normal - STRTOG_Denormal;
83 				rv1 &= ~STRTOG_Underflow;
84 				}
85 			}
86 		}
87 	else {
88 		swap = STRTOG_Neg;
89 		if ((rv & STRTOG_Retmask) == STRTOG_Infinite) {
90 			b1 = set_ones(b1, nb);
91 			e1 = fpi->emax;
92 			rv1 = STRTOG_Normal | STRTOG_Inexlo;
93 			goto swapcheck;
94 			}
95 		decrement(b1);
96 		if ((rv & STRTOG_Retmask) == STRTOG_Denormal) {
97 			for(i = nw1; !b1->x[i]; --i)
98 				if (!i) {
99 					rv1 = STRTOG_Zero | STRTOG_Inexlo;
100 					break;
101 					}
102 			goto swapcheck;
103 			}
104 		if (!(b1->x[nw1] & 1L << nb11)) {
105 			if (e1 == fpi->emin) {
106 				if (fpi->sudden_underflow)
107 					rv1 += STRTOG_Zero - STRTOG_Normal;
108 				else
109 					rv1 += STRTOG_Denormal - STRTOG_Normal;
110 				rv1 |= STRTOG_Underflow;
111 				}
112 			else {
113 				b1 = lshift(b1, 1);
114 				b1->x[0] |= 1;
115 				--e1;
116 				}
117 			}
118 		}
119  swapcheck:
120 	if (swap ^ (rv & STRTOG_Neg)) {
121 		rvp[0] = rv1;
122 		rvp[1] = rv;
123 		B[0] = b1;
124 		B[1] = b;
125 		exp[1] = exp[0];
126 		exp[0] = e1;
127 		}
128 	else {
129 		rvp[0] = rv;
130 		rvp[1] = rv1;
131 		B[1] = b1;
132 		exp[1] = e1;
133 		}
134 	return rv;
135 	}
136