summaryrefslogtreecommitdiff
path: root/libjava/java/lang/natVMDouble.cc
blob: f770bc422f8188a1f2cbd7609e64cce04630395c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// natVMDouble.cc - Implementation of java.lang.VMDouble native methods.

/* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007
   Free Software Foundation

   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.  */

#include <config.h>

#include <stdlib.h>

#include <gcj/cni.h>
#include <java/lang/String.h>
#include <java/lang/Double.h>
#include <java/lang/VMDouble.h>
#include <java/lang/Character.h>
#include <java/lang/NumberFormatException.h>
#include <jvm.h>

#include <stdio.h>
#include <string.h>

#include "fdlibm.h"

union u
{
  jlong l;
  jdouble d;
};

jlong 
java::lang::VMDouble::doubleToLongBits(jdouble value)
{
  union u u;
  u.d = value;
  
  jlong e = u.l & 0x7ff0000000000000LL;
  jlong f = u.l & 0x000fffffffffffffLL;

  if (e == 0x7ff0000000000000LL && f != 0L)
    u.l = 0x7ff8000000000000LL;

  return u.l;
}

jlong 
java::lang::VMDouble::doubleToRawLongBits(jdouble value)
{
  union u u;
  u.d = value;
  return u.l;
}

jdouble 
java::lang::VMDouble::longBitsToDouble(jlong bits)
{
  union u u;
  u.l = bits;
  return u.d;
}

jstring 
java::lang::VMDouble::toString(jdouble value, jboolean isFloat)
{
  if (Double::isNaN (value))
    return JvNewStringLatin1 ("NaN", sizeof ("NaN") - 1);
    
  if (value == Double::POSITIVE_INFINITY)
    return JvNewStringLatin1 ("Infinity", sizeof ("Infinity") - 1);
    
  if (value == Double::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 
java::lang::VMDouble::parseDouble(jstring str)
{
  int length = str->length();

  while (length > 0
	 && Character::isWhitespace(str->charAt(length - 1)))
    length--;

  // The String could end with a f/F/d/D which is valid but we don't need.
  bool saw_trailer = false;
  if (length > 0)
    {
      jchar last = str->charAt(length-1);
      if (last == 'f' || last == 'F' || last == 'd' || last == 'D')
	{
	  length--;
	  saw_trailer = true;
	}
    }

  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; 

      if (! saw_trailer)
	{
	  if (! strcmp (data, "NaN") || ! strcmp (data, "+NaN")
	      || ! strcmp (data, "-NaN"))
	    return Double::NaN;
	  else if (! strcmp (data, "Infinity") || ! strcmp (data, "+Infinity"))
	    return Double::POSITIVE_INFINITY;
	  else if (! strcmp (data, "-Infinity"))
	    return Double::NEGATIVE_INFINITY;
	}

      struct _Jv_reent reent;  
      memset (&reent, 0, sizeof reent);

      char *endptr;
      double val = _strtod_r (&reent, data, &endptr);
      if (endptr == data + blength)
	return val;
    }
  throw new NumberFormatException(str);
}