Adjusted.
[glibc.git] / sysdeps / ia64 / memccpy.S
blob589c9ccf1600cd70a4ce357f1a32e26d4ac6f17e
1 /* Optimized version of the memccpy() function.
2    This file is part of the GNU C Library.
3    Copyright (C) 2000 Free Software Foundation, Inc.
4    Contributed by Dan Pop <Dan.Pop@cern.ch>.
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
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    Library General Public License for more details.
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
21 /* Return: a pointer to the next byte after char in dest or NULL
23    Inputs:
24         in0:    dest
25         in1:    src
26         in2:    char
27         in3:    byte count
29    This implementation assumes little endian mode (UM.be = 0).
31    This implementation assumes that it is safe to do read ahead
32    in the src block, without getting beyond its limit.  */
34 #include <sysdep.h>
35 #undef ret
37 #define OP_T_THRES      16
38 #define OPSIZ           8
40 #define saved_pfs       r14
41 #define saved_pr        r17
42 #define saved_lc        r18
43 #define dest            r19
44 #define src             r20
45 #define len             r21
46 #define asrc            r22
47 #define tmp             r23
48 #define char            r24
49 #define charx8          r25
50 #define sh2             r28
51 #define sh1             r29
52 #define loopcnt         r30
53 #define value           r31
55 ENTRY(memccpy)
56         alloc   saved_pfs = ar.pfs, 4, 40 - 4, 0, 40
58 #include "softpipe.h"
59         .rotr   r[MEMLAT + 3], tmp1[4], tmp2[4], val[4], tmp3[2], pos0[2]
60         .rotp   p[MEMLAT + 6 + 1]
62         mov     ret0 = r0               // return NULL if no match
63         mov     saved_pr = pr           // save the predicate registers
64         mov     saved_lc = ar.lc        // save the loop counter
65         mov     dest = in0              // dest
66         mov     src = in1               // src
67         extr.u  char = in2, 0, 8        // char
68         mov     len = in3               // len
69         sub     tmp = r0, in0           // tmp = -dest
70         cmp.ne  p7, p0 = r0, r0         // clear p7
71         ;;
72         and     loopcnt = 7, tmp                // loopcnt = -dest % 8
73         cmp.ge  p6, p0 = OP_T_THRES, len        // is len <= OP_T_THRES
74 (p6)    br.cond.spnt    .cpyfew                 // copy byte by byte
75         ;;
76         cmp.eq  p6, p0 = loopcnt, r0
77         mux1    charx8 = char, @brcst
78 (p6)    br.cond.sptk .dest_aligned
79         sub     len = len, loopcnt      // len -= -dest % 8
80         adds    loopcnt = -1, loopcnt   // --loopcnt
81         ;;
82         mov     ar.lc = loopcnt
83 .l1:                                    // copy -dest % 8 bytes
84         ld1     value = [src], 1        // value = *src++
85         ;;
86         st1     [dest] = value, 1       // *dest++ = value
87         cmp.eq  p6, p0 = value, char
88 (p6)    br.cond.spnt .foundit
89         br.cloop.dptk .l1
90 .dest_aligned:
91         and     sh1 = 7, src            // sh1 = src % 8
92         and     tmp = -8, len           // tmp = len & -OPSIZ
93         and     asrc = -8, src          // asrc = src & -OPSIZ  -- align src
94         shr.u   loopcnt = len, 3        // loopcnt = len / 8
95         and     len = 7, len ;;         // len = len % 8
96         shl     sh1 = sh1, 3            // sh1 = 8 * (src % 8)
97         adds    loopcnt = -1, loopcnt   // --loopcnt
98         mov     pr.rot = 1 << 16 ;;     // set rotating predicates
99         sub     sh2 = 64, sh1           // sh2 = 64 - sh1
100         mov     ar.lc = loopcnt         // set LC
101         cmp.eq  p6, p0 = sh1, r0        // is the src aligned?
102 (p6)    br.cond.sptk .src_aligned ;;
103         add     src = src, tmp          // src += len & -OPSIZ
104         mov     ar.ec = MEMLAT + 6 + 1  // six more passes needed
105         ld8     r[1] = [asrc], 8        // r[1] = w0
106         cmp.ne  p6, p0 = r0, r0 ;;      // clear p6
107         .align  32
108 .l2:
109 (p[0])          ld8     r[0] = [asrc], 8                // r[0] = w1
110 (p[MEMLAT])     shr.u   tmp1[0] = r[1 + MEMLAT], sh1    // tmp1 = w0 >> sh1
111 (p[MEMLAT])     shl     tmp2[0] = r[0 + MEMLAT], sh2    // tmp2 = w1 << sh2
112 (p[MEMLAT+4])   xor     tmp3[0] = val[1], charx8
113 (p[MEMLAT+5])   czx1.r  pos0[0] = tmp3[1]
114 (p[MEMLAT+6])   cmp.ne  p6, p0 = 8, pos0[1]
115 (p6)            br.cond.spnt    .gotit
116 (p[MEMLAT+6])   st8     [dest] = val[3], 8              // store val to dest
117 (p[MEMLAT+3])   or      val[0] = tmp1[3], tmp2[3]       // val = tmp1 | tmp2
118                 br.ctop.sptk    .l2
119                 br.cond.sptk .cpyfew
120 .src_aligned:
121                 cmp.ne  p6, p0 = r0, r0                 // clear p6
122                 mov     ar.ec = MEMLAT + 2 + 1 ;;       // set EC
123 .l3:
124 (p[0])          ld8     r[0] = [src], 8
125 (p[MEMLAT])     xor     tmp3[0] = r[MEMLAT], charx8
126 (p[MEMLAT+1])   czx1.r  pos0[0] = tmp3[1]
127 (p[MEMLAT+2])   cmp.ne  p7, p0 = 8, pos0[1]
128 (p7)            br.cond.spnt    .gotit
129 (p[MEMLAT+2])   st8     [dest] = r[MEMLAT+2], 8
130                 br.ctop.dptk .l3
131 .cpyfew:
132         cmp.eq  p6, p0 = len, r0        // is len == 0 ?
133         adds    len = -1, len           // --len;
134 (p6)    br.cond.spnt    .restore_and_exit ;;
135         mov     ar.lc = len
136 .l4:
137         ld1     value = [src], 1
138         ;;
139         st1     [dest] = value, 1
140         cmp.eq  p6, p0 = value, char
141 (p6)    br.cond.spnt .foundit
142         br.cloop.dptk   .l4 ;;
143 .foundit:
144 (p6)    mov     ret0 = dest
145 .restore_and_exit:
146         mov     ar.pfs = saved_pfs      // restore the PFS
147         mov     pr = saved_pr, -1       // restore the predicate registers
148         mov     ar.lc = saved_lc        // restore the loop counter
149         br.ret.sptk.many b0
150 .gotit:
151         .pred.rel "mutex" p6, p7
152 (p6)    mov     value = val[3]          // if coming from l2
153 (p7)    mov     value = r[MEMLAT+2]     // if coming from l3
154         mov     ar.lc = pos0[1] ;;
155 .l5:
156         extr.u  tmp = value, 0, 8 ;;
157         st1     [dest] = tmp, 1
158         shr.u   value = value, 8
159         br.cloop.sptk .l5 ;;
160         mov     ret0 = dest
161         mov     ar.pfs = saved_pfs
162         mov     pr = saved_pr, -1
163         mov     ar.lc = saved_lc
164         br.ret.sptk.many b0
165 END(memccpy)