Squelch Java use of HOST_FLOAT_WORDS_BIG_ENDIAN (take two)

Zack Weinberg zack@codesourcery.com
Fri Mar 14 17:11:00 GMT 2003


Zack Weinberg <zack@codesourcery.com> writes:

> Revised patch which generates correct hex floating constants for
> java.lang.Float and java.lang.Double.  I was not able to incorporate
> Richard's suggestion to use __builtin_nan() in gcjh, because the C++
> compiler currently rejects that construct.  We can revisit that after
> the C++ compiler is fixed.

Re-revised patch which gets decimal floating point output in jcf-dump
right for normalized numbers.  I had forgotten the implicit bit.
Denormals, at least on my machine, get turned into the minimum
representable normalized number; I don't think there's anything that
can be done about that.  Tested by examining jcf-dump output for
Float.class, Double.class, and the .class file generated from

public final class test {
  public static final double PId = 3.14159265358979323846;
  public static final float  PIf = 3.14159265358979323846f;
};

Only jcf-dump.c has changed since the previous posting.

zw

>         * jcf-dump.c: Include math.h.  Use ldexp/frexp to assemble
>         finite floating point numbers for output; special case
>         non-finite floats.

===================================================================
Index: jcf-dump.c
--- jcf-dump.c	18 Jan 2003 22:15:51 -0000	1.55
+++ jcf-dump.c	14 Mar 2003 17:08:07 -0000
@@ -62,6 +62,7 @@ The Free Software Foundation is independ
 #include "version.h"
 
 #include <getopt.h>
+#include <math.h>
 
 /* Outout file. */
 FILE *out;
@@ -504,24 +505,86 @@ print_constant (FILE *out, JCF *jcf, int
       break;
     case CONSTANT_Float:
       {
-	union
-	{
-	  jfloat f;
-	  int32 i;
-	} pun;
-	
-	pun.f = JPOOL_FLOAT (jcf, index);
-	fprintf (out, "%s%.10g",
-		 verbosity > 0 ? "Float " : "", (double) pun.f);
+	jfloat fnum = JPOOL_FLOAT (jcf, index);
+
+	if (verbosity > 0)
+	  fputs ("Float ", out);
+
+	if (fnum.negative)
+	  putc ('-', out);
+
+	if (JFLOAT_FINITE (fnum))
+	  {
+	    int dummy;
+	    int exponent = fnum.exponent - JFLOAT_EXP_BIAS;
+	    double f;
+	    uint32 mantissa = fnum.mantissa;
+	    if (fnum.exponent == 0)
+	      /* Denormal.  */
+	      exponent++;
+	    else
+	      /* Normal; add the implicit bit.  */
+	      mantissa |= ((uint32)1 << 23);
+	    
+	    f = frexp (mantissa, &dummy);
+	    f = ldexp (f, exponent + 1);
+	    fprintf (out, "%.10g", f);
+	  }
+	else
+	  {
+	    if (fnum.mantissa == 0)
+	      fputs ("Inf", out);
+	    else if (fnum.mantissa & JFLOAT_QNAN_MASK)
+	      fprintf (out, "QNaN(%u)", (fnum.mantissa & ~JFLOAT_QNAN_MASK));
+	    else
+	      fprintf (out, "SNaN(%u)", (fnum.mantissa & ~JFLOAT_QNAN_MASK));
+	  }
+
 	if (verbosity > 1)
-	  fprintf (out, ", bits = 0x%08lx", (long) pun.i);
+	  fprintf (out, ", bits = 0x%08lx", JPOOL_UINT (jcf, index));
 	
 	break;
       }
     case CONSTANT_Double:
       {
 	jdouble dnum = JPOOL_DOUBLE (jcf, index);
-	fprintf (out, "%s%.20g", verbosity > 0 ? "Double " : "", dnum);
+
+	if (verbosity > 0)
+	  fputs ("Double ", out);
+
+	if (dnum.negative)
+	  putc ('-', out);
+
+	if (JDOUBLE_FINITE (dnum))
+	  {
+	    int dummy;
+	    int exponent = dnum.exponent - JDOUBLE_EXP_BIAS;
+	    double d;
+	    uint64 mantissa = ((((uint64) dnum.mantissa0) << 32)
+			       + dnum.mantissa1);
+	    if (dnum.exponent == 0)
+	      /* Denormal.  */
+	      exponent++;
+	    else
+	      /* Normal; add the implicit bit.  */
+	      mantissa |= ((uint64)1 << 52);
+
+	    d = frexp (mantissa, &dummy);
+	    d = ldexp (d, exponent + 1);
+	    fprintf (out, "%.20g", d);
+	  }
+	else
+	  {
+	    uint64 mantissa = dnum.mantissa0 & ~JDOUBLE_QNAN_MASK;
+	    mantissa = (mantissa << 32) + dnum.mantissa1;
+
+	    if (dnum.mantissa0 == 0 && dnum.mantissa1 == 0)
+	      fputs ("Inf", out);
+	    else if (dnum.mantissa0 & JDOUBLE_QNAN_MASK)
+	      fprintf (out, "QNaN(%llu)", (unsigned long long)mantissa);
+	    else
+	      fprintf (out, "SNaN(%llu)", (unsigned long long)mantissa);
+	  }
 	if (verbosity > 1)
 	  {
 	    int32 hi, lo;



More information about the Gcc-patches mailing list