Update.
[glibc.git] / sysdeps / powerpc / memset.S
blob1b95bc7591a47604df573a06a58a5bbe915f1a6f
1 /* Optimized memset implementation for PowerPC.
2    Copyright (C) 1997, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    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    Library General Public License for more details.
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
20 #include <sysdep.h>
22 EALIGN(memset,5,1)
23 /* __ptr_t [r3] memset (__ptr_t s [r3], int c [r4], size_t n [r5]));
24    Returns 's'.
26    The memset is done in three sizes: byte (8 bits), word (32 bits),
27    cache line (256 bits). There is a special case for setting cache lines
28    to 0, to take advantage of the dcbz instruction.
29    r6:  current address we are storing at
30    r7:  number of bytes we are setting now (when aligning)  */
32 /* take care of case for size <= 4  */
33         cmplwi cr1,r5,4
34         andi.  r7,r3,3
35         mr     r6,r3
36         ble-   cr1,L(small)
37 /* align to word boundary  */
38         cmplwi cr5,r5,31
39         rlwimi r4,r4,8,16,23
40         beq+   L(aligned)               # 8th instruction from .align
41         mtcrf  0x01,r3
42         subfic r7,r7,4
43         add    r6,r6,r7
44         sub    r5,r5,r7
45         bf+    31,L(g0)
46         stb    r4,0(r3)
47         bt     30,L(aligned)
48 L(g0):  sth    r4,-2(r6)                #  16th instruction from .align
49 /* take care of case for size < 31 */
50 L(aligned):
51         mtcrf  0x01,r5
52         rlwimi r4,r4,16,0,15
53         ble    cr5,L(medium)
54 /* align to cache line boundary...  */
55         andi.  r7,r6,0x1C
56         subfic r7,r7,0x20
57         beq    L(caligned)
58         mtcrf  0x01,r7
59         add    r6,r6,r7
60         sub    r5,r5,r7
61         cmplwi cr1,r7,0x10
62         mr     r8,r6
63         bf     28,L(a1)
64         stw    r4,-4(r8)
65         stwu   r4,-8(r8)
66 L(a1):  blt    cr1,L(a2)
67         stw    r4,-4(r8)        # 32nd instruction from .align
68         stw    r4,-8(r8)
69         stw    r4,-12(r8)
70         stwu   r4,-16(r8)
71 L(a2):  bf     29,L(caligned)
72         stw    r4,-4(r8)
73 /* now aligned to a cache line.  */
74 L(caligned):
75         cmplwi cr1,r4,0
76         clrrwi. r7,r5,5
77         mtcrf  0x01,r5          # 40th instruction from .align
78         beq    cr1,L(zloopstart) # special case for clearing memory using dcbz
79         srwi   r0,r7,5
80         mtctr  r0
81         beq    L(medium)        # we may not actually get to do a full line
82         clrlwi. r5,r5,27
83         add    r6,r6,r7
84         li     r8,-0x40
85         bdz    L(cloopdone)     # 48th instruction from .align
87 L(c3):  dcbz   r8,r6
88         stw    r4,-4(r6)
89         stw    r4,-8(r6)
90         stw    r4,-12(r6)
91         stw    r4,-16(r6)
92         nop                     # let 601 fetch last 4 instructions of loop
93         stw    r4,-20(r6)
94         stw    r4,-24(r6)       # 56th instruction from .align
95         nop                     # let 601 fetch first 8 instructions of loop
96         stw    r4,-28(r6)
97         stwu   r4,-32(r6)
98         bdnz   L(c3)
99 L(cloopdone):
100         stw    r4,-4(r6)
101         stw    r4,-8(r6)
102         stw    r4,-12(r6)
103         stw    r4,-16(r6)       # 64th instruction from .align
104         stw    r4,-20(r6)
105         cmplwi cr1,r5,16
106         stw    r4,-24(r6)
107         stw    r4,-28(r6)
108         stwu   r4,-32(r6)
109         beqlr
110         add    r6,r6,r7
111         b      L(medium_tail2)  # 72nd instruction from .align
113         .align 5
114         nop
115 /* Clear lines of memory in 128-byte chunks.  */
116 L(zloopstart):
117         clrlwi r5,r5,27
118         mtcrf  0x02,r7
119         srwi.  r0,r7,7
120         mtctr  r0
121         li     r7,0x20
122         li     r8,-0x40
123         cmplwi cr1,r5,16        # 8
124         bf     26,L(z0)
125         dcbz   0,r6
126         addi   r6,r6,0x20
127 L(z0):  li     r9,-0x20
128         bf     25,L(z1)
129         dcbz   0,r6
130         dcbz   r7,r6
131         addi   r6,r6,0x40       # 16
132 L(z1):  cmplwi cr5,r5,0
133         beq    L(medium)
134 L(zloop):
135         dcbz   0,r6
136         dcbz   r7,r6
137         addi   r6,r6,0x80
138         dcbz   r8,r6
139         dcbz   r9,r6
140         bdnz   L(zloop)
141         beqlr  cr5
142         b      L(medium_tail2)
144         .align 5
145 L(small):
146 /* Memset of 4 bytes or less.  */
147         cmplwi cr5,r5,1
148         cmplwi cr1,r5,3
149         bltlr  cr5
150         stb    r4,0(r6)
151         beqlr  cr5
152         nop
153         stb    r4,1(r6)
154         bltlr  cr1
155         stb    r4,2(r6)
156         beqlr  cr1
157         nop
158         stb    r4,3(r6)
159         blr
161 /* Memset of 0-31 bytes.  */
162         .align 5
163 L(medium):
164         cmplwi cr1,r5,16
165 L(medium_tail2):
166         add    r6,r6,r5
167 L(medium_tail):
168         bt-    31,L(medium_31t)
169         bt-    30,L(medium_30t)
170 L(medium_30f):
171         bt-    29,L(medium_29t)
172 L(medium_29f):
173         bge-   cr1,L(medium_27t)
174         bflr-  28
175         stw    r4,-4(r6)                # 8th instruction from .align
176         stw    r4,-8(r6)
177         blr
179 L(medium_31t):
180         stbu   r4,-1(r6)
181         bf-    30,L(medium_30f)
182 L(medium_30t):
183         sthu   r4,-2(r6)
184         bf-    29,L(medium_29f)
185 L(medium_29t):
186         stwu   r4,-4(r6)
187         blt-   cr1,L(medium_27f)        # 16th instruction from .align
188 L(medium_27t):
189         stw    r4,-4(r6)
190         stw    r4,-8(r6)
191         stw    r4,-12(r6)
192         stwu   r4,-16(r6)
193 L(medium_27f):
194         bflr-  28
195 L(medium_28t):
196         stw    r4,-4(r6)
197         stw    r4,-8(r6)
198         blr
199 END(memset)