Draft: Fix #156: Handle NaNs in comparisons correctly.

There are several parts here.

  • Add vops to handle the comparisons <= and >= for both 32-bit signed and unsigned words and for single-float and double-float.
  • Add two-arg->= and two-arg-<=
  • Add generic->= and generic-<=
  • Modify multi-compare so that <= isn't converted to >. Likewise for >=.

This is only implemented for x86 but probably should be done for other architectures.

Merge request reports

Loading
+2 −0
Changes for src/assembly/x86/arith.lisp: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -214,7 +214,9 @@
); eval-when

(define-cond-assem-rtn generic-< < two-arg-< :l)
(define-cond-assem-rtn generic-<= <= two-arg-<= :le)
(define-cond-assem-rtn generic-> > two-arg-> :g)
(define-cond-assem-rtn generic->= >= two-arg->= :ge)

(define-assembly-routine (generic-eql
			  (:cost 10)
+16 −0
Changes for src/bootfiles/21e/boot-2024-04-1.lisp: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
;; Bootstrap file for x86 to add two-arg->= and two-arg-<= to static
;; functions list.
;;
;; Use bin/build.sh -B boot-2021-07-3 to build this (along with any
;; other bootfiles that are still needed).
#+x86
(in-package :x86)

#+x86
(ext:without-package-locks
  (defparameter static-functions
    '(length
      two-arg-+ two-arg-- two-arg-* two-arg-/ two-arg-< two-arg-> two-arg-= eql
      %negate two-arg-and two-arg-ior two-arg-xor two-arg-gcd two-arg-lcm
      two-arg-<= two-arg->= two-arg-/=
      )))
+21 −0
Changes for src/code/numbers.lisp: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1033,6 +1033,27 @@
	     ((bignum bignum)
	      (plusp (bignum-compare x y))))

;; This should probably be used for all architectures, but we've only
;; written all the necessary support only on x86, currently.  See
;; issue #156.
#+x86
(progn
  (two-arg-</> two-arg-<= <= ceiling floor
	       ((fixnum bignum)
		(bignum-plus-p y))
	       ((bignum fixnum)
		(not (bignum-plus-p x)))
	       ((bignum bignum)
		(not (plusp (bignum-compare x y)))))

  (two-arg-</> two-arg->= >= floor ceiling
	       ((fixnum bignum)
		(not (bignum-plus-p y)))
	       ((bignum fixnum)
		(bignum-plus-p x))
	       ((bignum bignum)
		(not (minusp (bignum-compare x y))))))


(defun two-arg-= (x y)
  (number-dispatch ((x number) (y number))
+9 −1
Changes for src/compiler/x86/arith.lisp: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -898,8 +898,9 @@
	'(t t t t nil nil))))

(define-conditional-vop < :l :b :ge :ae)

(define-conditional-vop <= :le :be :g :a)
(define-conditional-vop > :g :a :le :be)
(define-conditional-vop >= :ge :ae :l :b)

(define-vop (fast-if-eql/signed fast-conditional/signed)
  (:translate eql)
@@ -1365,6 +1366,13 @@

(define-static-function two-arg-/ (x y) :translate /)

(define-static-function two-arg-< (x y) :translate <)
(define-static-function two-arg-<= (x y) :translate <=)
(define-static-function two-arg-> (x y) :translate >)
(define-static-function two-arg->= (x y) :translate >=)
(define-static-function two-arg-= (x y) :translate =)
(define-static-function two-arg-/= (x y) :translate /=)

(define-static-function two-arg-gcd (x y) :translate gcd)
(define-static-function two-arg-lcm (x y) :translate lcm)

+34 −0
Changes for src/compiler/x86/float-sse2.lisp: 34 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1000,6 +1000,40 @@
  (frob < single comiss)
  (frob < double comisd))



(macrolet
    ((frob (op size inst yep nope)
       (let ((ea (ecase size
		   (single
		    'ea-for-sf-desc)
		   (double
		    'ea-for-df-desc)))
	     (name (symbolicate op "/" size "-FLOAT"))
	     (sc-type (symbolicate size "-REG"))
	     (inherit (symbolicate size "-FLOAT-COMPARE")))
	 `(define-vop (,name ,inherit)
	    (:translate ,op)
	    (:info target not-p)
	    (:generator 3
	      (sc-case y
		(,sc-type
		 (inst ,inst x y))
		(descriptor-reg
		 (inst ,inst x (,ea y))))
	      (cond (not-p
		     (inst jmp :p target)
		     (inst jmp ,nope target))
		    (t
		     (let ((not-lab (gen-label)))
		       (inst jmp :p not-lab)
		       (inst jmp ,yep target)
		       (emit-label not-lab)))))))))
  (frob <= single comiss :be :nbe)
  (frob >= single comiss :ae :nae)
  (frob <= double comisd :be :nbe)
  (frob >= double comisd :ae :nae))


;;;; Conversion:

Loading
Loading