2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / lang / natDouble.cc
blobdfec5967a9b6aac443b90d05947a8e245fd33d8e
1 // natDouble.cc - Implementation of java.lang.Double native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2003 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 if (length > 0)
172 jchar last = str->charAt(length-1);
173 if (last == 'f' || last == 'F' || last == 'd' || last == 'D')
174 length--;
177 jsize start = 0;
178 while (length > 0
179 && Character::isWhitespace(str->charAt(start)))
180 start++, length--;
182 if (length > 0)
184 // Note that UTF can expand 3x.
185 char *data = (char *) __builtin_alloca (3 * length + 1);
186 jsize blength = _Jv_GetStringUTFRegion (str, start, length, data);
187 data[blength] = 0;
189 struct _Jv_reent reent;
190 memset (&reent, 0, sizeof reent);
192 char *endptr;
193 double val = _strtod_r (&reent, data, &endptr);
194 if (endptr == data + blength)
195 return val;
197 throw new NumberFormatException(str);
200 void
201 java::lang::Double::initIDs()
203 // Not used in libgcj