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