| 1 |
; |
| 2 |
; AMD64 mpn_sqr_basecase -- square an mpn number. |
| 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 mpn_sqr_basecase( |
| 29 |
; mp_ptr dst, rcx |
| 30 |
; mp_srcptr src, rdx |
| 31 |
; mp_size_t size r8 |
| 32 |
; ) |
| 33 |
; |
| 34 |
|
| 35 |
%include "amd64i.inc" |
| 36 |
|
| 37 |
%define UNROLL_COUNT 31 |
| 38 |
%define CODE_BYTES_PER_LIMB 25 ; must be odd |
| 39 |
|
| 40 |
%if UNROLL_COUNT > 15 |
| 41 |
%define off (UNROLL_COUNT - 15) * 8 |
| 42 |
%else |
| 43 |
%define off 0 |
| 44 |
%endif |
| 45 |
|
| 46 |
%define reg_list_2 x86_regs,r12,r13 |
| 47 |
|
| 48 |
%define r_ptr r10 |
| 49 |
%define x_ptr r9 |
| 50 |
%define x_len r8 |
| 51 |
|
| 52 |
%define v_ctr r12 |
| 53 |
%define v_jmp r13 |
| 54 |
|
| 55 |
bits 64 |
| 56 |
text |
| 57 |
|
| 58 |
global __gmpn_sqr_basecase |
| 59 |
|
| 60 |
%ifdef DLL |
| 61 |
export __gmpn_sqr_basecase |
| 62 |
%define PIC |
| 63 |
%endif |
| 64 |
|
| 65 |
__gmpn_sqr_basecase: |
| 66 |
movsxd x_len,r8d |
| 67 |
cmp x_len,2 |
| 68 |
je sqr_2 |
| 69 |
ja sqr_3_plus |
| 70 |
mov rax,[rdx] |
| 71 |
mul rax |
| 72 |
mov [rcx+8],rdx |
| 73 |
mov [rcx],rax |
| 74 |
ret |
| 75 |
|
| 76 |
sqr_2: |
| 77 |
mov r_ptr,rcx |
| 78 |
mov x_ptr,rdx |
| 79 |
mov r8,[x_ptr] |
| 80 |
mov r9,[x_ptr+8] |
| 81 |
mov rax,r8 |
| 82 |
mul r8 |
| 83 |
mov [r_ptr],rax |
| 84 |
mov [r_ptr+8],rdx |
| 85 |
mov rax,r9 |
| 86 |
mul r9 |
| 87 |
mov [r_ptr+16],rax |
| 88 |
mov [r_ptr+24],rdx |
| 89 |
xor rcx,rcx |
| 90 |
mov rax,r8 |
| 91 |
mul r9 |
| 92 |
add rax,rax |
| 93 |
adc rdx,rdx |
| 94 |
adc rcx,rcx |
| 95 |
add [r_ptr+8],rax |
| 96 |
adc [r_ptr+16],rdx |
| 97 |
adc [r_ptr+24],rcx |
| 98 |
ret |
| 99 |
|
| 100 |
sqr_3_plus: |
| 101 |
mov r_ptr,rcx |
| 102 |
mov x_ptr,rdx |
| 103 |
cmp x_len,4 |
| 104 |
jae sqr_4_plus |
| 105 |
f_push x86_regs |
| 106 |
mov rax,[x_ptr] |
| 107 |
mul rax |
| 108 |
mov [r_ptr],rax |
| 109 |
mov rax,[x_ptr+8] |
| 110 |
mov [r_ptr+8],rdx |
| 111 |
mul rax |
| 112 |
mov [r_ptr+16],rax |
| 113 |
mov rax,[x_ptr+16] |
| 114 |
mov [r_ptr+24],rdx |
| 115 |
mul rax |
| 116 |
mov [r_ptr+32],rax |
| 117 |
mov rax,[x_ptr] |
| 118 |
mov [r_ptr+40],rdx |
| 119 |
mul qword [x_ptr+8] |
| 120 |
mov rsi,rax |
| 121 |
mov rax,[x_ptr] |
| 122 |
mov rdi,rdx |
| 123 |
mul qword [x_ptr+16] |
| 124 |
add rdi,rax |
| 125 |
mov rbp,dword 0 |
| 126 |
mov rax,[x_ptr+8] |
| 127 |
adc rbp,rdx |
| 128 |
mul qword [x_ptr+16] |
| 129 |
xor x_ptr,x_ptr |
| 130 |
add rbp,rax |
| 131 |
adc rdx,dword 0 |
| 132 |
adc rdx,dword 0 |
| 133 |
add rsi,rsi |
| 134 |
adc rdi,rdi |
| 135 |
mov rax,[r_ptr+8] |
| 136 |
adc rbp,rbp |
| 137 |
adc rdx,rdx |
| 138 |
adc x_ptr,dword 0 |
| 139 |
add rsi,rax |
| 140 |
mov rax,[r_ptr+16] |
| 141 |
adc rdi,rax |
| 142 |
mov rax,[r_ptr+24] |
| 143 |
mov [r_ptr+8],rsi |
| 144 |
adc rbp,rax |
| 145 |
mov rax,[r_ptr+32] |
| 146 |
mov [r_ptr+16],rdi |
| 147 |
adc rdx,rax |
| 148 |
mov rax,[r_ptr+40] |
| 149 |
mov [r_ptr+24],rbp |
| 150 |
adc rax,x_ptr |
| 151 |
mov [r_ptr+32],rdx |
| 152 |
mov [r_ptr+40],rax |
| 153 |
f_pop x86_regs |
| 154 |
ret |
| 155 |
|
| 156 |
sqr_4_plus: |
| 157 |
f_push reg_list_2 |
| 158 |
mov rcx,x_len |
| 159 |
lea rdi,[r_ptr+rcx*8] |
| 160 |
lea rsi,[x_ptr+rcx*8] |
| 161 |
mov rbp,[x_ptr] |
| 162 |
mov rbx,dword 0 |
| 163 |
dec rcx |
| 164 |
neg rcx |
| 165 |
.0: mov rax,[rsi+rcx*8] |
| 166 |
mul rbp |
| 167 |
add rax,rbx |
| 168 |
mov [rdi+rcx*8],rax |
| 169 |
mov rbx,dword 0 |
| 170 |
adc rbx,rdx |
| 171 |
inc rcx |
| 172 |
jnz .0 |
| 173 |
mov rcx,x_len |
| 174 |
mov [rdi],rbx |
| 175 |
sub rcx,4 |
| 176 |
jz L_corner |
| 177 |
neg rcx |
| 178 |
%if off != 0 |
| 179 |
sub rdi,off |
| 180 |
sub rsi,off |
| 181 |
%endif |
| 182 |
mov rdx,rcx |
| 183 |
shl rcx,3 |
| 184 |
lea rcx,[rcx+rcx*2] |
| 185 |
%ifdef PIC |
| 186 |
lea v_jmp,[.3 wrt rip] |
| 187 |
lea rcx,[rcx+rdx+(UNROLL_COUNT - 2) * CODE_BYTES_PER_LIMB] |
| 188 |
lea rcx,[rcx+v_jmp] |
| 189 |
%else |
| 190 |
lea rcx,[rcx+rdx+(UNROLL_COUNT - 2) * CODE_BYTES_PER_LIMB+.3] |
| 191 |
%endif |
| 192 |
.2: lea v_jmp,[rcx+CODE_BYTES_PER_LIMB] |
| 193 |
mov rbp,[rsi+rdx*8-24+off] |
| 194 |
mov rax,[rsi+rdx*8-16+off] |
| 195 |
mov v_ctr,rdx |
| 196 |
mul rbp |
| 197 |
test cl,1 |
| 198 |
mov rbx,rdx |
| 199 |
mov rcx,rax |
| 200 |
%if (UNROLL_COUNT % 2) |
| 201 |
cmovnz rbx,rax |
| 202 |
cmovnz rcx,rdx |
| 203 |
%else |
| 204 |
cmovz rbx,rax |
| 205 |
cmovz rcx,rdx |
| 206 |
%endif |
| 207 |
xor rdx,rdx |
| 208 |
lea rdi,[rdi+8] |
| 209 |
jmp v_jmp |
| 210 |
|
| 211 |
align 2 |
| 212 |
.3: |
| 213 |
%assign i UNROLL_COUNT |
| 214 |
%rep UNROLL_COUNT |
| 215 |
|
| 216 |
%define disp_src off - 8 * i |
| 217 |
|
| 218 |
%if disp_src < -120 || disp_src >= 128 |
| 219 |
%error source dispacement too large |
| 220 |
%endif |
| 221 |
|
| 222 |
%if (i % 2) = 0 ; 25 bytes of code per limb |
| 223 |
nop |
| 224 |
mov rax,[byte rsi + disp_src] |
| 225 |
adc rbx,rdx |
| 226 |
mul rbp |
| 227 |
add [byte rdi + disp_src - 8],rcx |
| 228 |
mov rcx,dword 0 |
| 229 |
adc rbx,rax |
| 230 |
%else |
| 231 |
nop |
| 232 |
mov rax,[byte rsi + disp_src] |
| 233 |
adc rcx,rdx |
| 234 |
mul rbp |
| 235 |
add [byte rdi + disp_src - 8],rbx |
| 236 |
%if i != 1 |
| 237 |
mov rbx,dword 0 |
| 238 |
%endif |
| 239 |
adc rcx,rax |
| 240 |
%endif |
| 241 |
%assign i i - 1 |
| 242 |
%endrep |
| 243 |
|
| 244 |
adc rdx,dword 0 |
| 245 |
add [rdi-8+off],rcx |
| 246 |
mov rcx,v_jmp |
| 247 |
adc rdx,dword 0 |
| 248 |
mov [rdi+off],rdx |
| 249 |
mov rdx,v_ctr |
| 250 |
inc rdx |
| 251 |
jnz .2 |
| 252 |
|
| 253 |
%if off != 0 |
| 254 |
add rsi,off |
| 255 |
add rdi,off |
| 256 |
%endif |
| 257 |
|
| 258 |
L_corner: |
| 259 |
mov rbp,[rsi-24] |
| 260 |
mov rax,[rsi-16] |
| 261 |
mov rcx,rax |
| 262 |
mul rbp |
| 263 |
add [rdi-8],rax |
| 264 |
mov rax,[rsi-8] |
| 265 |
adc rdx,dword 0 |
| 266 |
mov rbx,rdx |
| 267 |
mov rsi,rax |
| 268 |
mul rbp |
| 269 |
add rax,rbx |
| 270 |
adc rdx,dword 0 |
| 271 |
add [rdi],rax |
| 272 |
mov rax,rsi |
| 273 |
adc rdx,dword 0 |
| 274 |
mov rbx,rdx |
| 275 |
mul rcx |
| 276 |
add rax,rbx |
| 277 |
mov [rdi+8],rax |
| 278 |
adc rdx,dword 0 |
| 279 |
mov [rdi+16],rdx |
| 280 |
mov rax,x_len ; start of shift |
| 281 |
mov rdi,r_ptr |
| 282 |
xor rcx,rcx |
| 283 |
lea r11,[rax+rax] |
| 284 |
lea rdi,[rdi+r11*8] |
| 285 |
not rax |
| 286 |
lea rax,[rax+2] |
| 287 |
.0: lea r11,[rax+rax] |
| 288 |
rcl qword [rdi+r11*8-8],1 |
| 289 |
rcl qword [rdi+r11*8],1 |
| 290 |
inc rax |
| 291 |
jnz .0 |
| 292 |
setc al |
| 293 |
mov rsi,x_ptr |
| 294 |
mov [rdi-8],rax |
| 295 |
mov rcx,x_len |
| 296 |
mov rax,[rsi] |
| 297 |
mul rax |
| 298 |
lea rsi,[rsi+rcx*8] |
| 299 |
neg rcx |
| 300 |
lea r11,[rcx+rcx] |
| 301 |
mov [rdi+r11*8],rax |
| 302 |
inc rcx |
| 303 |
.1: lea r11,[rcx+rcx] |
| 304 |
mov rax,[rsi+rcx*8] |
| 305 |
mov rbx,rdx |
| 306 |
mul rax |
| 307 |
add [rdi+r11*8-8],rbx |
| 308 |
adc [rdi+r11*8],rax |
| 309 |
adc rdx,dword 0 |
| 310 |
inc rcx |
| 311 |
jnz .1 |
| 312 |
add [rdi-8],rdx |
| 313 |
f_pop reg_list_2 |
| 314 |
ret |
| 315 |
|
| 316 |
end |