| 1 |
; |
| 2 |
; AMD64 mpn_mul_basecase -- multiply two mpn numbers. |
| 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 __gmpn_mul_basecase( |
| 29 |
; mp_ptr rp, rcx |
| 30 |
; mp_srcptr xp, rdx |
| 31 |
; mp_size_t xn, r8 |
| 32 |
; mp_srcptr yp, r9 |
| 33 |
; mp_size_t yn [rsp+0x28] as a *** 32-bit *** word |
| 34 |
; ) |
| 35 |
; |
| 36 |
; Multiply up[un] by vp[vn] and write the result to rp[un+vn] with un >= vn on |
| 37 |
; entry. |
| 38 |
|
| 39 |
%include "amd64i.inc" |
| 40 |
|
| 41 |
%define UNROLL_LOG2 4 |
| 42 |
%define UNROLL_COUNT (1 << UNROLL_LOG2) |
| 43 |
%define UNROLL_MASK (UNROLL_COUNT - 1) |
| 44 |
%define UNROLL_BYTES (8 * UNROLL_COUNT) |
| 45 |
%define UNROLL_THRESHOLD 5 |
| 46 |
|
| 47 |
bits 64 |
| 48 |
text |
| 49 |
|
| 50 |
%define v_par rsp + 16 |
| 51 |
%define v_adj rsp + 8 |
| 52 |
%define v_xlo rsp |
| 53 |
%define v_len 24 |
| 54 |
|
| 55 |
%define r_ptr rcx |
| 56 |
%define x_ptr r11 |
| 57 |
%define x_len r8 |
| 58 |
%define y_ptr r9 |
| 59 |
%define y_len r10 |
| 60 |
|
| 61 |
%define v_ctr r8 ; x_len reused |
| 62 |
%define v_jmp r11 ; x_ptr reused |
| 63 |
|
| 64 |
%define reg_list x86_regs,r12 |
| 65 |
|
| 66 |
global __gmpn_mul_basecase |
| 67 |
|
| 68 |
%ifdef DLL |
| 69 |
export __gmpn_mul_basecase |
| 70 |
%define PIC |
| 71 |
%endif |
| 72 |
|
| 73 |
__gmpn_mul_basecase: |
| 74 |
movsxd x_len,r8d |
| 75 |
mov rax,[y_ptr] |
| 76 |
cmp x_len,2 |
| 77 |
ja mul_m_by_n |
| 78 |
je mul_2_by_n |
| 79 |
mul qword [rdx] |
| 80 |
mov [r_ptr],rax |
| 81 |
mov [r_ptr+8],rdx |
| 82 |
ret |
| 83 |
|
| 84 |
mul_2_by_n: |
| 85 |
movsxd r10,dword[rsp+0x28] ; load as a 32-bit integer |
| 86 |
mov x_ptr,rdx |
| 87 |
dec qword y_len |
| 88 |
jnz mul_2_by_2 |
| 89 |
mov r8,rax ; y[0] -> r8 (was x_len) |
| 90 |
mov rax,[x_ptr] |
| 91 |
mul r8 |
| 92 |
mov [r_ptr],rax |
| 93 |
mov rax,[x_ptr+8] |
| 94 |
mov r9,rdx ; carry -> r9 (was y_ptr) |
| 95 |
mul r8 |
| 96 |
add r9,rax |
| 97 |
mov [r_ptr+8],r9 |
| 98 |
adc rdx,y_len ; note: r10 = 0 (was y_len) |
| 99 |
mov [r_ptr+16],rdx |
| 100 |
ret |
| 101 |
|
| 102 |
mul_2_by_2: ; r8 (x_len) and r10 (y_len) free |
| 103 |
mov r10,[x_ptr] ; x[0] |
| 104 |
mul r10 ; y[0] * x[0] |
| 105 |
mov [r_ptr],rax |
| 106 |
mov r8,rdx ; cry = { 0, r8 } |
| 107 |
mov rax,[y_ptr+8] ; y[1] |
| 108 |
mul r10 ; y[1] * x[0] |
| 109 |
add r8,rax |
| 110 |
adc rdx,byte 0 |
| 111 |
mov r10,[x_ptr+8] ; x[1] - r11 (x_ptr) now free |
| 112 |
mov r11,rdx ; cry = { r11, r8 } |
| 113 |
mov rax,[y_ptr] ; y[0] |
| 114 |
mul r10 ; y[0] * x[1] |
| 115 |
add r8,rax |
| 116 |
adc r11,rdx |
| 117 |
mov [r_ptr+8],r8 |
| 118 |
mov r8,dword 0 |
| 119 |
adc r8,r8 ; cry = { r8, r11 } |
| 120 |
mov rax,[y_ptr+8] ; y[1] |
| 121 |
mul r10 ; x[1] * y[1] |
| 122 |
add rax,r11 |
| 123 |
adc rdx,r8 |
| 124 |
mov [r_ptr+16],rax |
| 125 |
mov [r_ptr+24],rdx |
| 126 |
ret |
| 127 |
|
| 128 |
; do first multiply of y[0] * x[n] as it can simply be stored |
| 129 |
|
| 130 |
mul_m_by_n: |
| 131 |
mov r10d,dword[rsp+0x28] ; load as a 32-bit integer |
| 132 |
f_push reg_list |
| 133 |
mov x_ptr,rdx |
| 134 |
mov r12,x_len |
| 135 |
mov rbp,rax ; y[0] -> rbp |
| 136 |
xor rbx,rbx ; for carry |
| 137 |
lea rsi,[x_ptr+r12*8] ; past end of x[] |
| 138 |
lea rdi,[r_ptr+r12*8] ; past end of r[] |
| 139 |
neg r12 |
| 140 |
.0: mov rax,[rsi+r12*8] ; x[n] |
| 141 |
mul rbp ; x[n] * y[0] |
| 142 |
add rax,rbx ; add carry from previous round |
| 143 |
mov [rdi+r12*8],rax ; store r[n] |
| 144 |
mov rbx,dword 0 ; propagate carry |
| 145 |
adc rbx,rdx |
| 146 |
inc r12 ; next iteration |
| 147 |
jnz .0 |
| 148 |
mov [rdi],rbx ; store final digit in carry |
| 149 |
mov rdx,y_len ; done if y_len is 1 |
| 150 |
dec rdx |
| 151 |
jnz .1 ; more to do |
| 152 |
f_pop reg_list |
| 153 |
ret |
| 154 |
|
| 155 |
.1: cmp x_len,UNROLL_THRESHOLD ; unroll if many loops |
| 156 |
jae L_unroll |
| 157 |
lea y_ptr,[y_ptr+rdx*8+8] ; pointer to end limb of y[] |
| 158 |
neg x_len ; negative counter for x[n] |
| 159 |
neg rdx ; negative counter for y[n] |
| 160 |
mov rax,[rsi+x_len*8] ; x[0] -> rax |
| 161 |
mov y_len,rdx ; now -(y_len - 1) |
| 162 |
inc x_len ; negative counter for x[1] |
| 163 |
xor rbx,rbx ; for carry |
| 164 |
mov rcx,x_len ; now -(x_len - 1) -> rcx (was r_ptr) |
| 165 |
mov rbp,[y_ptr+rdx*8] ; y[n] -> rbp |
| 166 |
jmp .3 |
| 167 |
.2: mov rcx,x_len ; restore x[] counter |
| 168 |
xor rbx,rbx ; clear carry |
| 169 |
add rdi,8 ; increase end of r[] pointer |
| 170 |
mov rbp,[y_ptr+y_len*8] ; y[n] -> rbp |
| 171 |
mov rax,[rsi+rcx*8-8] ; x[m] -> rax |
| 172 |
.3: mul rbp ; x[m] * y[n] |
| 173 |
add rbx,rax ; add carry |
| 174 |
adc rdx,byte 0 |
| 175 |
add [rdi+rcx*8],rbx ; add into r[] |
| 176 |
mov rax,[rsi+rcx*8] ; next x[m] ->rax |
| 177 |
adc rdx,byte 0 ; add carry to rdx |
| 178 |
inc rcx ; got to next limb of x[] |
| 179 |
mov rbx,rdx ; move carry into rbx |
| 180 |
jnz .3 ; got to next limb of x[] |
| 181 |
mul rbp ; do last limb |
| 182 |
add rbx,rax ; propagate carry |
| 183 |
adc rdx,byte 0 |
| 184 |
add [rdi],rbx ; add into r[] |
| 185 |
adc rdx,byte 0 ; add add in any carry |
| 186 |
inc y_len |
| 187 |
mov [rdi+8],rdx ; move (not add) carry into r[] |
| 188 |
jnz .2 ; go to next limb of y[] |
| 189 |
f_pop reg_list |
| 190 |
ret |
| 191 |
|
| 192 |
L_unroll: |
| 193 |
f_add v_len |
| 194 |
mov rdi,r_ptr |
| 195 |
mov rcx,x_len |
| 196 |
mov rsi,x_ptr |
| 197 |
mov rbp,[y_ptr+8] |
| 198 |
lea y_ptr,[y_ptr+rdx*8+8] |
| 199 |
neg rdx |
| 200 |
mov y_len,rdx |
| 201 |
lea rbx,[UNROLL_COUNT-2+rcx] |
| 202 |
dec rcx |
| 203 |
mov rax,[rsi] ; x[0] |
| 204 |
and rbx,-UNROLL_MASK-1 |
| 205 |
neg rcx |
| 206 |
neg rbx |
| 207 |
and rcx,UNROLL_MASK |
| 208 |
mov [v_par],rcx |
| 209 |
mov [v_adj],rbx |
| 210 |
mov rdx,rcx |
| 211 |
shl rcx,3 |
| 212 |
%ifdef PIC |
| 213 |
lea rcx,[rcx+rcx*2] |
| 214 |
lea v_jmp,[.4 wrt rip] |
| 215 |
lea v_jmp,[v_jmp+rcx] |
| 216 |
%else |
| 217 |
lea v_jmp,[rcx+rcx*2+.4] |
| 218 |
%endif |
| 219 |
neg rdx |
| 220 |
mov [v_xlo],rax |
| 221 |
lea rdi,[rdi+rdx*8+8] |
| 222 |
lea rsi,[rsi+rdx*8+8] |
| 223 |
jmp .3 |
| 224 |
.2: mov rbx,[v_adj] |
| 225 |
mov rax,[v_xlo] |
| 226 |
lea rdi,[rdi+rbx*8+8] |
| 227 |
lea rsi,[rsi+rbx*8] |
| 228 |
mov rbp,[y_ptr+y_len*8] |
| 229 |
.3: mul rbp |
| 230 |
sar rbx,UNROLL_LOG2 |
| 231 |
mov rcx,[v_par] |
| 232 |
mov v_ctr,rbx |
| 233 |
test cl,1 ; low word of product + carry |
| 234 |
mov rbx,dword 0 ; is in rcx on even rounds and |
| 235 |
mov rcx,dword 0 ; rbx on odd rounds - we must |
| 236 |
cmovz rcx,rax ; put low word of first product |
| 237 |
cmovnz rbx,rax ; in the right register here |
| 238 |
jmp v_jmp |
| 239 |
.4: |
| 240 |
|
| 241 |
%define CHUNK_COUNT 2 |
| 242 |
%assign i 0 |
| 243 |
%rep UNROLL_COUNT / CHUNK_COUNT |
| 244 |
%define disp0 8 * i * CHUNK_COUNT |
| 245 |
|
| 246 |
mov rax,[byte rsi+disp0] |
| 247 |
adc rbx,rdx |
| 248 |
mul rbp |
| 249 |
add [byte rdi+disp0],rcx |
| 250 |
mov rcx,dword 0 |
| 251 |
adc rbx,rax |
| 252 |
mov rax,[byte rsi+disp0+8] |
| 253 |
adc rcx,rdx |
| 254 |
mul rbp |
| 255 |
add [byte rdi+disp0+8],rbx |
| 256 |
mov rbx,dword 0 |
| 257 |
adc rcx,rax |
| 258 |
|
| 259 |
%assign i i + 1 |
| 260 |
%endrep |
| 261 |
|
| 262 |
inc v_ctr |
| 263 |
lea rsi,[UNROLL_BYTES+rsi] |
| 264 |
lea rdi,[UNROLL_BYTES+rdi] |
| 265 |
jnz .4 |
| 266 |
|
| 267 |
adc rdx,byte 0 |
| 268 |
add [rdi],rcx |
| 269 |
adc rdx,byte 0 |
| 270 |
inc y_len |
| 271 |
mov [rdi+8],rdx |
| 272 |
jnz .2 |
| 273 |
f_sub v_len |
| 274 |
f_pop reg_list |
| 275 |
ret |
| 276 |
|
| 277 |
end |