2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <machine/cpu_capabilities.h>
26 /* This file contains the following functions:
28 * void *memset(void *b, int c, size_t len);
29 * void memset_pattern4(void *b, const void *c4, size_t len);
30 * void memset_pattern8(void *b, const void *c8, size_t len);
31 * void memset_pattern16(void *b, const void *c16, size_t len);
33 * Calls of memset() with c==0 are routed to the bzero() routine. Most of the
34 * others go to _memset_pattern, which is entered as follows:
35 * %rdi = ptr to memory to set (aligned)
36 * %edx = length (which can be short, though we bias in favor of long operands)
37 * %xmm0 = the pattern to store
39 * %eax, %edi, %esi, %ecx, and %edx all trashed
41 * NB: we avoid "stos" family of instructions (stosl, stosb), as they are very slow
42 * on P4s and probably other processors.
45 #define kShort 255 // for nonzero memset(), too short for commpage
51 _memset: // void *memset(void *b, int c, size_t len);
52 andl $0xFF,%esi // (c==0) ?
53 jnz LNonzero // not a bzero
55 movq %rdx,%rsi // put count where bzero() expects it
56 jmp _bzero // enter _bzero
59 // Handle memset of a nonzero value.
62 movq %rdi,%r8 // preserve the original pointer so we can return it
63 movl %esi,%eax // replicate byte in %esi into all four bytes
68 orl %esi,%eax // now %eax has "c" in all 4 bytes
69 cmpq $(kShort),%rdx // is operand too short for SSE?
70 ja LCallCommpage // no
72 // Nonzero memset() too short to call commpage.
73 // %eax = replicated 4-byte pattern
75 // %edx = length (<= kShort)
77 cmpl $16,%edx // long enough to word align?
79 test %edx,%edx // length==0?
82 movb %al,(%rdi) // pack in a byte
88 movb %al,(%rdi) // pack in a byte
92 test $3,%edi // is ptr doubleword aligned?
94 movl %edx,%ecx // copy length
95 shrl $2,%edx // #doublewords to store
97 movl %eax,(%rdi) // store aligned doubleword
101 andl $3,%ecx // any leftover bytes?
104 movb %al,(%rdi) // pack in a byte
109 movq %r8,%rax // get return value (ie, original ptr)
112 // Nonzero memset() is long enough to call commpage.
113 // %eax = replicated 4-byte pattern
115 // %rdx = length (> kShort)
118 movd %eax,%xmm0 // move %eax to low 4 bytes of %xmm0
119 pshufd $(0x00),%xmm0,%xmm0 // replicate across the vector
120 movq %rdi,%rcx // copy dest ptr
122 andl $15,%ecx // get #bytes to align ptr
123 jz 2f // skip if already aligned
124 subq %rcx,%rdx // decrement length
126 movb %al,(%rdi) // pack in a byte
130 2: // ptr aligned, length long enough to justify
131 call Lmemset_pattern // call commpage to do the heavy lifting
132 movq %r8,%rax // get return value (ie, original ptr)
136 // Handle memset of a 16-byte pattern.
138 .globl _memset_pattern16
140 _memset_pattern16: // void memset_pattern16(void *b, const void *c16, size_t len);
141 movdqu (%rsi),%xmm0 // load the pattern
145 // Handle memset of an 8-byte pattern.
147 .globl _memset_pattern8
149 _memset_pattern8: // void memset_pattern8(void *b, const void *c8, size_t len);
150 movq (%rsi),%xmm0 // load pattern into low 8 bytes
151 punpcklqdq %xmm0,%xmm0 // replicate into all 16
154 // Handle memset of a 4-byte pattern.
156 .globl _memset_pattern4
158 _memset_pattern4: // void memset_pattern4(void *b, const void *c4, size_t len);
159 movd (%rsi),%xmm0 // load pattern into low 4 bytes
160 pshufd $(0x00),%xmm0,%xmm0 // replicate the 4 bytes across the vector
163 // Align ptr if necessary. We must rotate the pattern right for each byte we
164 // store while aligning the ptr. Since there is no rotate instruction in SSE3,
165 // we have to synthesize the rotates.
170 LAlignPtr: // NB: can drop down to here!
171 cmpq $100,%rdx // long enough to bother aligning ptr?
172 movq %rdi,%rcx // copy ptr
173 jb LReady // not long enough
175 andl $15,%ecx // get #bytes to align ptr
176 jz LReady // already aligned
177 subq %rcx,%rdx // adjust length
179 test $1,%cl // 1-byte store required?
180 movd %xmm0,%eax // get 4 low bytes in %eax
182 movdqa %xmm0,%xmm1 // copy pattern so we can shift in both directions
183 movb %al,(%rdi) // pack in the low-order byte
184 psrldq $1,%xmm0 // shift pattern right 1 byte
186 pslldq $15,%xmm1 // shift pattern left 15 bytes
187 shrl $8,%eax // in case 2-byte store is required
188 por %xmm1,%xmm0 // complete right rotate of pattern by 1 byte
190 test $2,%cl // 2-byte store required?
192 psrldq $2,%xmm0 // shift pattern down 2 bytes
193 movw %ax,(%rdi) // pack in next two bytes
194 pinsrw $7,%eax,%xmm0 // insert low word of %eax into high word of %xmm0
195 addq $2,%rdi // adjust ptr
197 test $4,%cl // 4-byte store required?
199 movd %xmm0,(%rdi) // store low 4 bytes of %xmm0
200 pshufd $(0x39),%xmm0,%xmm0 // rotate %xmm0 right 4 bytes (mask == 00 11 10 01)
201 addq $4,%rdi // adjust ptr
203 test $8,%cl // 8-byte store required?
205 movq %xmm0,(%rdi) // store low 8 bytes of %xmm0
206 pshufd $(0x4e),%xmm0,%xmm0 // rotate %xmm0 right 8 bytes (mask == 01 00 11 10)
207 addq $8,%rdi // adjust ptr
209 // Ptr is aligned if practical, we're ready to call commpage to do the heavy lifting.
212 call Lmemset_pattern // call commpage to do the heavy lifting
217 #define kVeryLong (1024*1024)
220 cmpq $(kLShort),%rdx // long enough to bother aligning?
224 // Here for short operands or the end of long ones.
225 // %rdx = length (<= kLShort)
226 // %rdi = ptr (may not be not aligned)
230 movdqu %xmm0,(%rdi) // stuff in another 16 bytes
234 cmpl $16,%edx // room for another vector?
235 jge LUnalignedStore16 // yes
236 LLessThan16: // here at end of copy with < 16 bytes remaining
237 test $8,%dl // 8-byte store required?
239 movq %xmm0,(%rdi) // pack in 8 low bytes
240 psrldq $8,%xmm0 // then shift vector down 8 bytes
243 test $4,%dl // 4-byte store required?
245 movd %xmm0,(%rdi) // pack in 4 low bytes
246 psrldq $4,%xmm0 // then shift vector down 4 bytes
249 andl $3,%edx // more to go?
251 movd %xmm0,%eax // move remainders out into %eax
252 4: // loop on up to three bytes
253 movb %al,(%rdi) // pack in next byte
254 shrl $8,%eax // shift next byte into position
260 // Long enough to justify aligning ptr. Note that we have to rotate the
261 // pattern to account for any alignment. We do this by doing two unaligned
262 // stores, and then an aligned load from the middle of the two stores.
263 // This will stall on store forwarding alignment mismatch, and the unaligned
264 // stores can be pretty slow too, but the alternatives aren't any better.
265 // Fortunately, in most cases our caller has already aligned the ptr.
266 // %rdx = length (> kLShort)
267 // %rdi = ptr (may not be aligned)
271 movl %edi,%ecx // copy low bits of dest ptr
273 andl $15,%ecx // mask down to #bytes to 16-byte align
274 jz LAligned // skip if already aligned
275 movdqu %xmm0,(%rdi) // store 16 unaligned bytes
276 movdqu %xmm0,16(%rdi) // and 16 more, to be sure we have an aligned chunk
277 addq %rcx,%rdi // now point to the aligned chunk
278 subq %rcx,%rdx // adjust remaining count
279 movdqa (%rdi),%xmm0 // get the rotated pattern (probably stalling)
280 addq $16,%rdi // skip past the aligned chunk
283 // Set up for 64-byte loops.
284 // %rdx = length remaining
285 // %rdi = ptr (aligned)
286 // %xmm0 = rotated pattern
289 movq %rdx,%rcx // copy length remaining
290 andl $63,%edx // mask down to residual length (0..63)
291 andq $-64,%rcx // %ecx <- #bytes we will zero in by-64 loop
292 jz LNoMoreChunks // no 64-byte chunks
293 addq %rcx,%rdi // increment ptr by length to move
294 cmpq $(kVeryLong),%rcx // long enough to justify non-temporal stores?
296 negq %rcx // negate length to move
299 // Loop over 64-byte chunks, storing into cache.
301 .align 4,0x90 // keep inner loops 16-byte aligned
303 movdqa %xmm0,(%rdi,%rcx)
304 movdqa %xmm0,16(%rdi,%rcx)
305 movdqa %xmm0,32(%rdi,%rcx)
306 movdqa %xmm0,48(%rdi,%rcx)
312 // Very long operands: use non-temporal stores to bypass cache.
315 negq %rcx // negate length to move
318 .align 4,0x90 // keep inner loops 16-byte aligned
320 movntdq %xmm0,(%rdi,%rcx)
321 movntdq %xmm0,16(%rdi,%rcx)
322 movntdq %xmm0,32(%rdi,%rcx)
323 movntdq %xmm0,48(%rdi,%rcx)
327 sfence // required by non-temporal stores
330 // Handle leftovers: loop by 16.
331 // %edx = length remaining (<64)
332 // %edi = ptr (aligned)
333 // %xmm0 = rotated pattern
336 movdqa %xmm0,(%rdi) // pack in 16 more bytes
337 subl $16,%edx // decrement count
338 addq $16,%rdi // increment ptr
340 cmpl $16,%edx // more to go?
342 jmp LLessThan16 // handle up to 15 remaining bytes