1 /* $OpenBSD: bn_arch.h,v 1.14 2024/03/26 06:09:25 jsing Exp $ */
2 /*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <openssl/bn.h>
19
20 #ifndef HEADER_BN_ARCH_H
21 #define HEADER_BN_ARCH_H
22
23 #ifndef OPENSSL_NO_ASM
24
25 #define HAVE_BN_ADD
26 #define HAVE_BN_ADD_WORDS
27
28 #define HAVE_BN_DIV_WORDS
29
30 #define HAVE_BN_MUL_ADD_WORDS
31 #define HAVE_BN_MUL_COMBA4
32 #define HAVE_BN_MUL_COMBA8
33 #define HAVE_BN_MUL_WORDS
34
35 #define HAVE_BN_SQR
36 #define HAVE_BN_SQR_COMBA4
37 #define HAVE_BN_SQR_COMBA8
38
39 #define HAVE_BN_SUB
40 #define HAVE_BN_SUB_WORDS
41
42 #define HAVE_BN_WORD_CLZ
43
44 #if defined(__GNUC__)
45
46 #define HAVE_BN_DIV_REM_WORDS_INLINE
47
48 static inline void
bn_div_rem_words_inline(BN_ULONG h,BN_ULONG l,BN_ULONG d,BN_ULONG * out_q,BN_ULONG * out_r)49 bn_div_rem_words_inline(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q,
50 BN_ULONG *out_r)
51 {
52 BN_ULONG q, r;
53
54 /*
55 * Unsigned division of %rdx:%rax by d with quotient being stored in
56 * %rax and remainder in %rdx.
57 */
58 __asm__ volatile ("divq %4"
59 : "=a"(q), "=d"(r)
60 : "d"(h), "a"(l), "rm"(d)
61 : "cc");
62
63 *out_q = q;
64 *out_r = r;
65 }
66
67 #define HAVE_BN_MULW
68
69 static inline void
bn_mulw(BN_ULONG a,BN_ULONG b,BN_ULONG * out_r1,BN_ULONG * out_r0)70 bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
71 {
72 BN_ULONG r1, r0;
73
74 /*
75 * Unsigned multiplication of %rax, with the double word result being
76 * stored in %rdx:%rax.
77 */
78 __asm__ ("mulq %3"
79 : "=d"(r1), "=a"(r0)
80 : "a"(a), "rm"(b)
81 : "cc");
82
83 *out_r1 = r1;
84 *out_r0 = r0;
85 }
86
87 #define HAVE_BN_SUBW
88
89 static inline void
bn_subw(BN_ULONG a,BN_ULONG b,BN_ULONG * out_borrow,BN_ULONG * out_r0)90 bn_subw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_borrow, BN_ULONG *out_r0)
91 {
92 BN_ULONG borrow, r0;
93
94 __asm__ (
95 "subq %3, %1 \n"
96 "setb %b0 \n"
97 "and $1, %0 \n"
98 : "=r"(borrow), "=r"(r0)
99 : "1"(a), "rm"(b)
100 : "cc");
101
102 *out_borrow = borrow;
103 *out_r0 = r0;
104 }
105
106 #endif /* __GNUC__ */
107
108 #endif
109 #endif
110