| 1 |
; AMD64 mpn_modexact_1_odd -- exact division style remainder. |
| 2 |
|
| 3 |
; Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software |
| 4 |
; 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., 51 Franklin Street, |
| 21 |
; Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
; |
| 23 |
; cycles/limb |
| 24 |
; Hammer: 10 |
| 25 |
; Prescott/Nocona: 33 |
| 26 |
|
| 27 |
; mp_limb_t mpn_modexact_1_odd (mp_srcptr src, mp_size_t size, |
| 28 |
; mp_limb_t divisor); |
| 29 |
; mp_limb_t mpn_modexact_1c_odd (mp_srcptr src, mp_size_t size, |
| 30 |
; mp_limb_t divisor, mp_limb_t carry); |
| 31 |
; |
| 32 |
; The dependent chain in the main loop is |
| 33 |
; |
| 34 |
; cycles |
| 35 |
; subq %r8, %rax 1 |
| 36 |
; imulq %r9, %rax 4 |
| 37 |
; mulq %rsi 5 |
| 38 |
; ---- |
| 39 |
; total 10 |
| 40 |
; |
| 41 |
; The movq load from src seems to need to be scheduled back before the jz to |
| 42 |
; achieve this speed, out-of-order execution apparently can't completely |
| 43 |
; hide the latency otherwise. |
| 44 |
; |
| 45 |
; The l=src[i]-cbit step is rotated back too, since that allows us to avoid |
| 46 |
; it for the first iteration (where there's no cbit). |
| 47 |
; |
| 48 |
; The code alignment used (32-byte) for the loop also seems necessary. |
| 49 |
; Without that the non-PIC case has adcq crossing the 0x60 offset, |
| 50 |
; apparently making it run at 11 cycles instead of 10. |
| 51 |
; |
| 52 |
; Not done: |
| 53 |
; |
| 54 |
; divq for size==1 was measured at about 79 cycles, compared to the inverse |
| 55 |
; at about 25 cycles (both including function call overheads), so that's not |
| 56 |
; used. |
| 57 |
; |
| 58 |
; Enhancements: |
| 59 |
; |
| 60 |
; For PIC, we shouldn't really need the GOT fetch for modlimb_invert_table, |
| 61 |
; it'll be in rodata or text in libgmp.so and can be accessed directly %rip |
| 62 |
; relative. This would be for small model only (something we don't |
| 63 |
; presently detect, but which is all that gcc 3.3.3 supports), since 8-byte |
| 64 |
; PC-relative relocations are apparently not available. Some rough |
| 65 |
; experiments with binutils 2.13 looked worrylingly like it might come out |
| 66 |
; with an unwanted text segment relocation though, even with ".protected". |
| 67 |
|
| 68 |
; mp_limb_t mpn_modexact_1_odd ( |
| 69 |
; mp_srcptr src, rcx |
| 70 |
; mp_size_t size, rdx |
| 71 |
; mp_limb_t divisor r8 |
| 72 |
; ); |
| 73 |
; mp_limb_t mpn_modexact_1c_odd ( |
| 74 |
; mp_srcptr src, rcx |
| 75 |
; mp_size_t size, rdx |
| 76 |
; mp_limb_t divisor, r8 |
| 77 |
; mp_limb_t carry r10 |
| 78 |
; ); |
| 79 |
|
| 80 |
bits 64 |
| 81 |
text |
| 82 |
align 32 |
| 83 |
|
| 84 |
global __gmpn_modexact_1_odd |
| 85 |
global __gmpn_modexact_1c_odd |
| 86 |
extern __gmp_modlimb_invert_table |
| 87 |
|
| 88 |
%ifdef DLL |
| 89 |
export __gmpn_modexact_1_odd |
| 90 |
export __gmpn_modexact_1c_odd |
| 91 |
%endif |
| 92 |
|
| 93 |
__gmpn_modexact_1_odd: |
| 94 |
mov r9d, 0 ; carry |
| 95 |
|
| 96 |
__gmpn_modexact_1c_odd: |
| 97 |
push rsi |
| 98 |
push rdi |
| 99 |
mov rsi, rdx |
| 100 |
|
| 101 |
mov rdx, r8 |
| 102 |
shr edx, 1 ; div / 2 |
| 103 |
lea r10, [__gmp_modlimb_invert_table wrt rip] |
| 104 |
and edx, 127 |
| 105 |
movzx edx, byte [rdx+r10] ; inv -> rdx (8-bit approx) |
| 106 |
|
| 107 |
mov rax, [rcx] |
| 108 |
lea r11, [rcx+rsi*8] ; pointer to top of src |
| 109 |
mov rdi, r8 ; save divisor |
| 110 |
|
| 111 |
lea ecx, [rdx+rdx] |
| 112 |
imul edx, edx |
| 113 |
neg rsi ; limb offset from top of drc |
| 114 |
imul edx, edi |
| 115 |
sub ecx, edx ; inv -> rcx (16-bit approx) |
| 116 |
|
| 117 |
lea edx, [rcx+rcx] |
| 118 |
imul ecx, ecx |
| 119 |
imul ecx, edi |
| 120 |
sub edx, ecx ; inv -> rdx (32-bit approx) |
| 121 |
xor ecx, ecx |
| 122 |
|
| 123 |
lea r10, [rdx+rdx] |
| 124 |
imul rdx, rdx |
| 125 |
imul rdx, r8 |
| 126 |
sub r10, rdx ; inv -> r10 (64-bit approx) |
| 127 |
|
| 128 |
mov rdx, r9 ; intial carry -> rdx |
| 129 |
inc rsi ; adjust limb offset |
| 130 |
jz .1 |
| 131 |
|
| 132 |
align 16 |
| 133 |
|
| 134 |
.0: sub rax, rdx |
| 135 |
adc rcx, 0 |
| 136 |
imul rax, r10 |
| 137 |
mul r8 |
| 138 |
mov rax, [r11+8*rsi] |
| 139 |
sub rax, rcx |
| 140 |
setc cl |
| 141 |
inc rsi |
| 142 |
jnz .0 |
| 143 |
|
| 144 |
.1: sub rax, rdx |
| 145 |
adc rcx, 0 |
| 146 |
imul rax, r10 |
| 147 |
mul r8 |
| 148 |
lea rax, [rcx+rdx] |
| 149 |
pop rdi |
| 150 |
pop rsi |
| 151 |
ret |
| 152 |
|
| 153 |
end |