// memcpy.S: an AArch64 memcpy built from overlapping loads. // // void *memcpy_neon(void *dst, const void *src, size_t n); // // Size classes: // 0..16 overlapping scalar loads from both ends, no loop // 17..32 two Q registers, one from each end // 33+ 16 bytes per iteration, then the last 16 from the end // // Uses only x0-x8 and v0-v1, so nothing needs saving under AAPCS64. .text .p2align 6 .global memcpy_neon .type memcpy_neon, %function memcpy_neon: add x4, x1, x2 // src end add x5, x0, x2 // dst end cmp x2, #16 b.ls .Lcopy0_16 cmp x2, #32 b.hi .Lcopy_long // 17..32: two 16-byte moves that meet in the middle ldr q0, [x1] ldr q1, [x4, #-16] str q0, [x0] str q1, [x5, #-16] ret .Lcopy0_16: cmp x2, #8 b.lo .Lcopy0_8 ldr x6, [x1] // 8..16 ldr x7, [x4, #-8] str x6, [x0] str x7, [x5, #-8] ret .Lcopy0_8: tbz x2, #2, .Lcopy0_4 ldr w6, [x1] // 4..7 ldr w7, [x4, #-4] str w6, [x0] str w7, [x5, #-4] ret .Lcopy0_4: cbz x2, .Lreturn // 1..3: first, middle and last byte lsr x3, x2, #1 ldrb w6, [x1] ldrb w7, [x4, #-1] ldrb w8, [x1, x3] strb w6, [x0] strb w8, [x0, x3] strb w7, [x5, #-1] .Lreturn: ret .Lcopy_long: mov x6, x0 // dst cursor mov x7, x1 // src cursor sub x8, x5, #16 // stop when 16 or fewer bytes remain .Lloop16: ldr q0, [x7], #16 str q0, [x6], #16 cmp x6, x8 b.lo .Lloop16 ldr q0, [x4, #-16] // the last 16 bytes, from the end str q0, [x5, #-16] ret .size memcpy_neon, . - memcpy_neon