xref: /openbsd-src/lib/libcrypto/bn/arch/amd64/bignum_mul_4_8_alt.S (revision 22787c513b4b59ee1fb13a32326a50f73cd342c1)
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15// ----------------------------------------------------------------------------
16// Multiply z := x * y
17// Inputs x[4], y[4]; output z[8]
18//
19//    extern void bignum_mul_4_8_alt
20//      (uint64_t z[static 8], uint64_t x[static 4], uint64_t y[static 4]);
21//
22// Standard x86-64 ABI: RDI = z, RSI = x, RDX = y
23// Microsoft x64 ABI:   RCX = z, RDX = x, R8 = y
24// ----------------------------------------------------------------------------
25
26#include "s2n_bignum_internal.h"
27
28        .intel_syntax noprefix
29        S2N_BN_SYM_VISIBILITY_DIRECTIVE(bignum_mul_4_8_alt)
30        S2N_BN_SYM_PRIVACY_DIRECTIVE(bignum_mul_4_8_alt)
31        .text
32
33// These are actually right
34
35#define z rdi
36#define x rsi
37
38// This is moved from rdx to free it for muls
39
40#define y rcx
41
42// Other variables used as a rotating 3-word window to add terms to
43
44#define t0 r8
45#define t1 r9
46#define t2 r10
47
48// Macro for the key "multiply and add to (c,h,l)" step
49
50#define combadd(c,h,l,numa,numb)                \
51        mov     rax, numa;                      \
52        mul     QWORD PTR numb;                 \
53        add     l, rax;                         \
54        adc     h, rdx;                         \
55        adc     c, 0
56
57// A minutely shorter form for when c = 0 initially
58
59#define combadz(c,h,l,numa,numb)                \
60        mov     rax, numa;                      \
61        mul     QWORD PTR numb;                 \
62        add     l, rax;                         \
63        adc     h, rdx;                         \
64        adc     c, c
65
66// A short form where we don't expect a top carry
67
68#define combads(h,l,numa,numb)                  \
69        mov     rax, numa;                      \
70        mul     QWORD PTR numb;                 \
71        add     l, rax;                         \
72        adc     h, rdx
73
74S2N_BN_SYMBOL(bignum_mul_4_8_alt):
75	_CET_ENDBR
76
77#if WINDOWS_ABI
78        push    rdi
79        push    rsi
80        mov     rdi, rcx
81        mov     rsi, rdx
82        mov     rdx, r8
83#endif
84
85// Copy y into a safe register to start with
86
87        mov     y, rdx
88
89// Result term 0
90
91        mov     rax, [x]
92        mul     QWORD PTR [y]
93
94        mov     [z], rax
95        mov     t0, rdx
96        xor     t1, t1
97
98// Result term 1
99
100        xor     t2, t2
101        combads(t1,t0,[x],[y+8])
102        combadz(t2,t1,t0,[x+8],[y])
103        mov     [z+8], t0
104
105// Result term 2
106
107        xor     t0, t0
108        combadz(t0,t2,t1,[x],[y+16])
109        combadd(t0,t2,t1,[x+8],[y+8])
110        combadd(t0,t2,t1,[x+16],[y])
111        mov     [z+16], t1
112
113// Result term 3
114
115        xor     t1, t1
116        combadz(t1,t0,t2,[x],[y+24])
117        combadd(t1,t0,t2,[x+8],[y+16])
118        combadd(t1,t0,t2,[x+16],[y+8])
119        combadd(t1,t0,t2,[x+24],[y])
120        mov     [z+24], t2
121
122// Result term 4
123
124        xor     t2, t2
125        combadz(t2,t1,t0,[x+8],[y+24])
126        combadd(t2,t1,t0,[x+16],[y+16])
127        combadd(t2,t1,t0,[x+24],[y+8])
128        mov     [z+32], t0
129
130// Result term 5
131
132        xor     t0, t0
133        combadz(t0,t2,t1,[x+16],[y+24])
134        combadd(t0,t2,t1,[x+24],[y+16])
135        mov     [z+40], t1
136
137// Result term 6
138
139        xor     t1, t1
140        combads(t0,t2,[x+24],[y+24])
141        mov     [z+48], t2
142
143// Result term 7
144
145        mov     [z+56], t0
146
147// Return
148
149#if WINDOWS_ABI
150        pop    rsi
151        pop    rdi
152#endif
153        ret
154
155#if defined(__linux__) && defined(__ELF__)
156.section .note.GNU-stack,"",%progbits
157#endif
158