| 1 |
|
| 2 |
; Copyright 2000, 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_divexact_by3c |
| 26 |
|
| 27 |
%ifdef DLL |
| 28 |
export ___gmpn_divexact_by3c |
| 29 |
%endif |
| 30 |
|
| 31 |
%define PARAM_CARRY esp+frame+16 |
| 32 |
%define PARAM_SIZE esp+frame+12 |
| 33 |
%define PARAM_SRC esp+frame+8 |
| 34 |
%define PARAM_DST esp+frame+4 |
| 35 |
%define frame 0 |
| 36 |
|
| 37 |
; multiplicative inverse of 3,modulo 2^32 |
| 38 |
; ceil(b/3),ceil(b*2/3) and floor(b*2/3) where b=2^32 |
| 39 |
%define INVERSE_3 0xAAAAAAAB |
| 40 |
%define ONE_THIRD_CEIL 0x55555556 |
| 41 |
%define TWO_THIRDS_CEIL 0xAAAAAAAB |
| 42 |
%define TWO_THIRDS_FLOOR 0xAAAAAAAA |
| 43 |
|
| 44 |
section .text |
| 45 |
|
| 46 |
align 8 |
| 47 |
|
| 48 |
___gmpn_divexact_by3c: |
| 49 |
mov ecx,[PARAM_SRC] |
| 50 |
mov edx,[PARAM_SIZE] |
| 51 |
dec edx |
| 52 |
jnz Ltwo_or_more |
| 53 |
mov edx,[ecx] |
| 54 |
mov eax,[PARAM_CARRY] ; risk of cache bank clash here |
| 55 |
mov ecx,[PARAM_DST] |
| 56 |
sub edx,eax |
| 57 |
sbb eax,eax ; 0 or -1 |
| 58 |
imul edx,edx,INVERSE_3 |
| 59 |
neg eax ; 0 or 1 |
| 60 |
cmp edx,ONE_THIRD_CEIL |
| 61 |
sbb eax,-1 ; +1 if edx>=ceil(b/3) |
| 62 |
cmp edx,TWO_THIRDS_CEIL |
| 63 |
sbb eax,-1 ; +1 if edx>=ceil(b*2/3) |
| 64 |
mov [ecx],edx |
| 65 |
ret |
| 66 |
|
| 67 |
; eax |
| 68 |
; ebx |
| 69 |
; ecx src |
| 70 |
; edx size-1 |
| 71 |
; esi |
| 72 |
; edi |
| 73 |
; ebp |
| 74 |
|
| 75 |
Ltwo_or_more: |
| 76 |
FR_push ebx |
| 77 |
FR_push esi |
| 78 |
FR_push edi |
| 79 |
FR_push ebp |
| 80 |
mov edi,[PARAM_DST] |
| 81 |
mov esi,[PARAM_CARRY] |
| 82 |
mov eax,[ecx] ; src low limb |
| 83 |
xor ebx,ebx |
| 84 |
sub eax,esi |
| 85 |
mov esi,TWO_THIRDS_FLOOR |
| 86 |
lea ecx,[ecx+edx*4] ; &src[size-1] |
| 87 |
lea edi,[edi+edx*4] ; &dst[size-1] |
| 88 |
adc ebx,0 ; carry,0 or 1 |
| 89 |
neg edx ; -(size-1) |
| 90 |
|
| 91 |
; The loop needs a source limb ready at the top,which leads to one limb |
| 92 |
; handled separately at the end,and the special case above for size==1. |
| 93 |
; There doesn't seem to be any scheduling that would keep the speed but move |
| 94 |
; the source load and carry subtract up to the top. |
| 95 |
; |
| 96 |
; The destination cache line prefetching adds 1 cycle to the loop but is |
| 97 |
; considered worthwhile. The slowdown is a factor of 1.07,but will prevent |
| 98 |
; repeated write-throughs if the destination isn't in L1. A version using |
| 99 |
; an outer loop to prefetch only every 8 limbs (a cache line) proved to be |
| 100 |
; no faster,due to unavoidable branch mispreditions in the inner loop. |
| 101 |
; |
| 102 |
; setc is 2 cycles on P54,so an adcl is used instead. If the movl $0,%ebx |
| 103 |
; could be avoided then the src limb fetch could pair up and save a cycle. |
| 104 |
; This would probably mean going to a two limb loop with the carry limb |
| 105 |
; alternately positive or negative,since an sbbl %ebx,%ebx will leave a |
| 106 |
; value which is in the opposite sense to the preceding sbbl/adcl %ebx,%eax. |
| 107 |
; |
| 108 |
; A register is used for TWO_THIRDS_FLOOR because a cmp can't be done as |
| 109 |
; "cmpl %edx,$n" with the immediate as the second operand. |
| 110 |
; |
| 111 |
; The "4" source displacement is in the loop rather than the setup because |
| 112 |
; this gets Ltop aligned to 8 bytes at no cost. |
| 113 |
|
| 114 |
; eax source limb,carry subtracted |
| 115 |
; ebx carry (0 or 1) |
| 116 |
; ecx &src[size-1] |
| 117 |
; edx counter,limbs,negative |
| 118 |
; esi TWO_THIRDS_FLOOR |
| 119 |
; edi &dst[size-1] |
| 120 |
; ebp scratch (result limb) |
| 121 |
|
| 122 |
align 8 |
| 123 |
Ltop: |
| 124 |
imul ebp,eax,INVERSE_3 |
| 125 |
cmp ebp,ONE_THIRD_CEIL |
| 126 |
mov eax,[edi+edx*4] ; dst cache line prefetch |
| 127 |
sbb ebx,-1 ; +1 if ebp>=ceil(b/3) |
| 128 |
cmp esi,ebp |
| 129 |
mov eax,[4+ecx+edx*4] ; next src limb |
| 130 |
sbb eax,ebx ; and further -1 if ebp>=ceil(b*2/3) |
| 131 |
mov ebx,0 |
| 132 |
adc ebx,0 ; new carry |
| 133 |
mov [edi+edx*4],ebp |
| 134 |
inc edx |
| 135 |
jnz Ltop |
| 136 |
imul edx,eax,INVERSE_3 |
| 137 |
cmp edx,ONE_THIRD_CEIL |
| 138 |
mov [edi],edx |
| 139 |
sbb ebx,-1 ; +1 if edx>=ceil(b/3) |
| 140 |
cmp edx,TWO_THIRDS_CEIL |
| 141 |
sbb ebx,-1 ; +1 if edx>=ceil(b*2/3) |
| 142 |
pop ebp |
| 143 |
mov eax,ebx |
| 144 |
pop edi |
| 145 |
pop esi |
| 146 |
pop ebx |
| 147 |
ret |
| 148 |
|
| 149 |
end |