2 * Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <machine/cpu_capabilities.h>
33 * The bcopy/memcpy loops, tuned for Nehalem. This is the 64-bit version.
35 * The following #defines are tightly coupled to the u-architecture:
38 #define kShort 80 // too short to bother with SSE (must be >=80)
41 // void bcopy(const void *src, void *dst, size_t len);
43 PLATFUNC_FUNCTION_START(bcopy, sse42, 64, 5)
44 pushq %rbp // set up a frame for backtraces
46 movq %rsi,%rax // copy dest ptr
47 movq %rdi,%rsi // xchange source and dest ptrs
49 subq %rsi,%rax // (dest - source)
50 cmpq %rdx,%rax // must move in reverse if (dest - source) < length
52 cmpq $(kShort),%rdx // long enough to bother with SSE?
57 // void *memcpy(void *dst, const void *src, size_t len);
58 // void *memmove(void *dst, const void *src, size_t len);
61 PLATFUNC_FUNCTION_START(memcpy, sse42, 64, 0) // void *memcpy(void *dst, const void *src, size_t len)
62 PLATFUNC_FUNCTION_START(memmove, sse42, 64, 0) // void *memmove(void *dst, const void *src, size_t len)
63 pushq %rbp // set up a frame for backtraces
65 movq %rdi,%r11 // save return value here
67 subq %rsi,%rax // (dest - source)
68 cmpq %rdx,%rax // must move in reverse if (dest - source) < length
70 cmpq $(kShort),%rdx // long enough to bother with SSE?
73 // Handle short forward copies. As the most common case, this is the fall-through path.
74 // rdx = length (<= kShort)
79 movl %edx,%ecx // copy length using 32-bit operation
80 shrl $2,%ecx // get #doublewords
82 2: // loop copying doublewords
89 3: // handle leftover bytes (0..3) in last word
90 andl $3,%edx // any leftover bytes?
92 4: // loop copying bytes
100 movq %r11,%rax // get return value (dst ptr) for memcpy/memmove
105 LReverseIsland: // keep the "jb" above a short branch...
106 jmp LReverse // ...because reverse moves are uncommon
109 // Handle forward moves that are long enough to justify use of SSE.
110 // First, 16-byte align the destination.
111 // rdx = length (> kShort)
116 movl %edi,%ecx // copy low half of destination ptr
118 andl $15,%ecx // get #bytes to align destination
119 jz LDestAligned // already aligned
120 subl %ecx,%edx // decrement length
121 1: // loop copying 1..15 bytes
130 // Destination is now aligned. Nehalem does a great job with unaligned SSE loads,
131 // so we use MOVDQU rather than aligned loads and shifts. Since kShort>=80, we
132 // know there is at least one 64-byte chunk to move.
133 // When we enter the copy loops, the following registers are set up:
134 // rdx = residual length (0..63)
135 // rcx = -(length to move), a multiple of 64 less than 2GB
136 // rsi = ptr to 1st source byte not to move (unaligned)
137 // rdi = ptr to 1st dest byte not to move (aligned)
140 movq %rdx,%rcx // copy length
141 andl $63,%edx // get remaining bytes for LShort
142 andq $-64,%rcx // get number of bytes we will copy in inner loop
143 addq %rcx,%rsi // point to 1st byte not copied
145 negq %rcx // now generate offset to 1st byte to be copied
146 testl $15,%esi // source also aligned?
151 // Forward loop for aligned operands.
153 .align 4,0x90 // 16-byte align inner loops
154 LAlignedLoop: // loop over 64-byte chunks
155 movdqa (%rsi,%rcx),%xmm0
156 movdqa 16(%rsi,%rcx),%xmm1
157 movdqa 32(%rsi,%rcx),%xmm2
158 movdqa 48(%rsi,%rcx),%xmm3
160 movdqa %xmm0,(%rdi,%rcx)
161 movdqa %xmm1,16(%rdi,%rcx)
162 movdqa %xmm2,32(%rdi,%rcx)
163 movdqa %xmm3,48(%rdi,%rcx)
168 jmp LShort // copy remaining 0..63 bytes and done
171 // Forward loop for unaligned operands.
173 .align 4,0x90 // 16-byte align inner loops
174 LUnalignedLoop: // loop over 64-byte chunks
175 movdqu (%rsi,%rcx),%xmm0
176 movdqu 16(%rsi,%rcx),%xmm1
177 movdqu 32(%rsi,%rcx),%xmm2
178 movdqu 48(%rsi,%rcx),%xmm3
180 movdqa %xmm0,(%rdi,%rcx)
181 movdqa %xmm1,16(%rdi,%rcx)
182 movdqa %xmm2,32(%rdi,%rcx)
183 movdqa %xmm3,48(%rdi,%rcx)
188 jmp LShort // copy remaining 0..63 bytes and done
191 // Reverse moves. These are only used with destructive overlap.
197 addq %rdx,%rsi // point to end of strings
199 cmpq $(kShort),%rdx // long enough to bother with SSE?
200 ja LReverseNotShort // yes
202 // Handle reverse short copies.
203 // edx = length (<= kShort)
204 // rsi = one byte past end of source
205 // rdi = one byte past end of dest
208 movl %edx,%ecx // copy length
209 shrl $3,%ecx // #quadwords
219 andl $7,%edx // bytes?
229 movq %r11,%rax // get return value (dst ptr) for memcpy/memmove
233 // Handle a reverse move long enough to justify using SSE.
234 // rdx = length (> kShort)
235 // rsi = one byte past end of source
236 // rdi = one byte past end of dest
239 movl %edi,%ecx // copy destination
240 andl $15,%ecx // get #bytes to align destination
241 jz LReverseDestAligned // already aligned
242 subq %rcx,%rdx // adjust length
243 1: // loop copying 1..15 bytes
251 // Destination is now aligned. Prepare for reverse loops.
254 movq %rdx,%rcx // copy length
255 andl $63,%edx // get remaining bytes for LReverseShort
256 andq $-64,%rcx // get number of bytes we will copy in inner loop
257 subq %rcx,%rsi // point to endpoint of copy
259 testl $15,%esi // is source aligned too?
260 jnz LReverseUnalignedLoop // no
262 LReverseAlignedLoop: // loop over 64-byte chunks
263 movdqa -16(%rsi,%rcx),%xmm0
264 movdqa -32(%rsi,%rcx),%xmm1
265 movdqa -48(%rsi,%rcx),%xmm2
266 movdqa -64(%rsi,%rcx),%xmm3
268 movdqa %xmm0,-16(%rdi,%rcx)
269 movdqa %xmm1,-32(%rdi,%rcx)
270 movdqa %xmm2,-48(%rdi,%rcx)
271 movdqa %xmm3,-64(%rdi,%rcx)
274 jne LReverseAlignedLoop
276 jmp LReverseShort // copy remaining 0..63 bytes and done
279 // Reverse, unaligned loop. LDDQU==MOVDQU on these machines.
281 LReverseUnalignedLoop: // loop over 64-byte chunks
282 movdqu -16(%rsi,%rcx),%xmm0
283 movdqu -32(%rsi,%rcx),%xmm1
284 movdqu -48(%rsi,%rcx),%xmm2
285 movdqu -64(%rsi,%rcx),%xmm3
287 movdqa %xmm0,-16(%rdi,%rcx)
288 movdqa %xmm1,-32(%rdi,%rcx)
289 movdqa %xmm2,-48(%rdi,%rcx)
290 movdqa %xmm3,-64(%rdi,%rcx)
293 jne LReverseUnalignedLoop
295 jmp LReverseShort // copy remaining 0..63 bytes and done
298 PLATFUNC_DESCRIPTOR(bcopy,sse42,kHasSSE4_2,0)
299 PLATFUNC_DESCRIPTOR(memcpy,sse42,kHasSSE4_2,0)
300 PLATFUNC_DESCRIPTOR(memmove,sse42,kHasSSE4_2,0)