Skip to content

Commit

Permalink
Switch to SSE from AVX
Browse files Browse the repository at this point in the history
  • Loading branch information
Khepu committed Mar 28, 2024
1 parent 533ab97 commit 77b5ca8
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions jsoon.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

(defparameter *mock* " { \"my-data\": 123}")

(defconstant +chunk-length+ (the fixnum 32))
(defconstant +chunk-length+ (the fixnum 16))

(define-symbol-macro +space+ (sb-simd-avx2:u8.32 (char-code #\space)))
(define-symbol-macro +tab+ (sb-simd-avx2:u8.32 (char-code #\tab)))
(define-symbol-macro +newline+ (sb-simd-avx2:u8.32 (char-code #\newline)))
(define-symbol-macro +double-quote+ (sb-simd-avx2:u8.32 (char-code #\")))
(define-symbol-macro +backslash+ (sb-simd-avx2:u8.32 (char-code #\\)))
(define-symbol-macro +comma+ (sb-simd-avx2:u8.32 (char-code #\,)))
(define-symbol-macro +closing-bracket+ (sb-simd-avx2:u8.32 (char-code #\])))
(define-symbol-macro +closing-brace+ (sb-simd-avx2:u8.32 (char-code #\})))
(define-symbol-macro +space+ (sb-simd-sse4.2:u8.16 (char-code #\space)))
(define-symbol-macro +tab+ (sb-simd-sse4.2:u8.16 (char-code #\tab)))
(define-symbol-macro +newline+ (sb-simd-sse4.2:u8.16 (char-code #\newline)))
(define-symbol-macro +double-quote+ (sb-simd-sse4.2:u8.16 (char-code #\")))
(define-symbol-macro +backslash+ (sb-simd-sse4.2:u8.16 (char-code #\\)))
(define-symbol-macro +comma+ (sb-simd-sse4.2:u8.16 (char-code #\,)))
(define-symbol-macro +closing-bracket+ (sb-simd-sse4.2:u8.16 (char-code #\])))
(define-symbol-macro +closing-brace+ (sb-simd-sse4.2:u8.16 (char-code #\})))

(deftype string-chunk ()
`(array (unsigned-byte 8) (,+chunk-length+)))
Expand All @@ -41,27 +41,28 @@
(declaim (inline rightmost-bit unset-rightmost-bit chunk
%unescape-char next-offset high-surrogate-p %parse-surrogate surrogate-char
skip-to-next-character %parse-decimal-segment %parse-exponent-segment
not-whitespace-p end-of-number %parse-number))
not-whitespace-p %parse-number))

(defmacro chunk= (chunk value)
`(let ((value-mask (sb-simd-avx2:u8.32= (the (sb-ext:simd-pack-256 (unsigned-byte 8)) ,chunk)
(the (sb-ext:simd-pack-256 (unsigned-byte 8)) ,value))))
(declare (type (sb-ext:simd-pack-256 (unsigned-byte 8)) value-mask))
`(let ((value-mask (sb-simd-sse4.2:u8.16= (the (sb-ext:simd-pack (unsigned-byte 8)) ,chunk)
(the (sb-ext:simd-pack (unsigned-byte 8)) ,value))))
(declare (type (sb-ext:simd-pack (unsigned-byte 8)) value-mask))
value-mask))

(defmacro chunk=->bm (chunk value)
"Compare chunks and convert to bitmap"
`(sb-simd-avx2:u8.32-movemask (chunk= ,chunk ,value)))
`(sb-simd-sse4.2:u8.16-movemask (chunk= ,chunk ,value)))

(defmacro chunk/= (chunk value)
`(let ((value-mask (sb-simd-avx2:u8.32/= (the (sb-ext:simd-pack-256 (unsigned-byte 8)) ,chunk)
(the (sb-ext:simd-pack-256 (unsigned-byte 8)) ,value))))
(declare (type (sb-ext:simd-pack-256 (unsigned-byte 8)) value-mask))
`(let ((value-mask (sb-simd-sse4.2:u8.16-not
(sb-simd-sse4.2:u8.16= (the (sb-ext:simd-pack (unsigned-byte 8)) ,chunk)
(the (sb-ext:simd-pack (unsigned-byte 8)) ,value)))))
(declare (type (sb-ext:simd-pack (unsigned-byte 8)) value-mask))
value-mask))

(defmacro pack (chunk)
`(the (sb-ext:simd-pack-256 (unsigned-byte 8))
(sb-simd-avx2:u8.32-aref (the string-chunk ,chunk) 0)))
`(the (sb-ext:simd-pack (unsigned-byte 8))
(sb-simd-sse4.2:u8.16-aref (the string-chunk ,chunk) 0)))

(defun rightmost-bit (n)
(declare (type fixnum n))
Expand Down Expand Up @@ -110,11 +111,11 @@
(tab-mask (chunk/= chunk +tab+))
(newline-mask (chunk/= chunk +newline+))
;; Equality checks with simd will produce unsigned results of the same length
(whitespace-pack (sb-simd-avx2:u8.32-and space-mask
tab-mask
newline-mask))
(whitespace-pack (sb-simd-sse4.2:u8.16-and space-mask
tab-mask
newline-mask))
;; The produced mask is backwards
(whitespace-bitmap (sb-simd-avx2:u8.32-movemask whitespace-pack)))
(whitespace-bitmap (sb-simd-sse4.2:u8.16-movemask whitespace-pack)))
(declare (type fixnum whitespace-bitmap))
(if (zerop whitespace-bitmap)
(incf index +chunk-length+)
Expand Down Expand Up @@ -309,7 +310,8 @@ first character after the escaped sequence."
(type fixnum index integer))
(multiple-value-bind (decimal current-index)
(parse-integer string :start index :junk-allowed t)
(declare (type fixnum decimal current-index))
(declare (type fixnum decimal current-index)
(dynamic-extent decimal))
(let ((number (+ integer (/ decimal (expt 10.0d0 (- current-index index))))))
(declare (type double-float number))
(if (and current-index (char= #\e (char string current-index)))
Expand Down

0 comments on commit 77b5ca8

Please sign in to comment.