| 1 |
/* AMD64 (x86_64) sub_n -- Subtract two limb vectors of the same length > 0 and store |
| 2 |
* sum in a third limb vector. |
| 3 |
* |
| 4 |
* Copyright (C) 1992, 1994, 1995, 1998, |
| 5 |
* 2001, 2002, 2006 Free Software Foundation, Inc. |
| 6 |
* Copyright (C) 2023 Jussi Kivilinna <[email protected]> |
| 7 |
* |
| 8 |
* This file is part of Libgcrypt. |
| 9 |
* |
| 10 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU Lesser General Public License as |
| 12 |
* published by the Free Software Foundation; either version 2.1 of |
| 13 |
* the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public |
| 21 |
* License along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 22 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 23 |
* |
| 24 |
* Note: This code is heavily based on the GNU MP Library. |
| 25 |
* Actually it's the same code with only minor changes in the |
| 26 |
* way the data is stored; this is to support the abstraction |
| 27 |
* of an optional secure memory allocation which may be used |
| 28 |
* to avoid revealing of sensitive data due to paging etc. |
| 29 |
*/ |
| 30 |
|
| 31 |
|
| 32 |
#include "sysdep.h" |
| 33 |
#include "asm-syntax.h" |
| 34 |
|
| 35 |
|
| 36 |
/******************* |
| 37 |
* mpi_limb_t |
| 38 |
* _gcry_mpih_sub_n( mpi_ptr_t res_ptr, rdi |
| 39 |
* mpi_ptr_t s1_ptr, rsi |
| 40 |
* mpi_ptr_t s2_ptr, rdx |
| 41 |
* mpi_size_t size) rcx |
| 42 |
*/ |
| 43 |
TEXT |
| 44 |
ALIGN(4) |
| 45 |
.globl C_SYMBOL_NAME(_gcry_mpih_sub_n) |
| 46 |
C_SYMBOL_NAME(_gcry_mpih_sub_n:) |
| 47 |
FUNC_ENTRY() |
| 48 |
movl %ecx, %r9d |
| 49 |
andl $3, %r9d |
| 50 |
je .Lprehandle0 |
| 51 |
cmpl $2, %r9d |
| 52 |
jb .Lprehandle1 |
| 53 |
je .Lprehandle2 |
| 54 |
|
| 55 |
#define FIRST_SUB() \ |
| 56 |
movq (%rsi), %rax; \ |
| 57 |
subq (%rdx), %rax; \ |
| 58 |
movq %rax, (%rdi) |
| 59 |
|
| 60 |
#define NEXT_SUB(offset) \ |
| 61 |
movq offset(%rsi), %rax; \ |
| 62 |
sbbq offset(%rdx), %rax; \ |
| 63 |
movq %rax, offset(%rdi) |
| 64 |
|
| 65 |
.Lprehandle3: |
| 66 |
leaq -2(%rcx), %rcx |
| 67 |
FIRST_SUB(); |
| 68 |
NEXT_SUB(8); |
| 69 |
NEXT_SUB(16); |
| 70 |
decq %rcx |
| 71 |
je .Lend |
| 72 |
leaq 24(%rsi), %rsi |
| 73 |
leaq 24(%rdx), %rdx |
| 74 |
leaq 24(%rdi), %rdi |
| 75 |
jmp .Loop |
| 76 |
|
| 77 |
ALIGN(3) |
| 78 |
.Lprehandle2: |
| 79 |
leaq -1(%rcx), %rcx |
| 80 |
FIRST_SUB(); |
| 81 |
NEXT_SUB(8); |
| 82 |
decq %rcx |
| 83 |
je .Lend |
| 84 |
leaq 16(%rsi), %rsi |
| 85 |
leaq 16(%rdx), %rdx |
| 86 |
leaq 16(%rdi), %rdi |
| 87 |
jmp .Loop |
| 88 |
|
| 89 |
ALIGN(3) |
| 90 |
.Lprehandle1: |
| 91 |
FIRST_SUB(); |
| 92 |
decq %rcx |
| 93 |
je .Lend |
| 94 |
leaq 8(%rsi), %rsi |
| 95 |
leaq 8(%rdx), %rdx |
| 96 |
leaq 8(%rdi), %rdi |
| 97 |
jmp .Loop |
| 98 |
|
| 99 |
ALIGN(3) |
| 100 |
.Lprehandle0: |
| 101 |
clc /* clear cy */ |
| 102 |
|
| 103 |
ALIGN(4) /* minimal alignment for claimed speed */ |
| 104 |
.Loop: leaq -3(%rcx), %rcx |
| 105 |
NEXT_SUB(0); |
| 106 |
NEXT_SUB(8); |
| 107 |
NEXT_SUB(16); |
| 108 |
NEXT_SUB(24); |
| 109 |
leaq 32(%rsi), %rsi |
| 110 |
leaq 32(%rdx), %rdx |
| 111 |
leaq 32(%rdi), %rdi |
| 112 |
decq %rcx |
| 113 |
jne .Loop |
| 114 |
|
| 115 |
ALIGN(2) |
| 116 |
.Lend: |
| 117 |
movl $0, %eax /* zero %rax */ |
| 118 |
adcl %eax, %eax |
| 119 |
FUNC_EXIT() |