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 _COMM_PAGE_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 $(_COMM_PAGE_BZERO),%rax// map memset(p,0,n) into bzero(p,n)
56 movq %rdx,%rsi // put count where bzero() expects it
57 jmp *%rax // enter commpage
60 // Handle memset of a nonzero value.
63 movq %rdi,%r8 // preserve the original pointer so we can return it
64 movl %esi,%eax // replicate byte in %esi into all four bytes
69 orl %esi,%eax // now %eax has "c" in all 4 bytes
70 cmpq $(kShort),%rdx // is operand too short for SSE?
71 ja LCallCommpage // no
73 // Nonzero memset() too short to call commpage.
74 // %eax = replicated 4-byte pattern
76 // %edx = length (<= kShort)
78 cmpl $16,%edx // long enough to word align?
80 test %edx,%edx // length==0?
83 movb %al,(%rdi) // pack in a byte
89 movb %al,(%rdi) // pack in a byte
93 test $3,%edi // is ptr doubleword aligned?
95 movl %edx,%ecx // copy length
96 shrl $2,%edx // #doublewords to store
98 movl %eax,(%rdi) // store aligned doubleword
102 andl $3,%ecx // any leftover bytes?
105 movb %al,(%rdi) // pack in a byte
110 movq %r8,%rax // get return value (ie, original ptr)
113 // Nonzero memset() is long enough to call commpage.
114 // %eax = replicated 4-byte pattern
116 // %rdx = length (> kShort)
119 movd %eax,%xmm0 // move %eax to low 4 bytes of %xmm0
120 pshufd $(0x00),%xmm0,%xmm0 // replicate across the vector
121 movq %rdi,%rcx // copy dest ptr
123 andl $15,%ecx // get #bytes to align ptr
124 jz 2f // skip if already aligned
125 subq %rcx,%rdx // decrement length
127 movb %al,(%rdi) // pack in a byte
131 2: // ptr aligned, length long enough to justify
132 movq $(_COMM_PAGE_MEMSET_PATTERN),%rax
133 call *%rax // call commpage to do the heavy lifting
134 movq %r8,%rax // get return value (ie, original ptr)
138 // Handle memset of a 16-byte pattern.
140 .globl _memset_pattern16
142 _memset_pattern16: // void memset_pattern16(void *b, const void *c16, size_t len);
143 movdqu (%rsi),%xmm0 // load the pattern
147 // Handle memset of an 8-byte pattern.
149 .globl _memset_pattern8
151 _memset_pattern8: // void memset_pattern8(void *b, const void *c8, size_t len);
152 movq (%rsi),%xmm0 // load pattern into low 8 bytes
153 punpcklqdq %xmm0,%xmm0 // replicate into all 16
156 // Handle memset of a 4-byte pattern.
158 .globl _memset_pattern4
160 _memset_pattern4: // void memset_pattern4(void *b, const void *c4, size_t len);
161 movd (%rsi),%xmm0 // load pattern into low 4 bytes
162 pshufd $(0x00),%xmm0,%xmm0 // replicate the 4 bytes across the vector
165 // Align ptr if necessary. We must rotate the pattern right for each byte we
166 // store while aligning the ptr. Since there is no rotate instruction in SSE3,
167 // we have to synthesize the rotates.
172 LAlignPtr: // NB: can drop down to here!
173 cmpq $100,%rdx // long enough to bother aligning ptr?
174 movq %rdi,%rcx // copy ptr
175 jb LReady // not long enough
177 andl $15,%ecx // get #bytes to align ptr
178 jz LReady // already aligned
179 subq %rcx,%rdx // adjust length
181 test $1,%cl // 1-byte store required?
182 movd %xmm0,%eax // get 4 low bytes in %eax
184 movdqa %xmm0,%xmm1 // copy pattern so we can shift in both directions
185 movb %al,(%rdi) // pack in the low-order byte
186 psrldq $1,%xmm0 // shift pattern right 1 byte
188 pslldq $15,%xmm1 // shift pattern left 15 bytes
189 shrl $8,%eax // in case 2-byte store is required
190 por %xmm1,%xmm0 // complete right rotate of pattern by 1 byte
192 test $2,%cl // 2-byte store required?
194 psrldq $2,%xmm0 // shift pattern down 2 bytes
195 movw %ax,(%rdi) // pack in next two bytes
196 pinsrw $7,%eax,%xmm0 // insert low word of %eax into high word of %xmm0
197 addq $2,%rdi // adjust ptr
199 test $4,%cl // 4-byte store required?
201 movd %xmm0,(%rdi) // store low 4 bytes of %xmm0
202 pshufd $(0x39),%xmm0,%xmm0 // rotate %xmm0 right 4 bytes (mask == 00 11 10 01)
203 addq $4,%rdi // adjust ptr
205 test $8,%cl // 8-byte store required?
207 movq %xmm0,(%rdi) // store low 8 bytes of %xmm0
208 pshufd $(0x4e),%xmm0,%xmm0 // rotate %xmm0 right 8 bytes (mask == 01 00 11 10)
209 addq $8,%rdi // adjust ptr
211 // Ptr is aligned if practical, we're ready to call commpage to do the heavy lifting.
214 movq $(_COMM_PAGE_MEMSET_PATTERN),%rax
215 call *%rax // call commpage to do the heavy lifting