[MLton-devel] cvs commit: removed __asm__ for Int_quot and Int_rem

Stephen Weeks sweeks@users.sourceforge.net
Thu, 07 Nov 2002 11:15:06 -0800


sweeks      02/11/07 11:15:06

  Modified:    include  ccodegen.h
               runtime  Makefile
               runtime/basis/Int quot.c rem.c
  Removed:     runtime/basis/Word32 arshiftAsm.c
               runtime/basis/Word8 arshiftAsm.c
  Log:
  Also got rid of vestigal __asm__ for Word{8,32}_~>>

Revision  Changes    Path
1.39      +0 -3      mlton/include/ccodegen.h

Index: ccodegen.h
===================================================================
RCS file: /cvsroot/mlton/mlton/include/ccodegen.h,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -r1.38 -r1.39
--- ccodegen.h	2 Nov 2002 03:37:36 -0000	1.38
+++ ccodegen.h	7 Nov 2002 19:15:05 -0000	1.39
@@ -439,7 +439,6 @@
 	return n;
 }
 #define Int_negCheck(dst, n, l) dst = Int_negCheckFast(n)
-#define Int_quot(x, y) ((x)/(y))
 #define Int_rem(x, y) ((x)%(y))
 
 #else /* no FAST_INT */
@@ -569,7 +568,6 @@
  * sign extension.  We use it anyway cause it always seems to work.
  */
 #define Word8_arshift(w, s) ((signed char)(w) >> (s))
-/*#define Word8_arshift Word8_arshiftAsm */
 #define Word8_div(w1, w2) ((w1) / (w2))
 #define Word8_fromInt(x) ((uchar)(x))
 #define Word8_fromLargeWord(w) ((uchar)(w))
@@ -617,7 +615,6 @@
  * We do it because using a procedure call slows down IntInf by a factor of 2.
  */
 #define Word32_arshift(w, s) ((int)(w) >> (s))
-/*#define Word32_arshift Word32_arshiftAsm */
 #define Word32_div(w1, w2) ((w1) / (w2))
 #define Word32_fromInt(x) ((uint)(x))
 #define Word32_ge(w1, w2) ((w1) >= (w2))



1.38      +0 -4      mlton/runtime/Makefile

Index: Makefile
===================================================================
RCS file: /cvsroot/mlton/mlton/runtime/Makefile,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- Makefile	5 Nov 2002 15:02:26 -0000	1.37
+++ Makefile	7 Nov 2002 19:15:05 -0000	1.38
@@ -57,9 +57,7 @@
 	basis/Thread.o				\
 	basis/Time.o				\
 	basis/Word32/addOverflow.o		\
-	basis/Word32/arshiftAsm.o		\
 	basis/Word32/mulOverflow.o		\
-	basis/Word8/arshiftAsm.o		\
 	Posix/Error/clearErrno.o		\
 	Posix/Error/getErrno.o			\
 	Posix/Error/strerror.o			\
@@ -206,9 +204,7 @@
 	basis/Thread-gdb.o			\
 	basis/Time-gdb.o			\
 	basis/Word32/addOverflow-gdb.o		\
-	basis/Word32/arshiftAsm-gdb.o		\
 	basis/Word32/mulOverflow-gdb.o		\
-	basis/Word8/arshiftAsm-gdb.o		\
 	Posix/Error/clearErrno-gdb.o		\
 	Posix/Error/getErrno-gdb.o		\
 	Posix/Error/strerror-gdb.o		\



1.3       +29 -10    mlton/runtime/basis/Int/quot.c

Index: quot.c
===================================================================
RCS file: /cvsroot/mlton/mlton/runtime/basis/Int/quot.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- quot.c	6 Jul 2002 17:22:08 -0000	1.2
+++ quot.c	7 Nov 2002 19:15:06 -0000	1.3
@@ -2,15 +2,34 @@
 
 #include "mlton-basis.h"
 
-Int Int_quot(Int numerator, Int denominator) {
-	register int eax asm("ax");
+/*
+ * We have to be very careful implementing Int_quot and Int_rem using / and %
+ * because C allows
+ *  "The direction of truncation for / and the sign of the result for % are
+ *   machine-dependent for negative operands, ..." (K&R p. 41) (See also p. 205.)
+ * On the other hand, the SML Basis library spec is completely nailed down.
+ * On x86, gcc implements / and % using idiv, which fortunately does have the
+ * same semantics as the SML Basis library.  However, gcc's optimizer sometimes
+ * takes advantage of the flexibility in the C spec when one of the arguments
+ * is a constant, producing incorrect results.  So, we have two options:
+ *
+ * Put Int_quot and Int_rem in a separate file, all by themselves, without a
+ * static inline, and use / and % where we know gcc's optimer can't hurt us.
+ * OR
+ * Use inline assembler.
+ *
+ * We've gone for the first option because of simplicity, and because
+ * Int_quot and Int_rem are only used with the C codegen.  If you really want
+ * speed, you could try inline assembler.
+ *
+ * To get this working on another architecture, you need to check how gcc
+ * implements / and %.
+ */
 
-	eax = numerator ;
-	
-	__asm__ __volatile__ ("cdq\n        idivl %1"
-		: 
-		: "r" (eax), "m" (denominator)
-		: "eax", "edx");
-
-	return eax;
+Int Int_quot (Int n, Int d) {
+#if (defined (__i386__))
+	return n / d;
+#else
+#error check that C / correctly implements Int.quot from the basis library
+#endif
 }



1.2       +8 -12     mlton/runtime/basis/Int/rem.c

Index: rem.c
===================================================================
RCS file: /cvsroot/mlton/mlton/runtime/basis/Int/rem.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- rem.c	18 Jul 2001 05:51:06 -0000	1.1
+++ rem.c	7 Nov 2002 19:15:06 -0000	1.2
@@ -1,15 +1,11 @@
 #include "mlton-basis.h"
 
-Int Int_rem(Int numerator, Int denominator) {
-	register int eax asm("ax"),
-			edx asm("dx");
-	
-	eax = numerator ;
-	
-	__asm__ __volatile__ ("cdq\n        idivl %1"
-		: 
-		: "r" (eax), "m" (denominator)
-		: "eax", "edx");
-	
-	return edx;
+/* See the comment in quot.c. */
+
+Int Int_rem (Int n, Int d) {
+#if (defined (__i386__))
+	return n % d;
+#else
+#error check that C % correctly implements Int.rem from the basis library
+#endif
 }





-------------------------------------------------------
This sf.net email is sponsored by: See the NEW Palm 
Tungsten T handheld. Power & Color in a compact size!
http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel