On Mon, Feb 03, 2003 at 03:59:02PM -0800, Robert E. Brown wrote:
> William, thanks for the pointer to derive-type optimizers. Here is a patch
> to compiler/srctran.lisp that adds a derive-type optimizer for isqrt. I
> based the code on the optimizer already in place for integer-length.
(Usually "Bill", incidentally.)
I don't really remember much about derive-type optimizers, and I
probably won't try to think harder about this for another couple of
days, but I have one immediate comment.
> + (defoptimizer (isqrt derive-type) ((x))
> + (let ((x-type (continuation-type x)))
> + (when (and (numeric-type-p x-type)
> + (csubtypep x-type (specifier-type '(integer 0))))
> + (let* ((min (numeric-type-low x-type))
> + (max (numeric-type-high x-type))
> + (min-isqrt (and min (isqrt min)))
> + (max-isqrt (and max (isqrt max))))
> + (specifier-type `(integer ,(or min-isqrt *) ,(or max-isqrt '*)))))))
In deriving the return type of ISQRT (unlike things like FLOOR) we
don't need to worry about which disjoint subtype of NUMBER (e.g.
SINGLE-FLOAT or INTEGER) we're seeing. We know we're seeing an
INTEGER. (Otherwise, in safe code there should be no return, but
a TYPE-ERROR; and in unsafe code, the result is undefined anyway,
so we can bombard the user with coconuts or whatever.)
I think instead I'd try something like (not even typed into the
REPL, much less *tested* or anything):
(defoptimizer (isqrt derive-type) ((x))
(let* ((x-type (continuation-type x)))
;; Project out the values which won't cause a TYPE-ERROR.
;; That way, if e.g. the programmer knows from global
;; considerations that it's a (MOD 256), but Python can
;; only infer that it's an (OR (MOD 256) NULL), we can
;; still derive good bounds on the return type (given that
;; ISQRT returns instead of signalling an error).
(valid-x-type (type-intersection x-type
(specifier-type '(integer 0)))))
;; Unless intersection was either too hairy to figure out, or
;; something degenerate like *EMPTY-TYPE*
(when (numeric-type-p valid-x-type)
;; Now we can calculate MIN, MAX-ISQRT, etc. from LOW and HIGH values.
William Harold Newman <william.newman@...>
Furious activity is no substitute for understanding. -- H. H. Williams
PGP key fingerprint 85 CE 1C BA 79 8D 51 8C B9 25 FB EE E0 C3 E5 7C