Text file src/internal/bytealg/lastindexbyte_amd64.s

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !plan9
     6  
     7  #include "go_asm.h"
     8  #include "textflag.h"
     9  
    10  TEXT	·LastIndexByte(SB), NOSPLIT, $0-40
    11  	MOVQ b_base+0(FP), SI
    12  	MOVQ b_len+8(FP), BX
    13  	MOVB c+24(FP), AL
    14  	LEAQ ret+32(FP), R8
    15  	JMP  lastindexbytebody<>(SB)
    16  
    17  TEXT	·LastIndexByteString(SB), NOSPLIT, $0-32
    18  	MOVQ s_base+0(FP), SI
    19  	MOVQ s_len+8(FP), BX
    20  	MOVB c+16(FP), AL
    21  	LEAQ ret+24(FP), R8
    22  	JMP  lastindexbytebody<>(SB)
    23  
    24  // input:
    25  //   SI: data
    26  //   BX: data len
    27  //   AL: byte sought
    28  //   R8: address to put result
    29  TEXT	lastindexbytebody<>(SB), NOSPLIT, $0
    30  	// Shuffle X0 around so that each byte contains
    31  	// the character we're looking for.
    32  	MOVD AX, X0
    33  	PUNPCKLBW X0, X0
    34  	PUNPCKLBW X0, X0
    35  	PSHUFL $0, X0, X0
    36  
    37  	CMPQ BX, $16
    38  	JLT small
    39  
    40  	CMPQ BX, $32
    41  	JA avx2
    42  sse:
    43  	LEAQ	-16(SI)(BX*1), DI	// DI = address of last (rightmost) 16-byte chunk
    44  	PCALIGN $16
    45  sseloop:
    46  	// Move the 16-byte chunk of data at DI into X1.
    47  	MOVOU	(DI), X1
    48  	// Compare bytes in X0 to X1.
    49  	PCMPEQB	X0, X1
    50  	// Take the top bit of each byte in X1 and put the result in DX.
    51  	PMOVMSKB X1, DX
    52  	// Find last set bit, if any.
    53  	BSRL	DX, DX
    54  	JNZ	ssesuccess
    55  	// Move to the chunk to the left.
    56  	SUBQ	$16, DI
    57  	CMPQ	DI, SI
    58  	JA	sseloop
    59  
    60  	// Search the leftmost 16-byte chunk, starting exactly at SI. This
    61  	// chunk may overlap with the chunks we've already searched, but
    62  	// that's ok.
    63  	MOVQ	SI, DI
    64  	MOVOU	(SI), X1
    65  	PCMPEQB	X0, X1
    66  	PMOVMSKB X1, DX
    67  	BSRL	DX, DX
    68  	JZ	failure
    69  
    70  // We've found a chunk containing the byte.
    71  // The chunk was loaded from DI.
    72  // The index of the matching byte in the chunk is DX.
    73  // The start of the data is SI.
    74  ssesuccess:
    75  	SUBQ SI, DI	// Compute offset of chunk within data.
    76  	ADDQ DX, DI	// Add offset of byte within chunk.
    77  	MOVQ DI, (R8)
    78  	RET
    79  
    80  failure:
    81  	MOVQ $-1, (R8)
    82  	RET
    83  
    84  // handle for lengths < 16
    85  small:
    86  	TESTQ	BX, BX
    87  	JEQ	failure
    88  
    89  	// Check if we'll load across a page boundary.
    90  	LEAQ	16(SI), AX
    91  	TESTW	$0xff0, AX
    92  	JEQ	endofpage
    93  
    94  	MOVOU	(SI), X1 // Load data
    95  	PCMPEQB	X0, X1	// Compare target byte with each byte in data.
    96  	PMOVMSKB X1, DX	// Move result bits to integer register.
    97  	// Bits past the end of the slice refer to memory outside the slice that
    98  	// happens to be on the same page. Discard them by shifting them out of
    99  	// the register and undo the shift once the highest valid set bit is
   100  	// found.
   101  	MOVL	$32, CX
   102  	SUBL	BX, CX
   103  	SHLL	CX, DX
   104  	BSRL	DX, DX	// Find last set bit.
   105  	JZ	failure	// No set bit, failure.
   106  	SUBL	CX, DX
   107  	MOVQ	DX, (R8)
   108  	RET
   109  
   110  endofpage:
   111  	MOVOU	-16(SI)(BX*1), X1	// Load data into the high end of X1.
   112  	PCMPEQB	X0, X1	// Compare target byte with each byte in data.
   113  	PMOVMSKB X1, DX	// Move result bits to integer register.
   114  	MOVL	BX, CX
   115  	SHLL	CX, DX
   116  	SHRL	$16, DX	// Shift desired bits down to bottom of register.
   117  	BSRL	DX, DX	// Find last set bit.
   118  	JZ	failure	// No set bit, failure.
   119  	MOVQ	DX, (R8)
   120  	RET
   121  
   122  avx2:
   123  #ifndef hasAVX2
   124  	CMPB   internal∕cpu·X86+const_offsetX86HasAVX2(SB), $1
   125  	JNE sse
   126  #endif
   127  	LEAQ -32(SI)(BX*1), DI	// DI = address of last (rightmost) 32-byte chunk
   128  	VPBROADCASTB  X0, Y1
   129  
   130  	PCALIGN $32
   131  avx2_loop:
   132  	VMOVDQU (DI), Y2
   133  	VPCMPEQB Y1, Y2, Y3
   134  	VPTEST Y3, Y3
   135  	JNZ avx2success
   136  	SUBQ $32, DI
   137  	CMPQ DI, SI
   138  	JA avx2_loop
   139  	MOVQ SI, DI
   140  	VMOVDQU (SI), Y2
   141  	VPCMPEQB Y1, Y2, Y3
   142  	VPTEST Y3, Y3
   143  	JZ avx2failure
   144  	JMP avx2success
   145  
   146  avx2failure:
   147  	VZEROUPPER
   148  	MOVQ $-1, (R8)
   149  	RET
   150  
   151  avx2success:
   152  	VPMOVMSKB Y3, DX
   153  	BSRL DX, DX
   154  	SUBQ SI, DI
   155  	ADDQ DI, DX
   156  	MOVQ DX, (R8)
   157  	VZEROUPPER
   158  	RET
   159  

View as plain text