From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 2A7EB41C30E for ; Wed, 21 Jan 2026 10:57:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768993043; cv=none; b=gEzRMFwaAgEH40FtsbiBmvmnJAtz/vv/ufSkmi7HaOJw5m+YO3OPuCBP8zpxlsIk6jduzy1yG5VFncj0s+n5LfJJNfS9RRtT2q2tl4RnmzeHqT/j/5fiWZ0vs2yeRWDGEnoH6U+sFUPXelSa1lYPhjsSfPK1Zb/3OQhCWjN70Fo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768993043; c=relaxed/simple; bh=6qplPAyVow+/kBcikqN/WhOSwSmC06WyjvGEIzOsqts=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=pvW5L9pJBCiDw7QULIJZqngDiFYXyPkXs5oC8b5sWpRRSfWh+aRgFezo+naR7ArpGpQRIYk3vdZWRNEQboA2S2TCFO955x2Ih4G1kIUOcJPHi4w4qAZDCopHC0qX/L2GFIOmAROUwKBq1aGCsrTJC+QJYzuEvRZEdeOZ4W33FIc= 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=k6pWLf5b; arc=none smtp.client-ip=209.85.128.48 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="k6pWLf5b" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-47edd9024b1so43292355e9.3 for ; Wed, 21 Jan 2026 02:57:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768993039; x=1769597839; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=EVe0Qk3VMjV2txWtY8q1b+FCjHEWppjlIBEVCNldeh4=; b=k6pWLf5bxr5676PqQK9Ku/bSHMv4C9x5WbofSmXmMakGD/MMnzPEGf6wLsgcbkvcns bCGGZytiiIhzoNHcIIh+t1WbUdA2qfXaghKJkdlw0plxu9pGWwLHN3he9afpnYnz35XM VXbTzDPMfEIuGJzDthkUsXEFsOW47g/YwLxjOED49nJEbNceJs4C+qHQMkhqQLOQ9SA/ 5PxAYGq40U52CJ1Ll8xf3rZyIzGFl8E2wvTFvINhLeSo2emMkDt6O+v1AhsCT9i/6i1t TgU1DH3kFpc6yoKokJKnv1p+BSFs64uJ6eIMpwGtLA0ff/lKHi6WI0xNwH6dvjAkghRK L5Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768993039; x=1769597839; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=EVe0Qk3VMjV2txWtY8q1b+FCjHEWppjlIBEVCNldeh4=; b=EZvzkhenIRWpUEdi5e7foHlhuht1rkKIFlKtWKwKfk2TfzQo8lWfpeLVHlhpORmwCn Pf90NK7Jsj2bfEXu+NKFH5sBkNrhF6ovraH0WOQZxKOI+Lz4ohQMBOngOi+m5fs+P1zM LoaxUdIwzN1A1JdtmRYiMQ5/+KlZoRZHmpHG9UGE5E/edT9TUkWgAya8EMwVErKYhp1M I1GR3TnUQ72xgRYpSBTeB9tXrFQRMZoR8wB0hQf59w0hXu2frFwhPT4EPYf8gjaeP4nw YIEBwacVVM3kUCkxHOfZzDCWLtcuhaP4nzGi8EhpeD5oLrnQ4jSxH2vIwf1aR/aCgCZt d1pw== X-Forwarded-Encrypted: i=1; AJvYcCUJwqidHMzpcCDPC/opJSyBkvhcnAxq4B85EUXUhCKUS007u/bJxq2F4KMd6tquRU9NV9hZkiqaTIv22rQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxDsAdh0SbAZQCss0KGSYf55Xz8SILsIWuUKNObtqGkiOZwD2WL GWJEKnIvQ3oNYV5KmF3qDX0wqIiaqxG8MDWid8QzGpOcxNyC53SekojM X-Gm-Gg: AZuq6aL9zAT+T2dzjAqVcKZyBiC3yxrZbOt48U0PbCznNa/ImZ0DYdu8s55oTYquN1o cjuXm/hshGo+1OQZnQRDppN2RD2fsKhgHAzSmLnLq2tKTBAdB7hsF0CRMMQH6yJT4WFmylYuMus POf2ykrAk6LiN/HIFeQ+oQaxflUAUMI+FwHcmvE2wsNEjOfTZaZ6Xdr/zMjGWVLYDWUMBL2TCEv +r/oqvB5IIES2kFqCuQ8H75Rf/gSZA72h1+NzPYomQFg3eWExZu18SI3omGI9lwSpQM3JiEvuU+ QnAJUU+8Ez3AD3ifbZMpAtQ9Wd1qH5TS6W7CHv1Bky3Txe7iCuRoedOsTyx7D9RE0HmmUhjUvZm 2/A3s8qxyigEKeUXjQG90pqRP3j88NnMCwTMhFPLcYTW/aGdaoZIkttkT8qTMP6zVd3trtyqoaV R7XEU+0euu0LTUiSHuqRPa9k6wEFwGrHrKH/HxKIy2JUOE7pMQH91hzGRVQpsOkFc= X-Received: by 2002:a05:600c:34d6:b0:47e:e20e:bbb7 with SMTP id 5b1f17b1804b1-4801eb1080amr246838145e9.25.1768993039340; Wed, 21 Jan 2026 02:57:19 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4802dc90068sm259182585e9.7.2026.01.21.02.57.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jan 2026 02:57:19 -0800 (PST) Date: Wed, 21 Jan 2026 10:57:17 +0000 From: David Laight To: Andy Shevchenko Cc: Feng Jiang , Andy Shevchenko , pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org, andy@kernel.org, ebiggers@kernel.org, martin.petersen@oracle.com, ardb@kernel.org, charlie@rivosinc.com, conor.dooley@microchip.com, ajones@ventanamicro.com, linus.walleij@linaro.org, nathan@kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v3 0/8] riscv: optimize string functions and add kunit tests Message-ID: <20260121105717.04853c5d@pumpkin> In-Reply-To: References: <20260120065852.166857-1-jiangfeng@kylinos.cn> <691a0183-6f41-4956-82da-abb69a449919@kylinos.cn> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 21 Jan 2026 09:01:29 +0200 Andy Shevchenko wrote: ... > I understand that. My point is if we move the generic implementation > to use word-at-a-time technique the difference should not go 4x, > right? Perhaps 1.5x or so. I believe this will be a very useful > exercise. I posted a version earlier. After the initial setup (aligning the base address and loading some constants the loop on x86-64 is 7 instructions (should be similar for other architectures). I think it will execute in 4 clocks. You then need to find the byte in the word, easy enough on LE with a fast ffs() - but harder otherwise. The real problem is the cost for short strings. Like memcpy() you need a hint from the source of the 'expected' length (as a compile-time constant) to compile-time select the algorithm. OTOH: for (;;) { if (!ptr[0]) return ptr - start; ptr += 2; while (ptr[-1]); return ptr - start - 1; has two 'load+compare+branch' and one add per loop. On x86 that might all overlap and give you a two-clock loop that checks one byte every clock - faster than 'rep scasb'. (You can get a two clock loop, but not a 1 clock loop.) I think unrolling further will make little/no difference. The break-even for the word-at-a-time version is probably at least 64 characters. David