对于 uni 分配,我需要编写一个函数来计算汇编中字符串(由指针和索引定义)中的空格数。有一个使用要求pcmpeqb
(即使用 SSE 寄存器),以及使用popcnt
和的提示pmovmskb
。我的基本方法是处理 16 字节块中的字符串,将每个块加载到其中%xmm8
并将其与%xmm9
初始化为包含 16 个空格的块进行比较。但是,我需要以某种方式特别处理最后一块。
我的第一个想法是使用旋转指令删除字符串末尾的垃圾。(保证字符串在结束后分配了一些额外的空间以防止段错误,但那里的数据可能不应该用于比较。)我偶然发现,PSRLDQ
但它似乎不接受非直接参数。(或者至少拒绝了我扔给它的东西。)所以我的问题是:我怎样才能删除 SSE 寄存器的最后 X 个字节,而不会将它的一半归零,或者逐个字地这样做?(据我了解,它们上的大多数可用操作都可以。)
我的代码(模样板)目前看起来像这样 - 有问题的位在标签之后_last:
:
# === Arguments ===
# %rdi - char *input
# %rsi - size_t count
# === Temporaries ===
# %rdx - how many chars to process in final run
# %rcx - how many characters were "read" already
# %r8 - pop count of last iteration
# %r9
# %r11
# === SSE Temporaries ===
# %xmm8 - the chunk of the string being processed
# %xmm9 - 16 spaces
xor %rcx, %rcx
xor %rax, %rax
movdqu _spaces(%rip), %xmm9
_loop:
# set %rdx to number of characters left to process
mov %rsi, %rdx
sub %rcx, %rdx
# we've reached the end of the string
cmp %rdx, %rsi
jge _end
movdqu (%rdi, %rcx), %xmm8 # load chunk of string to process
add $16, %rcx
# less than 16 characters to process
cmp $16, %rdx
jg _last
_compare: #compare %xmm8 with spaces and add count of spaces to %eax
pcmpeqb %xmm9, %xmm8
pmovmskb %xmm8, %r8d
popcntl %r8d, %r8d
add %r8d, %eax
jmp _loop
_last: # last part of string, less than 16 chars
sub $16, %rdx
neg %rdx
# I need to delete possible garbage after the last chars
psrldq %edx, %xmm8
jmp _compare
_end:
ret
(那里的控制流可能仍然有问题,但我稍后会处理。)