xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Optimized version of the standard strncpy() function. |
| 2 | This file is part of the GNU C Library. |
| 3 | Copyright (C) 2000-2016 Free Software Foundation, Inc. |
| 4 | Contributed by Dan Pop <Dan.Pop@cern.ch> |
| 5 | and Jakub Jelinek <jakub@redhat.com>. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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/>. */ |
| 20 | |
| 21 | /* Return: dest |
| 22 | |
| 23 | Inputs: |
| 24 | in0: dest |
| 25 | in1: src |
| 26 | in2: len |
| 27 | |
| 28 | In this form, it assumes little endian mode. |
| 29 | */ |
| 30 | |
| 31 | #include <sysdep.h> |
| 32 | #undef ret |
| 33 | |
| 34 | #define saved_lc r15 |
| 35 | #define saved_pr r16 |
| 36 | #define thresh r17 |
| 37 | #define dest r18 |
| 38 | #define dest2 r19 |
| 39 | #define src r20 |
| 40 | #define len r21 |
| 41 | #define asrc r22 |
| 42 | #define tmp r23 |
| 43 | #define pos r24 |
| 44 | #define w0 r25 |
| 45 | #define w1 r26 |
| 46 | #define c r27 |
| 47 | #define sh2 r28 |
| 48 | #define sh1 r29 |
| 49 | #define loopcnt r30 |
| 50 | #define value r31 |
| 51 | |
| 52 | ENTRY(strncpy) |
| 53 | .prologue |
| 54 | alloc r2 = ar.pfs, 3, 0, 29, 32 |
| 55 | |
| 56 | #define MEMLAT 2 |
| 57 | .rotr r[MEMLAT + 2] |
| 58 | .rotp p[MEMLAT + 1] |
| 59 | |
| 60 | mov ret0 = in0 // return value = dest |
| 61 | .save pr, saved_pr |
| 62 | mov saved_pr = pr // save the predicate registers |
| 63 | .save ar.lc, saved_lc |
| 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 |
| 67 | .body |
| 68 | cmp.geu p6, p5 = 24, in2 |
| 69 | (p6) br.cond.spnt .short_len |
| 70 | sub tmp = r0, in0 ;; // tmp = -dest |
| 71 | mov len = in2 // len |
| 72 | mov dest = in0 // dest |
| 73 | mov src = in1 // src |
| 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 |
| 79 | mov ar.lc = loopcnt |
| 80 | .l1: // copy -dest % 8 bytes |
| 81 | (p5) ld1 c = [src], 1 // c = *src++ |
| 82 | ;; |
| 83 | st1 [dest] = c, 1 // *dest++ = c |
| 84 | cmp.ne p5, p7 = c, r0 |
| 85 | br.cloop.dptk .l1 ;; |
| 86 | (p7) br.cond.dpnt .found0_align |
| 87 | |
| 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 |
| 93 | sub thresh = 8, sh1 |
| 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) |
| 101 | ld8 r[1] = [asrc],8 |
| 102 | mov ar.lc = c ;; |
| 103 | |
| 104 | .align 32 |
| 105 | .l2: |
| 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 |
| 116 | ;; |
| 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 |
| 121 | br.ctop.dptk .l2 ;; |
| 122 | adds len = 8, len |
| 123 | br.cond.sptk .not_found0 ;; |
| 124 | .nonalign_found0: |
| 125 | cmp.gtu p6, p0 = -8, len |
| 126 | (p6) br.cond.dptk .found0 |
| 127 | adds len = 8, len |
| 128 | br.cond.sptk .not_found0 ;; |
| 129 | |
| 130 | .align 32 |
| 131 | .src_aligned: |
| 132 | .l3: |
| 133 | (p[0]) ld8.s r[0] = [src], 8 |
| 134 | (p[MEMLAT]) chk.s r[MEMLAT], .recovery3 |
| 135 | .back3: |
| 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 |
| 142 | br.ctop.dptk .l3 ;; |
| 143 | |
| 144 | chk.s r[MEMLAT-1], .recovery4 |
| 145 | .back4: |
| 146 | mov value = r[MEMLAT-1] |
| 147 | |
| 148 | .not_found0: |
| 149 | cmp.eq p5, p6 = len, r0 |
| 150 | adds len = -1, len |
| 151 | (p5) br.cond.dptk .restore_and_exit ;; |
| 152 | mov ar.lc = len |
| 153 | .l4: |
| 154 | (p6) extr.u c = value, 0, 8 // c = value & 0xff |
| 155 | (p6) shr.u value = value, 8 ;; |
| 156 | st1 [dest] = c, 1 |
| 157 | cmp.ne p6, p0 = c, r0 |
| 158 | br.cloop.dptk .l4 |
| 159 | br.cond.sptk .restore_and_exit |
| 160 | |
| 161 | .found0_align: |
| 162 | mov pos = 0 |
| 163 | adds len = -8, len |
| 164 | mov value = 0 ;; |
| 165 | .found0: |
| 166 | shl tmp = pos, 3 |
| 167 | shr.u loopcnt = len, 4 // loopcnt = len / 16 |
| 168 | mov c = -1 ;; |
| 169 | cmp.eq p6, p0 = loopcnt, r0 |
| 170 | adds loopcnt = -1, loopcnt |
| 171 | shl c = c, tmp ;; |
| 172 | and len = 0xf, len |
| 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 |
| 178 | and len = 0x7, len |
| 179 | (p6) br.cond.dpnt .l6 ;; |
| 180 | .l5: |
| 181 | st8 [dest] = r0, 16 |
| 182 | st8 [dest2] = r0, 16 |
| 183 | br.cloop.dptk .l5 ;; |
| 184 | .l6: |
| 185 | (p7) st8 [dest] = r0, 8 |
| 186 | cmp.eq p5, p0 = len, r0 |
| 187 | adds len = -1, len |
| 188 | (p5) br.cond.dptk .restore_and_exit ;; |
| 189 | mov ar.lc = len ;; |
| 190 | .l7: |
| 191 | st1 [dest] = r0, 1 |
| 192 | br.cloop.dptk .l7 ;; |
| 193 | .restore_and_exit: |
| 194 | mov ar.lc = saved_lc // restore the loop counter |
| 195 | mov pr = saved_pr, -1 // restore the predicate registers |
| 196 | br.ret.sptk.many b0 |
| 197 | |
| 198 | .short_len: |
| 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 |
| 203 | .l8: |
| 204 | (p6) ld1 c = [in1], 1 // c = *src++ |
| 205 | ;; |
| 206 | st1 [in0] = c, 1 // *dest++ = c |
| 207 | (p6) cmp.ne p6, p0 = c, r0 |
| 208 | br.cloop.dptk .l8 |
| 209 | ;; |
| 210 | mov ar.lc = saved_lc // restore the loop counter |
| 211 | mov pr = saved_pr, -1 // restore the predicate registers |
| 212 | br.ret.sptk.many b0 |
| 213 | .recovery2: |
| 214 | add c = 8, len |
| 215 | add tmp = -8, asrc ;; |
| 216 | cmp.gtu p8, p5 = c, thresh ;; |
| 217 | (p8) ld8 r[0] = [tmp] |
| 218 | (p5) mov r[0] = r0 |
| 219 | br.cond.sptk .back2 |
| 220 | .recovery3: |
| 221 | add tmp = -(MEMLAT + 1) * 8, src ;; |
| 222 | ld8 r[MEMLAT] = [tmp] |
| 223 | br.cond.sptk .back3 |
| 224 | .recovery4: |
| 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 |
| 229 | br.cond.sptk .back4 |
| 230 | END(strncpy) |
| 231 | libc_hidden_builtin_def (strncpy) |