From: jsing Date: Thu, 16 Feb 2023 11:13:05 +0000 (+0000) Subject: Enable s2n-bignum word_clz() on amd64. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=e9b52428a6f6b4f4b066defb2833e0a68b2f5f44;p=openbsd Enable s2n-bignum word_clz() on amd64. The BN_num_bits_word() function is a hot path, being called more than 80 million times during a libcrypto regress run. The word_clz() implementation uses five instructions to do the same as the generic code that uses more than 60 instructions. Discussed with tb@ --- diff --git a/lib/libcrypto/arch/amd64/Makefile.inc b/lib/libcrypto/arch/amd64/Makefile.inc index 5e433b572d4..e9c77326913 100644 --- a/lib/libcrypto/arch/amd64/Makefile.inc +++ b/lib/libcrypto/arch/amd64/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.11 2023/01/29 14:00:41 jsing Exp $ +# $OpenBSD: Makefile.inc,v 1.12 2023/02/16 11:13:05 jsing Exp $ # amd64-specific libcrypto build rules @@ -39,6 +39,7 @@ SRCS += bignum_sqr.S SRCS += bignum_sqr_4_8_alt.S SRCS += bignum_sqr_8_16_alt.S SRCS += bignum_sub.S +SRCS += word_clz.S # camellia SRCS+= cmll_misc.c diff --git a/lib/libcrypto/bn/arch/amd64/bn_arch.c b/lib/libcrypto/bn/arch/amd64/bn_arch.c index be2badc8a8a..a4a2d93ada6 100644 --- a/lib/libcrypto/bn/arch/amd64/bn_arch.c +++ b/lib/libcrypto/bn/arch/amd64/bn_arch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_arch.c,v 1.4 2023/02/04 14:00:18 jsing Exp $ */ +/* $OpenBSD: bn_arch.c,v 1.5 2023/02/16 11:13:05 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -118,3 +118,11 @@ bn_sqr_comba8(BN_ULONG *rd, const BN_ULONG *ad) bignum_sqr_8_16_alt((uint64_t *)rd, (uint64_t *)ad); } #endif + +#ifdef HAVE_BN_WORD_CLZ +int +bn_word_clz(BN_ULONG w) +{ + return word_clz(w); +} +#endif diff --git a/lib/libcrypto/bn/arch/amd64/bn_arch.h b/lib/libcrypto/bn/arch/amd64/bn_arch.h index 80f73bf15f6..f3653bcc402 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.12 2023/02/16 10:41:03 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.13 2023/02/16 11:13:05 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -39,6 +39,8 @@ #define HAVE_BN_SUB #define HAVE_BN_SUB_WORDS +#define HAVE_BN_WORD_CLZ + #if defined(__GNUC__) #define HAVE_BN_DIV_REM_WORDS_INLINE diff --git a/lib/libcrypto/bn/bn_local.h b/lib/libcrypto/bn/bn_local.h index 1830264fa25..51582f98330 100644 --- a/lib/libcrypto/bn/bn_local.h +++ b/lib/libcrypto/bn/bn_local.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_local.h,v 1.9 2023/02/14 18:45:39 jsing Exp $ */ +/* $OpenBSD: bn_local.h,v 1.10 2023/02/16 11:13:05 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -515,6 +515,8 @@ BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); +int bn_word_clz(BN_ULONG w); + void bn_correct_top(BIGNUM *a); int bn_expand(BIGNUM *a, int bits); int bn_wexpand(BIGNUM *a, int words);