[Openmcl-devel] Type-of and positive/negative integers

Stas Boukarev stassats at gmail.com
Thu Oct 24 04:38:11 PDT 2019


On Thu, Oct 24, 2019 at 10:44 AM Steven Nunez <steve_nunez at yahoo.com> wrote:
>
> Okay, having a specialised representation for positive integers makes sense as a further specialisation of INTEGER. I wish this was documented somewhere, a 1 minute google search did not turn up anything.
>
> I still have the problem of ensuring that the parameters to a function, a sequence and a number, are all of the same type so I can dispatch on specialised (typed declared) versions of loop. At the moment I have:
>
>   (assert (and (plusp (length coefficients))
>                (every (lambda (elt)
>                          (typep elt (type-of x)))
>                        coefficients)))
>
> which works fine for single-float and double-float, but fails if any of the coefficients are negative because their type-of is FIXNUM whilst the X or other coefficients may be (INTEGER 0 4611686018427387903)
>
> Any ideas?
Are you doing it for performance? Any performance gains you get from
declaring your variables in a loop will be destroyed by performing
typep at runtime.
The cost of determining type-of, parsing it and applying typep on it
is going to be very high, especially if it's done on every element of
a sequence.
If you do need to perform that operation, you can do
(defun foo (x sequence)
  (macrolet ((make-test (x types)
               `(etypecase ,x
                  ,@(loop for type in types
                          collect `(,type (lambda (x) (typep x ',type)))))))
    (every (make-test x (double-float single-float fixnum))
           sequence)))

Maybe even putting EVERY inside the expansion, to get better inlining.
And handle specialized arrays without going through each element.



More information about the Openmcl-devel mailing list