1 /* {wcs|wcp|str|stp}cpy with 256/512-bit EVEX instructions.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <isa-level.h>
20 #if ISA_SHOULD_BUILD (4)
23 /* Use evex-masked stores for small sizes. Turned off at the
25 # define USE_EVEX_MASKED_STORE 0
26 /* Use movsb in page cross case to save code size. */
27 # define USE_MOVSB_IN_PAGE_CROSS 1
32 # include "x86-evex256-vecs.h"
36 # define STRCPY __strcpy_evex
41 # define VMOVU_MASK vmovdqu32
42 # define VPMIN vpminud
43 # define VPTESTN vptestnmd
44 # define VPTEST vptestmd
45 # define VPCMPEQ vpcmpeqd
48 # define REP_MOVS rep movsd
50 # define USE_WIDE_CHAR
52 # define VMOVU_MASK vmovdqu8
53 # define VPMIN vpminub
54 # define VPTESTN vptestnmb
55 # define VPTEST vptestmb
56 # define VPCMPEQ vpcmpeqb
59 # define REP_MOVS rep movsb
62 # include "reg-macros.h"
68 # define END_REG rdi, %rdx, CHAR_SIZE
72 # define PAGE_ALIGN_REG edx
73 # define PAGE_ALIGN_REG_64 rdx
75 # define PAGE_ALIGN_REG eax
76 # define PAGE_ALIGN_REG_64 rax
80 # define VZERO_128 VMM_128(7)
83 # define PAGE_SIZE 4096
84 # define CHAR_PER_VEC (VEC_SIZE / CHAR_SIZE)
87 .section SECTION(.text), "ax", @progbits
91 # include "strcat-strlen-evex.h.S"
94 movl %esi, %PAGE_ALIGN_REG
95 andl $(PAGE_SIZE - 1), %PAGE_ALIGN_REG
96 cmpl $(PAGE_SIZE - VEC_SIZE), %PAGE_ALIGN_REG
98 L(page_cross_continue):
100 # if !defined USE_AS_STPCPY && !defined USE_AS_STRCAT
105 /* Two short string implementations. One with traditional
106 branching approach and one with masked instructions (which
107 have potential for dramatically bad perf if dst splits a
108 page and is not in the TLB). */
109 # if USE_EVEX_MASKED_STORE
110 VPTEST %VMM(0), %VMM(0), %k0
112 # ifdef USE_AS_WCSCPY
113 subl $((1 << CHAR_PER_VEC)- 1), %VRCX
121 VMOVU_MASK %VMM(0), (%rdi){%k1}
123 # ifdef USE_AS_STPCPY
125 leaq (%rdi, %rcx, CHAR_SIZE), %rax
130 VPTESTN %VMM(0), %VMM(0), %k0
137 # ifdef USE_AS_STPCPY
138 leaq (%rdi, %rdx, CHAR_SIZE), %rax
141 /* Use mask bits in rcx to detect which copy we need. If the low
142 mask is zero then there must be a bit set in the upper half.
143 I.e if rcx != 0 and ecx == 0, then match must be upper 32
144 bits so we use L(copy_32_63). */
146 # ifdef USE_AS_WCSCPY
154 # ifdef USE_AS_WCSCPY
162 # ifdef USE_AS_WCSCPY
170 # ifdef USE_AS_WCSCPY
171 vmovd %VMM_128(0), (%rdi)
172 /* No need to copy, we know its zero. */
185 /* NB: make this `vmovw` if support for AVX512-FP16 is added.
187 vmovd %VMM_128(0), %esi
192 /* No need to copy, we know its zero. */
200 VMOVU -(32 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %VMM_256(1)
201 VMOVU %VMM_256(0), (%rdi)
202 VMOVU %VMM_256(1), -(32 - CHAR_SIZE)(%END_REG)
209 /* Use xmm1 explicitly here as it won't require a `vzeroupper`
210 and will save code size. */
211 vmovdqu -(16 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %xmm1
212 VMOVU %VMM_128(0), (%rdi)
213 vmovdqu %xmm1, -(16 - CHAR_SIZE)(%END_REG)
218 # ifdef USE_AS_WCSCPY
219 movl -(8 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %ecx
221 movq -(8 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %rcx
223 vmovq %VMM_128(0), (%rdi)
224 movq %rcx, -(8 - CHAR_SIZE)(%END_REG)
229 # ifndef USE_AS_WCSCPY
232 movl -(4 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %ecx
233 vmovd %VMM_128(0), (%rdi)
234 movl %ecx, -(4 - CHAR_SIZE)(%END_REG)
241 # if defined USE_AS_STPCPY || defined USE_AS_STRCAT
242 VMOVU %VMM(0), (%rdi)
245 andq $-(VEC_SIZE), %rsi
247 VMOVA (VEC_SIZE * 1)(%rsi), %VMM(1)
249 /* Ideally we store after moves to minimize impact of potential
250 false-dependencies. */
251 # if !defined USE_AS_STPCPY && !defined USE_AS_STRCAT
252 VMOVU %VMM(0), (%rax)
255 VPTESTN %VMM(1), %VMM(1), %k0
260 VMOVA (VEC_SIZE * 2)(%rsi), %VMM(2)
261 VMOVU %VMM(1), VEC_SIZE(%rdi)
263 VPTESTN %VMM(2), %VMM(2), %k0
268 VMOVA (VEC_SIZE * 3)(%rsi), %VMM(3)
269 VMOVU %VMM(2), (VEC_SIZE * 2)(%rdi)
271 VPTESTN %VMM(3), %VMM(3), %k0
276 VMOVA (VEC_SIZE * 4)(%rsi), %VMM(4)
277 VMOVU %VMM(3), (VEC_SIZE * 3)(%rdi)
278 VPTESTN %VMM(4), %VMM(4), %k0
283 VMOVU %VMM(4), (VEC_SIZE * 4)(%rdi)
286 /* Align for 4x loop. */
289 /* + VEC_SIZE * 5 because we never added the original VEC_SIZE
290 we covered before aligning. */
291 subq $-(VEC_SIZE * 5), %rsi
292 andq $-(VEC_SIZE * 4), %rsi
295 /* Load first half of the loop before entry. */
296 VMOVA (VEC_SIZE * 0 + 0)(%rsi), %VMM(0)
297 VMOVA (VEC_SIZE * 1 + 0)(%rsi), %VMM(1)
298 VMOVA (VEC_SIZE * 2 + 0)(%rsi), %VMM(2)
299 VMOVA (VEC_SIZE * 3 + 0)(%rsi), %VMM(3)
301 VPMIN %VMM(0), %VMM(1), %VMM(4)
302 VPMIN %VMM(2), %VMM(3), %VMM(6)
303 VPTESTN %VMM(4), %VMM(4), %k2
304 VPTESTN %VMM(6), %VMM(6), %k4
311 VMOVU %VMM(0), (VEC_SIZE * 0 + 0)(%rdi, %rsi)
312 VMOVU %VMM(1), (VEC_SIZE * 1 + 0)(%rdi, %rsi)
313 VMOVU %VMM(2), (VEC_SIZE * 2 + 0)(%rdi, %rsi)
314 VMOVU %VMM(3), (VEC_SIZE * 3 + 0)(%rdi, %rsi)
316 subq $(VEC_SIZE * -4), %rsi
318 VMOVA (VEC_SIZE * 0 + 0)(%rsi), %VMM(0)
319 VMOVA (VEC_SIZE * 1 + 0)(%rsi), %VMM(1)
320 VMOVA (VEC_SIZE * 2 + 0)(%rsi), %VMM(2)
321 VMOVA (VEC_SIZE * 3 + 0)(%rsi), %VMM(3)
324 VPMIN %VMM(0), %VMM(1), %VMM(4)
325 VPMIN %VMM(2), %VMM(3), %VMM(6)
326 VPTESTN %VMM(4), %VMM(4), %k2
327 VPTESTN %VMM(6), %VMM(6), %k4
332 VPTESTN %VMM(0), %VMM(0), %k0
334 /* Restore rdi (%rdi). */
337 jnz L(ret_vec_x0_end)
338 VMOVU %VMM(0), (VEC_SIZE * 0 + 0)(%rdi)
343 VMOVU %VMM(1), (VEC_SIZE * 1 + 0)(%rdi)
345 VPTESTN %VMM(2), %VMM(2), %k0
349 VMOVU %VMM(2), (VEC_SIZE * 2 + 0)(%rdi)
350 /* Place L(ret_vec_x4) here to save code size. We get a
351 meaningfuly benefit doing this for stpcpy. */
355 VMOVU ((VEC_SIZE * 3)-(VEC_SIZE - CHAR_SIZE))(%rsi, %rdx, CHAR_SIZE), %VMM(0)
356 VMOVU %VMM(0), ((VEC_SIZE * 3 + 0)-(VEC_SIZE - CHAR_SIZE))(%rdi, %rdx, CHAR_SIZE)
357 # ifdef USE_AS_STPCPY
358 leaq (VEC_SIZE * 3 + 0)(%rdi, %rdx, CHAR_SIZE), %rax
366 # ifdef USE_AS_STPCPY
367 leaq (%rdi, %rcx, CHAR_SIZE), %rax
370 VMOVU (-(VEC_SIZE))(%rsi, %rcx, CHAR_SIZE), %VMM(0)
371 VMOVU %VMM(0), (-(VEC_SIZE))(%rdi, %rcx, CHAR_SIZE)
377 VMOVU (VEC_SIZE -(VEC_SIZE - CHAR_SIZE))(%rsi, %rcx, CHAR_SIZE), %VMM(0)
378 VMOVU %VMM(0), (VEC_SIZE -(VEC_SIZE - CHAR_SIZE))(%rdi, %rcx, CHAR_SIZE)
379 # ifdef USE_AS_STPCPY
380 leaq VEC_SIZE(%rdi, %rcx, CHAR_SIZE), %rax
387 VMOVU ((VEC_SIZE * 2)-(VEC_SIZE - CHAR_SIZE))(%rsi, %rcx, CHAR_SIZE), %VMM(0)
388 VMOVU %VMM(0), ((VEC_SIZE * 2)-(VEC_SIZE - CHAR_SIZE))(%rdi, %rcx, CHAR_SIZE)
389 # ifdef USE_AS_STPCPY
390 leaq (VEC_SIZE * 2)(%rdi, %rcx, CHAR_SIZE), %rax
394 /* ret_vec_x3 reuses return code after the loop. */
398 VMOVU ((VEC_SIZE * 4)-(VEC_SIZE - CHAR_SIZE))(%rsi, %rcx, CHAR_SIZE), %VMM(0)
399 VMOVU %VMM(0), ((VEC_SIZE * 4)-(VEC_SIZE - CHAR_SIZE))(%rdi, %rcx, CHAR_SIZE)
400 # ifdef USE_AS_STPCPY
401 leaq (VEC_SIZE * 4)(%rdi, %rcx, CHAR_SIZE), %rax
408 # ifndef USE_AS_STRCAT
409 vpxorq %VZERO_128, %VZERO_128, %VZERO_128
412 andq $(VEC_SIZE * -1), %rcx
414 VPCMPEQ (%rcx), %VZERO, %k0
416 # ifdef USE_AS_WCSCPY
417 andl $(VEC_SIZE - 1), %PAGE_ALIGN_REG
418 shrl $2, %PAGE_ALIGN_REG
420 shrx %VGPR(PAGE_ALIGN_REG_64), %VRCX, %VRCX
422 # if USE_MOVSB_IN_PAGE_CROSS
423 /* Optimizing more aggressively for space as this is very cold
424 code. This saves 2x cache lines. */
426 /* This adds once to the later result which will get correct
427 copy bounds. NB: this can never zero-out a non-zero RCX as
428 to be in the page cross case rsi cannot be aligned and we
429 already right-shift rcx by the misalignment. */
431 jz L(page_cross_continue)
432 # if !defined USE_AS_STPCPY && !defined USE_AS_STRCAT
438 # ifdef USE_AS_STPCPY
439 leaq -CHAR_SIZE(%rdi), %rax
445 /* Check if we found zero-char before end of page. */
447 jz L(page_cross_continue)
449 /* Traditional copy case, essentially same as used in non-page-
450 cross case but since we can't reuse VMM(0) we need twice as
451 many loads from rsi. */
453 # ifndef USE_AS_STRCAT
456 /* Dependency on rdi must already have been satisfied. */
458 # ifdef USE_AS_STPCPY
459 leaq (%rdi, %rdx, CHAR_SIZE), %rax
460 # elif !defined USE_AS_STRCAT
465 # ifdef USE_AS_WCSCPY
470 jz L(page_cross_copy_32_63)
473 # ifdef USE_AS_WCSCPY
478 jz L(page_cross_copy_16_31)
480 # ifdef USE_AS_WCSCPY
485 jz L(page_cross_copy_8_15)
487 # ifdef USE_AS_WCSCPY
495 jz L(page_cross_copy_4_7)
498 jz L(page_cross_set_null_term)
501 L(page_cross_set_null_term):
507 L(page_cross_copy_4_7):
509 movl -(4 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %esi
511 movl %esi, -(4 - CHAR_SIZE)(%END_REG)
517 L(page_cross_copy_32_63):
518 VMOVU (%rsi), %VMM_256(0)
519 VMOVU -(32 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %VMM_256(1)
520 VMOVU %VMM_256(0), (%rdi)
521 VMOVU %VMM_256(1), -(32 - CHAR_SIZE)(%END_REG)
526 L(page_cross_copy_16_31):
527 vmovdqu (%rsi), %xmm0
528 vmovdqu -(16 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %xmm1
529 vmovdqu %xmm0, (%rdi)
530 vmovdqu %xmm1, -(16 - CHAR_SIZE)(%END_REG)
534 L(page_cross_copy_8_15):
536 movq -(8 - CHAR_SIZE)(%rsi, %rdx, CHAR_SIZE), %rsi
538 movq %rsi, -(8 - CHAR_SIZE)(%END_REG)