2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 #define __APPLE_API_PRIVATE
27 #include <machine/cpu_capabilities.h>
28 #undef __APPLE_API_PRIVATE
30 // Bzero has migrated to the comm page.
37 _bzero: // void bzero(void *b, size_t len);
41 _memset: // void * memset(void *b, int c, size_t len);
42 andi. r9,r4,0xFF // copy "c" and test for 0
43 mr r4,r5 // move length down to where bzero() expects it
44 beqa++ _COMM_PAGE_BZERO // c==0, so treat like bzero()
46 // The nonzero memset() case is uncommon.
48 cmplwi r5,8 // too short to align?
49 rlwimi r9,r9,8,16,23 // replicate c to all 4 bytes
50 neg r7,r3 // start to compute #bytes to word align
51 mr r8,r3 // copy ptr so we can preserve r3
53 blt 4f // fewer than 8 bytes
54 andi. r0,r7,3 // get #bytes to word align
55 mtcrf 0x01,r7 // set up #bytes to word align
56 sub r5,r5,r0 // adjust length for word alignment
57 srwi r6,r5,3 // get #8-byte chunks to memset()
58 cmplwi cr1,r6,0 // any chunks?
60 beq 3f // already word aligned (r6!=0)
70 bne cr1,3f // handle 8-byte chunks
80 // Store up to 8 leftover bytes.
81 // r9 = value in all 4 bytes
86 mtcrf 0x01,r5 // move remaining length to cr7
102 // =============================
103 // BZERO and MEMSET FOR Mac OS X
104 // =============================
106 // We use DCBZ, and therefore are dependent on the cache block size (32.)
107 // Bzero and memset need to be in the same file since they are tightly
108 // coupled, so we can use bzero for memset of 0 without incurring extra
109 // overhead. (The issue is that bzero must preserve r3 for memset.)
112 // r3 = original ptr, not changed since memset returns it
113 // r4 = count of bytes to set ("rc")
114 // r11 = working operand ptr ("rp")
115 // r10 = value to set ("rv")
121 #include <architecture/ppc/asm_help.h>
132 _bzero: // void bzero(void *b, size_t len);
133 cmplwi cr1,rc,32 // too short for DCBZ?
135 Lbzero1: // enter from memset with cr1 and rv set up
136 neg r5,r3 // start to compute bytes to align
137 mr rp,r3 // make copy of operand ptr
138 andi. r6,r5,0x1F // r6 <- bytes to align on cache block
139 blt- cr1,Ltail // <32, so skip DCBZs
140 beq- cr0,Ldcbz // already aligned
142 // align on 32-byte boundary
144 mtcrf 0x01,r6 // move length to cr7 (faster if only 1 cr)
145 andi. r7,r6,16 // test bit 27 by hand
146 sub rc,rc,r6 // adjust length
147 bf 31,1f // test bits of count
171 // DCBZ 32-byte cache blocks
173 srwi. r5,rc,5 // r5 <- number of cache blocks to zero
175 mtctr r5 // set up loop count
176 andi. rc,rc,0x1F // will there be leftovers?
178 dcbz 0,rp // zero 32 bytes
181 beqlr // no leftovers so done
183 // store up to 31 trailing bytes
184 // rv = value to store (in all 4 bytes)
185 // rc = #bytes to store (0..31)
187 andi. r5,rc,16 // bit 27 set in length?
188 mtcrf 0x01,rc // low 4 bits of length to cr7
189 beq 1f // test bits of length
219 _memset: // void * memset(void *b, int c, size_t len);
220 andi. rv,r4,0xFF // copy value to working register, test for 0
221 mr rc,r5 // move length to working register
222 cmplwi cr1,r5,32 // length < 32 ?
223 beq Lbzero1 // memset of 0 is just a bzero
224 rlwimi rv,rv,8,16,23 // replicate value to low 2 bytes
225 mr rp,r3 // make working copy of operand ptr
226 rlwimi rv,rv,16,0,15 // value now in all 4 bytes
227 blt cr1,Ltail // length<32, so use common tail routine
228 neg r5,rp // start to compute #bytes to align
229 andi. r6,r5,0x7 // r6 <- #bytes to align on dw
230 beq- Lmemset1 // already aligned
232 ; align on 8-byte boundary
234 mtcrf 0x01,r6 // move count to cr7 (faster if only 1 cr)
235 sub rc,rc,r6 // adjust length
248 // loop on 16-byte blocks
250 stw rv,0(rp) // store first 8 bytes from rv
252 srwi r5,rc,4 // r5 <- #blocks (>=1)
253 mtcrf 0x01,rc // leftover length to cr7
254 mtctr r5 // set up loop count
255 lfd f0,0(rp) // pick up in a fp register
256 b 2f // enter loop in middle
258 1: // loop on 16-byte blocks
265 // store up to 16 trailing bytes (count in cr7)