1170530Ssam/* $NetBSD: strtorx.c,v 1.3 2008/03/21 23:13:48 christos Exp $ */
2178354Ssam
3170530Ssam/****************************************************************
4170530Ssam
5170530SsamThe author of this software is David M. Gay.
6170530Ssam
7170530SsamCopyright (C) 1998, 2000 by Lucent Technologies
8170530SsamAll Rights Reserved
9170530Ssam
10170530SsamPermission to use, copy, modify, and distribute this software and
11170530Ssamits documentation for any purpose and without fee is hereby
12170530Ssamgranted, provided that the above copyright notice appear in all
13170530Ssamcopies and that both that the copyright notice and this
14170530Ssampermission notice and warranty disclaimer appear in supporting
15170530Ssamdocumentation, and that the name of Lucent or any of its entities
16170530Ssamnot be used in advertising or publicity pertaining to
17170530Ssamdistribution of the software without specific, written prior
18170530Ssampermission.
19170530Ssam
20170530SsamLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21170530SsamINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22170530SsamIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23170530SsamSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24170530SsamWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25170530SsamIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26170530SsamARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27170530SsamTHIS SOFTWARE.
28170530Ssam
29170530Ssam****************************************************************/
30170530Ssam
31170530Ssam/* Please send bug reports to David M. Gay (dmg at acm dot org,
32170530Ssam * with " at " changed at "@" and " dot " changed to ".").	*/
33170530Ssam
34170530Ssam#include "gdtoaimp.h"
35170530Ssam
36170530Ssam#undef _0
37170530Ssam#undef _1
38170530Ssam
39170530Ssam/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */
40170530Ssam
41170530Ssam#ifdef IEEE_BIG_ENDIAN
42170530Ssam#define _0 0
43170530Ssam#define _1 1
44170530Ssam#define _2 2
45170530Ssam#define _3 3
46178354Ssam#define _4 4
47178354Ssam#endif
48170530Ssam#ifdef IEEE_LITTLE_ENDIAN
49170530Ssam#define _0 4
50178354Ssam#define _1 3
51170530Ssam#define _2 2
52170530Ssam#define _3 1
53170530Ssam#define _4 0
54178354Ssam#endif
55170530Ssam
56178354Ssam void
57178354Ssam#ifdef KR_headers
58170530SsamULtox(L, bits, expt, k) UShort *L; ULong *bits; Long expt; int k;
59170530Ssam#else
60178354SsamULtox(UShort *L, ULong *bits, Long expt, int k)
61178354Ssam#endif
62178354Ssam{
63178354Ssam	switch(k & STRTOG_Retmask) {
64170530Ssam	  case STRTOG_NoNumber:
65178354Ssam	  case STRTOG_Zero:
66178354Ssam		L[0] = L[1] = L[2] = L[3] = L[4] = 0;
67178354Ssam		break;
68170530Ssam
69178354Ssam	  case STRTOG_Denormal:
70170530Ssam		L[_0] = 0;
71170530Ssam		goto normal_bits;
72178354Ssam
73178354Ssam	  case STRTOG_Normal:
74178354Ssam	  case STRTOG_NaNbits:
75178354Ssam		L[_0] = expt + 0x3fff + 63;
76170530Ssam normal_bits:
77178354Ssam		L[_4] = (UShort)bits[0];
78178354Ssam		L[_3] = (UShort)(bits[0] >> 16);
79178354Ssam		L[_2] = (UShort)bits[1];
80178354Ssam		L[_1] = (UShort)(bits[1] >> 16);
81170530Ssam		break;
82170530Ssam
83178354Ssam	  case STRTOG_Infinite:
84178354Ssam		L[_0] = 0x7fff;
85170530Ssam		L[_1] = 0x8000;
86178354Ssam		L[_2] = L[_3] = L[_4] = 0;
87178354Ssam		break;
88178354Ssam
89170530Ssam	  case STRTOG_NaN:
90178354Ssam		L[0] = ldus_QNAN0;
91178354Ssam		L[1] = ldus_QNAN1;
92170530Ssam		L[2] = ldus_QNAN2;
93170530Ssam		L[3] = ldus_QNAN3;
94170530Ssam		L[4] = ldus_QNAN4;
95178354Ssam	  }
96178354Ssam	if (k & STRTOG_Neg)
97170530Ssam		L[_0] |= 0x8000;
98178354Ssam	}
99170530Ssam
100170530Ssam int
101170530Ssam#ifdef KR_headers
102178354Ssamstrtorx(s, sp, rounding, L) CONST char *s; char **sp; int rounding; void *L;
103178354Ssam#else
104170530Ssamstrtorx(CONST char *s, char **sp, int rounding, void *L)
105178354Ssam#endif
106178354Ssam{
107170530Ssam	static CONST FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI };
108178354Ssam	CONST FPI *fpi;
109170530Ssam	FPI fpi1;
110170530Ssam	ULong bits[2];
111178354Ssam	Long expt;
112178354Ssam	int k;
113178354Ssam
114178354Ssam	fpi = &fpi0;
115178354Ssam	if (rounding != FPI_Round_near) {
116178354Ssam		fpi1 = fpi0;
117170530Ssam		fpi1.rounding = rounding;
118170530Ssam		fpi = &fpi1;
119178354Ssam		}
120178354Ssam	k = strtodg(s, sp, fpi, &expt, bits);
121170530Ssam	if (k == STRTOG_NoMemory)
122178354Ssam		return k;
123178354Ssam	    ULtox((UShort*)L, bits, expt, k);
124178354Ssam	return k;
125178354Ssam	}
126170530Ssam