Skip to content

Instantly share code, notes, and snippets.

@no-defun-allowed
Last active August 9, 2020 07:27
Show Gist options
  • Save no-defun-allowed/b44f1a2223c73c28d2a71844614a2cac to your computer and use it in GitHub Desktop.
Save no-defun-allowed/b44f1a2223c73c28d2a71844614a2cac to your computer and use it in GitHub Desktop.
haha < goes brrr
(ql:quickload :the-cost-of-nothing)
(in-package :cl-user)
(defun foo (x)
(declare (integer x)
(optimize (speed 3)))
(< (- (expt 2 256)) x (expt 2 256)))
(the-cost-of-nothing:bench (foo 7))
(in-package :sb-c)
(defun interval-> (x y)
(and (not (interval-< x y))
(not (interval-= x y))))
(defun interval-<= (x y)
(not (interval-> x y)))
(macrolet ((def (comparator interval-comparator)
`(progn
(sb-c:deftransform ,comparator ((x y) (integer bignum) *)
,(format nil "eliminate (~a fixnum bignum) comparisons if~
the fixnum is not known to be a fixnum at compile-time"
comparator)
(multiple-value-bind (typep certain)
(csubtypep (specifier-type 'fixnum) (lvar-type x))
(when (and (not typep) certain)
;; Get out if X cannot be a FIXNUM (such as in the
;; generated comparator call).
(give-up-ir1-transform)))
`(if (fixnump x)
,(,interval-comparator (numeric-type->interval
(specifier-type 'fixnum))
(numeric-type->interval
(lvar-type y)))
(,',comparator (truly-the bignum x) y)))
(sb-c:deftransform ,comparator ((x y) (bignum integer) *)
,(format nil "eliminate (~a bignum fixnum) comparisons if ~
the fixnum is not known to be a fixnum at compile-time"
comparator)
(multiple-value-bind (typep certain)
(csubtypep (specifier-type 'fixnum) (lvar-type y))
(when (and (not typep) certain)
(give-up-ir1-transform)))
`(if (fixnump y)
,(,interval-comparator (numeric-type->interval
(lvar-type x))
(numeric-type->interval
(specifier-type 'fixnum)))
(,',comparator x (truly-the bignum y)))))))
(def < interval-<)
(def <= interval-<=)
(def = interval-=)
(def /= interval-/=)
(def >= interval->=)
(def > interval->))
(in-package :cl-user)
(defun foo (x)
(declare (integer x)
(optimize (speed 3)))
(< (- (expt 2 256)) x (expt 2 256)))
(the-cost-of-nothing:bench (foo 7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment