VMFloat.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* VMFloat.java -- VM Specific Float methods
  2. Copyright (C) 2003, 2006 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. import gnu.classpath.Configuration;
  33. /*
  34. * This class is a reference version, mainly for compiling a class library
  35. * jar. It is likely that VM implementers replace this with their own
  36. * version that can communicate effectively with the VM.
  37. */
  38. /**
  39. * Code relocated from java.lang.Float by
  40. * @author Dave Grove <groved@us.ibm.com>
  41. */
  42. final class VMFloat
  43. {
  44. /**
  45. * Convert the float to the IEEE 754 floating-point "single format" bit
  46. * layout. Bit 31 (the most significant) is the sign bit, bits 30-23
  47. * (masked by 0x7f800000) represent the exponent, and bits 22-0
  48. * (masked by 0x007fffff) are the mantissa. This function collapses all
  49. * versions of NaN to 0x7fc00000. The result of this function can be used
  50. * as the argument to <code>Float.intBitsToFloat(int)</code> to obtain the
  51. * original <code>float</code> value.
  52. *
  53. * @param value the <code>float</code> to convert
  54. * @return the bits of the <code>float</code>
  55. * @see #intBitsToFloat(int)
  56. */
  57. static native int floatToIntBits(float value);
  58. /**
  59. * Convert the float to the IEEE 754 floating-point "single format" bit
  60. * layout. Bit 31 (the most significant) is the sign bit, bits 30-23
  61. * (masked by 0x7f800000) represent the exponent, and bits 22-0
  62. * (masked by 0x007fffff) are the mantissa. This function leaves NaN alone,
  63. * rather than collapsing to a canonical value. The result of this function
  64. * can be used as the argument to <code>Float.intBitsToFloat(int)</code> to
  65. * obtain the original <code>float</code> value.
  66. *
  67. * @param value the <code>float</code> to convert
  68. * @return the bits of the <code>float</code>
  69. * @see #intBitsToFloat(int)
  70. */
  71. static native int floatToRawIntBits(float value);
  72. /**
  73. * Convert the argument in IEEE 754 floating-point "single format" bit
  74. * layout to the corresponding float. Bit 31 (the most significant) is the
  75. * sign bit, bits 30-23 (masked by 0x7f800000) represent the exponent, and
  76. * bits 22-0 (masked by 0x007fffff) are the mantissa. This function leaves
  77. * NaN alone, so that you can recover the bit pattern with
  78. * <code>Float.floatToRawIntBits(float)</code>.
  79. *
  80. * @param bits the bits to convert
  81. * @return the <code>float</code> represented by the bits
  82. * @see #floatToIntBits(float)
  83. * @see #floatToRawIntBits(float)
  84. */
  85. static native float intBitsToFloat(int bits);
  86. /**
  87. * @param f the <code>float</code> to convert
  88. * @return the <code>String</code> representing the <code>float</code>
  89. */
  90. static String toString(float f)
  91. {
  92. return VMDouble.toString(f, true);
  93. }
  94. /**
  95. * @param str the <code>String</code> to convert
  96. * @return the <code>float</code> value of <code>s</code>
  97. * @throws NumberFormatException if <code>str</code> cannot be parsed as a
  98. * <code>float</code>
  99. * @throws NullPointerException if <code>str</code> is null
  100. */
  101. static float parseFloat(String str)
  102. {
  103. // XXX Rounding parseDouble() causes some errors greater than 1 ulp from
  104. // the infinitely precise decimal.
  105. return (float) Double.parseDouble(str);
  106. }
  107. } // class VMFloat