svn merge -r108665:108708 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / libjava / java / lang / natDouble.cc
blob72fe5fbe1c64f38f240a17c6f85f087eaa801dfe
1 // natDouble.cc - Implementation of java.lang.Double native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include <stdlib.h>
15 #include <gcj/cni.h>
16 #include <java/lang/String.h>
17 #include <java/lang/Double.h>
18 #include <java/lang/Character.h>
19 #include <java/lang/NumberFormatException.h>
20 #include <jvm.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "fdlibm.h"
27 union u
29 jlong l;
30 jdouble d;
33 jlong
34 java::lang::Double::doubleToLongBits(jdouble value)
36 union u u;
37 u.d = value;
39 jlong e = u.l & 0x7ff0000000000000LL;
40 jlong f = u.l & 0x000fffffffffffffLL;
42 if (e == 0x7ff0000000000000LL && f != 0L)
43 u.l = 0x7ff8000000000000LL;
45 return u.l;
48 jlong
49 java::lang::Double::doubleToRawLongBits(jdouble value)
51 union u u;
52 u.d = value;
53 return u.l;
56 jdouble
57 java::lang::Double::longBitsToDouble(jlong bits)
59 union u u;
60 u.l = bits;
61 return u.d;
64 jstring
65 java::lang::Double::toString(jdouble value, jboolean isFloat)
67 if (isNaN (value))
68 return JvNewStringLatin1 ("NaN", sizeof ("NaN") - 1);
70 if (value == POSITIVE_INFINITY)
71 return JvNewStringLatin1 ("Infinity", sizeof ("Infinity") - 1);
73 if (value == NEGATIVE_INFINITY)
74 return JvNewStringLatin1 ("-Infinity", sizeof ("-Infinity") - 1);
76 char buffer[50], result[50];
77 int decpt, sign;
79 _dtoa (value, 0, 20, &decpt, &sign, NULL, buffer, (int)isFloat);
81 value = fabs (value);
83 char *s = buffer;
84 char *d = result;
86 if (sign)
87 *d++ = '-';
89 if (value >= 1e-3 && value < 1e7 || value == 0)
91 if (decpt <= 0)
92 *d++ = '0';
93 else
95 for (int i = 0; i < decpt; i++)
96 if (*s)
97 *d++ = *s++;
98 else
99 *d++ = '0';
102 *d++ = '.';
104 if (*s == 0)
106 *d++ = '0';
107 decpt++;
110 while (decpt++ < 0)
111 *d++ = '0';
113 while (*s)
114 *d++ = *s++;
116 *d = 0;
118 return JvNewStringLatin1 (result, strlen (result));
121 *d++ = *s++;
122 decpt--;
123 *d++ = '.';
125 if (*s == 0)
126 *d++ = '0';
128 while (*s)
129 *d++ = *s++;
131 *d++ = 'E';
133 if (decpt < 0)
135 *d++ = '-';
136 decpt = -decpt;
140 char exp[4];
141 char *e = exp + sizeof exp;
143 *--e = 0;
146 *--e = '0' + decpt % 10;
147 decpt /= 10;
149 while (decpt > 0);
151 while (*e)
152 *d++ = *e++;
155 *d = 0;
157 return JvNewStringLatin1 (result, strlen (result));
160 jdouble
161 java::lang::Double::parseDouble(jstring str)
163 int length = str->length();
165 while (length > 0
166 && Character::isWhitespace(str->charAt(length - 1)))
167 length--;
169 // The String could end with a f/F/d/D which is valid but we don't need.
170 bool saw_trailer = false;
171 if (length > 0)
173 jchar last = str->charAt(length-1);
174 if (last == 'f' || last == 'F' || last == 'd' || last == 'D')
176 length--;
177 saw_trailer = true;
181 jsize start = 0;
182 while (length > 0
183 && Character::isWhitespace(str->charAt(start)))
184 start++, length--;
186 if (length > 0)
188 // Note that UTF can expand 3x.
189 char *data = (char *) __builtin_alloca (3 * length + 1);
190 jsize blength = _Jv_GetStringUTFRegion (str, start, length, data);
191 data[blength] = 0;
193 if (! saw_trailer)
195 if (! strcmp (data, "NaN") || ! strcmp (data, "+NaN")
196 || ! strcmp (data, "-NaN"))
197 return NaN;
198 else if (! strcmp (data, "Infinity") || ! strcmp (data, "+Infinity"))
199 return POSITIVE_INFINITY;
200 else if (! strcmp (data, "-Infinity"))
201 return NEGATIVE_INFINITY;
204 struct _Jv_reent reent;
205 memset (&reent, 0, sizeof reent);
207 char *endptr;
208 double val = _strtod_r (&reent, data, &endptr);
209 if (endptr == data + blength)
210 return val;
212 throw new NumberFormatException(str);
215 void
216 java::lang::Double::initIDs()
218 // Not used in libgcj