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>
30 #include <machine/commpage.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 COMMPAGE_FUNCTION_START(bcopy_sse42_64, 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);
60 // NB: These need to be 32 bytes from bcopy():
64 Lmemcpy: // void *memcpy(void *dst, const void *src, size_t len)
65 Lmemmove: // void *memmove(void *dst, const void *src, size_t len)
66 pushq %rbp // set up a frame for backtraces
68 movq %rdi,%r11 // save return value here
70 subq %rsi,%rax // (dest - source)
71 cmpq %rdx,%rax // must move in reverse if (dest - source) < length
73 cmpq $(kShort),%rdx // long enough to bother with SSE?
76 // Handle short forward copies. As the most common case, this is the fall-through path.
77 // rdx = length (<= kShort)
82 movl %edx,%ecx // copy length using 32-bit operation
83 shrl $2,%ecx // get #doublewords
85 2: // loop copying doublewords
92 3: // handle leftover bytes (0..3) in last word
93 andl $3,%edx // any leftover bytes?
95 4: // loop copying bytes
103 movq %r11,%rax // get return value (dst ptr) for memcpy/memmove
108 LReverseIsland: // keep the "jb" above a short branch...
109 jmp LReverse // ...because reverse moves are uncommon
112 // Handle forward moves that are long enough to justify use of SSE.
113 // First, 16-byte align the destination.
114 // rdx = length (> kShort)
119 movl %edi,%ecx // copy low half of destination ptr
121 andl $15,%ecx // get #bytes to align destination
122 jz LDestAligned // already aligned
123 subl %ecx,%edx // decrement length
124 1: // loop copying 1..15 bytes
133 // Destination is now aligned. Nehalem does a great job with unaligned SSE loads,
134 // so we use MOVDQU rather than aligned loads and shifts. Since kShort>=80, we
135 // know there is at least one 64-byte chunk to move.
136 // When we enter the copy loops, the following registers are set up:
137 // rdx = residual length (0..63)
138 // rcx = -(length to move), a multiple of 64 less than 2GB
139 // rsi = ptr to 1st source byte not to move (unaligned)
140 // rdi = ptr to 1st dest byte not to move (aligned)
143 movq %rdx,%rcx // copy length
144 andl $63,%edx // get remaining bytes for LShort
145 andq $-64,%rcx // get number of bytes we will copy in inner loop
146 addq %rcx,%rsi // point to 1st byte not copied
148 negq %rcx // now generate offset to 1st byte to be copied
149 testl $15,%esi // source also aligned?
154 // Forward loop for aligned operands.
156 .align 4,0x90 // 16-byte align inner loops
157 LAlignedLoop: // loop over 64-byte chunks
158 movdqa (%rsi,%rcx),%xmm0
159 movdqa 16(%rsi,%rcx),%xmm1
160 movdqa 32(%rsi,%rcx),%xmm2
161 movdqa 48(%rsi,%rcx),%xmm3
163 movdqa %xmm0,(%rdi,%rcx)
164 movdqa %xmm1,16(%rdi,%rcx)
165 movdqa %xmm2,32(%rdi,%rcx)
166 movdqa %xmm3,48(%rdi,%rcx)
171 jmp LShort // copy remaining 0..63 bytes and done
174 // Forward loop for unaligned operands.
176 .align 4,0x90 // 16-byte align inner loops
177 LUnalignedLoop: // loop over 64-byte chunks
178 movdqu (%rsi,%rcx),%xmm0
179 movdqu 16(%rsi,%rcx),%xmm1
180 movdqu 32(%rsi,%rcx),%xmm2
181 movdqu 48(%rsi,%rcx),%xmm3
183 movdqa %xmm0,(%rdi,%rcx)
184 movdqa %xmm1,16(%rdi,%rcx)
185 movdqa %xmm2,32(%rdi,%rcx)
186 movdqa %xmm3,48(%rdi,%rcx)
191 jmp LShort // copy remaining 0..63 bytes and done
194 // Reverse moves. These are only used with destructive overlap.
200 addq %rdx,%rsi // point to end of strings
202 cmpq $(kShort),%rdx // long enough to bother with SSE?
203 ja LReverseNotShort // yes
205 // Handle reverse short copies.
206 // edx = length (<= kShort)
207 // rsi = one byte past end of source
208 // rdi = one byte past end of dest
211 movl %edx,%ecx // copy length
212 shrl $3,%ecx // #quadwords
222 andl $7,%edx // bytes?
232 movq %r11,%rax // get return value (dst ptr) for memcpy/memmove
236 // Handle a reverse move long enough to justify using SSE.
237 // rdx = length (> kShort)
238 // rsi = one byte past end of source
239 // rdi = one byte past end of dest
242 movl %edi,%ecx // copy destination
243 andl $15,%ecx // get #bytes to align destination
244 jz LReverseDestAligned // already aligned
245 subq %rcx,%rdx // adjust length
246 1: // loop copying 1..15 bytes
254 // Destination is now aligned. Prepare for reverse loops.
257 movq %rdx,%rcx // copy length
258 andl $63,%edx // get remaining bytes for LReverseShort
259 andq $-64,%rcx // get number of bytes we will copy in inner loop
260 subq %rcx,%rsi // point to endpoint of copy
262 testl $15,%esi // is source aligned too?
263 jnz LReverseUnalignedLoop // no
265 LReverseAlignedLoop: // loop over 64-byte chunks
266 movdqa -16(%rsi,%rcx),%xmm0
267 movdqa -32(%rsi,%rcx),%xmm1
268 movdqa -48(%rsi,%rcx),%xmm2
269 movdqa -64(%rsi,%rcx),%xmm3
271 movdqa %xmm0,-16(%rdi,%rcx)
272 movdqa %xmm1,-32(%rdi,%rcx)
273 movdqa %xmm2,-48(%rdi,%rcx)
274 movdqa %xmm3,-64(%rdi,%rcx)
277 jne LReverseAlignedLoop
279 jmp LReverseShort // copy remaining 0..63 bytes and done
282 // Reverse, unaligned loop. LDDQU==MOVDQU on these machines.
284 LReverseUnalignedLoop: // loop over 64-byte chunks
285 movdqu -16(%rsi,%rcx),%xmm0
286 movdqu -32(%rsi,%rcx),%xmm1
287 movdqu -48(%rsi,%rcx),%xmm2
288 movdqu -64(%rsi,%rcx),%xmm3
290 movdqa %xmm0,-16(%rdi,%rcx)
291 movdqa %xmm1,-32(%rdi,%rcx)
292 movdqa %xmm2,-48(%rdi,%rcx)
293 movdqa %xmm3,-64(%rdi,%rcx)
296 jne LReverseUnalignedLoop
298 jmp LReverseShort // copy remaining 0..63 bytes and done
301 COMMPAGE_DESCRIPTOR(bcopy_sse42_64,_COMM_PAGE_BCOPY,kHasSSE4_2,0)