lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Optimized version of the standard memcmp() function. |
| 2 | This file is part of the GNU C Library. |
| 3 | Copyright (C) 2000-2015 Free Software Foundation, Inc. |
| 4 | Contributed by Dan Pop <Dan.Pop@cern.ch>. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | /* Return: the result of the comparison |
| 21 | |
| 22 | Inputs: |
| 23 | in0: dest (aka s1) |
| 24 | in1: src (aka s2) |
| 25 | in2: byte count |
| 26 | |
| 27 | In this form, it assumes little endian mode. For big endian mode, |
| 28 | the two shifts in .l2 must be inverted: |
| 29 | |
| 30 | shl tmp1[0] = r[1 + MEMLAT], sh1 // tmp1 = w0 << sh1 |
| 31 | shr.u tmp2[0] = r[0 + MEMLAT], sh2 // tmp2 = w1 >> sh2 |
| 32 | |
| 33 | and all the mux1 instructions should be replaced by plain mov's. */ |
| 34 | |
| 35 | #include <sysdep.h> |
| 36 | #undef ret |
| 37 | |
| 38 | #define OP_T_THRES 16 |
| 39 | #define OPSIZ 8 |
| 40 | #define MEMLAT 2 |
| 41 | |
| 42 | #define start r15 |
| 43 | #define saved_pr r17 |
| 44 | #define saved_lc r18 |
| 45 | #define dest r19 |
| 46 | #define src r20 |
| 47 | #define len r21 |
| 48 | #define asrc r22 |
| 49 | #define tmp r23 |
| 50 | #define value1 r24 |
| 51 | #define value2 r25 |
| 52 | #define sh2 r28 |
| 53 | #define sh1 r29 |
| 54 | #define loopcnt r30 |
| 55 | |
| 56 | ENTRY(memcmp) |
| 57 | .prologue |
| 58 | alloc r2 = ar.pfs, 3, 37, 0, 40 |
| 59 | |
| 60 | .rotr r[MEMLAT + 2], q[MEMLAT + 5], tmp1[4], tmp2[4], val[2] |
| 61 | .rotp p[MEMLAT + 4 + 1] |
| 62 | |
| 63 | mov ret0 = r0 // by default return value = 0 |
| 64 | .save pr, saved_pr |
| 65 | mov saved_pr = pr // save the predicate registers |
| 66 | .save ar.lc, saved_lc |
| 67 | mov saved_lc = ar.lc // save the loop counter |
| 68 | .body |
| 69 | mov dest = in0 // dest |
| 70 | mov src = in1 // src |
| 71 | mov len = in2 // len |
| 72 | sub tmp = r0, in0 // tmp = -dest |
| 73 | ;; |
| 74 | and loopcnt = 7, tmp // loopcnt = -dest % 8 |
| 75 | cmp.ge p6, p0 = OP_T_THRES, len // is len <= OP_T_THRES |
| 76 | (p6) br.cond.spnt .cmpfew // compare byte by byte |
| 77 | ;; |
| 78 | cmp.eq p6, p0 = loopcnt, r0 |
| 79 | (p6) br.cond.sptk .dest_aligned |
| 80 | sub len = len, loopcnt // len -= -dest % 8 |
| 81 | adds loopcnt = -1, loopcnt // --loopcnt |
| 82 | ;; |
| 83 | mov ar.lc = loopcnt |
| 84 | .l1: // copy -dest % 8 bytes |
| 85 | ld1 value1 = [src], 1 // value = *src++ |
| 86 | ld1 value2 = [dest], 1 |
| 87 | ;; |
| 88 | cmp.ne p6, p0 = value1, value2 |
| 89 | (p6) br.cond.spnt .done |
| 90 | br.cloop.dptk .l1 |
| 91 | .dest_aligned: |
| 92 | and sh1 = 7, src // sh1 = src % 8 |
| 93 | and tmp = -8, len // tmp = len & -OPSIZ |
| 94 | and asrc = -8, src // asrc = src & -OPSIZ -- align src |
| 95 | shr.u loopcnt = len, 3 // loopcnt = len / 8 |
| 96 | and len = 7, len ;; // len = len % 8 |
| 97 | shl sh1 = sh1, 3 // sh1 = 8 * (src % 8) |
| 98 | adds loopcnt = -1, loopcnt // --loopcnt |
| 99 | mov pr.rot = 1 << 16 ;; // set rotating predicates |
| 100 | sub sh2 = 64, sh1 // sh2 = 64 - sh1 |
| 101 | mov ar.lc = loopcnt // set LC |
| 102 | cmp.eq p6, p0 = sh1, r0 // is the src aligned? |
| 103 | (p6) br.cond.sptk .src_aligned |
| 104 | add src = src, tmp // src += len & -OPSIZ |
| 105 | mov ar.ec = MEMLAT + 4 + 1 // four more passes needed |
| 106 | ld8 r[1] = [asrc], 8 ;; // r[1] = w0 |
| 107 | .align 32 |
| 108 | |
| 109 | // We enter this loop with p6 cleared by the above comparison |
| 110 | |
| 111 | .l2: |
| 112 | (p[0]) ld8 r[0] = [asrc], 8 // r[0] = w1 |
| 113 | (p[0]) ld8 q[0] = [dest], 8 |
| 114 | (p[MEMLAT]) shr.u tmp1[0] = r[1 + MEMLAT], sh1 // tmp1 = w0 >> sh1 |
| 115 | (p[MEMLAT]) shl tmp2[0] = r[0 + MEMLAT], sh2 // tmp2 = w1 << sh2 |
| 116 | (p[MEMLAT+4]) cmp.ne p6, p0 = q[MEMLAT + 4], val[1] |
| 117 | (p[MEMLAT+3]) or val[0] = tmp1[3], tmp2[3] // val = tmp1 | tmp2 |
| 118 | (p6) br.cond.spnt .l2exit |
| 119 | br.ctop.sptk .l2 |
| 120 | br.cond.sptk .cmpfew |
| 121 | .l3exit: |
| 122 | mux1 value1 = r[MEMLAT], @rev |
| 123 | mux1 value2 = q[MEMLAT], @rev |
| 124 | cmp.ne p6, p0 = r0, r0 ;; // clear p6 |
| 125 | .l2exit: |
| 126 | (p6) mux1 value1 = val[1], @rev |
| 127 | (p6) mux1 value2 = q[MEMLAT + 4], @rev ;; |
| 128 | cmp.ltu p6, p7 = value2, value1 ;; |
| 129 | (p6) mov ret0 = -1 |
| 130 | (p7) mov ret0 = 1 |
| 131 | mov pr = saved_pr, -1 // restore the predicate registers |
| 132 | mov ar.lc = saved_lc // restore the loop counter |
| 133 | br.ret.sptk.many b0 |
| 134 | .src_aligned: |
| 135 | cmp.ne p6, p0 = r0, r0 // clear p6 |
| 136 | mov ar.ec = MEMLAT + 1 ;; // set EC |
| 137 | .l3: |
| 138 | (p[0]) ld8 r[0] = [src], 8 |
| 139 | (p[0]) ld8 q[0] = [dest], 8 |
| 140 | (p[MEMLAT]) cmp.ne p6, p0 = r[MEMLAT], q[MEMLAT] |
| 141 | (p6) br.cond.spnt .l3exit |
| 142 | br.ctop.dptk .l3 ;; |
| 143 | .cmpfew: |
| 144 | cmp.eq p6, p0 = len, r0 // is len == 0 ? |
| 145 | adds len = -1, len // --len; |
| 146 | (p6) br.cond.spnt .restore_and_exit ;; |
| 147 | mov ar.lc = len |
| 148 | .l4: |
| 149 | ld1 value1 = [src], 1 |
| 150 | ld1 value2 = [dest], 1 |
| 151 | ;; |
| 152 | cmp.ne p6, p0 = value1, value2 |
| 153 | (p6) br.cond.spnt .done |
| 154 | br.cloop.dptk .l4 ;; |
| 155 | .done: |
| 156 | (p6) sub ret0 = value2, value1 // don't execute it if falling thru |
| 157 | .restore_and_exit: |
| 158 | mov pr = saved_pr, -1 // restore the predicate registers |
| 159 | mov ar.lc = saved_lc // restore the loop counter |
| 160 | br.ret.sptk.many b0 |
| 161 | END(memcmp) |
| 162 | |
| 163 | weak_alias (memcmp, bcmp) |
| 164 | libc_hidden_builtin_def (memcmp) |