]> git.saurik.com Git - apple/libc.git/blame - ppc/string/strcpy.s
Libc-262.2.12.tar.gz
[apple/libc.git] / ppc / string / strcpy.s
CommitLineData
734aad71
A
1/*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
29// ***************
30// * S T R C P Y *
31// ***************
32//
33// char* strcpy(const char *dst, const char *src);
34//
35// We optimize the move by doing it word parallel. This introduces
36// a complication: if we blindly did word load/stores until finding
37// a 0, we might get a spurious page fault by touching bytes past it.
38// To avoid this, we never do a "lwz" that crosses a page boundary,
39// and never store a byte we don't have to.
40//
41// The test for 0s relies on the following inobvious but very efficient
42// word-parallel test:
43// x = dataWord + 0xFEFEFEFF
44// y = ~dataWord & 0x80808080
45// if (x & y) == 0 then no zero found
46// The test maps any non-zero byte to zero, and any zero byte to 0x80,
47// with one exception: 0x01 bytes preceeding the first zero are also
48// mapped to 0x80.
49//
50// We align the _source_, which allows us to avoid all worries about
51// spurious page faults. Doing so is faster than aligning the dest.
52
53 .text
54 .globl EXT(strcpy)
55
56 .align 5
57LEXT(strcpy) // char* strcpy(const char *dst, const char *src);
58 andi. r0,r4,3 // is source aligned?
59 dcbt 0,r4 // touch in source
60 lis r6,hi16(0xFEFEFEFF) // start to load magic constants
61 lis r7,hi16(0x80808080)
62 dcbtst 0,r3 // touch in dst
63 ori r6,r6,lo16(0xFEFEFEFF)
64 ori r7,r7,lo16(0x80808080)
65 mr r9,r3 // use r9 for dest ptr (must return r3 intact)
66 beq LwordloopEnter // source is aligned
67 subfic r0,r0,4 // r0 <- #bytes to word align source
68 mtctr r0
69
70// Loop over bytes.
71// r4 = source ptr (unaligned)
72// r6 = 0xFEFEFEFF
73// r7 = 0x80808080
74// r9 = dest ptr (unaligned)
75// ctr = byte count
76
77Lbyteloop:
78 lbz r8,0(r4) // r8 <- next source byte
79 addi r4,r4,1
80 cmpwi r8,0 // 0 ?
81 stb r8,0(r9) // pack into dest
82 addi r9,r9,1
83 bdnzf eq,Lbyteloop // loop until (ctr==0) | (r8==0)
84
85 bne LwordloopEnter // 0-byte not found, so enter word loop
86 blr // 0-byte found, done
87
88// Word loop: move a word at a time until 0-byte found.
89// r4 = source ptr (word aligned)
90// r6 = 0xFEFEFEFF
91// r7 = 0x80808080
92// r9 = dest ptr (unaligned)
93
94 .align 5 // align inner loop, which is 8 words ling
95Lwordloop:
96 stw r8,0(r9) // pack word into destination
97 addi r9,r9,4
98LwordloopEnter:
99 lwz r8,0(r4) // r8 <- next 4 source bytes
100 addi r4,r4,4
101 add r10,r8,r6 // r10 <- word + 0xFEFEFEFF
102 andc r12,r7,r8 // r12 <- ~word & 0x80808080
103 and. r0,r10,r12 // r0 <- nonzero iff word has a 0-byte
104 beq Lwordloop // loop if ctr!=0 and cr0_eq
105
106// Found a 0-byte. Store last word up to and including the 0, a byte at a time.
107// r8 = last word, known to have a 0-byte
108// r9 = dest ptr
109
110Lstorelastbytes:
111 srwi. r0,r8,24 // right justify next byte and test for 0
112 slwi r8,r8,8 // shift next byte into position
113 stb r0,0(r9) // pack into dest
114 addi r9,r9,1
115 bne Lstorelastbytes // loop until 0 stored
116
117 blr
118