PageRenderTime 187ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/gcc-4.2.1/libjava/java/lang/natDouble.cc

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