1/*	$OpenBSD: bn_arch.h,v 1.9 2023/02/16 10:41:03 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_WORDS
26
27#define HAVE_BN_DIV_WORDS
28
29#define HAVE_BN_MUL_ADD_WORDS
30#define HAVE_BN_MUL_COMBA4
31#define HAVE_BN_MUL_COMBA8
32#define HAVE_BN_MUL_WORDS
33
34#define HAVE_BN_SQR_COMBA4
35#define HAVE_BN_SQR_COMBA8
36#define HAVE_BN_SQR_WORDS
37
38#define HAVE_BN_SUB_WORDS
39
40#if defined(__GNUC__)
41#define HAVE_BN_DIV_REM_WORDS_INLINE
42
43static inline void
44bn_div_rem_words_inline(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q,
45    BN_ULONG *out_r)
46{
47	BN_ULONG q, r;
48
49	/*
50	 * Unsigned division of %edx:%eax by d with quotient being stored in
51	 * %eax and remainder in %edx.
52	 */
53	__asm__ volatile ("divl %4"
54	    : "=a"(q), "=d"(r)
55	    : "a"(l), "d"(h), "rm"(d)
56	    : "cc");
57
58	*out_q = q;
59	*out_r = r;
60}
61#endif /* __GNUC__ */
62
63#if defined(__GNUC__)
64#define HAVE_BN_MULW
65
66static inline void
67bn_mulw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
68{
69	BN_ULONG r1, r0;
70
71	/*
72	 * Unsigned multiplication of %eax, with the double word result being
73	 * stored in %edx:%eax.
74	 */
75	__asm__ ("mull %3"
76	    : "=d"(r1), "=a"(r0)
77	    : "a"(a), "rm"(b)
78	    : "cc");
79
80	*out_r1 = r1;
81	*out_r0 = r0;
82}
83#endif /* __GNUC__ */
84
85#endif
86#endif
87