From 63f036e01dd6ac028579865a4ab08bde6956fb0f Mon Sep 17 00:00:00 2001
From: dtc <dtc>
Date: Fri, 7 Apr 2000 20:33:34 +0000
Subject: [PATCH] From Raymond Toy: o Define %max/%min functions for
 byte-compilation and constant   folding. o Always evaluate all arguments,
 even if we can prove we won't use   that argument.  (Don't shortcut
 %max/%min.)

---
 compiler/srctran.lisp | 106 ++++++++++++++++++------------------------
 1 file changed, 45 insertions(+), 61 deletions(-)

diff --git a/compiler/srctran.lisp b/compiler/srctran.lisp
index 923ce22bd..087a887ae 100644
--- a/compiler/srctran.lisp
+++ b/compiler/srctran.lisp
@@ -5,7 +5,7 @@
 ;;; Carnegie Mellon University, and has been placed in the public domain.
 ;;;
 (ext:file-comment
-  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/compiler/srctran.lisp,v 1.96 2000/04/06 18:40:15 dtc Exp $")
+  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/compiler/srctran.lisp,v 1.97 2000/04/07 20:33:34 dtc Exp $")
 ;;;
 ;;; **********************************************************************
 ;;;
@@ -3502,21 +3502,23 @@
   real
   (movable foldable flushable))
 
+;; Needed for the byte-compiled stuff and constant folding since we've
+;; declared these as foldable functions.
+(defun %max (x y)
+  (%max x y))
+(defun %min (x y)
+  (%min x y))
+
 ;; Convert max/min of many args into the obvious set of nested max/min's.
 (def-source-transform max (arg &rest more-args)
   (cond ((null more-args)
 	 `(values ,arg))
-	#+nil
-	((= (length more-args) 1)
-	 `(%max ,arg ,@more-args))
 	(t
 	 `(%max ,arg (max ,@more-args)))))
 
 (def-source-transform min (arg &rest more-args)
   (cond ((null more-args)
 	 `(values ,arg))
-	((= (length more-args) 1)
-	 `(%min ,arg ,@more-args))
 	(t
 	 `(%min ,arg (min ,@more-args)))))
 
@@ -3550,36 +3552,26 @@
 	(unsigned (specifier-type '(unsigned-byte 32)))
 	(d-float (specifier-type 'double-float))
 	(s-float (specifier-type 'single-float)))
-    (multiple-value-bind (definitely-< definitely->=)
-	(ir1-transform-<-helper x y)
-      ;; Return the appropriate arg if we can prove one is always
-      ;; greater than the other.  If not, use %%max if both args are
-      ;; good types of the same type.  As a last resort, use the
-      ;; obvious comparison to select the desired element.
-      (cond (definitely-<
-		`(values y))
-	    (definitely->=
-		`(values x))
-	    ((and (csubtypep x-type signed)
-		  (csubtypep y-type signed))
-	     `(sparc::%%max x y))
-	    ((and (csubtypep x-type unsigned)
-		  (csubtypep y-type unsigned))
-	     `(sparc::%%max x y))
-	    ((and (csubtypep x-type d-float)
-		  (csubtypep y-type d-float))
-	     `(sparc::%%max x y))
-	    ((and (csubtypep x-type s-float)
-		  (csubtypep y-type s-float))
-	     `(sparc::%%max x y))
-	    (t
-	     (let ((arg1 (gensym))
-		   (arg2 (gensym)))
-	       `(let ((,arg1 x)
-		      (,arg2 y))
-		 (if (> ,arg1 ,arg2)
-		     ,arg1 ,arg2))))))))
-	     
+    ;; Use %%max if both args are good types of the same type.  As a
+    ;; last resort, use the obvious comparison to select the desired
+    ;; element.
+    (cond ((or (and (csubtypep x-type signed)
+		    (csubtypep y-type signed))
+	       (and (csubtypep x-type unsigned)
+		    (csubtypep y-type unsigned))
+	       (and (csubtypep x-type d-float)
+		    (csubtypep y-type d-float))
+	       (and (csubtypep x-type s-float)
+		    (csubtypep y-type s-float)))
+	   `(sparc::%%max x y))
+	  (t
+	   (let ((arg1 (gensym))
+		 (arg2 (gensym)))
+	     `(let ((,arg1 x)
+		    (,arg2 y))
+		(if (> ,arg1 ,arg2)
+		    ,arg1 ,arg2)))))))
+
 (deftransform %min ((x y) (real real) * :when :both)
   (let ((x-type (continuation-type x))
 	(y-type (continuation-type y))
@@ -3587,33 +3579,25 @@
 	(unsigned (specifier-type '(unsigned-byte 32)))
 	(d-float (specifier-type 'double-float))
 	(s-float (specifier-type 'single-float)))
-    (multiple-value-bind (definitely-< definitely->=)
-	(ir1-transform-<-helper x y)
-      (cond (definitely-<
-		`(values x))
-	    (definitely->=
-		`(values y))
-	    ((and (csubtypep x-type signed)
-		  (csubtypep y-type signed))
-	     `(sparc::%%min x y))
-	    ((and (csubtypep x-type unsigned)
-		  (csubtypep y-type unsigned))
-	     `(sparc::%%min x y))
-	    ((and (csubtypep x-type d-float)
-		  (csubtypep y-type d-float))
-	     `(sparc::%%min x y))
-	    ((and (csubtypep x-type s-float)
-		  (csubtypep y-type s-float))
-	     `(sparc::%%min x y))
-	    (t
-	     (let ((arg1 (gensym))
-		   (arg2 (gensym)))
-	       `(let ((,arg1 x)
-		      (,arg2 y))
-		 (if (< ,arg1 ,arg2)
-		     ,arg1 ,arg2))))))))
+    (cond ((or (and (csubtypep x-type signed)
+		    (csubtypep y-type signed))
+	       (and (csubtypep x-type unsigned)
+		    (csubtypep y-type unsigned))
+	       (and (csubtypep x-type d-float)
+		    (csubtypep y-type d-float))
+	       (and (csubtypep x-type s-float)
+		    (csubtypep y-type s-float)))
+	   `(sparc::%%min x y))
+	  (t
+	   (let ((arg1 (gensym))
+		 (arg2 (gensym)))
+	     `(let ((,arg1 x)
+		    (,arg2 y))
+		(if (< ,arg1 ,arg2)
+		    ,arg1 ,arg2)))))))
 
 )
+
 
 ;;;; Converting N-arg arithmetic functions:
 ;;;
-- 
GitLab