Skip to content

VOP Example: SSE ADD

Masataro Asai edited this page Feb 21, 2018 · 1 revision

Code from http://keens.github.io/blog/2014/12/02/vopdeyou-bu/

(defknown simd-add ((simd-pack (unsigned-byte 64)) (simd-pack (unsigned-byte 64)))
    (simd-pack (unsigned-byte 32))
    (movable flushable always-translatable)
  :overwrite-fndb-silently t)
(in-package "SB-VM")
(define-vop (vop-sample::simd-add)
  (:translate vop-sample::simd-add)
  (:policy :fast-safe)
  (:args (x :scs (int-sse-reg))
         (y :scs (int-sse-reg)))
  (:arg-types simd-pack-int simd-pack-int)
  (:results (r :scs (int-sse-reg)))
  (:result-types simd-pack-int)
  (:generator 4
              (move r x)
              (inst padddw r y)))

(in-package :vop-sample)
(defun simd-add (x y)
  (simd-add x y))

Ideas:

  • I believe I can write a deftransform for a + function that has more than 4 additions. It compiles (+ a b c d) into SIMD addition of (+ a b) and (+ c d), then summing the result.

  • (setf (values x y) (values (+ a b) (+ c d))) is also a good candidate for conveniently using SIMD functions.