From: jsing Date: Tue, 31 Jan 2023 05:53:49 +0000 (+0000) Subject: Provide inline assembly versions of bn_umul_hilo() for aarch64/amd64/i386. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=0db1114f8fec2dd6ec6703fbb6f35148780ada1a;p=openbsd Provide inline assembly versions of bn_umul_hilo() for aarch64/amd64/i386. ok tb@ --- diff --git a/lib/libcrypto/bn/arch/aarch64/bn_arch.h b/lib/libcrypto/bn/arch/aarch64/bn_arch.h index 136adf0e977..5cf25adc489 100644 --- a/lib/libcrypto/bn/arch/aarch64/bn_arch.h +++ b/lib/libcrypto/bn/arch/aarch64/bn_arch.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_arch.h,v 1.1 2023/01/20 10:04:33 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.2 2023/01/31 05:53:49 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -15,10 +15,30 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include + #ifndef HEADER_BN_ARCH_H #define HEADER_BN_ARCH_H #ifndef OPENSSL_NO_ASM +#if defined(__GNUC__) +#define HAVE_BN_UMUL_HILO + +static inline void +bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l) +{ + BN_ULONG h, l; + + /* Unsigned multiplication using a umulh/mul pair. */ + __asm__ ("umulh %0, %2, %3; mul %1, %2, %3" + : "=r"(h), "=r"(l) + : "r"(a), "r"(b)); + + *out_h = h; + *out_l = l; +} +#endif /* __GNUC__ */ + #endif #endif diff --git a/lib/libcrypto/bn/arch/amd64/bn_arch.h b/lib/libcrypto/bn/arch/amd64/bn_arch.h index 6b7eaf5eee7..9e4b6b94426 100644 --- a/lib/libcrypto/bn/arch/amd64/bn_arch.h +++ b/lib/libcrypto/bn/arch/amd64/bn_arch.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_arch.h,v 1.8 2023/01/28 16:33:34 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.9 2023/01/31 05:53:49 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -61,5 +61,27 @@ bn_div_rem_words_inline(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q, } #endif /* __GNUC__ */ +#if defined(__GNUC__) +#define HAVE_BN_UMUL_HILO + +static inline void +bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l) +{ + BN_ULONG h, l; + + /* + * Unsigned multiplication of %rax, with the double word result being + * stored in %rdx:%rax. + */ + __asm__ ("mulq %3" + : "=d"(h), "=a"(l) + : "a"(a), "rm"(b) + : "cc"); + + *out_h = h; + *out_l = l; +} +#endif /* __GNUC__ */ + #endif #endif diff --git a/lib/libcrypto/bn/arch/i386/bn_arch.h b/lib/libcrypto/bn/arch/i386/bn_arch.h index e2b4957efc2..268c51e41aa 100644 --- a/lib/libcrypto/bn/arch/i386/bn_arch.h +++ b/lib/libcrypto/bn/arch/i386/bn_arch.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_arch.h,v 1.7 2023/01/28 16:33:34 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.8 2023/01/31 05:53:49 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -60,5 +60,27 @@ bn_div_rem_words_inline(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q, } #endif /* __GNUC__ */ +#if defined(__GNUC__) +#define HAVE_BN_UMUL_HILO + +static inline void +bn_umul_hilo(BN_ULONG a, BN_ULONG b, BN_ULONG *out_h, BN_ULONG *out_l) +{ + BN_ULONG h, l; + + /* + * Unsigned multiplication of %eax, with the double word result being + * stored in %edx:%eax. + */ + __asm__ ("mull %3" + : "=d"(h), "=a"(l) + : "a"(a), "rm"(b) + : "cc"); + + *out_h = h; + *out_l = l; +} +#endif /* __GNUC__ */ + #endif #endif