[RFC/PATCH] Add missing Java 1.5 java/lang/StrictMath methods

View: New views
3 Messages — Rating Filter:   Alert me  

[RFC/PATCH] Add missing Java 1.5 java/lang/StrictMath methods

by Pekka Enberg-4 :: Rate this Message:

| View Threaded | Show Only this Message

Signed-off-by: Pekka Enberg <penberg@...>
---
 java/lang/StrictMath.java |   72 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/java/lang/StrictMath.java b/java/lang/StrictMath.java
index 88f5e57..225aaa7 100644
--- a/java/lang/StrictMath.java
+++ b/java/lang/StrictMath.java
@@ -1317,6 +1317,78 @@ public final strictfp class StrictMath
   }
 
   /**
+   * <p>
+   * Returns the hypotenuse, <code>a<sup>2</sup> + b<sup>2</sup></code>,
+   * without intermediate overflow or underflow.  The returned result is
+   * within 1 ulp of the exact result.  If one parameter is held constant,
+   * then the result in the other parameter is semi-monotonic.
+   * </p>
+   * <p>
+   * If either of the arguments is an infinity, then the returned result
+   * is positive infinity.  Otherwise, if either argument is <code>NaN</code>,
+   * then <code>NaN</code> is returned.
+   * </p>
+   *
+   * @param a the first parameter.
+   * @param b the second parameter.
+   * @return the hypotenuse matching the supplied parameters.
+   * @since 1.5
+   */
+  public static double hypot(double a, double b)
+  {
+    return VMMath.hypot(a,b);
+  }
+
+  /**
+   * <p>
+   * Returns the base 10 logarithm of the supplied value.  The returned
+   * result is within 1 ulp of the exact result, and the results are
+   * semi-monotonic.
+   * </p>
+   * <p>
+   * Arguments of either <code>NaN</code> or less than zero return
+   * <code>NaN</code>.  An argument of positive infinity returns positive
+   * infinity.  Negative infinity is returned if either positive or negative
+   * zero is supplied.  Where the argument is the result of
+   * <code>10<sup>n</sup</code>, then <code>n</code> is returned.
+   * </p>
+   *
+   * @param a the numeric argument.
+   * @return the base 10 logarithm of <code>a</code>.
+   * @since 1.5
+   */
+  public static double log10(double a)
+  {
+    return VMMath.log10(a);
+  }
+
+  /**
+   * <p>
+   * Returns the natural logarithm resulting from the sum of the argument,
+   * <code>a</code> and 1.  For values close to 0, the
+   * result of <code>log1p(a)</code> tend to be much closer to the
+   * exact result than simply <code>log(1.0+a)</code>.  The returned
+   * result is within 1 ulp of the exact result, and the results are
+   * semi-monotonic.
+   * </p>
+   * <p>
+   * Arguments of either <code>NaN</code> or less than -1 return
+   * <code>NaN</code>.  An argument of positive infinity or zero
+   * returns the original argument.  Negative infinity is returned from an
+   * argument of -1.
+   * </p>
+   *
+   * @param a the numeric argument.
+   * @return the natural logarithm of <code>a</code> + 1.
+   * @since 1.5
+   */
+  public static double log1p(double a)
+  {
+    return VMMath.log1p(a);
+  }
+
+
+  /**
    * Take ln(a) (the natural log).  The opposite of <code>exp()</code>. If the
    * argument is NaN or negative, the result is NaN; if the argument is
    * positive infinity, the result is positive infinity; and if the argument
--
1.7.4.1



[RFC/PATCH v2] Add missing Java 1.6 java/lang/Math.getExponent() methods

by Pekka Enberg-4 :: Rate this Message:

| View Threaded | Show Only this Message

While at it, also add missing Java 1.6 java/lang package constants.

Signed-off-by: Pekka Enberg <penberg@...>
---
 java/lang/Double.java                     |   17 ++++++++++-
 java/lang/Float.java                      |   15 +++++++++
 java/lang/Math.java                       |   26 ++++++++++++++++
 java/lang/StrictMath.java                 |   18 +++++++++++
 native/fdlibm/Makefile.am                 |    1 +
 native/fdlibm/s_ilogb.c                   |   47 +++++++++++++++++++++++++++++
 native/jni/java-lang/java_lang_VMDouble.c |    8 +++++
 native/jni/java-lang/java_lang_VMFloat.c  |   10 ++++++
 vm/reference/java/lang/VMDouble.java      |    2 +
 vm/reference/java/lang/VMFloat.java       |    2 +
 10 files changed, 145 insertions(+), 1 deletions(-)
 create mode 100644 native/fdlibm/s_ilogb.c

diff --git a/java/lang/Double.java b/java/lang/Double.java
index 3ae1b01..680d2a2 100644
--- a/java/lang/Double.java
+++ b/java/lang/Double.java
@@ -96,7 +96,22 @@ public final class Double extends Number implements Comparable<Double>
    */
   public static final int SIZE = 64;
 
- /**
+  /**
+   * @since 1.6
+   */
+  public static final double MIN_NORMAL = 0x1.0p-1022;
+
+  /**
+   * @since 1.6
+   */
+  public static final int MAX_EXPONENT = 1023;
+
+  /**
+   * @since 1.6
+   */
+  public static final int MIN_EXPONENT = -1022;
+
+  /**
    * The primitive type <code>double</code> is represented by this
    * <code>Class</code> object.
    * @since 1.1
diff --git a/java/lang/Float.java b/java/lang/Float.java
index a4a766e..d0e9f1a 100644
--- a/java/lang/Float.java
+++ b/java/lang/Float.java
@@ -104,6 +104,21 @@ public final class Float extends Number implements Comparable<Float>
   public static final int SIZE = 32;
 
   /**
+   * @since 1.6
+   */
+  public static final float MIN_NORMAL = 0x1.0p-126f;
+
+  /**
+   * @since 1.6
+   */
+  public static final int MAX_EXPONENT = 127;
+
+  /**
+   * @since 1.6
+   */
+  public static final int MIN_EXPONENT = -126;
+
+  /**
    * Cache representation of 0
    */
   private static final Float ZERO = new Float(0.0f);
diff --git a/java/lang/Math.java b/java/lang/Math.java
index 6cf29b4..8dc911c 100644
--- a/java/lang/Math.java
+++ b/java/lang/Math.java
@@ -1049,4 +1049,30 @@ public final class Math
       }
     return Float.intBitsToFloat((newExponent << mantissaBits) | newMantissa);
   }
+
+  /**
+   * @since 1.6
+   */
+  public static int getExponent(float f)
+  {
+    if (Float.isNaN(f) || Float.isInfinite(f))
+      return Float.MAX_EXPONENT + 1;
+    else if (f == 0.0f)
+      return Float.MIN_EXPONENT - 1;
+    else
+      return VMFloat.getExponent(f);
+  }
+
+  /**
+   * @since 1.6
+   */
+  public static int getExponent(double d)
+  {
+    if (Double.isNaN(d) || Double.isInfinite(d))
+      return Double.MAX_EXPONENT + 1;
+    else if (d == 0.0)
+      return Double.MIN_EXPONENT - 1;
+    else
+      return VMDouble.getExponent(d);
+  }
 }
diff --git a/java/lang/StrictMath.java b/java/lang/StrictMath.java
index 225aaa7..800d00f 100644
--- a/java/lang/StrictMath.java
+++ b/java/lang/StrictMath.java
@@ -2644,4 +2644,22 @@ public final strictfp class StrictMath
     // There's no difference.
     return Math.ulp(f);
   }
+
+  /**
+   * @since 1.6
+   */
+  public static int getExponent(float f)
+  {
+    // There's no difference.
+    return Math.getExponent(f);
+  }
+
+  /**
+   * @since 1.6
+   */
+  public static int getExponent(double d)
+  {
+    // There's no difference.
+    return Math.getExponent(d);
+  }
 }
diff --git a/native/fdlibm/Makefile.am b/native/fdlibm/Makefile.am
index 29bf837..d274fac 100644
--- a/native/fdlibm/Makefile.am
+++ b/native/fdlibm/Makefile.am
@@ -35,6 +35,7 @@ libfdlibm_la_SOURCES =  \
  sf_fabs.c \
  s_finite.c \
  s_floor.c \
+ s_ilogb.c \
  s_log1p.c \
  sf_rint.c \
  s_rint.c \
diff --git a/native/fdlibm/s_ilogb.c b/native/fdlibm/s_ilogb.c
new file mode 100644
index 0000000..366f3a7
--- /dev/null
+++ b/native/fdlibm/s_ilogb.c
@@ -0,0 +1,47 @@
+
+/* @(#)s_ilogb.c 1.3 95/01/18 */
+/*
+ * ====================================================
+ * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
+ *
+ * Developed at SunSoft, a Sun Microsystems, Inc. business.
+ * Permission to use, copy, modify, and distribute this
+ * software is freely granted, provided that this notice
+ * is preserved.
+ * ====================================================
+ */
+
+/* ilogb(double x)
+ * return the binary exponent of non-zero x
+ * ilogb(0) = 0x80000001
+ * ilogb(inf/NaN) = 0x7fffffff (no signal is raised)
+ */
+
+#include "fdlibm.h"
+
+#ifdef __STDC__
+ int ilogb(double x)
+#else
+ int ilogb(x)
+ double x;
+#endif
+{
+ int hx,lx,ix;
+
+ GET_HIGH_WORD(hx,x);
+ hx &= 0x7fffffff;
+ if(hx<0x00100000) {
+    GET_LOW_WORD(lx,x);
+    if((hx|lx)==0)
+ return 0x80000001; /* ilogb(0) = 0x80000001 */
+    else /* subnormal x */
+ if(hx==0) {
+    for (ix = -1043; lx>0; lx<<=1) ix -=1;
+ } else {
+    for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1;
+ }
+    return ix;
+ }
+ else if (hx<0x7ff00000) return (hx>>20)-1023;
+ else return 0x7fffffff;
+}
diff --git a/native/jni/java-lang/java_lang_VMDouble.c b/native/jni/java-lang/java_lang_VMDouble.c
index e915a30..6380c07 100644
--- a/native/jni/java-lang/java_lang_VMDouble.c
+++ b/native/jni/java-lang/java_lang_VMDouble.c
@@ -453,3 +453,11 @@ Java_java_lang_VMDouble_parseDouble
 
   return val;
 }
+
+JNIEXPORT jint JNICALL
+Java_java_lang_VMDouble_getExponent
+  (JNIEnv *env __attribute__ ((__unused__)),
+   jclass cls __attribute__ ((__unused__)), jdouble d)
+{
+  return ilogb(d);
+}
diff --git a/native/jni/java-lang/java_lang_VMFloat.c b/native/jni/java-lang/java_lang_VMFloat.c
index acd07ff..cf6f914 100644
--- a/native/jni/java-lang/java_lang_VMFloat.c
+++ b/native/jni/java-lang/java_lang_VMFloat.c
@@ -40,6 +40,8 @@ exception statement from your version. */
 
 #include "java_lang_VMFloat.h"
 
+#include "fdlibm.h"
+
 /*
  * Class:     java_lang_VMFloat
  * Method:    floatToRawIntBits
@@ -69,3 +71,11 @@ Java_java_lang_VMFloat_intBitsToFloat
   u.i = bits;
   return u.f;
 }
+
+JNIEXPORT jint JNICALL
+Java_java_lang_VMFloat_getExponent
+  (JNIEnv *env __attribute__ ((__unused__)),
+   jclass cls __attribute__ ((__unused__)), jfloat f)
+{
+  return ilogb(f);
+}
diff --git a/vm/reference/java/lang/VMDouble.java b/vm/reference/java/lang/VMDouble.java
index edfa723..6992f44 100644
--- a/vm/reference/java/lang/VMDouble.java
+++ b/vm/reference/java/lang/VMDouble.java
@@ -119,4 +119,6 @@ final class VMDouble
    * @throws NullPointerException if str is null
    */
   static native double parseDouble(String str);
+
+  static native int getExponent(double d);
 }
diff --git a/vm/reference/java/lang/VMFloat.java b/vm/reference/java/lang/VMFloat.java
index e6e784f..03843ed 100644
--- a/vm/reference/java/lang/VMFloat.java
+++ b/vm/reference/java/lang/VMFloat.java
@@ -117,4 +117,6 @@ final class VMFloat
     // the infinitely precise decimal.
     return (float) Double.parseDouble(str);
   }
+
+  static native int getExponent(float f);
 } // class VMFloat
--
1.7.4.1



Re: [RFC/PATCH] Add missing Java 1.5 java/lang/StrictMath methods

by Andrew Hughes-7 :: Rate this Message:

| View Threaded | Show Only this Message

----- Original Message -----

> Signed-off-by: Pekka Enberg <penberg@...>
> ---
>  java/lang/StrictMath.java |   72
>  +++++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 72 insertions(+), 0 deletions(-)
>
> diff --git a/java/lang/StrictMath.java b/java/lang/StrictMath.java
> index 88f5e57..225aaa7 100644
> --- a/java/lang/StrictMath.java
> +++ b/java/lang/StrictMath.java
> @@ -1317,6 +1317,78 @@ public final strictfp class StrictMath
>    }
>  
>    /**
> +   * <p>
> +   * Returns the hypotenuse, <code>a<sup>2</sup> +
> b<sup>2</sup></code>,
> +   * without intermediate overflow or underflow.  The returned
> result is
> +   * within 1 ulp of the exact result.  If one parameter is held
> constant,
> +   * then the result in the other parameter is semi-monotonic.
> +   * </p>
> +   * <p>
> +   * If either of the arguments is an infinity, then the returned
> result
> +   * is positive infinity.  Otherwise, if either argument is
> <code>NaN</code>,
> +   * then <code>NaN</code> is returned.
> +   * </p>
> +   *
> +   * @param a the first parameter.
> +   * @param b the second parameter.
> +   * @return the hypotenuse matching the supplied parameters.
> +   * @since 1.5
> +   */
> +  public static double hypot(double a, double b)
> +  {
> +    return VMMath.hypot(a,b);
> +  }
> +
> +  /**
> +   * <p>
> +   * Returns the base 10 logarithm of the supplied value.  The
> returned
> +   * result is within 1 ulp of the exact result, and the results are
> +   * semi-monotonic.
> +   * </p>
> +   * <p>
> +   * Arguments of either <code>NaN</code> or less than zero return
> +   * <code>NaN</code>.  An argument of positive infinity returns
> positive
> +   * infinity.  Negative infinity is returned if either positive or
> negative
> +   * zero is supplied.  Where the argument is the result of
> +   * <code>10<sup>n</sup</code>, then <code>n</code> is returned.
> +   * </p>
> +   *
> +   * @param a the numeric argument.
> +   * @return the base 10 logarithm of <code>a</code>.
> +   * @since 1.5
> +   */
> +  public static double log10(double a)
> +  {
> +    return VMMath.log10(a);
> +  }
> +
> +  /**
> +   * <p>
> +   * Returns the natural logarithm resulting from the sum of the
> argument,
> +   * <code>a</code> and 1.  For values close to 0, the
> +   * result of <code>log1p(a)</code> tend to be much closer to the
> +   * exact result than simply <code>log(1.0+a)</code>.  The returned
> +   * result is within 1 ulp of the exact result, and the results are
> +   * semi-monotonic.
> +   * </p>
> +   * <p>
> +   * Arguments of either <code>NaN</code> or less than -1 return
> +   * <code>NaN</code>.  An argument of positive infinity or zero
> +   * returns the original argument.  Negative infinity is returned
> from an
> +   * argument of -1.
> +   * </p>
> +   *
> +   * @param a the numeric argument.
> +   * @return the natural logarithm of <code>a</code> + 1.
> +   * @since 1.5
> +   */
> +  public static double log1p(double a)
> +  {
> +    return VMMath.log1p(a);
> +  }
> +
> +
> +  /**
>     * Take ln(a) (the natural log).  The opposite of
>     <code>exp()</code>. If the
>     * argument is NaN or negative, the result is NaN; if the argument
>     is
>     * positive infinity, the result is positive infinity; and if the
>     argument
> --
> 1.7.4.1
>
>
>

No. If you look at the other methods in StrictMath, you'll see they provide Java versions of the methods.
--
Andrew :)

Free Java Software Engineer
Red Hat, Inc. (http://www.redhat.com)

PGP Key: 248BDC07 (https://keys.indymedia.org/)
Fingerprint = EC5A 1F5E C0AD 1D15 8F1F  8F91 3B96 A578 248B DC07