1 /* Optimized version of the standard strncpy() function.
2 This file is part of the GNU C Library.
3 Copyright (C) 2000-2017 Free Software Foundation, Inc.
4 Contributed by Dan Pop <Dan.Pop@cern.ch>
5 and Jakub Jelinek <jakub@redhat.com>.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
28 In this form, it assumes little endian mode.
54 alloc r2 = ar.pfs, 3, 0, 29, 32
60 mov ret0 = in0 // return value = dest
62 mov saved_pr = pr // save the predicate registers
64 mov saved_lc = ar.lc // save the loop counter
65 mov ar.ec = 0 // ec is not guaranteed to
66 // be zero upon function entry
68 cmp.geu p6, p5 = 24, in2
69 (p6) br.cond.spnt .short_len
70 sub tmp = r0, in0 ;; // tmp = -dest
72 mov dest = in0 // dest
74 and tmp = 7, tmp ;; // loopcnt = -dest % 8
75 cmp.eq p6, p7 = tmp, r0
76 adds loopcnt = -1, tmp // --loopcnt
77 (p6) br.cond.sptk .dest_aligned ;;
78 sub len = len, tmp // len -= -dest % 8
80 .l1: // copy -dest % 8 bytes
81 (p5) ld1 c = [src], 1 // c = *src++
83 st1 [dest] = c, 1 // *dest++ = c
86 (p7) br.cond.dpnt .found0_align
88 .dest_aligned: // p7 should be cleared here
89 shr.u c = len, 3 // c = len / 8
90 and sh1 = 7, src // sh1 = src % 8
91 and asrc = -8, src ;; // asrc = src & -OPSIZ -- align src
92 adds c = (MEMLAT-1), c // c = (len / 8) + MEMLAT - 1
94 mov pr.rot = 1 << 16 // set rotating predicates
95 shl sh1 = sh1, 3 ;; // sh1 = 8 * (src % 8)
96 mov ar.lc = c // "infinite" loop
97 sub sh2 = 64, sh1 // sh2 = 64 - sh1
98 cmp.eq p6, p0 = sh1, r0 // is the src aligned?
99 (p6) br.cond.sptk .src_aligned
100 adds c = -(MEMLAT-1), c ;; // c = (len / 8)
106 (p6) st8 [dest] = value, 8 // store val to dest
107 ld8.s r[0] = [asrc], 8
108 shr.u value = r[1], sh1 ;; // value = w0 >> sh1
109 czx1.r pos = value ;; // do we have an "early" zero
110 cmp.lt p7, p0 = pos, thresh // in w0 >> sh1?
111 adds len = -8, len // len -= 8
112 (p7) br.cond.dpnt .nonalign_found0
113 chk.s r[0], .recovery2 // it is safe to do that only
114 .back2: // after the previous test
115 shl tmp = r[0], sh2 // tmp = w1 << sh2
117 or value = value, tmp ;; // value |= tmp
118 czx1.r pos = value ;;
119 cmp.ne p7, p6 = 8, pos
120 (p7) br.cond.dpnt .nonalign_found0
123 br.cond.sptk .not_found0 ;;
125 cmp.gtu p6, p0 = -8, len
126 (p6) br.cond.dptk .found0
128 br.cond.sptk .not_found0 ;;
133 (p[0]) ld8.s r[0] = [src], 8
134 (p[MEMLAT]) chk.s r[MEMLAT], .recovery3
136 (p[MEMLAT]) mov value = r[MEMLAT]
137 (p[MEMLAT]) czx1.r pos = r[MEMLAT] ;;
138 (p[MEMLAT]) cmp.ne p7, p0 = 8, pos
139 (p[MEMLAT]) adds len = -8, len // len -= 8
140 (p7) br.cond.dpnt .found0
141 (p[MEMLAT]) st8 [dest] = r[MEMLAT], 8
144 chk.s r[MEMLAT-1], .recovery4
146 mov value = r[MEMLAT-1]
149 cmp.eq p5, p6 = len, r0
151 (p5) br.cond.dptk .restore_and_exit ;;
154 (p6) extr.u c = value, 0, 8 // c = value & 0xff
155 (p6) shr.u value = value, 8 ;;
157 cmp.ne p6, p0 = c, r0
159 br.cond.sptk .restore_and_exit
167 shr.u loopcnt = len, 4 // loopcnt = len / 16
169 cmp.eq p6, p0 = loopcnt, r0
170 adds loopcnt = -1, loopcnt
173 andcm value = value, c
174 mov ar.lc = loopcnt ;;
175 cmp.le p7, p0 = 8, len
176 adds dest2 = 16, dest
177 st8 [dest] = value, 8
179 (p6) br.cond.dpnt .l6 ;;
185 (p7) st8 [dest] = r0, 8
186 cmp.eq p5, p0 = len, r0
188 (p5) br.cond.dptk .restore_and_exit ;;
194 mov ar.lc = saved_lc // restore the loop counter
195 mov pr = saved_pr, -1 // restore the predicate registers
199 cmp.eq p5, p0 = in2, r0
200 adds loopcnt = -1, in2
201 (p5) br.cond.spnt .restore_and_exit ;;
202 mov ar.lc = loopcnt // p6 should be set when we get here
204 (p6) ld1 c = [in1], 1 // c = *src++
206 st1 [in0] = c, 1 // *dest++ = c
207 (p6) cmp.ne p6, p0 = c, r0
210 mov ar.lc = saved_lc // restore the loop counter
211 mov pr = saved_pr, -1 // restore the predicate registers
215 add tmp = -8, asrc ;;
216 cmp.gtu p8, p5 = c, thresh ;;
217 (p8) ld8 r[0] = [tmp]
221 add tmp = -(MEMLAT + 1) * 8, src ;;
222 ld8 r[MEMLAT] = [tmp]
225 cmp.eq p5, p6 = len, r0
226 add tmp = -MEMLAT * 8, src ;;
227 (p6) ld8 r[MEMLAT - 1] = [tmp]
228 (p5) mov r[MEMLAT - 1] = r0
231 libc_hidden_builtin_def (strncpy)