| 1 |
; |
| 2 |
; AMD64 mpn_mul_1 -- mpn by limb multiply. |
| 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 |
; mp_limb_t mpn_mul_1 ( |
| 28 |
; mp_ptr dst, rcx |
| 29 |
; mp_srcptr src, rdx |
| 30 |
; mp_size_t size, r8 |
| 31 |
; mp_limb_t multiplier r9 |
| 32 |
; ) |
| 33 |
; |
| 34 |
; mp_limb_t mpn_mul_1c ( |
| 35 |
; mp_ptr dst, rcx |
| 36 |
; mp_srcptr src, rdx |
| 37 |
; mp_size_t size, r8 |
| 38 |
; mp_limb_t multiplier, r9 |
| 39 |
; mp_limb_t carry [rsp+0x28] |
| 40 |
; ) |
| 41 |
; |
| 42 |
; Multiply src[size] by mult and store the result in dst[size]. Return the |
| 43 |
; carry limb from the top of the result. |
| 44 |
; |
| 45 |
; mpn_mul_1c() accepts an initial carry for the calculation, it's added into |
| 46 |
; the low limb of the destination. |
| 47 |
|
| 48 |
; Maximum possible UNROLL_COUNT with the current code is 64. |
| 49 |
|
| 50 |
%if 1 |
| 51 |
|
| 52 |
%define dst rcx |
| 53 |
%define len r8 |
| 54 |
%define mlt r9 |
| 55 |
%define cry [rsp+0x28] |
| 56 |
%define src r10 ; from rdx on input |
| 57 |
|
| 58 |
%define cnt rbx ; loop count |
| 59 |
|
| 60 |
%define UNROLL_LOG2 4 |
| 61 |
%define UNROLL_COUNT (1 << UNROLL_LOG2) |
| 62 |
%define UNROLL_MASK (UNROLL_COUNT - 1) |
| 63 |
%define UNROLL_BYTES 8 * UNROLL_COUNT |
| 64 |
|
| 65 |
%if UNROLL_BYTES >= 256 |
| 66 |
%error unroll count is too large |
| 67 |
%elif UNROLL_BYTES >= 128 |
| 68 |
%define off 128 |
| 69 |
%else |
| 70 |
%define off 0 |
| 71 |
%endif |
| 72 |
|
| 73 |
%define UNROLL_THRESHOLD 7 |
| 74 |
|
| 75 |
bits 64 |
| 76 |
text |
| 77 |
|
| 78 |
global __gmpn_mul_1 |
| 79 |
global __gmpn_mul_1c |
| 80 |
|
| 81 |
%ifdef DLL |
| 82 |
export __gmpn_mul_1 |
| 83 |
export __gmpn_mul_1c |
| 84 |
%define PIC |
| 85 |
%endif |
| 86 |
|
| 87 |
__gmpn_mul_1c: |
| 88 |
mov r11,[rsp+0x28] |
| 89 |
jmp start |
| 90 |
|
| 91 |
__gmpn_mul_1: |
| 92 |
xor r11,r11 |
| 93 |
|
| 94 |
start: |
| 95 |
movsxd len,r8d |
| 96 |
mov src,rdx |
| 97 |
cmp len,UNROLL_THRESHOLD |
| 98 |
jae .1 |
| 99 |
lea src,[src+len*8] |
| 100 |
lea dst,[dst+len*8] |
| 101 |
neg len |
| 102 |
.0: mov rax,[src+len*8] |
| 103 |
mul mlt |
| 104 |
add rax,r11 |
| 105 |
mov r11,dword 0 |
| 106 |
adc r11,rdx |
| 107 |
mov [dst+len*8],rax |
| 108 |
inc len |
| 109 |
jnz .0 |
| 110 |
mov rax,r11 |
| 111 |
ret |
| 112 |
|
| 113 |
; The mov to load the next source limb is done well ahead of the mul, this |
| 114 |
; is necessary for full speed. It leads to one limb handled separately |
| 115 |
; after the loop. |
| 116 |
; |
| 117 |
; When unrolling to 32 or more, an offset of +4 is used on the src pointer, |
| 118 |
; to avoid having an 0x80 displacement in the code for the last limb in the |
| 119 |
; unrolled loop. This is for a fair comparison between 16 and 32 unrolling. |
| 120 |
|
| 121 |
.1: |
| 122 |
push rbx |
| 123 |
|
| 124 |
lea cnt,[len-2] |
| 125 |
lea len,[len-1] |
| 126 |
neg len |
| 127 |
shr cnt,UNROLL_LOG2 |
| 128 |
and len,UNROLL_MASK |
| 129 |
mov rdx,len |
| 130 |
shl rdx,4 |
| 131 |
%ifdef PIC |
| 132 |
lea rax,[.3 wrt rip] |
| 133 |
lea rdx,[rdx+len*4] |
| 134 |
lea rdx,[rdx+rax] |
| 135 |
%else |
| 136 |
lea rdx,[rdx+len*4+.3] |
| 137 |
%endif |
| 138 |
mov rax,[src] |
| 139 |
neg len |
| 140 |
lea src,[src+len*8+off] |
| 141 |
lea dst,[dst+len*8+off] |
| 142 |
xor len,len ; len now zero |
| 143 |
jmp rdx |
| 144 |
|
| 145 |
.3: |
| 146 |
%assign i 0 |
| 147 |
%rep UNROLL_COUNT |
| 148 |
%define disp 8 * i - off |
| 149 |
|
| 150 |
mul mlt ; 20 bytes per block |
| 151 |
add r11,rax |
| 152 |
mov rax,[byte src+disp+8] |
| 153 |
mov [byte dst+disp],r11 |
| 154 |
mov r11,len |
| 155 |
adc r11,rdx |
| 156 |
|
| 157 |
%assign i i + 1 |
| 158 |
%endrep |
| 159 |
|
| 160 |
dec cnt |
| 161 |
lea src,[src+UNROLL_BYTES] |
| 162 |
lea dst,[dst+UNROLL_BYTES] |
| 163 |
jns .3 |
| 164 |
mul mlt |
| 165 |
add r11,rax |
| 166 |
mov rax,len |
| 167 |
mov [dst-off],r11 |
| 168 |
adc rax,rdx |
| 169 |
pop rbx |
| 170 |
ret |
| 171 |
|
| 172 |
%else |
| 173 |
|
| 174 |
bits 64 |
| 175 |
text |
| 176 |
global __gmpn_mul_1 |
| 177 |
global __gmpn_mul_1c |
| 178 |
|
| 179 |
%ifdef DLL |
| 180 |
export __gmpn_mul_1 |
| 181 |
export __gmpn_mul_1c |
| 182 |
%endif |
| 183 |
__gmpn_mul_1c: |
| 184 |
mov r11, [rsp+0x28] |
| 185 |
jmp start |
| 186 |
|
| 187 |
align 16 |
| 188 |
nop |
| 189 |
nop |
| 190 |
__gmpn_mul_1: |
| 191 |
xor r11, r11 |
| 192 |
start: |
| 193 |
lea r10, [rdx+8*r8] |
| 194 |
lea rcx, [rcx+8*r8] |
| 195 |
neg r8 |
| 196 |
.1: mov rax, [r10+8*r8] |
| 197 |
mul r9 |
| 198 |
add rax, r11 |
| 199 |
mov r11d, 0 |
| 200 |
adc r11, rdx |
| 201 |
mov [rcx+8*r8], rax |
| 202 |
inc r8 |
| 203 |
jne .1 |
| 204 |
mov rax, r11 |
| 205 |
ret |
| 206 |
|
| 207 |
%endif |
| 208 |
|
| 209 |
end |