1 // natVMDouble.cc - Implementation of java.lang.VMDouble native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007
4 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
17 #include <java/lang/String.h>
18 #include <java/lang/Double.h>
19 #include <java/lang/VMDouble.h>
20 #include <java/lang/Character.h>
21 #include <java/lang/NumberFormatException.h>
36 java::lang::VMDouble::doubleToLongBits(jdouble value
)
41 jlong e
= u
.l
& 0x7ff0000000000000LL
;
42 jlong f
= u
.l
& 0x000fffffffffffffLL
;
44 if (e
== 0x7ff0000000000000LL
&& f
!= 0L)
45 u
.l
= 0x7ff8000000000000LL
;
51 java::lang::VMDouble::doubleToRawLongBits(jdouble value
)
59 java::lang::VMDouble::longBitsToDouble(jlong bits
)
67 java::lang::VMDouble::toString(jdouble value
, jboolean isFloat
)
69 if (Double::isNaN (value
))
70 return JvNewStringLatin1 ("NaN", sizeof ("NaN") - 1);
72 if (value
== Double::POSITIVE_INFINITY
)
73 return JvNewStringLatin1 ("Infinity", sizeof ("Infinity") - 1);
75 if (value
== Double::NEGATIVE_INFINITY
)
76 return JvNewStringLatin1 ("-Infinity", sizeof ("-Infinity") - 1);
78 char buffer
[50], result
[50];
81 _dtoa (value
, 0, 20, &decpt
, &sign
, NULL
, buffer
, (int)isFloat
);
91 if ((value
>= 1e-3 && value
< 1e7
) || value
== 0)
97 for (int i
= 0; i
< decpt
; i
++)
120 return JvNewStringLatin1 (result
, strlen (result
));
143 char *e
= exp
+ sizeof exp
;
148 *--e
= '0' + decpt
% 10;
159 return JvNewStringLatin1 (result
, strlen (result
));
163 java::lang::VMDouble::parseDouble(jstring str
)
165 int length
= str
->length();
168 && Character::isWhitespace(str
->charAt(length
- 1)))
171 // The String could end with a f/F/d/D which is valid but we don't need.
172 bool saw_trailer
= false;
175 jchar last
= str
->charAt(length
-1);
176 if (last
== 'f' || last
== 'F' || last
== 'd' || last
== 'D')
185 && Character::isWhitespace(str
->charAt(start
)))
190 // Note that UTF can expand 3x.
191 char *data
= (char *) __builtin_alloca (3 * length
+ 1);
192 jsize blength
= _Jv_GetStringUTFRegion (str
, start
, length
, data
);
197 if (! strcmp (data
, "NaN") || ! strcmp (data
, "+NaN")
198 || ! strcmp (data
, "-NaN"))
200 else if (! strcmp (data
, "Infinity") || ! strcmp (data
, "+Infinity"))
201 return Double::POSITIVE_INFINITY
;
202 else if (! strcmp (data
, "-Infinity"))
203 return Double::NEGATIVE_INFINITY
;
206 struct _Jv_reent reent
;
207 memset (&reent
, 0, sizeof reent
);
210 double val
= _strtod_r (&reent
, data
, &endptr
);
211 if (endptr
== data
+ blength
)
214 throw new NumberFormatException(str
);