Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / native / jni / java-lang / java_lang_VMDouble.c
blob076f42b86a384095b8442bec56abdeece7ce226c
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)
10 any later version.
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
20 02110-1301 USA.
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
25 combination.
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. */
40 #include <config.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include "mprec.h"
46 #include "fdlibm.h"
47 #include "jcl.h"
49 #include "java_lang_VMDouble.h"
51 static jclass clsDouble;
52 static jmethodID isNaNID;
53 static jdouble NEGATIVE_INFINITY;
54 static jdouble POSITIVE_INFINITY;
55 static jdouble NaN;
58 * Class: java_lang_VMDouble
59 * Method: initIDs
60 * Signature: ()V
62 JNIEXPORT void JNICALL
63 Java_java_lang_VMDouble_initIDs (JNIEnv * env, jclass cls __attribute__ ((__unused__)))
65 jfieldID negInfID;
66 jfieldID posInfID;
67 jfieldID nanID;
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");
80 if (isNaNID == NULL)
82 DBG ("unable to determine method id of isNaN\n") return;
84 negInfID = (*env)->GetStaticFieldID (env, clsDouble, "NEGATIVE_INFINITY", "D");
85 if (negInfID == NULL)
87 DBG ("unable to determine field id of NEGATIVE_INFINITY\n") return;
89 posInfID = (*env)->GetStaticFieldID (env, clsDouble, "POSITIVE_INFINITY", "D");
90 if (posInfID == NULL)
92 DBG ("unable to determine field id of POSITIVE_INFINITY\n") return;
94 nanID = (*env)->GetStaticFieldID (env, clsDouble, "NaN", "D");
95 if (posInfID == NULL)
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);
103 #ifdef DEBUG
104 fprintf (stderr, "java.lang.Double.initIDs() POSITIVE_INFINITY = %g\n",
105 POSITIVE_INFINITY);
106 fprintf (stderr, "java.lang.Double.initIDs() NEGATIVE_INFINITY = %g\n",
107 NEGATIVE_INFINITY);
108 fprintf (stderr, "java.lang.Double.initIDs() NaN = %g\n", NaN);
109 #endif
113 * Class: java_lang_VMDouble
114 * Method: doubleToLongBits
115 * Signature: (D)J
117 JNIEXPORT jlong JNICALL
118 Java_java_lang_VMDouble_doubleToLongBits
119 (JNIEnv * env __attribute__ ((__unused__)),
120 jclass cls __attribute__ ((__unused__)), jdouble doubleValue)
122 jvalue val;
123 jlong e, f;
124 val.d = doubleValue;
126 e = val.j & 0x7ff0000000000000LL;
127 f = val.j & 0x000fffffffffffffLL;
129 if (e == 0x7ff0000000000000LL && f != 0L)
130 val.j = 0x7ff8000000000000LL;
132 return val.j;
136 * Class: java_lang_VMDouble
137 * Method: doubleToRawLongBits
138 * Signature: (D)J
140 JNIEXPORT jlong JNICALL
141 Java_java_lang_VMDouble_doubleToRawLongBits
142 (JNIEnv * env __attribute__ ((__unused__)),
143 jclass cls __attribute__ ((__unused__)), jdouble doubleValue)
145 jvalue val;
146 val.d = doubleValue;
147 return val.j;
151 * Class: java_lang_VMDouble
152 * Method: longBitsToDouble
153 * Signature: (J)D
155 JNIEXPORT jdouble JNICALL
156 Java_java_lang_VMDouble_longBitsToDouble
157 (JNIEnv * env __attribute__ ((__unused__)),
158 jclass cls __attribute__ ((__unused__)), jlong longValue)
160 jvalue val;
161 val.j = longValue;
162 return val.d;
166 * Class: java_lang_VMDouble
167 * Method: toString
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];
175 int decpt, sign;
176 char *s, *d;
177 int i;
179 #ifdef DEBUG
180 fprintf (stderr, "java.lang.VMDouble.toString (%g)\n", value);
181 #endif
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);
196 s = buffer;
197 d = result;
199 if (sign)
200 *d++ = '-';
202 if ((value >= 1e-3 && value < 1e7) || (value == 0))
204 if (decpt <= 0)
205 *d++ = '0';
206 else
208 for (i = 0; i < decpt; i++)
209 if (*s)
210 *d++ = *s++;
211 else
212 *d++ = '0';
215 *d++ = '.';
217 if (*s == 0)
219 *d++ = '0';
220 decpt++;
223 while (decpt++ < 0)
224 *d++ = '0';
226 while (*s)
227 *d++ = *s++;
229 *d = 0;
231 return (*env)->NewStringUTF (env, result);
234 *d++ = *s++;
235 decpt--;
236 *d++ = '.';
238 if (*s == 0)
239 *d++ = '0';
241 while (*s)
242 *d++ = *s++;
244 *d++ = 'E';
246 if (decpt < 0)
248 *d++ = '-';
249 decpt = -decpt;
253 char exp[4];
254 char *e = exp + sizeof exp;
256 *--e = 0;
259 *--e = '0' + decpt % 10;
260 decpt /= 10;
262 while (decpt > 0);
264 while (*e)
265 *d++ = *e++;
268 *d = 0;
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)
282 jboolean isCopy;
283 const char *buf;
284 char *endptr;
285 jdouble val = 0.0;
287 if (str == NULL)
289 JCL_ThrowException (env, "java/lang/NullPointerException", "null");
290 return val;
293 buf = (char *) (*env)->GetStringUTFChars (env, str, &isCopy);
294 if (buf == NULL)
296 /* OutOfMemoryError already thrown */
298 else
300 const char *p = buf, *end, *last_non_ws, *temp;
301 int ok = 1;
303 #ifdef DEBUG
304 fprintf (stderr, "java.lang.VMDouble.parseDouble (%s)\n", buf);
305 #endif
307 /* Trim the buffer, similar to String.trim(). First the leading
308 characters. */
309 while (*p && *p <= ' ')
310 ++p;
312 /* Find the last non-whitespace character. This method is safe
313 even with multi-byte UTF-8 characters. */
314 end = p;
315 last_non_ws = NULL;
316 while (*end)
318 if (*end > ' ')
319 last_non_ws = end;
320 ++end;
323 if (last_non_ws == NULL)
324 last_non_ws = p + strlen (p);
325 else
327 /* Skip past the last non-whitespace character. */
328 ++last_non_ws;
331 /* Check for infinity and NaN */
332 temp = p;
333 if (temp[0] == '+' || temp[0] == '-')
334 temp++;
335 if (strncmp ("Infinity", temp, (size_t) 8) == 0)
337 if (p[0] == '-')
338 return NEGATIVE_INFINITY;
339 return POSITIVE_INFINITY;
341 if (strncmp ("NaN", temp, (size_t) 3) == 0)
342 return NaN;
344 /* Skip a trailing `f' or `d'. */
345 if (last_non_ws > p
346 && (last_non_ws[-1] == 'f'
347 || last_non_ws[-1] == 'F'
348 || last_non_ws[-1] == 'd' || last_non_ws[-1] == 'D'))
349 --last_non_ws;
351 if (last_non_ws > p)
353 struct _Jv_reent reent;
354 memset (&reent, 0, sizeof reent);
356 val = _strtod_r (&reent, p, &endptr);
358 #ifdef DEBUG
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);
362 #endif
363 if (endptr != last_non_ws)
364 ok = 0;
366 else
367 ok = 0;
369 if (!ok)
371 val = 0.0;
372 JCL_ThrowException (env,
373 "java/lang/NumberFormatException",
374 "unable to parse double");
377 (*env)->ReleaseStringUTFChars (env, str, buf);
380 return val;