1 // natDouble.cc - Implementation of java.lang.VMDouble native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006 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/VMDouble.h>
19 #include <java/lang/Character.h>
20 #include <java/lang/NumberFormatException.h>
35 java::lang::VMDouble::doubleToLongBits(jdouble value
)
40 jlong e
= u
.l
& 0x7ff0000000000000LL
;
41 jlong f
= u
.l
& 0x000fffffffffffffLL
;
43 if (e
== 0x7ff0000000000000LL
&& f
!= 0L)
44 u
.l
= 0x7ff8000000000000LL
;
50 java::lang::VMDouble::doubleToRawLongBits(jdouble value
)
58 java::lang::VMDouble::longBitsToDouble(jlong bits
)
66 java::lang::VMDouble::toString(jdouble value
, jboolean isFloat
)
68 if (Double::isNaN (value
))
69 return JvNewStringLatin1 ("NaN", sizeof ("NaN") - 1);
71 if (value
== Double::POSITIVE_INFINITY
)
72 return JvNewStringLatin1 ("Infinity", sizeof ("Infinity") - 1);
74 if (value
== Double::NEGATIVE_INFINITY
)
75 return JvNewStringLatin1 ("-Infinity", sizeof ("-Infinity") - 1);
77 char buffer
[50], result
[50];
80 _dtoa (value
, 0, 20, &decpt
, &sign
, NULL
, buffer
, (int)isFloat
);
90 if (value
>= 1e-3 && value
< 1e7
|| value
== 0)
96 for (int i
= 0; i
< decpt
; i
++)
119 return JvNewStringLatin1 (result
, strlen (result
));
142 char *e
= exp
+ sizeof exp
;
147 *--e
= '0' + decpt
% 10;
158 return JvNewStringLatin1 (result
, strlen (result
));
162 java::lang::VMDouble::parseDouble(jstring str
)
164 int length
= str
->length();
167 && Character::isWhitespace(str
->charAt(length
- 1)))
170 // The String could end with a f/F/d/D which is valid but we don't need.
171 bool saw_trailer
= false;
174 jchar last
= str
->charAt(length
-1);
175 if (last
== 'f' || last
== 'F' || last
== 'd' || last
== 'D')
184 && Character::isWhitespace(str
->charAt(start
)))
189 // Note that UTF can expand 3x.
190 char *data
= (char *) __builtin_alloca (3 * length
+ 1);
191 jsize blength
= _Jv_GetStringUTFRegion (str
, start
, length
, data
);
196 if (! strcmp (data
, "NaN") || ! strcmp (data
, "+NaN")
197 || ! strcmp (data
, "-NaN"))
199 else if (! strcmp (data
, "Infinity") || ! strcmp (data
, "+Infinity"))
200 return Double::POSITIVE_INFINITY
;
201 else if (! strcmp (data
, "-Infinity"))
202 return Double::NEGATIVE_INFINITY
;
205 struct _Jv_reent reent
;
206 memset (&reent
, 0, sizeof reent
);
209 double val
= _strtod_r (&reent
, data
, &endptr
);
210 if (endptr
== data
+ blength
)
213 throw new NumberFormatException(str
);