| 1 |
|
| 2 |
; Copyright 1999, 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_preinv_mod_1 |
| 26 |
global ___gmpn_mod_1c |
| 27 |
global ___gmpn_mod_1 |
| 28 |
|
| 29 |
%ifdef DLL |
| 30 |
export ___gmpn_mod_1c |
| 31 |
export ___gmpn_mod_1 |
| 32 |
%endif |
| 33 |
|
| 34 |
%define MUL_NORM_THRESHOLD 4 |
| 35 |
%define MUL_UNNORM_THRESHOLD 5 |
| 36 |
|
| 37 |
%define MUL_NORM_DELTA MUL_NORM_THRESHOLD - MUL_UNNORM_THRESHOLD |
| 38 |
|
| 39 |
%define PARAM_INVERSE esp+frame+16 |
| 40 |
%define PARAM_CARRY esp+frame+16 |
| 41 |
%define PARAM_DIVISOR esp+frame+12 |
| 42 |
%define PARAM_SIZE esp+frame+8 |
| 43 |
%define PARAM_SRC esp+frame+4 |
| 44 |
|
| 45 |
%define SAVE_EBX esp+frame-4 |
| 46 |
%define SAVE_ESI esp+frame-8 |
| 47 |
%define SAVE_EDI esp+frame-12 |
| 48 |
%define SAVE_EBP esp+frame-16 |
| 49 |
|
| 50 |
%define VAR_NORM esp+frame-20 |
| 51 |
%define VAR_INVERSE esp+frame-24 |
| 52 |
%define STACK_SPACE 24 |
| 53 |
|
| 54 |
section .text |
| 55 |
|
| 56 |
align 16 |
| 57 |
|
| 58 |
%define frame 0 |
| 59 |
|
| 60 |
___gmpn_preinv_mod_1: |
| 61 |
mov edx,[PARAM_SRC] |
| 62 |
sub esp,STACK_SPACE |
| 63 |
FR_sesp STACK_SPACE |
| 64 |
mov [SAVE_EBX],ebx |
| 65 |
mov ebx,[PARAM_SIZE] |
| 66 |
mov [SAVE_EBP],ebp |
| 67 |
mov ebp,[PARAM_DIVISOR] |
| 68 |
mov [SAVE_ESI],esi |
| 69 |
mov eax,[PARAM_INVERSE] |
| 70 |
mov [SAVE_EDI],edi |
| 71 |
mov edi,[-4+edx+ebx*4] ; src high limb |
| 72 |
mov [VAR_NORM],dword 0 |
| 73 |
lea ecx,[-8+edx+ebx*4] ; &src[size-2] |
| 74 |
mov esi,edi |
| 75 |
sub edi,ebp ; high-divisor |
| 76 |
cmovc edi,esi |
| 77 |
dec ebx |
| 78 |
jnz Lpreinv_entry |
| 79 |
jmp Ldone_edi |
| 80 |
|
| 81 |
align 16 |
| 82 |
|
| 83 |
%define frame 0 |
| 84 |
|
| 85 |
___gmpn_mod_1c: |
| 86 |
mov ecx,[PARAM_SIZE] |
| 87 |
sub esp,STACK_SPACE |
| 88 |
FR_sesp STACK_SPACE |
| 89 |
mov [SAVE_EBP],ebp |
| 90 |
mov eax,[PARAM_DIVISOR] |
| 91 |
mov [SAVE_ESI],esi |
| 92 |
mov edx,[PARAM_CARRY] |
| 93 |
mov esi,[PARAM_SRC] |
| 94 |
or ecx,ecx |
| 95 |
jz Ldone_edx ; result==carry if size==0 |
| 96 |
sar eax,31 |
| 97 |
mov ebp,[PARAM_DIVISOR] |
| 98 |
and eax,MUL_NORM_DELTA |
| 99 |
add eax,MUL_UNNORM_THRESHOLD |
| 100 |
cmp ecx,eax |
| 101 |
jb Ldivide_top |
| 102 |
|
| 103 |
; The carry parameter pretends to be the src high limb. |
| 104 |
mov [SAVE_EBX],ebx |
| 105 |
lea ebx,[1+ecx] ; size+1 |
| 106 |
mov eax,edx ; carry |
| 107 |
jmp Lmul_by_inverse_1c |
| 108 |
|
| 109 |
align 16 |
| 110 |
|
| 111 |
%define frame 0 |
| 112 |
|
| 113 |
___gmpn_mod_1: |
| 114 |
mov ecx,[PARAM_SIZE] |
| 115 |
sub esp,STACK_SPACE |
| 116 |
FR_sesp STACK_SPACE |
| 117 |
mov edx,0 ; initial carry (if can't skip a div) |
| 118 |
mov [SAVE_ESI],esi |
| 119 |
mov eax,[PARAM_SRC] |
| 120 |
mov [SAVE_EBP],ebp |
| 121 |
mov ebp,[PARAM_DIVISOR] |
| 122 |
mov esi,[PARAM_DIVISOR] |
| 123 |
or ecx,ecx |
| 124 |
jz Ldone_edx |
| 125 |
mov eax,[-4+eax+ecx*4] ; src high limb |
| 126 |
sar ebp,31 |
| 127 |
and ebp,MUL_NORM_DELTA |
| 128 |
add ebp,MUL_UNNORM_THRESHOLD |
| 129 |
cmp eax,esi ; carry flag if high<divisor |
| 130 |
cmovc edx,eax |
| 131 |
mov esi,[PARAM_SRC] |
| 132 |
sbb ecx,0 ; size-1 to skip one div |
| 133 |
jz Ldone_eax ; done if had size==1 |
| 134 |
cmp ecx,ebp |
| 135 |
mov ebp,[PARAM_DIVISOR] |
| 136 |
jae Lmul_by_inverse |
| 137 |
|
| 138 |
; eax scratch (quotient) |
| 139 |
; ebx |
| 140 |
; ecx counter,limbs,decrementing |
| 141 |
; edx scratch (remainder) |
| 142 |
; esi src |
| 143 |
; edi |
| 144 |
; ebp divisor |
| 145 |
|
| 146 |
Ldivide_top: |
| 147 |
mov eax,[-4+esi+ecx*4] |
| 148 |
div ebp |
| 149 |
dec ecx |
| 150 |
jnz Ldivide_top |
| 151 |
Ldone_edx: |
| 152 |
mov eax,edx |
| 153 |
Ldone_eax: |
| 154 |
mov esi,[SAVE_ESI] |
| 155 |
mov ebp,[SAVE_EBP] |
| 156 |
add esp,STACK_SPACE |
| 157 |
ret |
| 158 |
|
| 159 |
; eax src high limb |
| 160 |
; ebx |
| 161 |
; ecx |
| 162 |
; edx |
| 163 |
; esi src |
| 164 |
; edi |
| 165 |
; ebp divisor |
| 166 |
|
| 167 |
Lmul_by_inverse: |
| 168 |
mov [SAVE_EBX],ebx |
| 169 |
mov ebx,[PARAM_SIZE] |
| 170 |
Lmul_by_inverse_1c: |
| 171 |
bsr ecx,ebp ; 31-l |
| 172 |
mov [SAVE_EDI],edi |
| 173 |
xor ecx,31 ; l |
| 174 |
mov [VAR_NORM],ecx |
| 175 |
shl ebp,cl ; d normalized |
| 176 |
mov edi,eax ; src high -> n2 |
| 177 |
sub eax,ebp |
| 178 |
cmovnc edi,eax |
| 179 |
mov eax,-1 |
| 180 |
mov edx,-1 |
| 181 |
sub edx,ebp ; (b-d)-1 so edx:eax = b*(b-d)-1 |
| 182 |
lea ecx,[-8+esi+ebx*4] ; &src[size-2] |
| 183 |
div ebp ; floor (b*(b-d)-1) / d |
| 184 |
Lpreinv_entry: |
| 185 |
mov [VAR_INVERSE],eax |
| 186 |
|
| 187 |
; No special scheduling of loads is necessary in this loop,out of order |
| 188 |
; execution hides the latencies already. |
| 189 |
; |
| 190 |
; The way q1+1 is generated in %ebx and d is moved to %eax for the multiply |
| 191 |
; seems fastest. The obvious change to generate q1+1 in %eax and then just |
| 192 |
; multiply by %ebp (as per mpn/x86/pentium/mod_1.asm in fact) runs 1 cycle |
| 193 |
; slower,for no obvious reason. |
| 194 |
|
| 195 |
; eax n10 (then scratch) |
| 196 |
; ebx scratch (nadj,q1) |
| 197 |
; ecx src pointer,decrementing |
| 198 |
; edx scratch |
| 199 |
; esi n10 |
| 200 |
; edi n2 |
| 201 |
; ebp divisor |
| 202 |
|
| 203 |
align 16 |
| 204 |
Linverse_top: |
| 205 |
mov eax,[ecx] ; next src limb |
| 206 |
mov esi,eax |
| 207 |
sar eax,31 ; -n1 |
| 208 |
mov ebx,ebp |
| 209 |
and ebx,eax ; -n1 & d |
| 210 |
neg eax ; n1 |
| 211 |
add eax,edi ; n2+n1 |
| 212 |
mul dword [VAR_INVERSE] ; m*(n2+n1) |
| 213 |
add ebx,esi ; nadj = n10 + (-n1 & d),ignoring overflow |
| 214 |
sub ecx,4 |
| 215 |
add eax,ebx ; m*(n2+n1) + nadj,low giving carry flag |
| 216 |
lea ebx,[1+edi] ; n2+1 |
| 217 |
mov eax,ebp ; d |
| 218 |
adc ebx,edx ; 1 + high(n2<<32 + m*(n2+n1) + nadj) = q1+1 |
| 219 |
jz Lq1_ff |
| 220 |
mul ebx ; (q1+1)*d |
| 221 |
sub esi,eax ; low n - (q1+1)*d |
| 222 |
sbb edi,edx ; high n - (q1+1)*d,0 or -1 |
| 223 |
and edi,ebp ; d if underflow |
| 224 |
add edi,esi ; remainder with addback if necessary |
| 225 |
cmp ecx,[PARAM_SRC] |
| 226 |
jae Linverse_top |
| 227 |
|
| 228 |
; %edi is the remainder modulo d*2^n and now must be reduced to |
| 229 |
; 0<=r<d by calculating r*2^n mod d*2^n and then right shifting by |
| 230 |
; n. If d was already normalized on entry so that n==0 then nothing |
| 231 |
; is needed here. The chance of n==0 is low,but it's true of say |
| 232 |
; PP from gmp-impl.h. |
| 233 |
; |
| 234 |
; eax |
| 235 |
; ebx |
| 236 |
; ecx |
| 237 |
; edx |
| 238 |
; esi |
| 239 |
; edi remainder |
| 240 |
; ebp divisor (normalized) |
| 241 |
|
| 242 |
Linverse_loop_done: |
| 243 |
mov ecx,[VAR_NORM] |
| 244 |
mov esi,0 |
| 245 |
or ecx,ecx |
| 246 |
jz Ldone_edi |
| 247 |
|
| 248 |
; Here use %edi=n10 and %esi=n2,opposite to the loop above. |
| 249 |
; |
| 250 |
; The q1=0xFFFFFFFF case is handled with an sbbl to adjust q1+1 |
| 251 |
; back,rather than q1_ff special case code. This is simpler and |
| 252 |
; costs only 2 uops. |
| 253 |
|
| 254 |
shld esi,edi,cl |
| 255 |
shl edi,cl |
| 256 |
mov eax,edi ; n10 |
| 257 |
mov ebx,ebp ; d |
| 258 |
sar eax,31 ; -n1 |
| 259 |
and ebx,eax ; -n1 & d |
| 260 |
neg eax ; n1 |
| 261 |
add ebx,edi ; nadj = n10 + (-n1 & d),ignoring overflow |
| 262 |
add eax,esi ; n2+n1 |
| 263 |
mul dword [VAR_INVERSE] ; m*(n2+n1) |
| 264 |
add eax,ebx ; m*(n2+n1) + nadj,low giving carry flag |
| 265 |
lea ebx,[1+esi] ; n2+1 |
| 266 |
adc ebx,edx ; 1 + high(n2<<32 + m*(n2+n1) + nadj) = q1+1 |
| 267 |
sbb ebx,0 |
| 268 |
mov eax,ebp ; d |
| 269 |
mul ebx ; (q1+1)*d |
| 270 |
mov ebx,[SAVE_EBX] |
| 271 |
sub edi,eax ; low n - (q1+1)*d is remainder |
| 272 |
sbb esi,edx ; high n - (q1+1)*d,0 or -1 |
| 273 |
and esi,ebp |
| 274 |
mov ebp,[SAVE_EBP] |
| 275 |
lea eax,[esi+edi] ; remainder |
| 276 |
mov esi,[SAVE_ESI] |
| 277 |
shr eax,cl ; denorm remainder |
| 278 |
mov edi,[SAVE_EDI] |
| 279 |
add esp,STACK_SPACE |
| 280 |
ret |
| 281 |
Ldone_edi: |
| 282 |
mov ebx,[SAVE_EBX] |
| 283 |
mov eax,edi |
| 284 |
mov esi,[SAVE_ESI] |
| 285 |
mov edi,[SAVE_EDI] |
| 286 |
mov ebp,[SAVE_EBP] |
| 287 |
add esp,STACK_SPACE |
| 288 |
ret |
| 289 |
|
| 290 |
; Special case for q1=0xFFFFFFFF,giving q=0xFFFFFFFF meaning the low dword |
| 291 |
; of q*d is simply -d and the remainder n-q*d = n10+d. |
| 292 |
; |
| 293 |
; This is reached only very rarely. |
| 294 |
; |
| 295 |
; eax (divisor) |
| 296 |
; ebx (q1+1 == 0) |
| 297 |
; ecx src pointer |
| 298 |
; edx |
| 299 |
; esi n10 |
| 300 |
; edi (n2) |
| 301 |
; ebp divisor |
| 302 |
|
| 303 |
Lq1_ff: |
| 304 |
lea edi,[ebp+esi] ; n-q*d remainder -> next n2 |
| 305 |
cmp ecx,[PARAM_SRC] |
| 306 |
jae Linverse_top |
| 307 |
jmp Linverse_loop_done |
| 308 |
|
| 309 |
end |