1999-04-07 22:42:40 +08:00
|
|
|
// natDouble.cc - Implementation of java.lang.Double native methods.
|
|
|
|
|
2001-02-10 06:13:33 +08:00
|
|
|
/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
1999-11-20 03:13:42 +08:00
|
|
|
#include <config.h>
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
1999-09-11 06:03:10 +08:00
|
|
|
#include <gcj/cni.h>
|
1999-04-07 22:42:40 +08:00
|
|
|
#include <java/lang/String.h>
|
|
|
|
#include <java/lang/Double.h>
|
2001-03-24 08:59:57 +08:00
|
|
|
#include <java/lang/Character.h>
|
1999-04-07 22:42:40 +08:00
|
|
|
#include <java/lang/NumberFormatException.h>
|
|
|
|
#include <jvm.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-12-04 16:22:34 +08:00
|
|
|
#include "fdlibm.h"
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
union u
|
|
|
|
{
|
|
|
|
jlong l;
|
|
|
|
jdouble d;
|
|
|
|
};
|
|
|
|
|
|
|
|
jlong
|
|
|
|
java::lang::Double::doubleToLongBits(jdouble value)
|
|
|
|
{
|
|
|
|
union u u;
|
|
|
|
u.d = value;
|
1999-04-14 15:10:22 +08:00
|
|
|
|
|
|
|
jlong e = u.l & 0x7ff0000000000000LL;
|
|
|
|
jlong f = u.l & 0x000fffffffffffffLL;
|
|
|
|
|
|
|
|
if (e == 0x7ff0000000000000LL && f != 0L)
|
|
|
|
u.l = 0x7ff8000000000000LL;
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
return u.l;
|
|
|
|
}
|
|
|
|
|
2001-02-10 06:13:33 +08:00
|
|
|
jlong
|
|
|
|
java::lang::Double::doubleToRawLongBits(jdouble value)
|
|
|
|
{
|
|
|
|
union u u;
|
|
|
|
u.d = value;
|
|
|
|
return u.l;
|
|
|
|
}
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
jdouble
|
|
|
|
java::lang::Double::longBitsToDouble(jlong bits)
|
|
|
|
{
|
|
|
|
union u u;
|
|
|
|
u.l = bits;
|
|
|
|
return u.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
jstring
|
|
|
|
java::lang::Double::toString(jdouble value, jboolean isFloat)
|
|
|
|
{
|
|
|
|
if (isNaN (value))
|
|
|
|
return JvNewStringLatin1 ("NaN", sizeof ("NaN") - 1);
|
|
|
|
|
|
|
|
if (value == POSITIVE_INFINITY)
|
|
|
|
return JvNewStringLatin1 ("Infinity", sizeof ("Infinity") - 1);
|
|
|
|
|
|
|
|
if (value == NEGATIVE_INFINITY)
|
|
|
|
return JvNewStringLatin1 ("-Infinity", sizeof ("-Infinity") - 1);
|
|
|
|
|
|
|
|
char buffer[50], result[50];
|
|
|
|
int decpt, sign;
|
|
|
|
|
|
|
|
_dtoa (value, 0, 20, &decpt, &sign, NULL, buffer, (int)isFloat);
|
|
|
|
|
|
|
|
value = fabs (value);
|
|
|
|
|
|
|
|
char *s = buffer;
|
|
|
|
char *d = result;
|
|
|
|
|
|
|
|
if (sign)
|
|
|
|
*d++ = '-';
|
|
|
|
|
|
|
|
if (value >= 1e-3 && value < 1e7 || value == 0)
|
|
|
|
{
|
|
|
|
if (decpt <= 0)
|
|
|
|
*d++ = '0';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < decpt; i++)
|
|
|
|
if (*s)
|
|
|
|
*d++ = *s++;
|
|
|
|
else
|
|
|
|
*d++ = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
*d++ = '.';
|
|
|
|
|
|
|
|
if (*s == 0)
|
|
|
|
{
|
|
|
|
*d++ = '0';
|
|
|
|
decpt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (decpt++ < 0)
|
|
|
|
*d++ = '0';
|
|
|
|
|
|
|
|
while (*s)
|
|
|
|
*d++ = *s++;
|
|
|
|
|
|
|
|
*d = 0;
|
|
|
|
|
|
|
|
return JvNewStringLatin1 (result, strlen (result));
|
|
|
|
}
|
|
|
|
|
|
|
|
*d++ = *s++;
|
|
|
|
decpt--;
|
|
|
|
*d++ = '.';
|
|
|
|
|
|
|
|
if (*s == 0)
|
|
|
|
*d++ = '0';
|
|
|
|
|
|
|
|
while (*s)
|
|
|
|
*d++ = *s++;
|
|
|
|
|
|
|
|
*d++ = 'E';
|
|
|
|
|
|
|
|
if (decpt < 0)
|
|
|
|
{
|
|
|
|
*d++ = '-';
|
|
|
|
decpt = -decpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char exp[4];
|
|
|
|
char *e = exp + sizeof exp;
|
|
|
|
|
|
|
|
*--e = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*--e = '0' + decpt % 10;
|
|
|
|
decpt /= 10;
|
|
|
|
}
|
|
|
|
while (decpt > 0);
|
|
|
|
|
|
|
|
while (*e)
|
|
|
|
*d++ = *e++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = 0;
|
|
|
|
|
|
|
|
return JvNewStringLatin1 (result, strlen (result));
|
|
|
|
}
|
|
|
|
|
|
|
|
jdouble
|
2000-06-25 02:11:01 +08:00
|
|
|
java::lang::Double::parseDouble(jstring str)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
|
|
|
int length = str->length();
|
2001-03-24 08:59:57 +08:00
|
|
|
while (length > 0
|
|
|
|
&& Character::isWhitespace(str->charAt(length - 1)))
|
|
|
|
length--;
|
|
|
|
jsize start = 0;
|
|
|
|
while (length > 0
|
|
|
|
&& Character::isWhitespace(str->charAt(start)))
|
|
|
|
start++, length--;
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
// Note that UTF can expand 3x.
|
|
|
|
char *data = (char *) __builtin_alloca (3 * length + 1);
|
|
|
|
jsize blength = _Jv_GetStringUTFRegion (str, start, length, data);
|
|
|
|
data[blength] = 0;
|
|
|
|
|
|
|
|
struct _Jv_reent reent;
|
|
|
|
memset (&reent, 0, sizeof reent);
|
|
|
|
|
|
|
|
char *endptr;
|
|
|
|
double val = _strtod_r (&reent, data, &endptr);
|
|
|
|
if (endptr == data + blength)
|
|
|
|
return val;
|
|
|
|
}
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new NumberFormatException;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|