spirv: sem.lisp

DEFSPV: SPIR-V instruction semantics in the core vocabulary.

Other definitions

(defvar *spv-undefined*) source
(defvar *spv-undefined* 0
  "Reference evaluation only: set to 1 when an undefined value was produced
   (SPV-UNDEF), to 2 when behaviour was undefined (SPV-UB). Tests reset it.")
(defun spv-undef) source
(defun spv-undef (w)
  "Any (unsigned-byte W): SPIR-V's 'the result is undefined'. The reference
   evaluator gives 0."
  (declare (type (unsigned-byte 16) w) (ignorable w))
  (when (= *spv-undefined* 0) (setq *spv-undefined* 1))
  0)
(defun spv-ub) source
(defun spv-ub ()
  "Undefined behaviour: SPIR-V's 'behavior is undefined'. Nothing is
   guaranteed, not even a value. The reference evaluator gives 0."
  (setq *spv-undefined* 2)
  0)
(defun spv-either) source
(defun spv-either (x y)
  "Either X or Y, at the implementation's choice. The reference gives X."
  (declare (ignorable y))
  x)
(defun spv-ulp) source
(defun spv-ulp (w tenths x)
  "Any W-bit float within TENTHS/10 ULP of X (bits). The reference gives X."
  (declare (type (unsigned-byte 16) w tenths) (ignorable w tenths))
  x)
(defun spv-inexact) source
(defun spv-inexact (w x)
  "A W-bit float whose error from X (bits) Vulkan bounds only by derivation
   from other operations ('inherited'), or not at all. The reference gives X."
  (declare (type (unsigned-byte 16) w) (ignorable w))
  x)
(defmacro spv-fzsign) source
(defmacro spv-fzsign (w x)
  (let ((v (gensym "V")))
    `(let ((,v ,x))
       (if (fzero-p ,(spv-fmt w) ,v) (spv-either ,v (fneg ,(spv-fmt w) ,v)) ,v))))
(defmacro spv-vmap) source
(defmacro spv-vmap (n rw widths fn &rest vectors)
  (if (and (= (* n rw) 128) (every (lambda (w) (= w rw)) widths))
      `(vmap ,rw ,fn ,@vectors)
      (let ((vars (mapcar (lambda (v) (declare (ignore v)) (gensym "V")) vectors)))
        `(let ,(mapcar #'list vars vectors)
           (logior
            ,@(loop for k below n
                    collect `(ash (ldb (byte ,rw 0)
                                       (let ,(mapcar (lambda (p v w) `(,p (ldb (byte ,w ,(* w (- n 1 k))) ,v)))
                                                     (cadr fn) vars widths)
                                         (declare (type (unsigned-byte ,(reduce #'max widths)) ,@(cadr fn))
                                                  (ignorable ,@(cadr fn)))
                                         ,@(cddr fn)))
                                  ,(* rw (- n 1 k)))))))))
(defun spv-bitrev) source
(defun spv-bitrev (n x)
  (declare (type (unsigned-byte 8) n))
  (let ((r 0))
    (dotimes (i n r)
      (when (logbitp i x) (setq r (logior r (ash 1 (- n 1 i))))))))
(defmacro defspv) source
(defmacro defspv (op params &rest clauses)
  (multiple-value-bind (name set) (spv-op-designator op)
    (let* ((inst (spv-find-inst name set))
           (named (spv-check-params inst params))
           (doc (and (stringp (car clauses)) (pop clauses)))
           (expand (assoc :expand clauses)))
      `(eval-when (:compile-toplevel :load-toplevel :execute)
         (setf (spvi-sem (spv-find-inst ',name ,set))
               (make-spv-sem :name ,(spvi-name inst) :params ',named :doc ,doc
                             :clauses ',(remove :expand clauses :key #'car)
                             :expander ,(and expand `(lambda ,@(cdr expand)))))
         ',name))))
(defmacro spv-eval) source
(defmacro spv-eval ((op rtype &rest keys) &rest args)
  `(,(apply #'spv-sem-lambda op rtype keys) ,@args))
(defmacro define-spv-fn) source
(defmacro define-spv-fn (name (op rtype &rest keys))
  (let ((l (apply #'spv-sem-lambda op rtype keys)))
    `(defun ,name ,@(cdr l))))

Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.