strtoIg.c revision 1.2
1/* $NetBSD: strtoIg.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */
2
3/****************************************************************
4
5The author of this software is David M. Gay.
6
7Copyright (C) 1998 by Lucent Technologies
8All Rights Reserved
9
10Permission to use, copy, modify, and distribute this software and
11its documentation for any purpose and without fee is hereby
12granted, provided that the above copyright notice appear in all
13copies and that both that the copyright notice and this
14permission notice and warranty disclaimer appear in supporting
15documentation, and that the name of Lucent or any of its entities
16not be used in advertising or publicity pertaining to
17distribution of the software without specific, written prior
18permission.
19
20LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27THIS 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
38strtoIg(s00, se, fpi, exp, B, rvp) CONST char *s00; char **se; FPI *fpi; Long *exp; Bigint **B; int *rvp;
39#else
40strtoIg(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_NoMemory)
51		return rv;
52	if (!(rv & STRTOG_Inexact)) {
53		B[1] = 0;
54		return *rvp = rv;
55		}
56	e1 = exp[0];
57	rv1 = rv ^ STRTOG_Inexact;
58	b1 = Balloc(b->k);
59	if (b1 == NULL)
60		return STRTOG_NoMemory;
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