From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E47773655F3 for ; Tue, 12 May 2026 14:10:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778595024; cv=none; b=nwZtFPqJSCsVXZY6kIo8WYt9GE/ZzgznX9u2xC0Mo3JskwKaj+E/OCXeMhtwxyQ+FfrR3IqV7zlo8C59DHWvxUfWqyliE28X7CT4GzyupJ/pXDNbmwjs8jWAIXAHG6mING/FoLXaejFdu8oakkEUYubZ9aTVNRPY9hPqMw0bOfo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778595024; c=relaxed/simple; bh=f77PhhpAUw6LYc2TchI4TzFjE6ULOCkwhW78MQpCuYg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=mOVFF0pVKFbANff6VI3+FzvEYqQL8HU3z3e3YGJqiOEYMQ/jaF8vvp8ZCV29XETX9s3jdTltBPk2omzQnlsshjwVu/TmyPmdXIfVW+anxxDvIgC7TqD2zL2K+IxeVuK2Xi94EoRWpIijG6r0ie+Oicu3UsamtCDtljPJbNxRAro= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Z3rM+BGv; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z3rM+BGv" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-488b0046078so46864535e9.1 for ; Tue, 12 May 2026 07:10:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778595021; x=1779199821; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZZM+fzCNYkJrcKy3N4OsZwqA734P6megTGbF20hl8lY=; b=Z3rM+BGv3T35a3Qdh3+uX46SKoCNNgE/1WUd3E2pzZdtfzY892IrYP7f442Wj/kFyU 5dy7bT+sQ4mA5FFIsJpUvTXfCh+Jx3GcJfo+mV554mVcGSUufeZw6V/4GZfUKN0X0Vwf hJb5Uoh2oMlnszmI8iGyC2pJTZ5tgrrLGf4kdCw84botxhF/NNGQ0HlrZYzfTxIYt0ID +V2npH04GlUy6UnhY6HJjja6b5X/3KqI56vga/Zx59gwDqy42KPnNsg/wxgikMDP0e7T HQL25aiRhbmTUc0TX50hW9sqJwBxxtMX7g0hTmpymzE79UXlj1ajmDUSed2kMcKlmfm6 nV7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778595021; x=1779199821; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ZZM+fzCNYkJrcKy3N4OsZwqA734P6megTGbF20hl8lY=; b=UvVStinQKKYY0f20v/RbeozhVL5z33eWEEbyXUFG8rTeOgvh2ptdPXkcfrr5l8BZIG muirk4AKHMqvgUp/I3Xu/4/qtG4T8h0s3C2qx/L6wrv2iAcyG37n3rfhW1ULSWWIc8oD bwI+SkdEij3djeYB6z/PJHsUKle67tVRTZmxhd9tMFkxgA0JoZWOtoibOp4cuqgpezAJ WiVj3mel1kwT1Wtlt8yNlOhCPGpYB/3WuX+KKSNfvjLxaZrtcRrBFAeUJRNmNgw8Aq2B FXei91PilJ7g0XOpQQmFyy62toUcB91PfwEtsH9QMbBaVSM7pW7fFVqlhzQJNXFHAPlK mDPQ== X-Forwarded-Encrypted: i=1; AFNElJ+hdMM2TGa0gHlW78qZITvN5wgEj+HtuaUE6VAcyDwWrultzo1PsEPHtTGn2Ma51CQ0uRleJOYjwcgBk5s=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3UoQugIUM1hKrAqwaJxHkYIxHqn3qcblwJ2VcHgwb//PXHk8c iuUTYO1+ClIkb/aDfpA/twzBdV0/0JuBJ07c/TDAby6ZsRhRqXwCX7FT X-Gm-Gg: Acq92OHlt0rdrIybX3k0wb656KFwkY1vCxUQ/8wtIsMPOI8IiqKytheOluVZOF2Yt4L /8fuxGmQA1D/MRemD1dKtRanMst0/pmR9Q322H4441RV9rSxYTdJmdLsXFzfRl5hrrUK1vU+q4Y AVPiFK67JDAauhe+By4gKldhRU3vvtCghX22TSs9Daf1iA/qfbCY+uHvpunmGq/Kl6upVfliC1O xwkJNVF2DvMPCJjgk7jFBqsWbowkHP79xnAiDF1ASQQXqs0s2b2N03mMbUFtXXFxF5crMGEBkII kGPUha0wE3hjyBHiR2aAi99i6cmeHLOO1Xm/Il2i24zG+c4QUnrzX71GnLYI9HYwAnnLjo1NuTi oc8OOzWGtN2jxKPYe8pm9H+CKw/3dSigBKPmqXB6T+GMjLVJ5ug6hc7j0XGyul0hkkMPvBu0qLZ AnflGtqqarO95Zg3wH0VbZe6wHmZbyPEbXin4ZMnpoeg== X-Received: by 2002:a05:600c:8706:b0:48a:8905:a500 with SMTP id 5b1f17b1804b1-48e70696dfdmr245353365e9.12.1778595019740; Tue, 12 May 2026 07:10:19 -0700 (PDT) Received: from RTRKW671-LIN.domain.local ([77.243.27.125]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e8e568b04sm40557965e9.0.2026.05.12.07.10.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 May 2026 07:10:19 -0700 (PDT) From: Milan Tripkovic To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Alexandre Ghiti , Dusan Stojkovic , Milan Tripkovic , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Milan Tripkovic Subject: [PATCH 1/2] riscv: lib: add memcmp() implementation Date: Tue, 12 May 2026 16:10:06 +0200 Message-ID: <20260512141007.1193033-1-milant2002@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Milan Tripkovic Add an assembly implementation of memcmp() for RISC-V. The implementation uses the ZBB extension for word-at-a-time comparison and an assembly fallback for non-ZBB systems. Benchmark results (QEMU TCG, rv64): Len | Def | NoZBB | ZBB | %NoZBB | %ZBB -----|-------|-------|-------|--------|------- 1 B | 22.4 | 24.6 | 23.2 | +9.8% | +3.5% 7 B | 96.9 | 108.5 | 107.3 | +12.0% | +10.7% 8 B | 107.0 | 116.3 | 176.7 | +8.7% | +65.1% 16 B | 148.4 | 172.8 | 315.6 | +16.4% | +112.6% 31 B | 182.2 | 217.1 | 377.6 | +19.2% | +107.2% 64 B | 220.6 | 239.4 | 874.2 | +8.5% | +296.2% 127 B| 213.7 | 254.8 | 1042.9| +19.2% | +388.0% 512 B| 255.1 | 269.0 | 1778.6| +5.4% | +597.2% 1024B| 252.3 | 280.9 | 1887.7| +11.3% | +648.1% 3173B| 241.3 | 288.7 | 2063.2| +19.6% | +755.0% 4096B| 240.9 | 280.5 | 2064.5| +16.4% | +756.9% Signed-off-by: Milan Tripkovic --- arch/riscv/include/asm/string.h | 2 + arch/riscv/lib/Makefile | 1 + arch/riscv/lib/memcmp.S | 103 ++++++++++++++++++++++++++++++++ arch/riscv/purgatory/Makefile | 5 +- 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/lib/memcmp.S diff --git a/arch/riscv/include/asm/string.h b/arch/riscv/include/asm/string.h index 764ffe8f6..5c5299678 100644 --- a/arch/riscv/include/asm/string.h +++ b/arch/riscv/include/asm/string.h @@ -18,6 +18,8 @@ extern asmlinkage void *__memcpy(void *, const void *, size_t); #define __HAVE_ARCH_MEMMOVE extern asmlinkage void *memmove(void *, const void *, size_t); extern asmlinkage void *__memmove(void *, const void *, size_t); +#define __HAVE_ARCH_MEMCMP +extern asmlinkage int memcmp(const void *, const void *, size_t); #if !(defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) #define __HAVE_ARCH_STRCMP diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile index 6f767b2a3..b529e1be1 100644 --- a/arch/riscv/lib/Makefile +++ b/arch/riscv/lib/Makefile @@ -3,6 +3,7 @@ lib-y += delay.o lib-y += memcpy.o lib-y += memset.o lib-y += memmove.o +lib-y += memcmp.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) lib-y += strcmp.o lib-y += strlen.o diff --git a/arch/riscv/lib/memcmp.S b/arch/riscv/lib/memcmp.S new file mode 100644 index 000000000..444b082d9 --- /dev/null +++ b/arch/riscv/lib/memcmp.S @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include +#include +#include + +/* int memcmp(const void *cs, const void *ct, size_t n) */ +SYM_FUNC_START(memcmp) + + __ALTERNATIVE_CFG("nop", "j memcmp_zbb", 0, RISCV_ISA_EXT_ZBB, + IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZBB)) +/* + * Parameters + * a0 - Pointer to first memory block (cs), also return value + * a1 - Pointer to second memory block (ct) + * a2 - Number of bytes to compare (n), transformed to end pointer (a0 + n) + * + * Returns + * a0 - 0 if equal, positive if cs > ct, negative if cs < ct + * + * Clobbers + * t0, t1 + */ + beqz a2, 2f + add a2, a0, a2 +1: + lbu t0, 0(a0) + lbu t1, 0(a1) + bne t0, t1, 3f + addi a0, a0, 1 + addi a1, a1, 1 + bne a0, a2, 1b +2: + li a0, 0 + ret +3: + sub a0, t0, t1 + ret + + +memcmp_zbb: +.option push +.option arch,+zbb +/* + * Parameters + * a0 - Pointer to first memory block (cs), also return value + * a1 - Pointer to second memory block (ct) + * a2 - Number of bytes to compare (n), decremented during loop + * + * Returns + * a0 - 0 if equal, positive if cs > ct, negative if cs < ct + * + * Clobbers + * t0, t1, t2 + */ + beq a0, a1, 4f + + li t0, SZREG + bltu a2, t0, 5f + +1: + REG_L t1, 0(a0) + REG_L t2, 0(a1) + bne t1, t2, 2f + + addi a0, a0, SZREG + addi a1, a1, SZREG + addi a2, a2, -SZREG + bgeu a2, t0, 1b + +5: + beqz a2, 4f +6: + lbu t1, 0(a0) + lbu t2, 0(a1) + bne t1, t2, 3f + addi a0, a0, 1 + addi a1, a1, 1 + addi a2, a2, -1 + bnez a2, 6b + +4: li a0, 0 + ret +2: +#ifndef CONFIG_CPU_BIG_ENDIAN + rev8 t1, t1 + rev8 t2, t2 +#endif + sltu a0, t2, t1 + sltu t0, t1, t2 + sub a0, a0, t0 + ret + +3: + sub a0, t1, t2 + ret + +.option pop + +SYM_FUNC_END(memcmp) +SYM_FUNC_ALIAS(__pi_memcmp, memcmp) +EXPORT_SYMBOL(memcmp) diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile index b0358a78f..456929971 100644 --- a/arch/riscv/purgatory/Makefile +++ b/arch/riscv/purgatory/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -purgatory-y := purgatory.o sha256.o entry.o string.o ctype.o memcpy.o memset.o +purgatory-y := purgatory.o sha256.o entry.o string.o ctype.o memcpy.o memset.o memcmp.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) purgatory-y += strcmp.o strlen.o strncmp.o strnlen.o strchr.o strrchr.o endif @@ -41,6 +41,9 @@ $(obj)/strchr.o: $(srctree)/arch/riscv/lib/strchr.S FORCE $(obj)/strrchr.o: $(srctree)/arch/riscv/lib/strrchr.S FORCE $(call if_changed_rule,as_o_S) +$(obj)/memcmp.o: $(srctree)/arch/riscv/lib/memcmp.S FORCE + $(call if_changed_rule,as_o_S) + CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY CFLAGS_string.o := -D__DISABLE_EXPORTS CFLAGS_ctype.o := -D__DISABLE_EXPORTS -- 2.43.0