Debian Codegraph @

msvc/gmp/mpn/x86i/p6/mmx/divrem_1.asm

raw
1
2 ; Copyright 1999, 2000, 2001, 2002 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 global ___gmpn_preinv_divrem_1
26 global ___gmpn_divrem_1c
27 global ___gmpn_divrem_1
28
29 %ifdef DLL
30 export ___gmpn_divrem_1c
31 export ___gmpn_divrem_1
32 %endif
33
34 %define MUL_THRESHOLD 4
35 %define PARAM_PREINV_SHIFT esp+frame+28
36 %define PARAM_PREINV_INVERSE esp+frame+24
37 %define PARAM_CARRY esp+frame+24
38 %define PARAM_DIVISOR esp+frame+20
39 %define PARAM_SIZE esp+frame+16
40 %define PARAM_SRC esp+frame+12
41 %define PARAM_XSIZE esp+frame+8
42 %define PARAM_DST esp+frame+4
43
44 %define SAVE_EBX esp+frame-4
45 %define SAVE_ESI esp+frame-8
46 %define SAVE_EDI esp+frame-12
47 %define SAVE_EBP esp+frame-16
48
49 %define VAR_NORM esp+frame-20
50 %define VAR_INVERSE esp+frame-24
51 %define VAR_SRC esp+frame-28
52 %define VAR_DST esp+frame-32
53 %define VAR_DST_STOP esp+frame-36
54 %define STACK_SPACE 36
55 %define frame 0
56
57 section .text
58
59 align 16
60
61 ___gmpn_preinv_divrem_1:
62 mov ecx,[PARAM_XSIZE]
63 sub esp,STACK_SPACE
64 FR_sesp STACK_SPACE
65 mov [SAVE_ESI],esi
66 mov esi,[PARAM_SRC]
67 mov [SAVE_EBX],ebx
68 mov ebx,[PARAM_SIZE]
69 mov [SAVE_EBP],ebp
70 mov ebp,[PARAM_DIVISOR]
71 mov [SAVE_EDI],edi
72 mov edx,[PARAM_DST]
73 mov eax,[-4+esi+ebx*4] ; src high limb
74 xor edi,edi ; initial carry (if can't skip a div)
75 lea edx,[8+edx+ecx*4] ; &dst[xsize+2]
76 xor ecx,ecx
77 mov [VAR_DST_STOP],edx ; &dst[xsize+2]
78 cmp eax,ebp ; high cmp divisor
79 cmovc edi,eax
80 cmovnc ecx,eax ; (the latter in case src==dst)
81 mov [-12+edx+ebx*4],ecx ; dst high limb
82 sbb ebx,0 ; skip one division if high<divisor
83 mov ecx,[PARAM_PREINV_SHIFT]
84 lea edx,[-8+edx+ebx*4] ; &dst[xsize+size]
85 mov eax,32
86 mov [VAR_DST],edx ; &dst[xsize+size]
87 shl ebp,cl ; d normalized
88 sub eax,ecx
89 mov [VAR_NORM],ecx
90 movd mm7,eax ; rshift
91 mov eax,[PARAM_PREINV_INVERSE]
92 jmp Lstart_preinv
93
94 align 16
95
96 %define frame 0
97
98 ___gmpn_divrem_1c:
99 mov edx,[PARAM_CARRY]
100 mov ecx,[PARAM_SIZE]
101 sub esp,STACK_SPACE
102 %define frame STACK_SPACE
103 mov [SAVE_EBX],ebx
104 mov ebx,[PARAM_XSIZE]
105 mov [SAVE_EDI],edi
106 mov edi,[PARAM_DST]
107 mov [SAVE_EBP],ebp
108 mov ebp,[PARAM_DIVISOR]
109 mov [SAVE_ESI],esi
110 mov esi,[PARAM_SRC]
111 lea edi,[-4+edi+ebx*4]
112 jmp Lstart_1c
113
114 ; offset 0x31,close enough to aligned
115
116 %define frame 0
117
118 ___gmpn_divrem_1:
119 mov ecx,[PARAM_SIZE]
120 mov edx,0 ; initial carry (if can't skip a div)
121 sub esp,STACK_SPACE
122 %define frame STACK_SPACE
123 mov [SAVE_EBP],ebp
124 mov ebp,[PARAM_DIVISOR]
125 mov [SAVE_EBX],ebx
126 mov ebx,[PARAM_XSIZE]
127 mov [SAVE_ESI],esi
128 mov esi,[PARAM_SRC]
129 or ecx,ecx ; size
130 mov [SAVE_EDI],edi
131 mov edi,[PARAM_DST]
132 lea edi,[-4+edi+ebx*4] ; &dst[xsize-1]
133 jz Lno_skip_div ; if size==0
134 mov eax,[-4+esi+ecx*4] ; src high limb
135 xor esi,esi
136 cmp eax,ebp ; high cmp divisor
137 cmovc edx,eax
138 cmovnc esi,eax ; (the latter in case src==dst)
139 mov [edi+ecx*4],esi ; dst high limb
140 sbb ecx,0 ; size-1 if high<divisor
141 mov esi,[PARAM_SRC] ; reload
142 Lno_skip_div:
143
144 ; eax
145 ; ebx xsize
146 ; ecx size
147 ; edx carry
148 ; esi src
149 ; edi &dst[xsize-1]
150 ; ebp divisor
151
152 Lstart_1c:
153 lea eax,[ebx+ecx] ; size+xsize
154 cmp eax,MUL_THRESHOLD
155 jae Lmul_by_inverse
156 or ecx,ecx
157 jz Ldivide_no_integer
158
159 ; eax scratch (quotient)
160 ; ebx xsize
161 ; ecx counter
162 ; edx scratch (remainder)
163 ; esi src
164 ; edi &dst[xsize-1]
165 ; ebp divisor
166
167 Ldivide_integer:
168 mov eax,[-4+esi+ecx*4]
169 div ebp
170 mov [edi+ecx*4],eax
171 dec ecx
172 jnz Ldivide_integer
173 Ldivide_no_integer:
174 mov edi,[PARAM_DST]
175 or ebx,ebx
176 jnz Ldivide_fraction
177 Ldivide_done:
178 mov esi,[SAVE_ESI]
179 mov edi,[SAVE_EDI]
180 mov ebx,[SAVE_EBX]
181 mov eax,edx
182 mov ebp,[SAVE_EBP]
183 add esp,STACK_SPACE
184 ret
185
186 ; eax scratch (quotient)
187 ; ebx counter
188 ; ecx
189 ; edx scratch (remainder)
190 ; esi
191 ; edi dst
192 ; ebp divisor
193
194 Ldivide_fraction:
195 mov eax,0
196 div ebp
197 mov [-4+edi+ebx*4],eax
198 dec ebx
199 jnz Ldivide_fraction
200 jmp Ldivide_done
201
202 ; eax
203 ; ebx xsize
204 ; ecx size
205 ; edx carry
206 ; esi src
207 ; edi &dst[xsize-1]
208 ; ebp divisor
209
210 Lmul_by_inverse:
211 lea ebx,[12+edi] ; &dst[xsize+2],loop dst stop
212 mov [VAR_DST_STOP],ebx
213 lea edi,[4+edi+ecx*4] ; &dst[xsize+size]
214 mov [VAR_DST],edi
215 mov ebx,ecx ; size
216 bsr ecx,ebp ; 31-l
217 mov edi,edx ; carry
218 lea eax,[1+ecx] ; 32-l
219 xor ecx,31 ; l
220 mov [VAR_NORM],ecx
221 mov edx,-1
222 shl ebp,cl ; d normalized
223 movd mm7,eax
224 mov eax,-1
225 sub edx,ebp ; (b-d)-1 giving edx:eax = b*(b-d)-1
226 div ebp ; floor (b*(b-d)-1) / d
227
228 ; eax inverse
229 ; ebx size
230 ; ecx shift
231 ; edx
232 ; esi src
233 ; edi carry
234 ; ebp divisor
235 ;
236 ; mm7 rshift
237
238 Lstart_preinv:
239 mov [VAR_INVERSE],eax
240 or ebx,ebx ; size
241 lea eax,[-12+esi+ebx*4] ; &src[size-3]
242 mov [VAR_SRC],eax
243 jz Lstart_zero
244 mov esi,[8+eax] ; src high limb
245 cmp ebx,1
246 jz Lstart_one
247 Lstart_two_or_more:
248 mov edx,[4+eax] ; src second highest limb
249 shld edi,esi,cl
250 shld esi,edx,cl
251 cmp ebx,2
252 je Linteger_two_left
253 jmp Linteger_top
254
255 Lstart_one:
256 shld edi,esi,cl
257 shl esi,cl ; n10 = high << l
258 jmp Linteger_one_left
259
260 Lstart_zero:
261 ; Can be here with xsize==0 if mpn_preinv_divrem_1 had size==1 and
262 ; skipped a division.
263
264 shl edi,cl ; n2 = carry << l
265 mov eax,edi ; return value for zero_done
266 cmp [PARAM_XSIZE],dword 0
267 je Lzero_done
268 jmp Lfraction_some
269
270 ; This loop runs at about 25 cycles,which is probably sub-optimal,and
271 ; certainly more than the dependent chain would suggest. A better loop,or
272 ; a better rough analysis of what's possible,would be welcomed.
273 ;
274 ; In the current implementation,the following successively dependent
275 ; micro-ops seem to exist.
276 ;
277 ; uops
278 ; n2+n1 1 (addl)
279 ; mul 5
280 ; q1+1 3 (addl/adcl)
281 ; mul 5
282 ; sub 3 (subl/sbbl)
283 ; addback 2 (cmov)
284 ; ---
285 ; 19
286 ;
287 ; Lack of registers hinders explicit scheduling and it might be that the
288 ; normal out of order execution isn't able to hide enough under the mul
289 ; latencies.
290 ;
291 ; Using sarl/negl to pick out n1 for the n2+n1 stage is a touch faster than
292 ; cmov (and takes one uop off the dependent chain). A sarl/andl/addl
293 ; combination was tried for the addback (despite the fact it would lengthen
294 ; the dependent chain) but found to be no faster.
295
296 ; eax scratch
297 ; ebx scratch (nadj,q1)
298 ; ecx scratch (src,dst)
299 ; edx scratch
300 ; esi n10
301 ; edi n2
302 ; ebp d
303 ;
304 ; mm0 scratch (src qword)
305 ; mm7 rshift for normalization
306
307 align 16
308 Linteger_top:
309 mov eax,esi
310 mov ebx,ebp
311 sar eax,31 ; -n1
312 mov ecx,[VAR_SRC]
313 and ebx,eax ; -n1 & d
314 neg eax ; n1
315 add ebx,esi ; nadj = n10 + (-n1 & d),ignoring overflow
316 add eax,edi ; n2+n1
317 movq mm0,[ecx] ; next src limb and the one below it
318 mul dword [VAR_INVERSE] ; m*(n2+n1)
319 sub ecx,4
320 mov [VAR_SRC],ecx
321 add eax,ebx ; m*(n2+n1) + nadj,low giving carry flag
322 mov eax,ebp ; d
323 lea ebx,[1+edi] ; n2+1
324 adc ebx,edx ; 1 + high(n2<<32 + m*(n2+n1) + nadj) = q1+1
325 jz Lq1_ff
326 mul ebx ; (q1+1)*d
327 mov ecx,[VAR_DST]
328 psrlq mm0,mm7
329 sub esi,eax
330 mov eax,[VAR_DST_STOP]
331 sbb edi,edx ; n - (q1+1)*d
332 mov edi,esi ; remainder -> n2
333 lea edx,[ebp+esi]
334 cmovc edi,edx
335 movd esi,mm0
336 sbb ebx,0 ; q
337 sub ecx,4
338 mov [ecx],ebx
339 cmp ecx,eax
340 mov [VAR_DST],ecx
341 jne Linteger_top
342 Linteger_loop_done:
343
344 ; Here,and in integer_one_left below,an sbbl $0 is used rather than a jz
345 ; q1_ff special case. This make the code a bit smaller and simpler,and
346 ; costs only 2 cycles (each).
347
348 ; eax scratch
349 ; ebx scratch (nadj,q1)
350 ; ecx scratch (src,dst)
351 ; edx scratch
352 ; esi n10
353 ; edi n2
354 ; ebp divisor
355 ;
356 ; mm7 rshift
357
358 Linteger_two_left:
359 mov eax,esi
360 mov ebx,ebp
361 sar eax,31 ; -n1
362 mov ecx,[PARAM_SRC]
363 and ebx,eax ; -n1 & d
364 neg eax ; n1
365 add ebx,esi ; nadj = n10 + (-n1 & d),ignoring overflow
366 add eax,edi ; n2+n1
367 mul dword [VAR_INVERSE] ; m*(n2+n1)
368 movd mm0,[ecx] ; src low limb
369 mov ecx,[VAR_DST_STOP]
370 add eax,ebx ; m*(n2+n1) + nadj,low giving carry flag
371 lea ebx,[1+edi] ; n2+1
372 mov eax,ebp ; d
373 adc ebx,edx ; 1 + high(n2<<32 + m*(n2+n1) + nadj) = q1+1
374 sbb ebx,0
375 mul ebx ; (q1+1)*d
376 psllq mm0,32
377 psrlq mm0,mm7
378 sub esi,eax
379 sbb edi,edx ; n - (q1+1)*d
380 mov edi,esi ; remainder -> n2
381 lea edx,[ebp+esi]
382 cmovc edi,edx
383 movd esi,mm0
384 sbb ebx,0 ; q
385 mov [-4+ecx],ebx
386
387 ; eax scratch
388 ; ebx scratch (nadj,q1)
389 ; ecx scratch (dst)
390 ; edx scratch
391 ; esi n10
392 ; edi n2
393 ; ebp divisor
394 ;
395 ; mm7 rshift
396
397 Linteger_one_left:
398 mov eax,esi
399 mov ebx,ebp
400 sar eax,31 ; -n1
401 mov ecx,[VAR_DST_STOP]
402 and ebx,eax ; -n1 & d
403 neg eax ; n1
404 add ebx,esi ; nadj = n10 + (-n1 & d),ignoring overflow
405 add eax,edi ; n2+n1
406 mul dword [VAR_INVERSE] ; m*(n2+n1)
407 add eax,ebx ; m*(n2+n1) + nadj,low giving carry flag
408 lea ebx,[1+edi] ; n2+1
409 mov eax,ebp ; d
410 adc ebx,edx ; 1 + high(n2<<32 + m*(n2+n1) + nadj) = q1+1
411 sbb ebx,0 ; q1 if q1+1 overflowed
412 mul ebx
413 sub esi,eax
414 mov eax,[PARAM_XSIZE]
415 sbb edi,edx ; n - (q1+1)*d
416 mov edi,esi ; remainder -> n2
417 lea edx,[ebp+esi]
418 cmovc edi,edx
419 sbb ebx,0 ; q
420 mov [-8+ecx],ebx
421 sub ecx,8
422 or eax,eax ; xsize
423 jnz Lfraction_some
424 mov eax,edi
425 Lfraction_done:
426 mov ecx,[VAR_NORM]
427 Lzero_done:
428 mov ebp,[SAVE_EBP]
429 mov edi,[SAVE_EDI]
430 mov esi,[SAVE_ESI]
431 mov ebx,[SAVE_EBX]
432 add esp,STACK_SPACE
433 shr eax,cl
434 emms
435 ret
436
437 ; Special case for q1=0xFFFFFFFF,giving q=0xFFFFFFFF meaning the low dword
438 ; of q*d is simply -d and the remainder n-q*d = n10+d
439 ;
440 ; eax (divisor)
441 ; ebx (q1+1 == 0)
442 ; ecx
443 ; edx
444 ; esi n10
445 ; edi n2
446 ; ebp divisor
447
448 Lq1_ff:
449 mov ecx,[VAR_DST]
450 mov edx,[VAR_DST_STOP]
451 sub ecx,4
452 mov [VAR_DST],ecx
453 psrlq mm0,mm7
454 lea edi,[ebp+esi] ; n-q*d remainder -> next n2
455 mov [ecx],dword -1
456 movd esi,mm0 ; next n10
457 cmp edx,ecx
458 jne Linteger_top
459 jmp Linteger_loop_done
460
461 ;
462 ; In the current implementation,the following successively dependent
463 ; micro-ops seem to exist.
464 ;
465 ; uops
466 ; mul 5
467 ; q1+1 1 (addl)
468 ; mul 5
469 ; sub 3 (negl/sbbl)
470 ; addback 2 (cmov)
471 ; ---
472 ; 16
473 ;
474 ; The loop in fact runs at about 17.5 cycles. Using a sarl/andl/addl for
475 ; the addback was found to be a touch slower.
476
477 ; eax
478 ; ebx
479 ; ecx
480 ; edx
481 ; esi
482 ; edi carry
483 ; ebp divisor
484
485 align 16
486 Lfraction_some:
487 mov esi,[PARAM_DST]
488 mov ecx,[VAR_DST_STOP] ; &dst[xsize+2]
489 mov eax,edi
490 sub ecx,8 ; &dst[xsize]
491
492 ; eax n2,then scratch
493 ; ebx scratch (nadj,q1)
494 ; ecx dst,decrementing
495 ; edx scratch
496 ; esi dst stop point
497 ; edi n2
498 ; ebp divisor
499
500 align 16
501 Lfraction_top:
502 mul dword [VAR_INVERSE] ; m*n2
503 mov eax,ebp ; d
504 sub ecx,4 ; dst
505 lea ebx,[edi+1]
506 add ebx,edx ; 1 + high(n2<<32 + m*n2) = q1+1
507 mul ebx ; (q1+1)*d
508 neg eax ; low of n - (q1+1)*d
509 sbb edi,edx ; high of n - (q1+1)*d,caring only about carry
510 lea edx,[ebp+eax]
511 cmovc eax,edx
512 sbb ebx,0 ; q
513 mov edi,eax ; remainder->n2
514 cmp ecx,esi
515 mov [ecx],ebx ; previous q
516 jne Lfraction_top
517 jmp Lfraction_done
518
519 end