2009-01-10 10:15:37 -05:00
|
|
|
|
|
|
|
; Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|
|
|
;
|
|
|
|
; Copyright 2008 Brian Gladman
|
2008-06-25 03:33:36 -04:00
|
|
|
;
|
2009-01-10 10:15:37 -05:00
|
|
|
; 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
|
2008-06-25 03:33:36 -04:00
|
|
|
; 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
|
|
|
; mp_limb_t mpn_modexact_1_odd (mp_srcptr src, mp_size_t size,
|
|
|
|
; mp_limb_t divisor);
|
|
|
|
; mp_limb_t mpn_modexact_1c_odd (mp_srcptr src, mp_size_t size,
|
|
|
|
; mp_limb_t divisor, mp_limb_t carry);
|
|
|
|
;
|
|
|
|
; AMD64 mpn_modexact_1_odd -- exact division style remainder.
|
|
|
|
;
|
|
|
|
; mp_limb_t mpn_modexact_1_odd (
|
|
|
|
; mp_srcptr src, rcx
|
|
|
|
; mp_size_t size, rdx
|
|
|
|
; mp_limb_t divisor r8
|
|
|
|
; );
|
|
|
|
; mp_limb_t mpn_modexact_1c_odd (
|
|
|
|
; mp_srcptr src, rcx
|
|
|
|
; mp_size_t size, rdx
|
|
|
|
; mp_limb_t divisor, r8
|
|
|
|
; mp_limb_t carry r9
|
|
|
|
; );
|
|
|
|
;
|
|
|
|
; This is an SEH Frame Function with a leaf prologue
|
|
|
|
|
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-03-07 10:00:35 -05:00
|
|
|
LEAF_PROC mpn_modexact_1_odd
|
2008-07-04 06:39:15 -04:00
|
|
|
mov r9, 0 ; carry
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
FRAME_PROC mpn_modexact_1c_odd, 0, reg_save_list
|
2008-07-04 06:39:15 -04:00
|
|
|
|
|
|
|
; first use Newton's iteration to invert the divisor limb (d) using
|
|
|
|
; f(x) = 1/x - d and x[i+1] = x[i] - f(x[i]) / f'(x[i]) to give
|
|
|
|
; the iteration formula: x[i+1] = x[i] * (2 - d * x[i])
|
|
|
|
|
2008-06-25 03:33:36 -04:00
|
|
|
movsxd rsi, edx
|
2008-07-04 06:39:15 -04:00
|
|
|
mov rdx, r8
|
2008-06-25 03:33:36 -04:00
|
|
|
shr edx, 1 ; div / 2
|
|
|
|
lea r10, [rel __gmp_modlimb_invert_table]
|
|
|
|
and edx, 127
|
|
|
|
movzx edx, byte [rdx+r10] ; inv -> rdx (8-bit approx)
|
|
|
|
|
2008-07-04 06:39:15 -04:00
|
|
|
mov rax, [rcx] ; first limb of numerator
|
2008-06-25 03:33:36 -04:00
|
|
|
lea r11, [rcx+rsi*8] ; pointer to top of src
|
|
|
|
mov rdi, r8 ; save divisor
|
|
|
|
|
|
|
|
lea ecx, [rdx+rdx]
|
|
|
|
imul edx, edx
|
2008-07-04 06:39:15 -04:00
|
|
|
neg rsi ; limb offset from top of src
|
2008-06-25 03:33:36 -04:00
|
|
|
imul edx, edi
|
|
|
|
sub ecx, edx ; inv -> rcx (16-bit approx)
|
|
|
|
|
|
|
|
lea edx, [rcx+rcx]
|
|
|
|
imul ecx, ecx
|
|
|
|
imul ecx, edi
|
|
|
|
sub edx, ecx ; inv -> rdx (32-bit approx)
|
|
|
|
xor ecx, ecx
|
|
|
|
|
|
|
|
lea r10, [rdx+rdx]
|
|
|
|
imul rdx, rdx
|
|
|
|
imul rdx, r8
|
|
|
|
sub r10, rdx ; inv -> r10 (64-bit approx)
|
|
|
|
|
|
|
|
mov rdx, r9 ; intial carry -> rdx
|
2009-02-27 09:24:25 -05:00
|
|
|
add rsi, 1 ; adjust limb offset
|
2008-06-25 03:33:36 -04:00
|
|
|
jz .1
|
|
|
|
|
|
|
|
mov r9, r11
|
|
|
|
lea rsi,[r11+rsi*8]
|
|
|
|
align 16
|
|
|
|
|
|
|
|
.0: sub rax, rdx
|
|
|
|
adc rcx, 0
|
|
|
|
imul rax, r10
|
|
|
|
mul r8
|
|
|
|
lodsq
|
|
|
|
sub rax, rcx
|
|
|
|
setc cl
|
|
|
|
cmp r9, rsi
|
|
|
|
jne .0
|
|
|
|
|
|
|
|
.1: sub rax, rdx
|
|
|
|
adc rcx, 0
|
|
|
|
imul rax, r10
|
|
|
|
mul r8
|
|
|
|
lea rax, [rcx+rdx]
|
2009-03-07 10:00:35 -05:00
|
|
|
END_PROC reg_save_list
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
end
|