]>
Commit | Line | Data |
---|---|---|
f24a6d08 | 1 | /* copy no more than N bytes from SRC to DEST, returning the address of |
5929563f | 2 | the terminating '\0' in DEST. |
6d52618b | 3 | For Intel 80x86, x>=3. |
2b778ceb | 4 | Copyright (C) 1994-2021 Free Software Foundation, Inc. |
6d52618b UD |
5 | This file is part of the GNU C Library. |
6 | Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu> | |
7 | Some bug fixes by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au> | |
8 | - original wrote n+1 chars in some cases. | |
9 | - stpncpy() ought to behave like strncpy() ie. not null-terminate | |
10 | if limited by n. glibc-1.09 stpncpy() does this. | |
11 | ||
12 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
13 | modify it under the terms of the GNU Lesser General Public |
14 | License as published by the Free Software Foundation; either | |
15 | version 2.1 of the License, or (at your option) any later version. | |
6d52618b UD |
16 | |
17 | The GNU C Library is distributed in the hope that it will be useful, | |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 20 | Lesser General Public License for more details. |
6d52618b | 21 | |
41bdb6e2 | 22 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 23 | License along with the GNU C Library; if not, see |
5a82c748 | 24 | <https://www.gnu.org/licenses/>. */ |
8f5ca04b RM |
25 | |
26 | #include <sysdep.h> | |
27 | #include "asm-syntax.h" | |
28 | ||
2366713d | 29 | #define PARMS 4+4 /* space for 1 saved reg */ |
3f02f778 | 30 | #define RTN PARMS |
2366713d JM |
31 | #define DEST RTN |
32 | #define SRC DEST+4 | |
33 | #define LEN SRC+4 | |
8f5ca04b RM |
34 | |
35 | .text | |
2366713d | 36 | ENTRY (__stpncpy) |
8f5ca04b RM |
37 | |
38 | pushl %esi | |
1ad9da69 | 39 | cfi_adjust_cfa_offset (4) |
8f5ca04b | 40 | |
3f02f778 GM |
41 | movl DEST(%esp), %eax |
42 | movl SRC(%esp), %esi | |
1ad9da69 | 43 | cfi_rel_offset (esi, 0) |
3f02f778 | 44 | movl LEN(%esp), %ecx |
8f5ca04b RM |
45 | |
46 | subl %eax, %esi /* magic: reduce number of loop variants | |
47 | to one using addressing mode */ | |
5929563f | 48 | jmp L(1) /* jump to loop "head" */ |
8f5ca04b RM |
49 | |
50 | ALIGN(4) | |
51 | ||
52 | /* Four times unfolded loop with two loop counters. We get the | |
ded5b9b7 | 53 | third value (the source address) by using the index+base |
6d52618b | 54 | addressing mode. */ |
5929563f | 55 | L(2): movb (%eax,%esi), %dl /* load current char */ |
8f5ca04b RM |
56 | movb %dl, (%eax) /* and store it */ |
57 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 58 | jz L(7) /* yes, then exit */ |
8f5ca04b RM |
59 | |
60 | movb 1(%eax,%esi), %dl /* load current char */ | |
61 | movb %dl, 1(%eax) /* and store it */ | |
62 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 63 | jz L(6) /* yes, then exit */ |
8f5ca04b RM |
64 | |
65 | movb 2(%eax,%esi), %dl /* load current char */ | |
66 | movb %dl, 2(%eax) /* and store it */ | |
67 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 68 | jz L(5) /* yes, then exit */ |
8f5ca04b RM |
69 | |
70 | movb 3(%eax,%esi), %dl /* load current char */ | |
71 | movb %dl, 3(%eax) /* and store it */ | |
72 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 73 | jz L(4) /* yes, then exit */ |
8f5ca04b RM |
74 | |
75 | addl $4, %eax /* increment loop counter for full round */ | |
76 | ||
5929563f UD |
77 | L(1): subl $4, %ecx /* still more than 4 bytes allowed? */ |
78 | jae L(2) /* yes, then go to start of loop */ | |
8f5ca04b RM |
79 | |
80 | /* The maximal remaining 15 bytes are not processed in a loop. */ | |
81 | ||
82 | addl $4, %ecx /* correct above subtraction */ | |
5929563f | 83 | jz L(9) /* maximal allowed char reached => go to end */ |
8f5ca04b RM |
84 | |
85 | movb (%eax,%esi), %dl /* load current char */ | |
86 | movb %dl, (%eax) /* and store it */ | |
87 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 88 | jz L(3) /* yes, then exit */ |
8f5ca04b RM |
89 | |
90 | incl %eax /* increment pointer */ | |
91 | decl %ecx /* decrement length counter */ | |
5929563f | 92 | jz L(9) /* no more allowed => exit */ |
8f5ca04b RM |
93 | |
94 | movb (%eax,%esi), %dl /* load current char */ | |
95 | movb %dl, (%eax) /* and store it */ | |
96 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 97 | jz L(3) /* yes, then exit */ |
8f5ca04b RM |
98 | |
99 | incl %eax /* increment pointer */ | |
100 | decl %ecx /* decrement length counter */ | |
5929563f | 101 | jz L(9) /* no more allowed => exit */ |
8f5ca04b RM |
102 | |
103 | movb (%eax,%esi), %dl /* load current char */ | |
104 | movb %dl, (%eax) /* and store it */ | |
105 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 106 | jz L(3) /* yes, then exit */ |
8f5ca04b RM |
107 | |
108 | incl %eax /* increment pointer */ | |
5929563f | 109 | jmp L(9) /* we don't have to test for counter underflow |
8f5ca04b RM |
110 | because we know we had a most 3 bytes |
111 | remaining => exit */ | |
112 | ||
113 | /* When coming from the main loop we have to adjust the pointer. */ | |
5929563f | 114 | L(4): decl %ecx /* decrement counter */ |
8f5ca04b RM |
115 | incl %eax /* increment pointer */ |
116 | ||
5929563f | 117 | L(5): decl %ecx /* increment pointer */ |
8f5ca04b RM |
118 | incl %eax /* increment pointer */ |
119 | ||
5929563f | 120 | L(6): decl %ecx /* increment pointer */ |
8f5ca04b | 121 | incl %eax /* increment pointer */ |
5929563f | 122 | L(7): |
8f5ca04b RM |
123 | |
124 | addl $3, %ecx /* correct pre-decrementation of counter | |
125 | at the beginning of the loop; but why 3 | |
126 | and not 4? Very simple, we have to count | |
127 | the NUL char we already wrote. */ | |
5929563f | 128 | jz L(9) /* counter is also 0 => exit */ |
8f5ca04b RM |
129 | |
130 | /* We now have to fill the rest of the buffer with NUL. This | |
6d52618b | 131 | is done in a tricky way. Please note that the addressing mode |
8f5ca04b RM |
132 | used below is not the same we used above. Here we use the |
133 | %ecx register. */ | |
5929563f | 134 | L(8): |
8f5ca04b | 135 | movb $0, (%ecx,%eax) /* store NUL char */ |
5929563f UD |
136 | L(3): decl %ecx /* all bytes written? */ |
137 | jnz L(8) /* no, then again */ | |
8f5ca04b | 138 | |
92945b52 | 139 | L(9): popl %esi /* restore saved register content */ |
1ad9da69 UD |
140 | cfi_adjust_cfa_offset (-4) |
141 | cfi_restore (esi) | |
8f5ca04b | 142 | |
2366713d JM |
143 | ret |
144 | END (__stpncpy) | |
8f5ca04b | 145 | |
2366713d JM |
146 | libc_hidden_def (__stpncpy) |
147 | weak_alias (__stpncpy, stpncpy) |