PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libjava/java/lang/natVMDouble.cc

https://bitbucket.org/lpimports/gcc
C++ | 215 lines | 158 code | 46 blank | 11 comment | 52 complexity | 2187d73a9f804645a7e98a9b252b6795 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-3.0, BSD-3-Clause, LGPL-2.0, GPL-2.0
  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. }