Now you're
thinking with
PCMPISTRI!

SSE instructions for string operations

by David Turner
https://github.com/novalis/thinking-with-pcmpistri

Topological order

foo
foo/bar
foo/bar/baz
foo-fleem
foo-fleem/morx

Simple version



/* Put re-ordered character from 
 * string sa into ca */
if (*sa == '/')
    ca = 1;
else if (*sa > 0 && *sa < '/')
    ca = *sa + 1;
else
    ca = *sa;

  

Single Instruction
Multiple Data

SIMD

Some 128-bit SIMD registers

four 32-bit integers

eight 16-bit integers

sixteen bytes

movdqa (%rdi), %xmm0
paddusb (%rsi), %xmm0
movdqa %xmm0, (%rdi)





becomes

we already know this tune

movdqa (%rdi), %xmm0
paddusb (%rsi), %xmm0
movdqa %xmm0, (%rdi)

Constructing if-then-else


from


AND, XOR, and OR


if (*sa == '/')
    ca = 1;
else
    ca = *sa;
IF THEN ELSE
AND =
AND =
OR =
xmm7

xmm6

xmm5

xmm0

movdqa %xmm0, %xmm1
xmm1 before pcmpeqb


xmm7


xmm1 after pcmpeqb

xmm6


movdqa %xmm1, %xmm2
pand %xmm6, %xmm2

xmm2
xmm5

xmm1 before pxor


pxor %xmm5, %xmm1

xmm5 after pxor
xmm0 before pand


xmm1


pand %xmm1, %xmm0

xmm0 after pand
xmm0

xmm2


por %xmm2, %xmm0

xmm0, after por

PCMPISTRI

xmm0

xmm1

pcmpistri %xmm0, %xmm1
rcx register gets 7
carry flag and zero flag get set

pshufb


Thank you

https://github.com/novalis/thinking-with-pcmpistri

Tree image by Floyd Wilde, CC-BY-SA.

Brick wall image by HÃ¥kan Svensson (Xauxa), CC-BY-SA

Bonus slide

Extracting a byte

xmm5

add $1, %rcx
movq %rcx, %xmm4

paddb %xmm5, %xmm4

pshufb %xmm4, %xmm0
movq %xmm0, %rax