]>
Commit | Line | Data |
---|---|---|
9385eb3d A |
1 | /* |
2 | * Copyright (c) 2002 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
59e0d9fe A |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
7 | * | |
9385eb3d A |
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 | |
13 | * file. | |
14 | * | |
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. | |
22 | * | |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | #define ASSEMBLER | |
26 | #include <mach/ppc/asm.h> | |
27 | #undef ASSEMBLER | |
28 | ||
59e0d9fe A |
29 | #define __APPLE_API_PRIVATE |
30 | #include <machine/cpu_capabilities.h> | |
31 | #undef __APPLE_API_PRIVATE | |
32 | ||
33 | /* We use mode-independent "g" opcodes such as "srgi". These expand | |
34 | * into word operations when targeting __ppc__, and into doubleword | |
35 | * operations when targeting __ppc64__. | |
36 | */ | |
37 | #include <architecture/ppc/mode_independent_asm.h> | |
38 | ||
39 | ||
9385eb3d A |
40 | // *************** |
41 | // * S T R C P Y * | |
42 | // *************** | |
43 | // | |
44 | // char* strcpy(const char *dst, const char *src); | |
45 | // | |
46 | // We optimize the move by doing it word parallel. This introduces | |
47 | // a complication: if we blindly did word load/stores until finding | |
48 | // a 0, we might get a spurious page fault by touching bytes past it. | |
49 | // To avoid this, we never do a "lwz" that crosses a page boundary, | |
50 | // and never store a byte we don't have to. | |
51 | // | |
52 | // The test for 0s relies on the following inobvious but very efficient | |
53 | // word-parallel test: | |
54 | // x = dataWord + 0xFEFEFEFF | |
55 | // y = ~dataWord & 0x80808080 | |
56 | // if (x & y) == 0 then no zero found | |
57 | // The test maps any non-zero byte to zero, and any zero byte to 0x80, | |
58 | // with one exception: 0x01 bytes preceeding the first zero are also | |
59 | // mapped to 0x80. | |
60 | // | |
61 | // We align the _source_, which allows us to avoid all worries about | |
62 | // spurious page faults. Doing so is faster than aligning the dest. | |
59e0d9fe A |
63 | // |
64 | // In 64-bit mode, the algorithm is doubleword parallel. | |
9385eb3d A |
65 | |
66 | .text | |
67 | .globl EXT(strcpy) | |
68 | ||
69 | .align 5 | |
59e0d9fe A |
70 | LEXT(strcpy) // char* strcpy(const char *dst, const char *src); |
71 | andi. r0,r4,GPR_BYTES-1 // is source aligned? | |
72 | #if defined(__ppc__) | |
73 | lis r6,hi16(0xFEFEFEFF) // start to generate 32-bit magic constants | |
9385eb3d | 74 | lis r7,hi16(0x80808080) |
9385eb3d A |
75 | ori r6,r6,lo16(0xFEFEFEFF) |
76 | ori r7,r7,lo16(0x80808080) | |
59e0d9fe A |
77 | #else |
78 | ld r6,_COMM_PAGE_MAGIC_FE(0) // get 0xFEFEFEFE FEFEFEFF from commpage | |
79 | ld r7,_COMM_PAGE_MAGIC_80(0) // get 0x80808080 80808080 from commpage | |
80 | #endif | |
9385eb3d A |
81 | mr r9,r3 // use r9 for dest ptr (must return r3 intact) |
82 | beq LwordloopEnter // source is aligned | |
59e0d9fe | 83 | subfic r0,r0,GPR_BYTES // r0 <- #bytes to align source |
9385eb3d A |
84 | mtctr r0 |
85 | ||
86 | // Loop over bytes. | |
87 | // r4 = source ptr (unaligned) | |
88 | // r6 = 0xFEFEFEFF | |
89 | // r7 = 0x80808080 | |
90 | // r9 = dest ptr (unaligned) | |
91 | // ctr = byte count | |
92 | ||
93 | Lbyteloop: | |
94 | lbz r8,0(r4) // r8 <- next source byte | |
95 | addi r4,r4,1 | |
96 | cmpwi r8,0 // 0 ? | |
97 | stb r8,0(r9) // pack into dest | |
98 | addi r9,r9,1 | |
99 | bdnzf eq,Lbyteloop // loop until (ctr==0) | (r8==0) | |
100 | ||
101 | bne LwordloopEnter // 0-byte not found, so enter word loop | |
102 | blr // 0-byte found, done | |
103 | ||
59e0d9fe A |
104 | // Word loop: move a word or doubleword at a time until 0-byte found. |
105 | // r4 = source ptr (aligned) | |
9385eb3d A |
106 | // r6 = 0xFEFEFEFF |
107 | // r7 = 0x80808080 | |
108 | // r9 = dest ptr (unaligned) | |
109 | ||
110 | .align 5 // align inner loop, which is 8 words ling | |
111 | Lwordloop: | |
59e0d9fe A |
112 | stg r8,0(r9) // pack word or doubleword into destination |
113 | addi r9,r9,GPR_BYTES | |
9385eb3d | 114 | LwordloopEnter: |
59e0d9fe A |
115 | lg r8,0(r4) // r8 <- next source word or doubleword |
116 | addi r4,r4,GPR_BYTES | |
9385eb3d A |
117 | add r10,r8,r6 // r10 <- word + 0xFEFEFEFF |
118 | andc r12,r7,r8 // r12 <- ~word & 0x80808080 | |
119 | and. r0,r10,r12 // r0 <- nonzero iff word has a 0-byte | |
59e0d9fe | 120 | beq Lwordloop // loop if no 0-byte |
9385eb3d A |
121 | |
122 | // Found a 0-byte. Store last word up to and including the 0, a byte at a time. | |
59e0d9fe | 123 | // r8 = last word or doubleword, known to have a 0-byte |
9385eb3d A |
124 | // r9 = dest ptr |
125 | ||
126 | Lstorelastbytes: | |
59e0d9fe A |
127 | srgi. r0,r8,GPR_BYTES*8-8 // shift leftmost byte into bottom so we can "stb" |
128 | slgi r8,r8,8 // move on to next | |
9385eb3d A |
129 | stb r0,0(r9) // pack into dest |
130 | addi r9,r9,1 | |
131 | bne Lstorelastbytes // loop until 0 stored | |
132 | ||
133 | blr | |
134 |