From 243d62a9528f7bbc3c5f71deb2f4aa857a8473cd Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Tue, 2 Oct 2007 08:58:35 -0600 Subject: [PATCH 1/6] non-working implementation of float and double.toString() --- classpath/java-lang.cpp | 7 +++++++ classpath/java/lang/Double.java | 7 ++++++- classpath/java/lang/StringBuilder.java | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index e47facf630..3cd963eee1 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -69,3 +69,10 @@ Java_java_lang_System_doMapLibraryName(JNIEnv* e, jclass, jstring name) } return r; } + +extern "C" JNIEXPORT jint JNICALL +Java_java_lang_Double_fillBufferWithDouble(JNIEnv *e, jclass me, jdouble val, + jbyteArray buffer, jint bufferSize) { + if (e or me or val or buffer or bufferSize) return 0; + return 0; +} diff --git a/classpath/java/lang/Double.java b/classpath/java/lang/Double.java index 8cab976e1b..58d96b9438 100644 --- a/classpath/java/lang/Double.java +++ b/classpath/java/lang/Double.java @@ -35,7 +35,9 @@ public final class Double extends Number { } public static String toString(double v) { - return "Double.toString: todo"; + byte[] buffer = new byte[20]; + int numChars = fillBufferWithDouble(v, buffer, 20); + return new String(buffer, 0, numChars, false); } public byte byteValue() { @@ -67,6 +69,9 @@ public final class Double extends Number { throw new NumberFormatException(s); } + public static native int fillBufferWithDouble(double value, byte[] buffer, + int charCount); + public static native long doubleToRawLongBits(double value); public static native double longBitsToDouble(long bits); diff --git a/classpath/java/lang/StringBuilder.java b/classpath/java/lang/StringBuilder.java index dda0148296..731da9169d 100644 --- a/classpath/java/lang/StringBuilder.java +++ b/classpath/java/lang/StringBuilder.java @@ -81,6 +81,15 @@ public class StringBuilder { return append(String.valueOf(v)); } + public StringBuilder append(float v) { + return append(String.valueOf(v)); + } + + public StringBuilder append(double v) { + return append(String.valueOf(v)); + } + + public char charAt(int i) { if (i < 0 || i >= length) { throw new IndexOutOfBoundsException(); From 7bc85a1247e4a8b23c4c68e805d34b2d27c57905 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Tue, 2 Oct 2007 09:23:49 -0600 Subject: [PATCH 2/6] Implemented printing of doubles using snprintf --- classpath/java-lang.cpp | 9 ++++++--- classpath/java/lang/Float.java | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index 3cd963eee1..6f680f12e7 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -71,8 +71,11 @@ Java_java_lang_System_doMapLibraryName(JNIEnv* e, jclass, jstring name) } extern "C" JNIEXPORT jint JNICALL -Java_java_lang_Double_fillBufferWithDouble(JNIEnv *e, jclass me, jdouble val, +Java_java_lang_Double_fillBufferWithDouble(JNIEnv *e, jclass, jdouble val, jbyteArray buffer, jint bufferSize) { - if (e or me or val or buffer or bufferSize) return 0; - return 0; + jboolean isCopy; + jbyte* buf = e->GetByteArrayElements(buffer, &isCopy); + jint count = snprintf(reinterpret_cast(buf), bufferSize, "%g", val); + e->ReleaseByteArrayElements(buffer, buf, 0); + return count; } diff --git a/classpath/java/lang/Float.java b/classpath/java/lang/Float.java index b563a69bf4..88274aa4e9 100644 --- a/classpath/java/lang/Float.java +++ b/classpath/java/lang/Float.java @@ -26,7 +26,7 @@ public final class Float extends Number { } public static String toString(float v) { - return "Float.toString: todo"; + return Double.toString(v); } public byte byteValue() { From d2053d51d206feb7b8f13fa99730cd2327de41cb Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Tue, 2 Oct 2007 09:50:08 -0600 Subject: [PATCH 3/6] Floats are always passed as doubles in varargs calls. Properly pop them off the stack and cast them --- src/interpret.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/interpret.cpp b/src/interpret.cpp index 778d56b3ea..2ada7d129e 100644 --- a/src/interpret.cpp +++ b/src/interpret.cpp @@ -2663,6 +2663,14 @@ pushArguments(Thread* t, object this_, const char* spec, bool indirectObjects, ++ s; pushLong(t, va_arg(a, uint64_t)); break; + + case 'F': { + ++ s; + float f = va_arg(a, double); + uint32_t i; + memcpy(&i, &f, 4); + pushInt(t, i); + } break; default: ++ s; From ace36f1b151ea5c4ea8f7335aba02567e373ac78 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 4 Oct 2007 07:30:39 -0600 Subject: [PATCH 4/6] Simplify code for Mac stack alignment conventions --- src/cdecl.S | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cdecl.S b/src/cdecl.S index 25b8c9ff5c..bce6257a5e 100644 --- a/src/cdecl.S +++ b/src/cdecl.S @@ -20,14 +20,13 @@ cdeclCall: // reserve space for arguments movl 16(%ebp),%ecx + subl %ecx,%esp + #ifdef __APPLE__ // align to a 16 byte boundary on Darwin - addl $15,%ecx - andl $0xFFFFFFF0,%ecx - addl $8,%ecx + andl $0xFFFFFFF0,%esp #endif - subl %ecx,%esp // copy arguments into place movl $0,%ecx From a9fcb59e6ca2444269e94cdd1b14d26ba99f2e0a Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 4 Oct 2007 10:21:14 -0600 Subject: [PATCH 5/6] Fix a floating point comparison bug - Gets simple SWT examples to work on Mac OS X --- src/interpret.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/interpret.cpp b/src/interpret.cpp index 2ada7d129e..b8704bbb9e 100644 --- a/src/interpret.cpp +++ b/src/interpret.cpp @@ -1253,14 +1253,30 @@ interpret(Thread* t) float b = popFloat(t); float a = popFloat(t); - pushInt(t, (a > b ? 1 : 0)); + if (a < b) { + pushInt(t, static_cast(-1)); + } else if (a > b) { + pushInt(t, 1); + } else if (a == b) { + pushInt(t, 0); + } else { + pushInt(t, 1); + } } goto loop; case fcmpl: { float b = popFloat(t); float a = popFloat(t); - pushInt(t, (a < b ? 1 : 0)); + if (a < b) { + pushInt(t, static_cast(-1)); + } else if (a > b) { + pushInt(t, 1); + } else if (a == b) { + pushInt(t, 0); + } else { + pushInt(t, static_cast(-1)); + } } goto loop; case fconst_0: { From 8b607d4aa4cba67b3efcb526649069dc891963d0 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 4 Oct 2007 13:57:39 -0600 Subject: [PATCH 6/6] FileInputStream read() should return an unsigned byte --- classpath/java-io.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classpath/java-io.cpp b/classpath/java-io.cpp index bfda2cb730..f0f58a1c43 100644 --- a/classpath/java-io.cpp +++ b/classpath/java-io.cpp @@ -198,7 +198,7 @@ Java_java_io_FileInputStream_read__I(JNIEnv* e, jclass, jint fd) if (r <= 0) { return -1; } else { - return data; + return data & 0xff; } }