semantics of SPIR-V's integer, bit, logical and select operations.
(defspv OpIAdd (a b) (:int (w) (wrap w (+ a b))))
(defspv OpISub (a b) (:int (w) (wrap w (- a b))))
(defspv OpIMul (a b) (:int (w) (wrap w (* a b))))
(defspv OpSNegate (a) (:int (w) (wrap w (- (sext w a)))))
(defspv OpUDiv (a b)
"Behavior is undefined if Operand 2 is 0."
(:int (w) (if (= b 0) (spv-ub) (floor a b))))
(defspv OpUMod (a b)
"Behavior is undefined if Operand 2 is 0."
(:int (w) (if (= b 0) (spv-ub) (mod a b))))
(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))))))
(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))))))
(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))))))
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))))
(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)))))
(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))))
(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))))
(defspv OpBitwiseAnd (a b) (:int (w) (logand a b)))
(defspv OpBitwiseOr (a b) (:int (w) (logior a b)))
(defspv OpBitwiseXor (a b) (:int (w) (logxor a b)))
(defspv OpNot (a) (:int (w) (wrap w (lognot a))))
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)))))
(defspv OpShiftRightLogical (base shift)
"Fills with 0. Undefined result for Shift >= the width."
(:int (w) (if (>= shift w) (spv-undef w) (ash base (- shift)))))
(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))))))
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))))
(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))))
(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)))))
(defspv OpBitReverse (base) (:int (w) (spv-bitrev w base)))
(defspv OpBitCount (base)
"The number of 1 bits; the result components may be of another width."
(:int (w rw) (popcnt w base)))
(defspv OpIEqual (a b) (:int (w) (b2i (= a b))))
(defspv OpINotEqual (a b) (:int (w) (b2i (/= a b))))
(defspv OpUGreaterThan (a b) (:int (w) (b2i (> a b))))
(defspv OpUGreaterThanEqual (a b) (:int (w) (b2i (>= a b))))
(defspv OpULessThan (a b) (:int (w) (b2i (< a b))))
(defspv OpULessThanEqual (a b) (:int (w) (b2i (<= a b))))
(defspv OpSGreaterThan (a b) (:int (w) (b2i (> (sext w a) (sext w b)))))
(defspv OpSGreaterThanEqual (a b) (:int (w) (b2i (>= (sext w a) (sext w b)))))
(defspv OpSLessThan (a b) (:int (w) (b2i (< (sext w a) (sext w b)))))
(defspv OpSLessThanEqual (a b) (:int (w) (b2i (<= (sext w a) (sext w b)))))
(defspv OpLogicalEqual (a b) (:bool (w) (b2i (= a b))))
(defspv OpLogicalNotEqual (a b) (:bool (w) (b2i (/= a b))))
(defspv OpLogicalOr (a b) (:bool (w) (logior a b)))
(defspv OpLogicalAnd (a b) (:bool (w) (logand a b)))
(defspv OpLogicalNot (a) (:bool (w) (logxor a 1)))
(defspv OpAny (vector)
"1 if any component is true"
(:expand (rt ts lits)
(declare (ignore rt lits))
`(b2i (/= vector 0))))
(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)))))))
(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)))))
(defspv OpCopyObject (operand)
(:expand (rt ts lits) (declare (ignore rt ts lits)) 'operand))
(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)))
(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.