[git commit] libbb/sha: do not read shaNI variable twice, and factor out its setting

Denys Vlasenko vda.linux at googlemail.com
Wed Mar 29 13:46:32 UTC 2023


commit: https://git.busybox.net/busybox/commit/?id=a59e5a7d08cb26ec029d00dd11e693765aca17be
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

My gcc inlines both calls, so instead of "-20 bytes" I get only this:

function                                             old     new   delta
sha256_begin                                          84      83      -1
sha1_begin                                           114     111      -3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-4)               Total: -4 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 libbb/hash_md5_sha.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c
index bbe58c77b..88baf51dc 100644
--- a/libbb/hash_md5_sha.c
+++ b/libbb/hash_md5_sha.c
@@ -23,6 +23,14 @@ static void cpuid(unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *edx)
 	);
 }
 static smallint shaNI;
+static int get_shaNI(void)
+{
+	unsigned eax = 7, ebx = ebx, ecx = 0, edx = edx;
+	cpuid(&eax, &ebx, &ecx, &edx);
+	ebx = ((ebx >> 28) & 2) - 1; /* bit 29 -> 1 or -1 */
+	shaNI = (int)ebx;
+	return (int)ebx;
+}
 void FAST_FUNC sha1_process_block64_shaNI(sha1_ctx_t *ctx);
 void FAST_FUNC sha256_process_block64_shaNI(sha256_ctx_t *ctx);
 #  if defined(__i386__)
@@ -1175,12 +1183,10 @@ void FAST_FUNC sha1_begin(sha1_ctx_t *ctx)
 #if ENABLE_SHA1_HWACCEL
 # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
 	{
-		if (!shaNI) {
-			unsigned eax = 7, ebx = ebx, ecx = 0, edx = edx;
-			cpuid(&eax, &ebx, &ecx, &edx);
-			shaNI = ((ebx >> 28) & 2) - 1; /* bit 29 -> 1 or -1 */
-		}
-		if (shaNI > 0)
+		int ni = shaNI;
+		if (!ni)
+			ni = get_shaNI();
+		if (ni > 0)
 			ctx->process_block = sha1_process_block64_shaNI;
 	}
 # endif
@@ -1229,12 +1235,10 @@ void FAST_FUNC sha256_begin(sha256_ctx_t *ctx)
 #if ENABLE_SHA256_HWACCEL
 # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
 	{
-		if (!shaNI) {
-			unsigned eax = 7, ebx = ebx, ecx = 0, edx = edx;
-			cpuid(&eax, &ebx, &ecx, &edx);
-			shaNI = ((ebx >> 28) & 2) - 1; /* bit 29 -> 1 or -1 */
-		}
-		if (shaNI > 0)
+		int ni = shaNI;
+		if (!ni)
+			ni = get_shaNI();
+		if (ni > 0)
 			ctx->process_block = sha256_process_block64_shaNI;
 	}
 # endif


More information about the busybox-cvs mailing list