[kaffe] [Fwd: Patch for: Kaffe doesn't work with GMP on x86_64]

Alan Eliasen eliasen at mindspring.com
Sun Dec 11 18:20:50 PST 2005


Alan Eliasen wrote:
>    I really like using Kaffe as my JVM for working with really big
> numbers, as it can be compiled to use the GMP library.  It makes certain
> programs that work with really big integers run thousands of times
> faster than any other JVM.  I was really excited about running it on the
> x86_64 with 64-bit multiplies that might be up to 4 times faster.  There
> seem to be major bugs preventing this from working on the x86_64
> architecture, though.  (I tested against the CVS head.)

   I think that I have a patch for this problem.  (See below.)  It
appears that all of the steps in Java_java_math_BigInteger_assignLong0
in the file can simply be replaced with a single call to mpz_set_si(res,
v) which sets a GMP integer from a signed long int.  (The signature is
void mpz_set_ui (mpz t rop, unsigned long int op), see the GMP
documentation at http://www.swox.com/gmp/gmp-man-4.1.4.pdf , section 5.2. )

   I think that this code should be simpler and faster than the original
code, if a jlong is an unsigned long, but I haven't checked it on all
architectures, only on x86_64 where the problem originally occurred.

   This works and passes my regression tests.

this is a patch to libraries/clib/math/BigInteger.c .  Please let me
know if I can produce this patch in a better format, and what I can do
to get this fixed in Kaffe.


*** BigInteger.c        2005-12-11 18:45:45.000000000 -0700
--- BigInteger.c.new    2005-12-11 18:34:54.000000000 -0700
***************
*** 77,100 ****
  Java_java_math_BigInteger_assignLong0(JNIEnv* env, jobject r, jlong v)
  {
     mpz_ptr res;
-    int negative = v < 0 ? -1 : 0;
-
     res = (*env)->GetObjectField(env, r, number);

!    if (negative)
!       v = -v;
!    /* Note that v will remain negative if it's LONG_LONG_MIN.
!       This is not a problem because any sign copying in the right
!       shift will be stripped with the cast to jint, and the
!       number will be considered positive.  Furthermore, in this
!       case, (jint)v will be zero, so the addition will be a
!       do-nothing operation.  At last, the number will be made
!       negative, as appropriate.  */
!    mpz_set_ui(res, (unsigned long)(jint)(v >> 32));
!    mpz_mul_2exp(res, res, 32);
!    mpz_add_ui(res, res, (unsigned long)(jint)v);
!    if (negative)
!       mpz_neg(res, res);
  }

  void
--- 77,85 ----
  Java_java_math_BigInteger_assignLong0(JNIEnv* env, jobject r, jlong v)
  {
     mpz_ptr res;
     res = (*env)->GetObjectField(env, r, number);

!    mpz_set_si(res, v);
  }

  void

-- 
  Alan Eliasen                 |  "It's amazing how much mature wisdom
  eliasen at mindspring.com       |   resembles being too tired."
  http://futureboy.homeip.net/ |     -- Robert Heinlein



-- 
  Alan Eliasen                 |  "It's amazing how much mature wisdom
  eliasen at mindspring.com       |   resembles being too tired."
  http://futureboy.homeip.net/ |     -- Robert Heinlein




More information about the kaffe mailing list