| 1 |
; |
| 2 |
; This file is a part of TTMath Bignum Library |
| 3 |
; and is distributed under the (new) BSD licence. |
| 4 |
; Author: Christian Kaiser <[email protected]> |
| 5 |
; |
| 6 |
|
| 7 |
; |
| 8 |
; Copyright (c) 2009, Christian Kaiser |
| 9 |
; All rights reserved. |
| 10 |
; |
| 11 |
; Redistribution and use in source and binary forms, with or without |
| 12 |
; modification, are permitted provided that the following conditions are met: |
| 13 |
; |
| 14 |
; * Redistributions of source code must retain the above copyright notice, |
| 15 |
; this list of conditions and the following disclaimer. |
| 16 |
; |
| 17 |
; * Redistributions in binary form must reproduce the above copyright |
| 18 |
; notice, this list of conditions and the following disclaimer in the |
| 19 |
; documentation and/or other materials provided with the distribution. |
| 20 |
; |
| 21 |
; * Neither the name Christian Kaiser nor the names of contributors to this |
| 22 |
; project may be used to endorse or promote products derived |
| 23 |
; from this software without specific prior written permission. |
| 24 |
; |
| 25 |
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 |
; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 |
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 |
; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 29 |
; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 30 |
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 31 |
; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 32 |
; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 33 |
; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 34 |
; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 35 |
; THE POSSIBILITY OF SUCH DAMAGE. |
| 36 |
; |
| 37 |
|
| 38 |
; |
| 39 |
; compile with debug info: ml64.exe /c /Zd /Zi ttmathuint_x86_64_msvc.asm |
| 40 |
; compile without debug info: ml64.exe /c ttmathuint_x86_64_msvc.asm |
| 41 |
; this creates ttmathuint_x86_64_msvc.obj file which can be linked with your program |
| 42 |
; |
| 43 |
|
| 44 |
PUBLIC ttmath_adc_x64 |
| 45 |
PUBLIC ttmath_addindexed_x64 |
| 46 |
PUBLIC ttmath_addindexed2_x64 |
| 47 |
PUBLIC ttmath_addvector_x64 |
| 48 |
|
| 49 |
PUBLIC ttmath_sbb_x64 |
| 50 |
PUBLIC ttmath_subindexed_x64 |
| 51 |
PUBLIC ttmath_subvector_x64 |
| 52 |
|
| 53 |
PUBLIC ttmath_rcl_x64 |
| 54 |
PUBLIC ttmath_rcr_x64 |
| 55 |
|
| 56 |
PUBLIC ttmath_rcl2_x64 |
| 57 |
PUBLIC ttmath_rcr2_x64 |
| 58 |
|
| 59 |
PUBLIC ttmath_div_x64 |
| 60 |
|
| 61 |
; |
| 62 |
; Microsoft x86_64 convention: http://msdn.microsoft.com/en-us/library/9b372w95.aspx |
| 63 |
; |
| 64 |
; "rax, rcx, rdx, r8-r11 are volatile." |
| 65 |
; "rbx, rbp, rdi, rsi, r12-r15 are nonvolatile." |
| 66 |
; |
| 67 |
|
| 68 |
|
| 69 |
.CODE |
| 70 |
|
| 71 |
|
| 72 |
ALIGN 8 |
| 73 |
|
| 74 |
;---------------------------------------- |
| 75 |
|
| 76 |
ttmath_adc_x64 PROC |
| 77 |
; rcx = p1 |
| 78 |
; rdx = p2 |
| 79 |
; r8 = nSize |
| 80 |
; r9 = nCarry |
| 81 |
|
| 82 |
xor rax, rax |
| 83 |
xor r11, r11 |
| 84 |
sub rax, r9 ; sets CARRY if r9 != 0 |
| 85 |
|
| 86 |
ALIGN 16 |
| 87 |
loop1: |
| 88 |
mov rax,qword ptr [rdx + r11 * 8] |
| 89 |
adc qword ptr [rcx + r11 * 8], rax |
| 90 |
lea r11, [r11+1] |
| 91 |
dec r8 |
| 92 |
jnz loop1 |
| 93 |
|
| 94 |
setc al |
| 95 |
movzx rax, al |
| 96 |
|
| 97 |
ret |
| 98 |
|
| 99 |
ttmath_adc_x64 ENDP |
| 100 |
|
| 101 |
;---------------------------------------- |
| 102 |
|
| 103 |
ALIGN 8 |
| 104 |
|
| 105 |
;---------------------------------------- |
| 106 |
|
| 107 |
ttmath_addindexed_x64 PROC |
| 108 |
|
| 109 |
; rcx = p1 |
| 110 |
; rdx = nSize |
| 111 |
; r8 = nPos |
| 112 |
; r9 = nValue |
| 113 |
|
| 114 |
xor rax, rax ; rax = result |
| 115 |
sub rdx, r8 ; rdx = remaining count of uints |
| 116 |
|
| 117 |
add qword ptr [rcx + r8 * 8], r9 |
| 118 |
jc next1 |
| 119 |
|
| 120 |
ret |
| 121 |
|
| 122 |
next1: |
| 123 |
mov r9, 1 |
| 124 |
|
| 125 |
ALIGN 16 |
| 126 |
loop1: |
| 127 |
dec rdx |
| 128 |
jz done_with_cy |
| 129 |
lea r8, [r8+1] |
| 130 |
add qword ptr [rcx + r8 * 8], r9 |
| 131 |
jc loop1 |
| 132 |
|
| 133 |
ret |
| 134 |
|
| 135 |
done_with_cy: |
| 136 |
lea rax, [rax+1] ; rax = 1 |
| 137 |
|
| 138 |
ret |
| 139 |
|
| 140 |
ttmath_addindexed_x64 ENDP |
| 141 |
|
| 142 |
;---------------------------------------- |
| 143 |
|
| 144 |
ALIGN 8 |
| 145 |
|
| 146 |
;---------------------------------------- |
| 147 |
|
| 148 |
ttmath_addindexed2_x64 PROC |
| 149 |
|
| 150 |
; rcx = p1 (pointer) |
| 151 |
; rdx = b (value size) |
| 152 |
; r8 = nPos |
| 153 |
; r9 = nValue1 |
| 154 |
; [rsp+0x28] = nValue2 |
| 155 |
|
| 156 |
xor rax, rax ; return value |
| 157 |
mov r11, rcx ; table |
| 158 |
sub rdx, r8 ; rdx = remaining count of uints |
| 159 |
mov r10, [rsp+028h] ; r10 = nValue2 |
| 160 |
|
| 161 |
add qword ptr [r11 + r8 * 8], r9 |
| 162 |
lea r8, [r8+1] |
| 163 |
lea rdx, [rdx-1] |
| 164 |
adc qword ptr [r11 + r8 * 8], r10 |
| 165 |
jc next |
| 166 |
ret |
| 167 |
|
| 168 |
ALIGN 16 |
| 169 |
loop1: |
| 170 |
lea r8, [r8+1] |
| 171 |
add qword ptr [r11 + r8 * 8], 1 |
| 172 |
jc next |
| 173 |
ret |
| 174 |
|
| 175 |
next: |
| 176 |
dec rdx ; does not modify CY too... |
| 177 |
jnz loop1 |
| 178 |
lea rax, [rax+1] |
| 179 |
ret |
| 180 |
|
| 181 |
ttmath_addindexed2_x64 ENDP |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
;---------------------------------------- |
| 186 |
|
| 187 |
ALIGN 8 |
| 188 |
|
| 189 |
;---------------------------------------- |
| 190 |
|
| 191 |
|
| 192 |
ttmath_addvector_x64 PROC |
| 193 |
; rcx = ss1 |
| 194 |
; rdx = ss2 |
| 195 |
; r8 = ss1_size |
| 196 |
; r9 = ss2_size |
| 197 |
; [rsp+0x28] = result |
| 198 |
|
| 199 |
mov r10, [rsp+028h] |
| 200 |
sub r8, r9 |
| 201 |
xor r11, r11 ; r11=0, cf=0 |
| 202 |
|
| 203 |
ALIGN 16 |
| 204 |
loop1: |
| 205 |
mov rax, qword ptr [rcx + r11 * 8] |
| 206 |
adc rax, qword ptr [rdx + r11 * 8] |
| 207 |
mov qword ptr [r10 + r11 * 8], rax |
| 208 |
inc r11 |
| 209 |
dec r9 |
| 210 |
jnz loop1 |
| 211 |
|
| 212 |
adc r9, r9 ; r9 has the cf state |
| 213 |
|
| 214 |
or r8, r8 |
| 215 |
jz done |
| 216 |
|
| 217 |
neg r9 ; setting cf from r9 |
| 218 |
mov r9, 0 ; don't use xor here (cf is used) |
| 219 |
loop2: |
| 220 |
mov rax, qword ptr [rcx + r11 * 8] |
| 221 |
adc rax, r9 |
| 222 |
mov qword ptr [r10 + r11 * 8], rax |
| 223 |
inc r11 |
| 224 |
dec r8 |
| 225 |
jnz loop2 |
| 226 |
|
| 227 |
adc r8, r8 |
| 228 |
mov rax, r8 |
| 229 |
|
| 230 |
ret |
| 231 |
|
| 232 |
done: |
| 233 |
mov rax, r9 |
| 234 |
ret |
| 235 |
|
| 236 |
ttmath_addvector_x64 ENDP |
| 237 |
|
| 238 |
|
| 239 |
;---------------------------------------- |
| 240 |
|
| 241 |
ALIGN 8 |
| 242 |
|
| 243 |
;---------------------------------------- |
| 244 |
|
| 245 |
ttmath_sbb_x64 PROC |
| 246 |
|
| 247 |
; rcx = p1 |
| 248 |
; rdx = p2 |
| 249 |
; r8 = nCount |
| 250 |
; r9 = nCarry |
| 251 |
|
| 252 |
xor rax, rax |
| 253 |
xor r11, r11 |
| 254 |
sub rax, r9 ; sets CARRY if r9 != 0 |
| 255 |
|
| 256 |
ALIGN 16 |
| 257 |
loop1: |
| 258 |
mov rax,qword ptr [rdx + r11 * 8] |
| 259 |
sbb qword ptr [rcx + r11 * 8], rax |
| 260 |
lea r11, [r11+1] |
| 261 |
dec r8 |
| 262 |
jnz loop1 |
| 263 |
|
| 264 |
setc al |
| 265 |
movzx rax, al |
| 266 |
|
| 267 |
ret |
| 268 |
|
| 269 |
ttmath_sbb_x64 ENDP |
| 270 |
|
| 271 |
;---------------------------------------- |
| 272 |
|
| 273 |
ALIGN 8 |
| 274 |
|
| 275 |
;---------------------------------------- |
| 276 |
|
| 277 |
ttmath_subindexed_x64 PROC |
| 278 |
; rcx = p1 |
| 279 |
; rdx = nSize |
| 280 |
; r8 = nPos |
| 281 |
; r9 = nValue |
| 282 |
|
| 283 |
sub rdx, r8 ; rdx = remaining count of uints |
| 284 |
|
| 285 |
ALIGN 16 |
| 286 |
loop1: |
| 287 |
sub qword ptr [rcx + r8 * 8], r9 |
| 288 |
jnc done |
| 289 |
|
| 290 |
lea r8, [r8+1] |
| 291 |
mov r9, 1 |
| 292 |
dec rdx |
| 293 |
jnz loop1 |
| 294 |
|
| 295 |
mov rax, 1 |
| 296 |
ret |
| 297 |
|
| 298 |
done: |
| 299 |
xor rax, rax |
| 300 |
ret |
| 301 |
|
| 302 |
ttmath_subindexed_x64 ENDP |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
;---------------------------------------- |
| 307 |
|
| 308 |
ALIGN 8 |
| 309 |
|
| 310 |
;---------------------------------------- |
| 311 |
|
| 312 |
; the same asm code as in addvector_x64 only two instructions 'adc' changed to 'sbb' |
| 313 |
|
| 314 |
ttmath_subvector_x64 PROC |
| 315 |
; rcx = ss1 |
| 316 |
; rdx = ss2 |
| 317 |
; r8 = ss1_size |
| 318 |
; r9 = ss2_size |
| 319 |
; [rsp+0x28] = result |
| 320 |
|
| 321 |
mov r10, [rsp+028h] |
| 322 |
sub r8, r9 |
| 323 |
xor r11, r11 ; r11=0, cf=0 |
| 324 |
|
| 325 |
ALIGN 16 |
| 326 |
loop1: |
| 327 |
mov rax, qword ptr [rcx + r11 * 8] |
| 328 |
sbb rax, qword ptr [rdx + r11 * 8] |
| 329 |
mov qword ptr [r10 + r11 * 8], rax |
| 330 |
inc r11 |
| 331 |
dec r9 |
| 332 |
jnz loop1 |
| 333 |
|
| 334 |
adc r9, r9 ; r9 has the cf state |
| 335 |
|
| 336 |
or r8, r8 |
| 337 |
jz done |
| 338 |
|
| 339 |
neg r9 ; setting cf from r9 |
| 340 |
mov r9, 0 ; don't use xor here (cf is used) |
| 341 |
loop2: |
| 342 |
mov rax, qword ptr [rcx + r11 * 8] |
| 343 |
sbb rax, r9 |
| 344 |
mov qword ptr [r10 + r11 * 8], rax |
| 345 |
inc r11 |
| 346 |
dec r8 |
| 347 |
jnz loop2 |
| 348 |
|
| 349 |
adc r8, r8 |
| 350 |
mov rax, r8 |
| 351 |
|
| 352 |
ret |
| 353 |
|
| 354 |
done: |
| 355 |
mov rax, r9 |
| 356 |
ret |
| 357 |
|
| 358 |
ttmath_subvector_x64 ENDP |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
;---------------------------------------- |
| 364 |
|
| 365 |
ALIGN 8 |
| 366 |
|
| 367 |
;---------------------------------------- |
| 368 |
|
| 369 |
ttmath_rcl_x64 PROC |
| 370 |
; rcx = p1 |
| 371 |
; rdx = b |
| 372 |
; r8 = nLowestBit |
| 373 |
|
| 374 |
mov r11, rcx ; table |
| 375 |
xor r10, r10 |
| 376 |
neg r8 ; CY set if r8 <> 0 |
| 377 |
|
| 378 |
ALIGN 16 |
| 379 |
loop1: |
| 380 |
rcl qword ptr [r11 + r10 * 8], 1 |
| 381 |
lea r10, [r10+1] |
| 382 |
dec rdx |
| 383 |
jnz loop1 |
| 384 |
|
| 385 |
setc al |
| 386 |
movzx rax, al |
| 387 |
|
| 388 |
ret |
| 389 |
|
| 390 |
ttmath_rcl_x64 ENDP |
| 391 |
|
| 392 |
;---------------------------------------- |
| 393 |
|
| 394 |
ALIGN 8 |
| 395 |
|
| 396 |
;---------------------------------------- |
| 397 |
|
| 398 |
ttmath_rcr_x64 PROC |
| 399 |
; rcx = p1 |
| 400 |
; rdx = nSize |
| 401 |
; r8 = nLowestBit |
| 402 |
|
| 403 |
xor r10, r10 |
| 404 |
neg r8 ; CY set if r8 <> 0 |
| 405 |
|
| 406 |
ALIGN 16 |
| 407 |
loop1: |
| 408 |
rcr qword ptr -8[rcx + rdx * 8], 1 |
| 409 |
dec rdx |
| 410 |
jnz loop1 |
| 411 |
|
| 412 |
setc al |
| 413 |
movzx rax, al |
| 414 |
|
| 415 |
ret |
| 416 |
|
| 417 |
ttmath_rcr_x64 ENDP |
| 418 |
|
| 419 |
;---------------------------------------- |
| 420 |
|
| 421 |
ALIGN 8 |
| 422 |
|
| 423 |
;---------------------------------------- |
| 424 |
|
| 425 |
ttmath_div_x64 PROC |
| 426 |
|
| 427 |
; rcx = &Hi |
| 428 |
; rdx = &Lo |
| 429 |
; r8 = nDiv |
| 430 |
|
| 431 |
mov r11, rcx |
| 432 |
mov r10, rdx |
| 433 |
|
| 434 |
mov rdx, qword ptr [r11] |
| 435 |
mov rax, qword ptr [r10] |
| 436 |
div r8 |
| 437 |
mov qword ptr [r10], rdx ; remainder |
| 438 |
mov qword ptr [r11], rax ; value |
| 439 |
|
| 440 |
ret |
| 441 |
|
| 442 |
ttmath_div_x64 ENDP |
| 443 |
|
| 444 |
;---------------------------------------- |
| 445 |
|
| 446 |
ALIGN 8 |
| 447 |
|
| 448 |
;---------------------------------------- |
| 449 |
|
| 450 |
ttmath_rcl2_x64 PROC |
| 451 |
; rcx = p1 |
| 452 |
; rdx = nSize |
| 453 |
; r8 = bits |
| 454 |
; r9 = c |
| 455 |
|
| 456 |
push rbx |
| 457 |
|
| 458 |
mov r10, rcx ; r10 = p1 |
| 459 |
xor rax, rax |
| 460 |
|
| 461 |
mov rcx, 64 |
| 462 |
sub rcx, r8 |
| 463 |
|
| 464 |
mov r11, -1 |
| 465 |
shr r11, cl ; r11 = mask |
| 466 |
|
| 467 |
mov rcx, r8 ; rcx = count of bits |
| 468 |
|
| 469 |
mov rbx, rax ; rbx = old value = 0 |
| 470 |
or r9, r9 |
| 471 |
cmovnz rbx, r11 ; if (c) then old value = mask |
| 472 |
|
| 473 |
mov r9, rax ; r9 = index (0..nSize-1) |
| 474 |
|
| 475 |
ALIGN 16 |
| 476 |
loop1: |
| 477 |
rol qword ptr [r10+r9*8], cl |
| 478 |
mov rax, qword ptr [r10+r9*8] |
| 479 |
and rax, r11 |
| 480 |
xor qword ptr [r10+r9*8], rax |
| 481 |
or qword ptr [r10+r9*8], rbx |
| 482 |
mov rbx, rax |
| 483 |
|
| 484 |
lea r9, [r9+1] |
| 485 |
dec rdx |
| 486 |
|
| 487 |
jnz loop1 |
| 488 |
|
| 489 |
and rax, 1 |
| 490 |
pop rbx |
| 491 |
ret |
| 492 |
|
| 493 |
ttmath_rcl2_x64 ENDP |
| 494 |
|
| 495 |
;---------------------------------------- |
| 496 |
|
| 497 |
ALIGN 8 |
| 498 |
|
| 499 |
;---------------------------------------- |
| 500 |
|
| 501 |
ttmath_rcr2_x64 PROC |
| 502 |
; rcx = p1 |
| 503 |
; rdx = nSize |
| 504 |
; r8 = bits |
| 505 |
; r9 = c |
| 506 |
|
| 507 |
push rbx |
| 508 |
mov r10, rcx ; r10 = p1 |
| 509 |
xor rax, rax |
| 510 |
|
| 511 |
mov rcx, 64 |
| 512 |
sub rcx, r8 |
| 513 |
|
| 514 |
mov r11, -1 |
| 515 |
shl r11, cl ; r11 = mask |
| 516 |
|
| 517 |
mov rcx, r8 ; rcx = count of bits |
| 518 |
|
| 519 |
mov rbx, rax ; rbx = old value = 0 |
| 520 |
or r9, r9 |
| 521 |
cmovnz rbx, r11 ; if (c) then old value = mask |
| 522 |
|
| 523 |
mov r9, rdx ; r9 = index (0..nSize-1) |
| 524 |
lea r9, [r9-1] |
| 525 |
|
| 526 |
ALIGN 16 |
| 527 |
loop1: |
| 528 |
ror qword ptr [r10+r9*8], cl |
| 529 |
mov rax, qword ptr [r10+r9*8] |
| 530 |
and rax, r11 |
| 531 |
xor qword ptr [r10+r9*8], rax |
| 532 |
or qword ptr [r10+r9*8], rbx |
| 533 |
mov rbx, rax |
| 534 |
|
| 535 |
lea r9, [r9-1] |
| 536 |
dec rdx |
| 537 |
|
| 538 |
jnz loop1 |
| 539 |
|
| 540 |
rol rax, 1 |
| 541 |
and rax, 1 |
| 542 |
pop rbx |
| 543 |
|
| 544 |
ret |
| 545 |
|
| 546 |
ttmath_rcr2_x64 ENDP |
| 547 |
|
| 548 |
END |