// memcpy.S: an AArch64 memcpy built from overlapping loads and Q-register pairs. // // 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..64 two Q-register pairs, one from each end // 65+ 64 bytes per iteration, then the last 64 from the end // // Uses only x0-x8 and v0-v3, 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.ls .Lcopy17_32 cmp x2, #64 b.hi .Lcopy_long ldp q0, q1, [x1] // 33..64 ldp q2, q3, [x4, #-32] stp q0, q1, [x0] stp q2, q3, [x5, #-32] ret .Lcopy17_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, #64 // stop when 64 or fewer bytes remain .Lloop64: ldp q0, q1, [x7] ldp q2, q3, [x7, #32] add x7, x7, #64 stp q0, q1, [x6] stp q2, q3, [x6, #32] add x6, x6, #64 cmp x6, x8 b.lo .Lloop64 ldp q0, q1, [x4, #-64] // the last 64 bytes, from the end ldp q2, q3, [x4, #-32] stp q0, q1, [x5, #-64] stp q2, q3, [x5, #-32] ret .size memcpy_neon, . - memcpy_neon