1 | /* Copyright (C) 1996-2022 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library. If not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | /* Copy no more than COUNT bytes of the null-terminated string from |
19 | SRC to DST. If SRC does not cover all of COUNT, the balance is |
20 | zeroed. Return the address of the terminating null in DEST, if |
21 | any, else DEST + COUNT. */ |
22 | |
23 | #include <sysdep.h> |
24 | |
25 | .set noat |
26 | .set noreorder |
27 | |
28 | .text |
29 | |
30 | ENTRY(__stpncpy) |
31 | ldgp gp, 0(pv) |
32 | #ifdef PROF |
33 | lda AT, _mcount |
34 | jsr AT, (AT), _mcount |
35 | #endif |
36 | .prologue 1 |
37 | |
38 | beq a2, $zerocount |
39 | jsr t9, __stxncpy # do the work of the copy |
40 | |
41 | and t8, 0xf0, t3 # binary search for byte offset of the |
42 | and t8, 0xcc, t2 # last byte written. |
43 | and t8, 0xaa, t1 |
44 | andnot a0, 7, v0 |
45 | cmovne t3, 4, t3 |
46 | cmovne t2, 2, t2 |
47 | cmovne t1, 1, t1 |
48 | addq v0, t3, v0 |
49 | addq t1, t2, t1 |
50 | addq v0, t1, v0 |
51 | |
52 | bne a2, $multiword # do we have full words left? |
53 | |
54 | .align 3 |
55 | zapnot t0, t8, t4 # e0 : was last byte a null? |
56 | subq t8, 1, t2 # .. e1 : |
57 | addq v0, 1, t5 # e0 : |
58 | subq t10, 1, t3 # .. e1 : |
59 | or t2, t8, t2 # e0 : clear the bits between the last |
60 | or t3, t10, t3 # .. e1 : written byte and the last byte in |
61 | andnot t3, t2, t3 # e0 : COUNT |
62 | cmovne t4, t5, v0 # .. e1 : if last written wasnt null, inc v0 |
63 | zap t0, t3, t0 # e0 : |
64 | stq_u t0, 0(a0) # e1 : |
65 | ret # .. e1 : |
66 | |
67 | .align 3 |
68 | $multiword: |
69 | subq t8, 1, t7 # e0 : clear the final bits in the prev |
70 | or t7, t8, t7 # e1 : word |
71 | zapnot t0, t7, t0 # e0 : |
72 | subq a2, 1, a2 # .. e1 : |
73 | stq_u t0, 0(a0) # e0 : |
74 | addq a0, 8, a0 # .. e1 : |
75 | |
76 | beq a2, 1f # e1 : |
77 | blbc a2, 0f # e1 : |
78 | |
79 | stq_u zero, 0(a0) # e0 : zero one word |
80 | subq a2, 1, a2 # .. e1 : |
81 | addq a0, 8, a0 # e0 : |
82 | beq a2, 1f # .. e1 : |
83 | |
84 | 0: stq_u zero, 0(a0) # e0 : zero two words |
85 | subq a2, 2, a2 # .. e1 : |
86 | stq_u zero, 8(a0) # e0 : |
87 | addq a0, 16, a0 # .. e1 : |
88 | bne a2, 0b # e1 : |
89 | unop |
90 | |
91 | 1: ldq_u t0, 0(a0) # e0 : clear the leading bits in the final |
92 | subq t10, 1, t7 # .. e1 : word |
93 | or t7, t10, t7 # e0 : |
94 | zap t0, t7, t0 # e1 (stall) |
95 | stq_u t0, 0(a0) # e0 : |
96 | ret # .. e1 : |
97 | |
98 | $zerocount: |
99 | mov a0, v0 |
100 | ret |
101 | |
102 | END(__stpncpy) |
103 | |
104 | libc_hidden_def (__stpncpy) |
105 | weak_alias (__stpncpy, stpncpy) |
106 | |