spirv: flt.lisp

semantics of SPIR-V's floating-point arithmetic, comparisons, conversions.

OpFAdd source

(defspv OpFAdd (a b) "Correctly rounded." (:float (w) (fop w (a b) (fadd (ffmt w) :rne a b))))

OpFSub source

(defspv OpFSub (a b) "Correctly rounded." (:float (w) (fop w (a b) (fsub (ffmt w) :rne a b))))

OpFMul source

(defspv OpFMul (a b) "Correctly rounded." (:float (w) (fop w (a b) (fmul (ffmt w) :rne a b))))

OpFDiv source

(defspv OpFDiv (a b)
  "Within 2.5 ULP. A non-zero over a zero is the signed infinity, exactly."
  (:float (w) (fop w (a b) (fdiv (ffmt w) :rne a b) :ulp 25
                   :guard ((fzero-p (ffmt w) b) (fnonfinite w (fdiv (ffmt w) :rne a b))))))

OpFRem source

(defspv OpFRem (a b)
  "a - b * trunc(a / b), Vulkan's definition (precision inherited from
   those operations): the sign of a non-0 result comes from Operand 1. The
   result is undefined if Operand 2 is 0."
  (:float (w)
    (fop w (a b)
         (fsub (ffmt w) :rne a (fmul (ffmt w) :rne b (fround-int (ffmt w) :rtz (fdiv (ffmt w) :rne a b))))
         :inexact t :guard ((fzero-p (ffmt w) b) (spv-undef w)))))

OpFMod source

(defspv OpFMod (a b)
  "a - b * floor(a / b): the sign of a non-0 result comes from Operand 2.
   Precision inherited. The result is undefined if Operand 2 is 0."
  (:float (w)
    (fop w (a b)
         (fsub (ffmt w) :rne a (fmul (ffmt w) :rne b (fround-int (ffmt w) :rdn (fdiv (ffmt w) :rne a b))))
         :inexact t :guard ((fzero-p (ffmt w) b) (spv-undef w)))))

OpFNegate source

(defspv OpFNegate (a)
  "Inverts the sign bit (still a float operation: denormals, NaNs)."
  (:float (w) (fop w (a) (fneg (ffmt w) a))))

OpFOrdEqual source

(defspv OpFOrdEqual (a b) (:float (w) (fop w (a b) (feq (ffmt w) a b) :result :bool)))

OpFUnordEqual source

(defspv OpFUnordEqual (a b)
  (:float (w) (fop w (a b) (logior (funord (ffmt w) a b) (feq (ffmt w) a b)) :result :bool)))

OpFOrdNotEqual source

(defspv OpFOrdNotEqual (a b)
  (:float (w) (fop w (a b) (- 1 (logior (funord (ffmt w) a b) (feq (ffmt w) a b))) :result :bool)))

OpFUnordNotEqual source

(defspv OpFUnordNotEqual (a b) (:float (w) (fop w (a b) (- 1 (feq (ffmt w) a b)) :result :bool)))

OpFOrdLessThan source

(defspv OpFOrdLessThan (a b) (:float (w) (fop w (a b) (flt (ffmt w) a b) :result :bool)))

OpFUnordLessThan source

(defspv OpFUnordLessThan (a b)
  (:float (w) (fop w (a b) (logior (funord (ffmt w) a b) (flt (ffmt w) a b)) :result :bool)))

OpFOrdGreaterThan source

(defspv OpFOrdGreaterThan (a b) (:float (w) (fop w (a b) (flt (ffmt w) b a) :result :bool)))

OpFUnordGreaterThan source

(defspv OpFUnordGreaterThan (a b)
  (:float (w) (fop w (a b) (logior (funord (ffmt w) a b) (flt (ffmt w) b a)) :result :bool)))

OpFOrdLessThanEqual source

(defspv OpFOrdLessThanEqual (a b) (:float (w) (fop w (a b) (fle (ffmt w) a b) :result :bool)))

OpFUnordLessThanEqual source

(defspv OpFUnordLessThanEqual (a b)
  (:float (w) (fop w (a b) (logior (funord (ffmt w) a b) (fle (ffmt w) a b)) :result :bool)))

OpFOrdGreaterThanEqual source

(defspv OpFOrdGreaterThanEqual (a b) (:float (w) (fop w (a b) (fle (ffmt w) b a) :result :bool)))

OpFUnordGreaterThanEqual source

(defspv OpFUnordGreaterThanEqual (a b)
  (:float (w) (fop w (a b) (logior (funord (ffmt w) a b) (fle (ffmt w) b a)) :result :bool)))

OpIsNan source

(defspv OpIsNan (x)
  "Under :nan :fast an implementation may assume there are no NaNs."
  (:float (w) (fop w (x) (b2i (fnan-p (ffmt w) x)) :result :bool)))

OpIsInf source

(defspv OpIsInf (x)
  (:float (w) (fop w (x) (b2i (finf-p (ffmt w) x)) :result :bool)))

OpConvertFToU source

(defspv OpConvertFToU (float-value)
  "Rounds toward 0. Behavior is undefined if the result type is not wide
   enough to hold the converted value (and for an infinity or NaN): the
   conversion saturated one past each end tells it."
  (:float (w rw)
    (fop w (float-value)
         (let ((i (fcvt-to-int (ffmt w) :rtz float-value -1 (ash 1 rw))))
           (if (<= 0 i (ones rw)) i (spv-ub)))
         :result :int :rw rw
         :guard ((or (finf-p (ffmt w) float-value) (fnan-p (ffmt w) float-value)) (spv-ub)))))

OpConvertFToS source

(defspv OpConvertFToS (float-value)
  "Rounds toward 0. Behavior is undefined if the result type is not wide
   enough to hold the converted value (and for an infinity or NaN)."
  (:float (w rw)
    (fop w (float-value)
         (let ((i (fcvt-to-int (ffmt w) :rtz float-value (- -1 (ash 1 (1- rw))) (ash 1 (1- rw)))))
           (if (<= (- (ash 1 (1- rw))) i (1- (ash 1 (1- rw)))) (wrap rw i) (spv-ub)))
         :result :int :rw rw
         :guard ((or (finf-p (ffmt w) float-value) (fnan-p (ffmt w) float-value)) (spv-ub)))))

OpConvertSToF source

(defspv OpConvertSToF (signed-value)
  "Correctly rounded (to nearest even)."
  (:int (w rw) (int-to-f (ffmt rw) :rne (sext w signed-value))))

OpConvertUToF source

(defspv OpConvertUToF (unsigned-value)
  "Correctly rounded (to nearest even)."
  (:int (w rw) (int-to-f (ffmt rw) :rne unsigned-value)))

OpFConvert source

(defspv OpFConvert (float-value)
  "To another float width: exact when widening, correctly rounded when
   narrowing."
  (:float (w rw) :require (/= w rw)
    (fop w (float-value) (fcvt (ffmt rw) (ffmt w) :rne float-value) :rw rw)))

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