natVMDouble.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // natVMDouble.cc - Implementation of java.lang.VMDouble native methods.
  2. /* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007
  3. Free Software Foundation
  4. This file is part of libgcj.
  5. This software is copyrighted work licensed under the terms of the
  6. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  7. details. */
  8. #include <config.h>
  9. #include <stdlib.h>
  10. #include <gcj/cni.h>
  11. #include <java/lang/String.h>
  12. #include <java/lang/Double.h>
  13. #include <java/lang/VMDouble.h>
  14. #include <java/lang/Character.h>
  15. #include <java/lang/NumberFormatException.h>
  16. #include <jvm.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "fdlibm.h"
  20. union u
  21. {
  22. jlong l;
  23. jdouble d;
  24. };
  25. jlong
  26. java::lang::VMDouble::doubleToLongBits(jdouble value)
  27. {
  28. union u u;
  29. u.d = value;
  30. jlong e = u.l & 0x7ff0000000000000LL;
  31. jlong f = u.l & 0x000fffffffffffffLL;
  32. if (e == 0x7ff0000000000000LL && f != 0L)
  33. u.l = 0x7ff8000000000000LL;
  34. return u.l;
  35. }
  36. jlong
  37. java::lang::VMDouble::doubleToRawLongBits(jdouble value)
  38. {
  39. union u u;
  40. u.d = value;
  41. return u.l;
  42. }
  43. jdouble
  44. java::lang::VMDouble::longBitsToDouble(jlong bits)
  45. {
  46. union u u;
  47. u.l = bits;
  48. return u.d;
  49. }
  50. jstring
  51. java::lang::VMDouble::toString(jdouble value, jboolean isFloat)
  52. {
  53. if (Double::isNaN (value))
  54. return JvNewStringLatin1 ("NaN", sizeof ("NaN") - 1);
  55. if (value == Double::POSITIVE_INFINITY)
  56. return JvNewStringLatin1 ("Infinity", sizeof ("Infinity") - 1);
  57. if (value == Double::NEGATIVE_INFINITY)
  58. return JvNewStringLatin1 ("-Infinity", sizeof ("-Infinity") - 1);
  59. char buffer[50], result[50];
  60. int decpt, sign;
  61. _dtoa (value, 0, 20, &decpt, &sign, NULL, buffer, (int)isFloat);
  62. value = fabs (value);
  63. char *s = buffer;
  64. char *d = result;
  65. if (sign)
  66. *d++ = '-';
  67. if ((value >= 1e-3 && value < 1e7) || value == 0)
  68. {
  69. if (decpt <= 0)
  70. *d++ = '0';
  71. else
  72. {
  73. for (int i = 0; i < decpt; i++)
  74. if (*s)
  75. *d++ = *s++;
  76. else
  77. *d++ = '0';
  78. }
  79. *d++ = '.';
  80. if (*s == 0)
  81. {
  82. *d++ = '0';
  83. decpt++;
  84. }
  85. while (decpt++ < 0)
  86. *d++ = '0';
  87. while (*s)
  88. *d++ = *s++;
  89. *d = 0;
  90. return JvNewStringLatin1 (result, strlen (result));
  91. }
  92. *d++ = *s++;
  93. decpt--;
  94. *d++ = '.';
  95. if (*s == 0)
  96. *d++ = '0';
  97. while (*s)
  98. *d++ = *s++;
  99. *d++ = 'E';
  100. if (decpt < 0)
  101. {
  102. *d++ = '-';
  103. decpt = -decpt;
  104. }
  105. {
  106. char exp[4];
  107. char *e = exp + sizeof exp;
  108. *--e = 0;
  109. do
  110. {
  111. *--e = '0' + decpt % 10;
  112. decpt /= 10;
  113. }
  114. while (decpt > 0);
  115. while (*e)
  116. *d++ = *e++;
  117. }
  118. *d = 0;
  119. return JvNewStringLatin1 (result, strlen (result));
  120. }
  121. jdouble
  122. java::lang::VMDouble::parseDouble(jstring str)
  123. {
  124. int length = str->length();
  125. while (length > 0
  126. && Character::isWhitespace(str->charAt(length - 1)))
  127. length--;
  128. // The String could end with a f/F/d/D which is valid but we don't need.
  129. bool saw_trailer = false;
  130. if (length > 0)
  131. {
  132. jchar last = str->charAt(length-1);
  133. if (last == 'f' || last == 'F' || last == 'd' || last == 'D')
  134. {
  135. length--;
  136. saw_trailer = true;
  137. }
  138. }
  139. jsize start = 0;
  140. while (length > 0
  141. && Character::isWhitespace(str->charAt(start)))
  142. start++, length--;
  143. if (length > 0)
  144. {
  145. // Note that UTF can expand 3x.
  146. char *data = (char *) __builtin_alloca (3 * length + 1);
  147. jsize blength = _Jv_GetStringUTFRegion (str, start, length, data);
  148. data[blength] = 0;
  149. if (! saw_trailer)
  150. {
  151. if (! strcmp (data, "NaN") || ! strcmp (data, "+NaN")
  152. || ! strcmp (data, "-NaN"))
  153. return Double::NaN;
  154. else if (! strcmp (data, "Infinity") || ! strcmp (data, "+Infinity"))
  155. return Double::POSITIVE_INFINITY;
  156. else if (! strcmp (data, "-Infinity"))
  157. return Double::NEGATIVE_INFINITY;
  158. }
  159. struct _Jv_reent reent;
  160. memset (&reent, 0, sizeof reent);
  161. char *endptr;
  162. double val = _strtod_r (&reent, data, &endptr);
  163. if (endptr == data + blength)
  164. return val;
  165. }
  166. throw new NumberFormatException(str);
  167. }