| 1 |
; |
| 2 |
; AMD64 mpn_divexact_1 -- mpn by limb exact division |
| 3 |
; |
| 4 |
; Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
| 5 |
; |
| 6 |
; This file is part of the GNU MP Library. |
| 7 |
; |
| 8 |
; The GNU MP Library is free software; you can redistribute it and/or |
| 9 |
; modify it under the terms of the GNU Lesser General Public License as |
| 10 |
; published by the Free Software Foundation; either version 2.1 of the |
| 11 |
; License, or (at your option) any later version. |
| 12 |
; |
| 13 |
; The GNU MP Library is distributed in the hope that it will be useful, |
| 14 |
; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
; Lesser General Public License for more details. |
| 17 |
; |
| 18 |
; You should have received a copy of the GNU Lesser General Public |
| 19 |
; License along with the GNU MP Library; see the file COPYING.LIB. If |
| 20 |
; not, write to the Free Software Foundation, Inc., 59 Temple Place - |
| 21 |
; Suite 330, Boston, MA 02111-1307, USA. |
| 22 |
; |
| 23 |
; Adapted by Brian Gladman AMD64 using the Microsoft VC++ v8 64-bit |
| 24 |
; compiler and the YASM assembler. |
| 25 |
; |
| 26 |
; Calling interface: |
| 27 |
; |
| 28 |
; void mpn_divexact_1( |
| 29 |
; mp_ptr dst, rcx |
| 30 |
; mp_srcptr src, rdx |
| 31 |
; mp_size_t size, r8 |
| 32 |
; mp_limb_t divisor r9 |
| 33 |
; ) |
| 34 |
; |
| 35 |
; since the inverse takes a while to setup,plain division is used for small |
| 36 |
; Multiplying works out faster for size>=3 when the divisor is odd or size>=4 |
| 37 |
; when the divisor is even. |
| 38 |
|
| 39 |
bits 64 |
| 40 |
text |
| 41 |
|
| 42 |
extern __gmp_modlimb_invert_table |
| 43 |
global __gmpn_divexact_1 |
| 44 |
|
| 45 |
%ifdef DLL |
| 46 |
export __gmpn_divexact_1 |
| 47 |
%endif |
| 48 |
|
| 49 |
__gmpn_divexact_1: |
| 50 |
movsxd r8,r8d |
| 51 |
mov r10,rdx |
| 52 |
mov rax,r9 |
| 53 |
and rax,byte 1 |
| 54 |
add rax,r8 |
| 55 |
cmp rax,byte 4 |
| 56 |
jae L_mul_by_inverse |
| 57 |
xor rdx,rdx |
| 58 |
L_div_top: |
| 59 |
mov rax,[r10+r8*8-8] |
| 60 |
div r9 |
| 61 |
mov [rcx+r8*8-8],rax |
| 62 |
dec r8 |
| 63 |
jnz L_div_top |
| 64 |
rep ret ; avoid single byte return |
| 65 |
|
| 66 |
L_mul_by_inverse: |
| 67 |
push rsi |
| 68 |
push rdi |
| 69 |
mov rsi,rdx ; src pointer |
| 70 |
mov rdi,rcx ; dst pointer |
| 71 |
mov rax,r9 |
| 72 |
stc |
| 73 |
sbb rcx,rcx ; -1 -> rcx, r11 |
| 74 |
mov r11,rcx |
| 75 |
L_strip_twos: |
| 76 |
shr rax,1 |
| 77 |
inc rcx |
| 78 |
jnc L_strip_twos |
| 79 |
lea r9,[rax+rax+1] |
| 80 |
and rax,byte 127 |
| 81 |
lea rdx,[__gmp_modlimb_invert_table wrt rip] |
| 82 |
movzx rax,byte [rdx+rax] |
| 83 |
|
| 84 |
; If f(x) = 0, then x[n+1] = x[n] - f(x) / f'(x) is Newton's iteration for a |
| 85 |
; root. With f(x) = 1/x - v we obtain x[n + 1] = 2 * x[n] - v * x[n] * x[n] |
| 86 |
; as an iteration for x = 1 / v. This provides quadratic convergence so |
| 87 |
; that the number of bits of precision doubles on each iteration. The |
| 88 |
; iteration starts with 8-bit precision. |
| 89 |
|
| 90 |
lea edx, [rax+rax] |
| 91 |
imul eax, eax |
| 92 |
imul eax, r9d |
| 93 |
sub edx, eax ; inv -> rdx (16-bit approx) |
| 94 |
|
| 95 |
lea eax, [rdx+rdx] |
| 96 |
imul edx, edx |
| 97 |
imul edx, r9d |
| 98 |
sub eax, edx ; inv -> rcx (32-bit approx) |
| 99 |
|
| 100 |
lea rdx, [rax+rax] |
| 101 |
imul rax, rax |
| 102 |
imul rax, r9 |
| 103 |
sub rdx, rax ; inv -> rcx (64-bit approx) |
| 104 |
|
| 105 |
mov r8,r8 |
| 106 |
lea rsi,[rsi+r8*8] |
| 107 |
lea rdi,[rdi+r8*8] |
| 108 |
neg r8 |
| 109 |
|
| 110 |
mov r10,rdx |
| 111 |
xor r11,r11 |
| 112 |
mov rax,[rsi+r8*8] |
| 113 |
or rcx,rcx |
| 114 |
mov rdx,[rsi+r8*8+8] |
| 115 |
jz L_odd_entry |
| 116 |
shrd rax,rdx,cl |
| 117 |
inc r8 |
| 118 |
jmp L_even_entry |
| 119 |
|
| 120 |
L_odd_top: |
| 121 |
mul r9 |
| 122 |
mov rax,[rsi+r8*8] |
| 123 |
sub rdx,r11 |
| 124 |
sub rax,rdx |
| 125 |
sbb r11,r11 |
| 126 |
L_odd_entry: |
| 127 |
imul rax,r10 |
| 128 |
mov [rdi+r8*8],rax |
| 129 |
inc r8 |
| 130 |
jnz L_odd_top |
| 131 |
pop rdi |
| 132 |
pop rsi |
| 133 |
ret |
| 134 |
|
| 135 |
L_even_top: |
| 136 |
mul r9 |
| 137 |
sub rdx,r11 |
| 138 |
mov rax,[rsi+r8*8-8] |
| 139 |
mov r11,[rsi+r8*8] |
| 140 |
shrd rax,r11,cl |
| 141 |
sub rax,rdx |
| 142 |
sbb r11,r11 |
| 143 |
|
| 144 |
L_even_entry: |
| 145 |
imul rax,r10 |
| 146 |
mov [rdi+r8*8-8],rax |
| 147 |
inc r8 |
| 148 |
jnz L_even_top |
| 149 |
mul r9 |
| 150 |
mov rax,[rsi-8] |
| 151 |
sub rdx,r11 |
| 152 |
shr rax,cl |
| 153 |
sub rax,rdx |
| 154 |
imul rax,r10 |
| 155 |
mov [rdi-8],rax |
| 156 |
pop rdi |
| 157 |
pop rsi |
| 158 |
ret |
| 159 |
|
| 160 |
end |