1 /* VMDouble.c - java.lang.VMDouble native functions
2 Copyright (C) 1998, 1999, 2001, 2003, 2004i, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
49 #include "java_lang_VMDouble.h"
51 static jclass clsDouble
;
52 static jmethodID isNaNID
;
53 static jdouble NEGATIVE_INFINITY
;
54 static jdouble POSITIVE_INFINITY
;
58 * Class: java_lang_VMDouble
62 JNIEXPORT
void JNICALL
63 Java_java_lang_VMDouble_initIDs (JNIEnv
* env
, jclass cls
__attribute__ ((__unused__
)))
69 clsDouble
= (*env
)->FindClass (env
, "java/lang/Double");
70 if (clsDouble
== NULL
)
72 DBG ("unable to get class java.lang.Double\n") return;
74 clsDouble
= (*env
)->NewGlobalRef(env
, clsDouble
);
75 if (clsDouble
== NULL
)
77 DBG ("unable to register class java.lang.Double as global ref\n") return;
79 isNaNID
= (*env
)->GetStaticMethodID (env
, clsDouble
, "isNaN", "(D)Z");
82 DBG ("unable to determine method id of isNaN\n") return;
84 negInfID
= (*env
)->GetStaticFieldID (env
, clsDouble
, "NEGATIVE_INFINITY", "D");
87 DBG ("unable to determine field id of NEGATIVE_INFINITY\n") return;
89 posInfID
= (*env
)->GetStaticFieldID (env
, clsDouble
, "POSITIVE_INFINITY", "D");
92 DBG ("unable to determine field id of POSITIVE_INFINITY\n") return;
94 nanID
= (*env
)->GetStaticFieldID (env
, clsDouble
, "NaN", "D");
97 DBG ("unable to determine field id of NaN\n") return;
99 POSITIVE_INFINITY
= (*env
)->GetStaticDoubleField (env
, clsDouble
, posInfID
);
100 NEGATIVE_INFINITY
= (*env
)->GetStaticDoubleField (env
, clsDouble
, negInfID
);
101 NaN
= (*env
)->GetStaticDoubleField (env
, clsDouble
, nanID
);
104 fprintf (stderr
, "java.lang.Double.initIDs() POSITIVE_INFINITY = %g\n",
106 fprintf (stderr
, "java.lang.Double.initIDs() NEGATIVE_INFINITY = %g\n",
108 fprintf (stderr
, "java.lang.Double.initIDs() NaN = %g\n", NaN
);
113 * Class: java_lang_VMDouble
114 * Method: doubleToLongBits
117 JNIEXPORT jlong JNICALL
118 Java_java_lang_VMDouble_doubleToLongBits
119 (JNIEnv
* env
__attribute__ ((__unused__
)),
120 jclass cls
__attribute__ ((__unused__
)), jdouble doubleValue
)
126 e
= val
.j
& 0x7ff0000000000000LL
;
127 f
= val
.j
& 0x000fffffffffffffLL
;
129 if (e
== 0x7ff0000000000000LL
&& f
!= 0L)
130 val
.j
= 0x7ff8000000000000LL
;
136 * Class: java_lang_VMDouble
137 * Method: doubleToRawLongBits
140 JNIEXPORT jlong JNICALL
141 Java_java_lang_VMDouble_doubleToRawLongBits
142 (JNIEnv
* env
__attribute__ ((__unused__
)),
143 jclass cls
__attribute__ ((__unused__
)), jdouble doubleValue
)
151 * Class: java_lang_VMDouble
152 * Method: longBitsToDouble
155 JNIEXPORT jdouble JNICALL
156 Java_java_lang_VMDouble_longBitsToDouble
157 (JNIEnv
* env
__attribute__ ((__unused__
)),
158 jclass cls
__attribute__ ((__unused__
)), jlong longValue
)
166 * Class: java_lang_VMDouble
168 * Signature: (DZ)Ljava/lang/String;
170 JNIEXPORT jstring JNICALL
171 Java_java_lang_VMDouble_toString
172 (JNIEnv
* env
, jclass cls
__attribute__ ((__unused__
)), jdouble value
, jboolean isFloat
)
174 char buffer
[50], result
[50];
180 fprintf (stderr
, "java.lang.VMDouble.toString (%g)\n", value
);
183 if ((*env
)->CallStaticBooleanMethod (env
, clsDouble
, isNaNID
, value
))
184 return (*env
)->NewStringUTF (env
, "NaN");
186 if (value
== POSITIVE_INFINITY
)
187 return (*env
)->NewStringUTF (env
, "Infinity");
189 if (value
== NEGATIVE_INFINITY
)
190 return (*env
)->NewStringUTF (env
, "-Infinity");
192 _dtoa (value
, 0, 20, &decpt
, &sign
, NULL
, buffer
, (int) isFloat
);
194 value
= fabs (value
);
202 if ((value
>= 1e-3 && value
< 1e7
) || (value
== 0))
208 for (i
= 0; i
< decpt
; i
++)
231 return (*env
)->NewStringUTF (env
, result
);
254 char *e
= exp
+ sizeof exp
;
259 *--e
= '0' + decpt
% 10;
270 return (*env
)->NewStringUTF (env
, result
);
274 * Class: java_lang_VMDouble
275 * Method: parseDouble
276 * Signature: (Ljava/lang/String;)D
278 JNIEXPORT jdouble JNICALL
279 Java_java_lang_VMDouble_parseDouble
280 (JNIEnv
* env
, jclass cls
__attribute__ ((__unused__
)), jstring str
)
289 JCL_ThrowException (env
, "java/lang/NullPointerException", "null");
293 buf
= (char *) (*env
)->GetStringUTFChars (env
, str
, &isCopy
);
296 /* OutOfMemoryError already thrown */
300 const char *p
= buf
, *end
, *last_non_ws
, *temp
;
304 fprintf (stderr
, "java.lang.VMDouble.parseDouble (%s)\n", buf
);
307 /* Trim the buffer, similar to String.trim(). First the leading
309 while (*p
&& *p
<= ' ')
312 /* Find the last non-whitespace character. This method is safe
313 even with multi-byte UTF-8 characters. */
323 if (last_non_ws
== NULL
)
324 last_non_ws
= p
+ strlen (p
);
327 /* Skip past the last non-whitespace character. */
331 /* Check for infinity and NaN */
333 if (temp
[0] == '+' || temp
[0] == '-')
335 if (strncmp ("Infinity", temp
, (size_t) 8) == 0)
338 return NEGATIVE_INFINITY
;
339 return POSITIVE_INFINITY
;
341 if (strncmp ("NaN", temp
, (size_t) 3) == 0)
344 /* Skip a trailing `f' or `d'. */
346 && (last_non_ws
[-1] == 'f'
347 || last_non_ws
[-1] == 'F'
348 || last_non_ws
[-1] == 'd' || last_non_ws
[-1] == 'D'))
353 struct _Jv_reent reent
;
354 memset (&reent
, 0, sizeof reent
);
356 val
= _strtod_r (&reent
, p
, &endptr
);
359 fprintf (stderr
, "java.lang.VMDouble.parseDouble val = %g\n", val
);
360 fprintf (stderr
, "java.lang.VMDouble.parseDouble %i != %i ???\n",
361 endptr
, last_non_ws
);
363 if (endptr
!= last_non_ws
)
372 JCL_ThrowException (env
,
373 "java/lang/NumberFormatException",
374 "unable to parse double");
377 (*env
)->ReleaseStringUTFChars (env
, str
, buf
);