2009-01-10 10:15:37 -05:00
|
|
|
|
2008-06-25 03:33:36 -04:00
|
|
|
; Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|
|
|
;
|
2009-01-10 10:15:37 -05:00
|
|
|
; Copyright 2008 Brian Gladman
|
|
|
|
;
|
|
|
|
; This file is part of the MPIR Library.
|
2008-06-25 03:33:36 -04:00
|
|
|
;
|
2009-01-10 10:15:37 -05:00
|
|
|
; The MPIR Library is free software; you can redistribute it and/or
|
2008-06-25 03:33:36 -04:00
|
|
|
; modify it under the terms of the GNU Lesser General Public License as
|
|
|
|
; published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
; License, or (at your option) any later version.
|
|
|
|
;
|
2009-01-10 10:15:37 -05:00
|
|
|
; The MPIR Library is distributed in the hope that it will be useful,
|
2008-06-25 03:33:36 -04:00
|
|
|
; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
; Lesser General Public License for more details.
|
|
|
|
;
|
|
|
|
; You should have received a copy of the GNU Lesser General Public
|
2009-01-10 10:15:37 -05:00
|
|
|
; License along with the MPIR Library; see the file COPYING.LIB. If
|
|
|
|
; not, write to the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
; Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-22 16:03:08 -05:00
|
|
|
;
|
2008-06-25 03:33:36 -04:00
|
|
|
; since the inverse takes a while to setup,plain division is used for small
|
|
|
|
; Multiplying works out faster for size>=3 when the divisor is odd or size>=4
|
|
|
|
; when the divisor is even.
|
|
|
|
;
|
2009-09-02 07:41:43 -04:00
|
|
|
; void mpn_divexact_1(mp_ptr, mp_ptr, mp_size_t, mp_limb_t)
|
|
|
|
; rdi rsi rdx rcx
|
2010-07-02 07:52:24 -04:00
|
|
|
; rcx rdx r8 r9
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
%include "yasm_mac.inc"
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
%define reg_save_list rsi, rdi
|
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
BITS 64
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
extern __gmp_modlimb_invert_table
|
2009-04-03 05:07:52 -04:00
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
LEAF_PROC mpn_divexact_1
|
2009-02-27 09:24:25 -05:00
|
|
|
mov r10, rdx
|
|
|
|
mov rax, r9
|
|
|
|
and rax, byte 1
|
|
|
|
add rax, r8
|
|
|
|
cmp rax, byte 4
|
2009-12-02 11:24:00 -05:00
|
|
|
jae .2
|
2008-06-25 03:33:36 -04:00
|
|
|
xor rdx,rdx
|
2009-12-02 11:24:00 -05:00
|
|
|
.1:
|
|
|
|
mov rax, [r10+r8*8-8]
|
2008-06-25 03:33:36 -04:00
|
|
|
div r9
|
2009-02-27 09:24:25 -05:00
|
|
|
mov [rcx+r8*8-8], rax
|
|
|
|
sub r8, 1
|
2009-12-02 11:24:00 -05:00
|
|
|
jnz .1
|
2009-02-27 09:24:25 -05:00
|
|
|
ret ; avoid single byte return
|
2009-12-02 11:24:00 -05:00
|
|
|
.2:
|
2009-04-03 05:07:52 -04:00
|
|
|
FRAME_PROC ?mpn_divexact, 0, reg_save_list
|
2009-02-27 09:24:25 -05:00
|
|
|
mov rsi, rdx ; src pointer
|
|
|
|
mov rdi, rcx ; dst pointer
|
|
|
|
bsf rcx, r9 ; remove powers of two
|
|
|
|
shr r9, cl
|
|
|
|
mov rax, r9
|
|
|
|
shr rax, 1
|
|
|
|
and rax, 127
|
|
|
|
lea rdx, [rel __gmp_modlimb_invert_table]
|
|
|
|
movzx rax, byte [rdx+rax]
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
; If f(x) = 0, then x[n+1] = x[n] - f(x) / f'(x) is Newton's iteration for a
|
|
|
|
; root. With f(x) = 1/x - v we obtain x[n + 1] = 2 * x[n] - v * x[n] * x[n]
|
|
|
|
; as an iteration for x = 1 / v. This provides quadratic convergence so
|
|
|
|
; that the number of bits of precision doubles on each iteration. The
|
|
|
|
; iteration starts with 8-bit precision.
|
|
|
|
|
|
|
|
lea edx, [rax+rax]
|
|
|
|
imul eax, eax
|
|
|
|
imul eax, r9d
|
2009-02-27 09:24:25 -05:00
|
|
|
sub edx, eax ; inv -> rdx (16-bit approx)
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
lea eax, [rdx+rdx]
|
|
|
|
imul edx, edx
|
|
|
|
imul edx, r9d
|
2009-02-27 09:24:25 -05:00
|
|
|
sub eax, edx ; inv -> rdx (32-bit approx)
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
lea rdx, [rax+rax]
|
|
|
|
imul rax, rax
|
|
|
|
imul rax, r9
|
2009-02-27 09:24:25 -05:00
|
|
|
sub rdx, rax ; inv -> rdx (64-bit approx)
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2009-02-27 09:24:25 -05:00
|
|
|
lea rsi, [rsi+r8*8]
|
|
|
|
lea rdi, [rdi+r8*8]
|
2008-06-25 03:33:36 -04:00
|
|
|
neg r8
|
|
|
|
|
2009-02-27 09:24:25 -05:00
|
|
|
mov r10, rdx ; inverse multiplier -> r10
|
|
|
|
xor r11, r11
|
|
|
|
mov rax, [rsi+r8*8]
|
|
|
|
or rcx, rcx
|
|
|
|
mov rdx, [rsi+r8*8+8]
|
2009-12-02 11:24:00 -05:00
|
|
|
jz .4 ; if divisor is odd
|
2008-06-25 03:33:36 -04:00
|
|
|
shrd rax, rdx, cl
|
2009-02-27 09:24:25 -05:00
|
|
|
add r8, 1
|
2009-12-02 11:24:00 -05:00
|
|
|
jmp .6
|
2009-02-27 09:24:25 -05:00
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
xalign 16
|
2009-12-02 11:24:00 -05:00
|
|
|
.3:
|
|
|
|
mul r9 ; divisor is odd
|
2009-02-27 09:24:25 -05:00
|
|
|
mov rax, [rsi+r8*8]
|
|
|
|
sub rdx, r11
|
|
|
|
sub rax, rdx
|
|
|
|
sbb r11, r11
|
2009-12-02 11:24:00 -05:00
|
|
|
.4:
|
|
|
|
imul rax, r10
|
2009-02-27 09:24:25 -05:00
|
|
|
mov [rdi+r8*8], rax
|
|
|
|
add r8, 1
|
2009-12-02 11:24:00 -05:00
|
|
|
jnz .3
|
|
|
|
jmp .7
|
2009-02-27 09:24:25 -05:00
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
xalign 16
|
2009-12-02 11:24:00 -05:00
|
|
|
.5:
|
|
|
|
mul r9 ; divisor is even
|
2009-02-27 09:24:25 -05:00
|
|
|
sub rdx, r11
|
|
|
|
mov rax, [rsi+r8*8-8]
|
|
|
|
mov r11, [rsi+r8*8]
|
|
|
|
shrd rax, r11, cl
|
|
|
|
sub rax, rdx
|
|
|
|
sbb r11, r11
|
2009-12-02 11:24:00 -05:00
|
|
|
.6:
|
|
|
|
imul rax, r10
|
2008-06-25 03:33:36 -04:00
|
|
|
mov [rdi+r8*8-8],rax
|
2009-02-27 09:24:25 -05:00
|
|
|
add r8, 1
|
2009-12-02 11:24:00 -05:00
|
|
|
jnz .5
|
2009-02-27 09:24:25 -05:00
|
|
|
|
2008-06-25 03:33:36 -04:00
|
|
|
mul r9
|
2009-02-27 09:24:25 -05:00
|
|
|
mov rax, [rsi-8]
|
|
|
|
sub rdx, r11
|
|
|
|
shr rax, cl
|
|
|
|
sub rax, rdx
|
|
|
|
imul rax, r10
|
|
|
|
mov [rdi-8], rax
|
2009-12-02 11:24:00 -05:00
|
|
|
.7:
|
|
|
|
END_PROC reg_save_list
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
end
|