1 // natDouble.cc - Implementation of java.lang.Double native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001 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
16 #include <java/lang/String.h>
17 #include <java/lang/Double.h>
18 #include <java/lang/Character.h>
19 #include <java/lang/NumberFormatException.h>
34 java::lang::Double::doubleToLongBits(jdouble value)
39 jlong e = u.l & 0x7ff0000000000000LL;
40 jlong f = u.l & 0x000fffffffffffffLL;
42 if (e == 0x7ff0000000000000LL && f != 0L)
43 u.l = 0x7ff8000000000000LL;
49 java::lang::Double::doubleToRawLongBits(jdouble value)
57 java::lang::Double::longBitsToDouble(jlong bits)
65 java::lang::Double::toString(jdouble value, jboolean isFloat)
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];
79 _dtoa (value, 0, 20, &decpt, &sign, NULL, buffer, (int)isFloat);
89 if (value >= 1e-3 && value < 1e7 || value == 0)
95 for (int i = 0; i < decpt; i++)
118 return JvNewStringLatin1 (result, strlen (result));
141 char *e = exp + sizeof exp;
146 *--e = '0' + decpt % 10;
157 return JvNewStringLatin1 (result, strlen (result));
161 java::lang::Double::parseDouble(jstring str)
163 int length = str->length();
165 && Character::isWhitespace(str->charAt(length - 1)))
169 && Character::isWhitespace(str->charAt(start)))
174 // Note that UTF can expand 3x.
175 char *data = (char *) __builtin_alloca (3 * length + 1);
176 jsize blength = _Jv_GetStringUTFRegion (str, start, length, data);
179 struct _Jv_reent reent;
180 memset (&reent, 0, sizeof reent);
183 double val = _strtod_r (&reent, data, &endptr);
184 if (endptr == data + blength)
187 throw new NumberFormatException;