1 /* Optimized version of the standard strncpy() function.
2 This file is part of the GNU C Library.
3 Copyright (C) 2000-2023 Free Software Foundation, Inc.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
26 In this form, it assumes little endian mode.
52 alloc r2 = ar.pfs, 3, 0, 29, 32
58 mov ret0 = in0 // return value = dest
60 mov saved_pr = pr // save the predicate registers
62 mov saved_lc = ar.lc // save the loop counter
63 mov ar.ec = 0 // ec is not guaranteed to
64 // be zero upon function entry
66 cmp.geu p6, p5 = 24, in2
67 (p6) br.cond.spnt .short_len
68 sub tmp = r0, in0 ;; // tmp = -dest
70 mov dest = in0 // dest
72 and tmp = 7, tmp ;; // loopcnt = -dest % 8
73 cmp.eq p6, p7 = tmp, r0
74 adds loopcnt = -1, tmp // --loopcnt
75 (p6) br.cond.sptk .dest_aligned ;;
76 sub len = len, tmp // len -= -dest % 8
78 .l1: // copy -dest % 8 bytes
79 (p5) ld1 c = [src], 1 // c = *src++
81 st1 [dest] = c, 1 // *dest++ = c
84 (p7) br.cond.dpnt .found0_align
86 .dest_aligned: // p7 should be cleared here
87 shr.u c = len, 3 // c = len / 8
88 and sh1 = 7, src // sh1 = src % 8
89 and asrc = -8, src ;; // asrc = src & -OPSIZ -- align src
90 adds c = (MEMLAT-1), c // c = (len / 8) + MEMLAT - 1
92 mov pr.rot = 1 << 16 // set rotating predicates
93 shl sh1 = sh1, 3 ;; // sh1 = 8 * (src % 8)
94 mov ar.lc = c // "infinite" loop
95 sub sh2 = 64, sh1 // sh2 = 64 - sh1
96 cmp.eq p6, p0 = sh1, r0 // is the src aligned?
97 (p6) br.cond.sptk .src_aligned
98 adds c = -(MEMLAT-1), c ;; // c = (len / 8)
104 (p6) st8 [dest] = value, 8 // store val to dest
105 ld8.s r[0] = [asrc], 8
106 shr.u value = r[1], sh1 ;; // value = w0 >> sh1
107 czx1.r pos = value ;; // do we have an "early" zero
108 cmp.lt p7, p0 = pos, thresh // in w0 >> sh1?
109 adds len = -8, len // len -= 8
110 (p7) br.cond.dpnt .nonalign_found0
111 chk.s r[0], .recovery2 // it is safe to do that only
112 .back2: // after the previous test
113 shl tmp = r[0], sh2 // tmp = w1 << sh2
115 or value = value, tmp ;; // value |= tmp
116 czx1.r pos = value ;;
117 cmp.ne p7, p6 = 8, pos
118 (p7) br.cond.dpnt .nonalign_found0
121 br.cond.sptk .not_found0 ;;
123 cmp.gtu p6, p0 = -8, len
124 (p6) br.cond.dptk .found0
126 br.cond.sptk .not_found0 ;;
131 (p[0]) ld8.s r[0] = [src], 8
132 (p[MEMLAT]) chk.s r[MEMLAT], .recovery3
134 (p[MEMLAT]) mov value = r[MEMLAT]
135 (p[MEMLAT]) czx1.r pos = r[MEMLAT] ;;
136 (p[MEMLAT]) cmp.ne p7, p0 = 8, pos
137 (p[MEMLAT]) adds len = -8, len // len -= 8
138 (p7) br.cond.dpnt .found0
139 (p[MEMLAT]) st8 [dest] = r[MEMLAT], 8
142 chk.s r[MEMLAT-1], .recovery4
144 mov value = r[MEMLAT-1]
147 cmp.eq p5, p6 = len, r0
149 (p5) br.cond.dptk .restore_and_exit ;;
152 (p6) extr.u c = value, 0, 8 // c = value & 0xff
153 (p6) shr.u value = value, 8 ;;
155 cmp.ne p6, p0 = c, r0
157 br.cond.sptk .restore_and_exit
165 shr.u loopcnt = len, 4 // loopcnt = len / 16
167 cmp.eq p6, p0 = loopcnt, r0
168 adds loopcnt = -1, loopcnt
171 andcm value = value, c
172 mov ar.lc = loopcnt ;;
173 cmp.le p7, p0 = 8, len
174 adds dest2 = 16, dest
175 st8 [dest] = value, 8
177 (p6) br.cond.dpnt .l6 ;;
183 (p7) st8 [dest] = r0, 8
184 cmp.eq p5, p0 = len, r0
186 (p5) br.cond.dptk .restore_and_exit ;;
192 mov ar.lc = saved_lc // restore the loop counter
193 mov pr = saved_pr, -1 // restore the predicate registers
197 cmp.eq p5, p0 = in2, r0
198 adds loopcnt = -1, in2
199 (p5) br.cond.spnt .restore_and_exit ;;
200 mov ar.lc = loopcnt // p6 should be set when we get here
202 (p6) ld1 c = [in1], 1 // c = *src++
204 st1 [in0] = c, 1 // *dest++ = c
205 (p6) cmp.ne p6, p0 = c, r0
208 mov ar.lc = saved_lc // restore the loop counter
209 mov pr = saved_pr, -1 // restore the predicate registers
213 add tmp = -8, asrc ;;
214 cmp.gtu p8, p5 = c, thresh ;;
215 (p8) ld8 r[0] = [tmp]
219 add tmp = -(MEMLAT + 1) * 8, src ;;
220 ld8 r[MEMLAT] = [tmp]
223 cmp.eq p5, p6 = len, r0
224 add tmp = -MEMLAT * 8, src ;;
225 (p6) ld8 r[MEMLAT - 1] = [tmp]
226 (p5) mov r[MEMLAT - 1] = r0
229 libc_hidden_builtin_def (strncpy)