Skip to content
Myr

indexbyte2_arm64.s

This file

Path

src/algo/indexbyte2_arm64.s

249 lines · 6.2 KiB

Last changed by

Clean up non-ascii charactersJunegunn Choibefore adoption8b66489

History of this file

Take it

1#include "textflag.h"23// func IndexByteTwo(s []byte, b1, b2 byte) int4//5// Returns the index of the first occurrence of b1 or b2 in s, or -1.6// Uses ARM64 NEON to search for both bytes in a single pass over the data.7// Adapted from Go's internal/bytealg/indexbyte_arm64.s (single-byte version).8TEXT ·IndexByteTwo(SB),NOSPLIT,$0-409	MOVD	s_base+0(FP), R010	MOVD	s_len+8(FP), R211	MOVBU	b1+24(FP), R112	MOVBU	b2+25(FP), R713	MOVD	$ret+32(FP), R81415	// Core algorithm:16	// For each 32-byte chunk we calculate a 64-bit syndrome value,17	// with two bits per byte. We compare against both b1 and b2,18	// OR the results, then use the same syndrome extraction as19	// Go's IndexByte.2021	CBZ	R2, fail22	MOVD	R0, R1123	// Magic constant 0x40100401 allows us to identify which lane matches.24	// Each byte in the group of 4 gets a distinct bit: 1, 4, 16, 64.25	MOVD	$0x40100401, R526	VMOV	R1, V0.B16    // V0 = splat(b1)27	VMOV	R7, V7.B16    // V7 = splat(b2)28	// Work with aligned 32-byte chunks29	BIC	$0x1f, R0, R330	VMOV	R5, V5.S431	ANDS	$0x1f, R0, R932	AND	$0x1f, R2, R1033	BEQ	loop3435	// Input string is not 32-byte aligned. Process the first36	// aligned 32-byte block and mask off bytes before our start.37	VLD1.P	(R3), [V1.B16, V2.B16]38	SUB	$0x20, R9, R439	ADDS	R4, R2, R240	// Compare against both needles41	VCMEQ	V0.B16, V1.B16, V3.B16  // b1 vs first 16 bytes42	VCMEQ	V7.B16, V1.B16, V8.B16  // b2 vs first 16 bytes43	VORR	V8.B16, V3.B16, V3.B16  // combine44	VCMEQ	V0.B16, V2.B16, V4.B16  // b1 vs second 16 bytes45	VCMEQ	V7.B16, V2.B16, V9.B16  // b2 vs second 16 bytes46	VORR	V9.B16, V4.B16, V4.B16  // combine47	// Build syndrome48	VAND	V5.B16, V3.B16, V3.B1649	VAND	V5.B16, V4.B16, V4.B1650	VADDP	V4.B16, V3.B16, V6.B1651	VADDP	V6.B16, V6.B16, V6.B1652	VMOV	V6.D[0], R653	// Clear the irrelevant lower bits54	LSL	$1, R9, R455	LSR	R4, R6, R656	LSL	R4, R6, R657	// The first block can also be the last58	BLS	masklast59	// Have we found something already?60	CBNZ	R6, tail6162loop:63	VLD1.P	(R3), [V1.B16, V2.B16]64	SUBS	$0x20, R2, R265	// Compare against both needles, OR results66	VCMEQ	V0.B16, V1.B16, V3.B1667	VCMEQ	V7.B16, V1.B16, V8.B1668	VORR	V8.B16, V3.B16, V3.B1669	VCMEQ	V0.B16, V2.B16, V4.B1670	VCMEQ	V7.B16, V2.B16, V9.B1671	VORR	V9.B16, V4.B16, V4.B1672	// If we're out of data we finish regardless of the result73	BLS	end74	// Fast check: OR both halves and check for any match75	VORR	V4.B16, V3.B16, V6.B1676	VADDP	V6.D2, V6.D2, V6.D277	VMOV	V6.D[0], R678	CBZ	R6, loop7980end:81	// Found something or out of data, build full syndrome82	VAND	V5.B16, V3.B16, V3.B1683	VAND	V5.B16, V4.B16, V4.B1684	VADDP	V4.B16, V3.B16, V6.B1685	VADDP	V6.B16, V6.B16, V6.B1686	VMOV	V6.D[0], R687	// Only mask for the last block88	BHS	tail8990masklast:91	// Clear irrelevant upper bits92	ADD	R9, R10, R493	AND	$0x1f, R4, R494	SUB	$0x20, R4, R495	NEG	R4<<1, R496	LSL	R4, R6, R697	LSR	R4, R6, R69899tail:100	CBZ	R6, fail101	RBIT	R6, R6102	SUB	$0x20, R3, R3103	CLZ	R6, R6104	ADD	R6>>1, R3, R0105	SUB	R11, R0, R0106	MOVD	R0, (R8)107	RET108109fail:110	MOVD	$-1, R0111	MOVD	R0, (R8)112	RET113114// func lastIndexByteTwo(s []byte, b1, b2 byte) int115//116// Returns the index of the last occurrence of b1 or b2 in s, or -1.117// Scans backward using ARM64 NEON.118TEXT ·lastIndexByteTwo(SB),NOSPLIT,$0-40119	MOVD	s_base+0(FP), R0120	MOVD	s_len+8(FP), R2121	MOVBU	b1+24(FP), R1122	MOVBU	b2+25(FP), R7123	MOVD	$ret+32(FP), R8124125	CBZ	R2, lfail126	MOVD	R0, R11          // save base127	ADD	R0, R2, R12      // R12 = end = base + len128	MOVD	$0x40100401, R5129	VMOV	R1, V0.B16       // V0 = splat(b1)130	VMOV	R7, V7.B16       // V7 = splat(b2)131	VMOV	R5, V5.S4132133	// Align: find the aligned block containing the last byte134	SUB	$1, R12, R3135	BIC	$0x1f, R3, R3    // R3 = start of aligned block containing last byte136137	// --- Process tail block ---138	VLD1	(R3), [V1.B16, V2.B16]139	VCMEQ	V0.B16, V1.B16, V3.B16140	VCMEQ	V7.B16, V1.B16, V8.B16141	VORR	V8.B16, V3.B16, V3.B16142	VCMEQ	V0.B16, V2.B16, V4.B16143	VCMEQ	V7.B16, V2.B16, V9.B16144	VORR	V9.B16, V4.B16, V4.B16145	VAND	V5.B16, V3.B16, V3.B16146	VAND	V5.B16, V4.B16, V4.B16147	VADDP	V4.B16, V3.B16, V6.B16148	VADDP	V6.B16, V6.B16, V6.B16149	VMOV	V6.D[0], R6150151	// Mask upper bits (bytes past end of slice)152	// tail_bytes = end - R3 (1..32)153	SUB	R3, R12, R10     // R10 = tail_bytes154	MOVD	$64, R4155	SUB	R10<<1, R4, R4   // R4 = 64 - 2*tail_bytes156	LSL	R4, R6, R6157	LSR	R4, R6, R6158159	// Is this also the head block?160	CMP	R11, R3          // R3 - R11161	BLO	lmaskfirst       // R3 < base: head+tail in same block162	BEQ	ltailonly         // R3 == base: single aligned block163164	// R3 > base: more blocks before this one165	CBNZ	R6, llast166	B	lbacksetup167168ltailonly:169	// Single block, already masked upper bits170	CBNZ	R6, llast171	B	lfail172173lmaskfirst:174	// Mask lower bits (bytes before start of slice)175	SUB	R3, R11, R4      // R4 = base - R3176	LSL	$1, R4, R4177	LSR	R4, R6, R6178	LSL	R4, R6, R6179	CBNZ	R6, llast180	B	lfail181182lbacksetup:183	SUB	$0x20, R3184185lbackloop:186	VLD1	(R3), [V1.B16, V2.B16]187	VCMEQ	V0.B16, V1.B16, V3.B16188	VCMEQ	V7.B16, V1.B16, V8.B16189	VORR	V8.B16, V3.B16, V3.B16190	VCMEQ	V0.B16, V2.B16, V4.B16191	VCMEQ	V7.B16, V2.B16, V9.B16192	VORR	V9.B16, V4.B16, V4.B16193	// Quick check: any match in this block?194	VORR	V4.B16, V3.B16, V6.B16195	VADDP	V6.D2, V6.D2, V6.D2196	VMOV	V6.D[0], R6197198	// Is this a head block? (R3 < base)199	CMP	R11, R3200	BLO	lheadblock201202	// Full block (R3 >= base)203	CBNZ	R6, lbackfound204	// More blocks?205	BEQ	lfail            // R3 == base, no more206	SUB	$0x20, R3207	B	lbackloop208209lbackfound:210	// Build full syndrome211	VAND	V5.B16, V3.B16, V3.B16212	VAND	V5.B16, V4.B16, V4.B16213	VADDP	V4.B16, V3.B16, V6.B16214	VADDP	V6.B16, V6.B16, V6.B16215	VMOV	V6.D[0], R6216	B	llast217218lheadblock:219	// R3 < base. Build full syndrome if quick check had a match.220	CBZ	R6, lfail221	VAND	V5.B16, V3.B16, V3.B16222	VAND	V5.B16, V4.B16, V4.B16223	VADDP	V4.B16, V3.B16, V6.B16224	VADDP	V6.B16, V6.B16, V6.B16225	VMOV	V6.D[0], R6226	// Mask lower bits227	SUB	R3, R11, R4      // R4 = base - R3228	LSL	$1, R4, R4229	LSR	R4, R6, R6230	LSL	R4, R6, R6231	CBZ	R6, lfail232233llast:234	// Find last match: highest set bit in syndrome235	// Syndrome has bit 2i set for matching byte i.236	// CLZ gives leading zeros; byte_offset = (63 - CLZ) / 2.237	CLZ	R6, R6238	MOVD	$63, R4239	SUB	R6, R4, R6       // R6 = 63 - CLZ = bit position240	LSR	$1, R6            // R6 = byte offset within block241	ADD	R3, R6, R0        // R0 = absolute address242	SUB	R11, R0, R0       // R0 = slice index243	MOVD	R0, (R8)244	RET245246lfail:247	MOVD	$-1, R0248	MOVD	R0, (R8)249	RET