spirv: int.lisp

semantics of SPIR-V's integer, bit, logical and select operations.

OpIAdd source

(defspv OpIAdd (a b) (:int (w) (wrap w (+ a b))))

OpISub source

(defspv OpISub (a b) (:int (w) (wrap w (- a b))))

OpIMul source

(defspv OpIMul (a b) (:int (w) (wrap w (* a b))))

OpSNegate source

(defspv OpSNegate (a) (:int (w) (wrap w (- (sext w a)))))

OpUDiv source

(defspv OpUDiv (a b)
  "Behavior is undefined if Operand 2 is 0."
  (:int (w) (if (= b 0) (spv-ub) (floor a b))))

OpUMod source

(defspv OpUMod (a b)
  "Behavior is undefined if Operand 2 is 0."
  (:int (w) (if (= b 0) (spv-ub) (mod a b))))

OpSDiv source

(defspv OpSDiv (a b)
  "Rounds toward 0. Behavior is undefined if Operand 2 is 0, or if Operand 2
   is -1 and Operand 1 is the minimum representable value."
  (:int (w)
    (if (or (= b 0) (and (= a (ash 1 (1- w))) (= b (ones w))))
        (spv-ub)
        (wrap w (truncate (sext w a) (sext w b))))))

OpSRem source

(defspv OpSRem (a b)
  "The sign of a non-0 result comes from Operand 1. Undefined as OpSDiv."
  (:int (w)
    (if (or (= b 0) (and (= a (ash 1 (1- w))) (= b (ones w))))
        (spv-ub)
        (wrap w (rem (sext w a) (sext w b))))))

OpSMod source

(defspv OpSMod (a b)
  "The sign of a non-0 result comes from Operand 2. Undefined as OpSDiv."
  (:int (w)
    (if (or (= b 0) (and (= a (ash 1 (1- w))) (= b (ones w))))
        (spv-ub)
        (wrap w (mod (sext w a) (sext w b))))))

OpIAddCarry source

struct results: member 0 is the most significant (sem.lisp)
(defspv OpIAddCarry (a b)
  "Result Type is a struct of two members of the operands' type: the low
   bits of the sum, then the carry (0 or 1)."
  (:int (w) (spv-members (wrap w (+ a b)) (carry-out w a b))))

OpISubBorrow source

(defspv OpISubBorrow (a b)
  "The low bits of a - b, then 1 if b > a (unsigned) else 0."
  (:int (w) (spv-members (wrap w (- a b)) (b2i (> b a)))))

OpUMulExtended source

(defspv OpUMulExtended (a b)
  "The low then the high half of the unsigned product."
  (:int (w) (spv-members (wrap w (* a b)) (bit-field (* a b) w w))))

OpSMulExtended source

(defspv OpSMulExtended (a b)
  "The low then the high half of the signed product."
  (:int (w) (spv-members (wrap w (* (sext w a) (sext w b)))
                         (bit-field (* (sext w a) (sext w b)) w w))))

OpBitwiseAnd source

(defspv OpBitwiseAnd (a b) (:int (w) (logand a b)))

OpBitwiseOr source

(defspv OpBitwiseOr (a b) (:int (w) (logior a b)))

OpBitwiseXor source

(defspv OpBitwiseXor (a b) (:int (w) (logxor a b)))

OpNot source

(defspv OpNot (a) (:int (w) (wrap w (lognot a))))

OpShiftLeftLogical source

Shift may have another width than Base; it is read unsigned.
(defspv OpShiftLeftLogical (base shift)
  "The result is undefined if Shift is greater than or equal to the bit
   width of the components of Base."
  (:int (w) (if (>= shift w) (spv-undef w) (wrap w (ash base shift)))))

OpShiftRightLogical source

(defspv OpShiftRightLogical (base shift)
  "Fills with 0. Undefined result for Shift >= the width."
  (:int (w) (if (>= shift w) (spv-undef w) (ash base (- shift)))))

OpShiftRightArithmetic source

(defspv OpShiftRightArithmetic (base shift)
  "Fills with the sign bit. Undefined result for Shift >= the width."
  (:int (w) (if (>= shift w) (spv-undef w) (wrap w (ash (sext w base) (- shift))))))

OpBitFieldInsert source

Offset and Count are scalars even for a vector Base, read unsigned.
(defspv OpBitFieldInsert (base insert offset count)
  "Bits [Offset, Offset+Count) of Base replaced by the low Count bits of
   Insert. Count 0 gives Base. The resulting value is undefined if Count or
   Offset or their sum is greater than the number of bits in the result."
  (:int (w)
    (if (> (+ offset count) w)
        (spv-undef w)
        (dpb insert (byte count offset) base))))

OpBitFieldUExtract source

(defspv OpBitFieldUExtract (base offset count)
  "Bits [Offset, Offset+Count) of Base, zero-extended. Count 0 gives 0.
   Undefined result if Count, Offset or their sum exceeds the width."
  (:int (w)
    (if (> (+ offset count) w)
        (spv-undef w)
        (bit-field base offset count))))

OpBitFieldSExtract source

(defspv OpBitFieldSExtract (base offset count)
  "Bits [Offset, Offset+Count) of Base, sign-extended from bit
   Offset+Count-1. Count 0 gives 0. Undefined result as UExtract."
  (:int (w)
    (if (> (+ offset count) w)
        (spv-undef w)
        (let ((f (bit-field base offset count)))
          (if (and (> count 0) (logbitp (1- count) f))
              (wrap w (- f (ash 1 count)))
              f)))))

OpBitReverse source

(defspv OpBitReverse (base) (:int (w) (spv-bitrev w base)))

OpBitCount source

(defspv OpBitCount (base)
  "The number of 1 bits; the result components may be of another width."
  (:int (w rw) (popcnt w base)))

OpIEqual source

(defspv OpIEqual (a b) (:int (w) (b2i (= a b))))

OpINotEqual source

(defspv OpINotEqual (a b) (:int (w) (b2i (/= a b))))

OpUGreaterThan source

(defspv OpUGreaterThan (a b) (:int (w) (b2i (> a b))))

OpUGreaterThanEqual source

(defspv OpUGreaterThanEqual (a b) (:int (w) (b2i (>= a b))))

OpULessThan source

(defspv OpULessThan (a b) (:int (w) (b2i (< a b))))

OpULessThanEqual source

(defspv OpULessThanEqual (a b) (:int (w) (b2i (<= a b))))

OpSGreaterThan source

(defspv OpSGreaterThan (a b) (:int (w) (b2i (> (sext w a) (sext w b)))))

OpSGreaterThanEqual source

(defspv OpSGreaterThanEqual (a b) (:int (w) (b2i (>= (sext w a) (sext w b)))))

OpSLessThan source

(defspv OpSLessThan (a b) (:int (w) (b2i (< (sext w a) (sext w b)))))

OpSLessThanEqual source

(defspv OpSLessThanEqual (a b) (:int (w) (b2i (<= (sext w a) (sext w b)))))

OpLogicalEqual source

(defspv OpLogicalEqual (a b) (:bool (w) (b2i (= a b))))

OpLogicalNotEqual source

(defspv OpLogicalNotEqual (a b) (:bool (w) (b2i (/= a b))))

OpLogicalOr source

(defspv OpLogicalOr (a b) (:bool (w) (logior a b)))

OpLogicalAnd source

(defspv OpLogicalAnd (a b) (:bool (w) (logand a b)))

OpLogicalNot source

(defspv OpLogicalNot (a) (:bool (w) (logxor a 1)))

OpAny source

(defspv OpAny (vector)
  "1 if any component is true"
  (:expand (rt ts lits)
    (declare (ignore rt lits))
    `(b2i (/= vector 0))))

OpAll source

(defspv OpAll (vector)
  "1 if every component is true"
  (:expand (rt ts lits)
    (declare (ignore rt lits))
    `(b2i (= vector ,(ones (spv-type-bits (first ts)))))))

OpSelect source

(defspv OpSelect (condition object1 object2)
  "A scalar Condition picks a whole object (of any type). A vector
   Condition (SPIR-V 1.4) picks per component between vectors."
  (:expand (rt ts lits)
    (declare (ignore lits))
    (if (= (nth-value 2 (spv-comp (first ts))) 1)
        `(if (= condition 1) object1 object2)
        (spv-componentwise-form '(condition object1 object2) ts rt '(w rw n)
                                '(if (= condition 1) object1 object2)))))

OpCopyObject source

(defspv OpCopyObject (operand)
  (:expand (rt ts lits) (declare (ignore rt ts lits)) 'operand))

OpUConvert source

(defspv OpUConvert (unsigned-value)
  "Zero-extends or truncates to the result's width (which must differ)."
  (:int (w rw) :require (/= w rw) (wrap rw unsigned-value)))

OpSConvert source

(defspv OpSConvert (signed-value)
  "Sign-extends or truncates to the result's width (which must differ)."
  (:int (w rw) :require (/= w rw) (wrap rw (sext w signed-value))))

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