Debian Codegraph @

msvc/gmp/mpn/x86i/pentium4/mmx/rshift.asm

raw
1
2 ; Copyright 2001 Free Software Foundation, Inc.
3 ;
4 ; This file is part of the GNU MP Library.
5 ;
6 ; The GNU MP Library is free software; you can redistribute it and/or
7 ; modify it under the terms of the GNU Lesser General Public License as
8 ; published by the Free Software Foundation; either version 2.1 of the
9 ; License, or (at your option) any later version.
10 ;
11 ; The GNU MP 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 MP Library; see the file COPYING.LIB. If
18 ; not, write to the Free Software Foundation, Inc., 59 Temple Place -
19 ; Suite 330, Boston, MA 02111-1307, USA.
20 ;
21 ; Translation of AT&T syntax code by Brian Gladman
22
23 %include "..\..\x86i.inc"
24
25 %define PARAM_SHIFT esp+frame+16
26 %define PARAM_SIZE esp+frame+12
27 %define PARAM_SRC esp+frame+8
28 %define PARAM_DST esp+frame+4
29 %define frame 8
30
31 ; Minimum 5,because the unrolled loop can't handle less.
32 %define UNROLL_THRESHOLD 5
33
34 section .text
35
36 global ___gmpn_rshift
37 %ifdef DLL
38 export ___gmpn_rshift
39 %endif
40
41 align 8
42 ___gmpn_rshift:
43 push ebx
44 push edi
45 mov eax,[PARAM_SIZE]
46 mov edx,[PARAM_DST]
47 mov ebx,[PARAM_SRC]
48 mov ecx,[PARAM_SHIFT]
49 cmp eax,UNROLL_THRESHOLD
50 jae Lunroll
51 dec eax
52 mov edi,[ebx] ; src low limb
53 jnz Lsimple
54 shrd eax,edi,cl
55 shr edi,cl
56 mov [edx],edi ; dst low limb
57 pop edi ; risk of data cache bank clash
58 pop ebx
59 ret
60
61 ; eax size-1
62 ; ebx src
63 ; ecx shift
64 ; edx dst
65 ; esi
66 ; edi
67 ; ebp
68
69 align 8
70 Lsimple:
71 movd mm5,[ebx] ; src[0]
72 lea ebx,[ebx+eax*4] ; &src[size-1]
73 movd mm6,ecx ; rshift
74 lea edx,[-4+edx+eax*4] ; &dst[size-2]
75 psllq mm5,32
76 neg eax
77
78 ; This loop is 5 or 8 cycles,with every second load unaligned and a wasted
79 ; cycle waiting for the mm0 result to be ready. For comparison a shrdl is 4
80 ; cycles and would be 8 in a simple loop. Using mmx helps the return value
81 ; and last limb calculations too.
82
83 ; eax counter,limbs,negative
84 ; ebx &src[size-1]
85 ; ecx return value
86 ; edx &dst[size-2]
87 ;
88 ; mm0 scratch
89 ; mm5 return value
90 ; mm6 shift
91
92 Lsimple_top:
93 movq mm0,[ebx+eax*4]
94 inc eax
95 psrlq mm0,mm6
96 movd [edx+eax*4],mm0
97 jnz Lsimple_top
98 movd mm0,[ebx]
99 psrlq mm5,mm6 ; return value
100 psrlq mm0,mm6
101 pop edi
102 movd eax,mm5
103 pop ebx
104 movd [4+edx],mm0
105 emms
106 ret
107
108 ; eax size
109 ; ebx src
110 ; ecx shift
111 ; edx dst
112 ; esi
113 ; edi
114 ; ebp
115
116 align 8
117 Lunroll:
118 movd mm5,[ebx] ; src[0]
119 mov edi,4
120 movd mm6,ecx ; rshift
121 test ebx,edi
122 psllq mm5,32
123 jz Lstart_src_aligned
124
125 ; src isn't aligned,process low limb separately (marked xxx) and
126 ; step src and dst by one limb,making src aligned.
127 ;
128 ; source ebx
129 ; --+-------+-------+-------+
130 ; | xxx |
131 ; --+-------+-------+-------+
132 ; 4mod8 0mod8 4mod8
133 ;
134 ; dest edx
135 ; --+-------+-------+
136 ; | | xxx |
137 ; --+-------+-------+
138
139 movq mm0,[ebx] ; unaligned load
140 psrlq mm0,mm6
141 add ebx,4
142 dec eax
143 movd [edx],mm0
144 add edx,4
145 Lstart_src_aligned:
146 movq mm1,[ebx]
147 test edx,edi
148 psrlq mm5,mm6 ; retval
149 jz Lstart_dst_aligned
150
151 ; dst isn't aligned,add 4 to make it so,and pretend the shift is
152 ; 32 bits extra. Low limb of dst (marked xxx) handled here
153 ; separately.
154 ;
155 ; source ebx
156 ; --+-------+-------+
157 ; | mm1 |
158 ; --+-------+-------+
159 ; 4mod8 0mod8
160 ;
161 ; dest edx
162 ; --+-------+-------+-------+
163 ; | xxx |
164 ; --+-------+-------+-------+
165 ; 4mod8 0mod8 4mod8
166
167 movq mm0,mm1
168 add ecx,32 ; new shift
169 psrlq mm0,mm6
170 movd mm6,ecx
171 movd [edx],mm0
172 add edx,4
173 Lstart_dst_aligned:
174 movq mm3,[8+ebx]
175 neg ecx
176 movq mm2,mm3 ; mm2 src qword
177 add ecx,64
178 movd mm7,ecx
179 psrlq mm1,mm6
180 lea ebx,[-12+ebx+eax*4]
181 lea edx,[-20+edx+eax*4]
182 psllq mm3,mm7
183 sub eax,7 ; size-7
184 por mm3,mm1 ; mm3 ready to store
185 neg eax ; -(size-7)
186 jns Lfinish
187
188 ; This loop is the important bit,the rest is just support. Careful
189 ; instruction scheduling achieves the claimed 1.75 c/l. The
190 ; relevant parts of the pairing rules are:
191 ;
192 ; - mmx loads and stores execute only in the U pipe
193 ; - only one mmx shift in a pair
194 ; - wait one cycle before storing an mmx register result
195 ; - the usual address generation interlock
196 ;
197 ; Two qword calculations are slightly interleaved. The instructions
198 ; marked "C" belong to the second qword,and the "C prev" one is for
199 ; the second qword from the previous iteration.
200
201 ; eax counter,limbs,negative
202 ; ebx &src[size-12]
203 ; ecx
204 ; edx &dst[size-12]
205 ; esi
206 ; edi
207 ;
208 ; mm0
209 ; mm1
210 ; mm2 src qword from -8(%ebx,%eax,4)
211 ; mm3 dst qword ready to store to -8(%edx,%eax,4)
212 ;
213 ; mm5 return value
214 ; mm6 rshift
215 ; mm7 lshift
216
217 align 8
218 Lunroll_loop:
219 movq mm0,[ebx+eax*4]
220 psrlq mm2,mm6
221 movq mm1,mm0
222 psllq mm0,mm7
223 movq [-8+edx+eax*4],mm3
224 por mm0,mm2
225
226 movq mm3,[ebx+eax*4+8]
227 psrlq mm1,mm6
228 movq [edx+eax*4],mm0
229 movq mm2,mm3
230 psllq mm3,mm7
231 add eax,4
232 por mm3,mm1
233 js Lunroll_loop
234
235 Lfinish:
236 ; eax 0 to 3 representing respectively 3 to 0 limbs remaining
237
238 test al,2
239 jnz Lfinish_no_two
240 movq mm0,[ebx+eax*4]
241 psrlq mm2,mm6
242 movq mm1,mm0
243 psllq mm0,mm7
244 movq [-8+edx+eax*4],mm3 ; prev
245 por mm0,mm2
246 movq mm2,mm1
247 movq mm3,mm0
248 add eax,2
249 Lfinish_no_two:
250
251 ; eax 2 or 3 representing respectively 1 or 0 limbs remaining
252 ;
253 ; mm2 src prev qword,from -8(%ebx,%eax,4)
254 ; mm3 dst qword,for -8(%edx,%eax,4)
255
256 test al,1
257 pop edi
258 movd eax,mm5 ; retval
259 jnz Lfinish_zero
260
261 ; One extra limb,destination was aligned.
262 ;
263 ; source ebx
264 ; +-------+---------------+--
265 ; | | mm2 |
266 ; +-------+---------------+--
267 ;
268 ; dest edx
269 ; +-------+---------------+---------------+--
270 ; | | | mm3 |
271 ; +-------+---------------+---------------+--
272 ;
273 ; mm6 = shift
274 ; mm7 = ecx = 64-shift
275
276 ; One extra limb,destination was unaligned.
277 ;
278 ; source ebx
279 ; +-------+---------------+--
280 ; | | mm2 |
281 ; +-------+---------------+--
282 ;
283 ; dest edx
284 ; +---------------+---------------+--
285 ; | | mm3 |
286 ; +---------------+---------------+--
287 ;
288 ; mm6 = shift+32
289 ; mm7 = ecx = 64-(shift+32)
290
291 ; In both cases there's one extra limb of src to fetch and combine
292 ; with mm2 to make a qword at 8(%edx),and in the aligned case
293 ; there's a further extra limb of dst to be formed.
294
295
296 movd mm0,[8+ebx]
297 psrlq mm2,mm6
298 movq mm1,mm0
299 psllq mm0,mm7
300 movq [edx],mm3
301 por mm0,mm2
302 psrlq mm1,mm6
303 and ecx,32
304 pop ebx
305 jz Lfinish_one_unaligned
306
307 ; dst was aligned,must store one extra limb
308 movd [16+edx],mm1
309 Lfinish_one_unaligned:
310
311 movq [8+edx],mm0
312 emms
313 ret
314 Lfinish_zero:
315
316 ; No extra limbs,destination was aligned.
317 ;
318 ; source ebx
319 ; +---------------+--
320 ; | mm2 |
321 ; +---------------+--
322 ;
323 ; dest edx+4
324 ; +---------------+---------------+--
325 ; | | mm3 |
326 ; +---------------+---------------+--
327 ;
328 ; mm6 = shift
329 ; mm7 = ecx = 64-shift
330
331 ; No extra limbs,destination was unaligned.
332 ;
333 ; source ebx
334 ; +---------------+--
335 ; | mm2 |
336 ; +---------------+--
337 ;
338 ; dest edx+4
339 ; +-------+---------------+--
340 ; | | mm3 |
341 ; +-------+---------------+--
342 ;
343 ; mm6 = shift+32
344 ; mm7 = 64-(shift+32)
345
346 ; The movd for the unaligned case is clearly the same data as the
347 ; movq for the aligned case,it's just a choice between whether one
348 ; or two limbs should be written.
349
350 movq [4+edx],mm3
351 psrlq mm2,mm6
352 movd [12+edx],mm2
353 and ecx,32
354 pop ebx
355 jz Lfinish_zero_unaligned
356 movq [12+edx],mm2
357 Lfinish_zero_unaligned:
358 emms
359 ret
360
361 end