如何提取位于AL中定义的索引位置的字节

时间:2013-11-17 14:55:39

标签: assembly x86-64 avx avx2

问题陈述:需要从ymm0中提取位于其值为寄存器AL的位置的字节。

我的方法:(相当难看):

        ; Set XMM1 to be a "shift one byte by right" mask
        ; XMM1 : 000F0E0D0C0B0A090807060504030201

        cmp al,15   ; check if in lower xmmword of ymm0 or higher
        ja  is_in_higher
        xor CX,CX
        mov CL,AL
    loop_for_next :
       vpextrb edx,ymm0,ymm0,0
       vpshufb xmm0,xmm0,xmm1  ; right shifts xmm0 as mask
       loop loop_for_next
    ..
    is_in_higher :
        vperm2i128 ymm0,ymm0,ymm0,01 ; swaps upper 128 to lower 128
    jmp loop_for_next

有更优雅的方式吗?任何建议表示赞赏。挑战的关键在于VPEXTRB仅采用立即索引值,而不是CL(或AL)寄存器作为索引值

...谢谢

2 个答案:

答案 0 :(得分:0)

你的代码需要AVX2(vperm2i128)而我无法测试它,因为我只有AVX。无论如何,您的代码使用循环来执行不需要循环的任务。我的解决方案使用简单的查找表和vpshufb(需要SSSE3)指令来重新排序字节。在YASM中测试过。

以下是代码:

[bits 64]

section .text
global _start

_start:

set_example_values:
        mov     al,0x1e                  ; byte index: 0...31, 0x00...0x1f
        vmovaps ymm0,[example_data]      ; define the data

code_starts_here:
        cmp     al,15
        jna     no_need_to_reorder_octalwords

        vperm2f128 ymm0,ymm0,ymm0,0x81   ; reorder ymm0. zero top 16 bytes.

no_need_to_reorder_octalwords:
        and     eax,15
        shl     eax,4
        vmovaps xmm1,[rax+shuffle_table] ; each byte is an index, f0 = set to 0.
        vpshufb xmm0,xmm1                ; copy the right byte to byte 0 of xmm0.
                                         ; zero the rest bytes of xmm0.

        movq    rdx,xmm0                 ; copy to rdx.

        ...

.data
align 32
;                  f e d c b a 9 8 7 6 5 4 3 2 1 0
example_data do 0xafaeadacabaaa9a8a7a6a5a4a3a2a1a0
;                 1f1e1d1c1b1a19181716151413121110
             do 0xbfbebdbcbbbab9b8b7b6b5b4b3b2b1b0

shuffle_table   dd 0xf0f0f000, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f001, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f002, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f003, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f004, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f005, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f006, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f007, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f008, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f009, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f00a, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f00b, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f00c, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f00d, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f00e, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0
                dd 0xf0f0f00f, 0xf0f0f0f0, 0xf0f0f0f0, 0xf0f0f0f0

答案 1 :(得分:0)

虽然我不确定但可能是这样的:

and         eax,  0x0000001F    // eax  = [al & 31, 0, 0, 0] 
or          eax,  0x80808000    // eax  = [al & 31, 0x80, 0x80, 0x80]
vmovd       xmm1, eax           // ymm1 = [eax, 0, 0, 0, 0, 0, 0, 0]
vpshufb     ymm0, ymm0, ymm1    // ... 
vmovd       eax,  xmm0          // eax  = [ymm0.byte[al & 31], 0, 0, 0]

从位置al的ymm0提取的字节存储在eax中。