Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp2122299rdb; Sun, 21 Jan 2024 08:17:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IEELlts4/0EU1Uq3W4WkZRLTSCIU+aOlQQr0tivmOGdzhJ2Hsct+NWd7yHzTHQMyXQbDYUA X-Received: by 2002:a17:90a:948b:b0:28e:875c:4493 with SMTP id s11-20020a17090a948b00b0028e875c4493mr629860pjo.38.1705853877421; Sun, 21 Jan 2024 08:17:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705853877; cv=pass; d=google.com; s=arc-20160816; b=yhUHmLYXHO3WUP45pmIYzQT9fwZ6WLyTMrt7bzpOgFrfPMK+ineqeCinNUFeoTssab lL7VPQYcM7v46ff9ey8s9YtemkAGNnNfrMqY6g+aSogupSsQ66KagQqU64raG13LuGSl zhxWDvKRD65pOZYWpfL4v/gpNIs30cugixWv+RKQ48Q18CFVkioILPPhNw2Hb2XU7Z+T QMQjLfLvoZ9FnNX2+o6bGcWOeg7CnppkqmeBya12+b8NHjEYk10pBKUb50Km22rKUIjB ALCL5F9AkcawqUNUBOZ6rZfmLizSwUvNnlzqIibzPFSzzuxef96f43B2d+6dRUvyyyYO B0Ww== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:dkim-signature:date; bh=aBUz93f2jOBbyF/oUQgr9OHoouGr0fMqGUXTlJhH8ow=; fh=Qa0Du3tp9W8XPJlojWqG/hiIP9RCqJ6yVRwNsBvO/0A=; b=It0HW6vek8QNnBfV9NWZVXMAbFik7nwxbdC76+o6UypwZxhiuLQ9uCfyKMqXiyXHcD rZmsGcZsRpcS/CTCDZ8yzlw+ESwSSqZZStNwHbRKOdSLYUQkZDqzyv8rvAaL/pzwsIVA kjJ8wswTX33cAQCKzTNkRU/8t5gVDLEOOdzgehMcnWv6xJFSSKSrLuxxILlxEizQanN0 nw3Ez6yj+2yV4GRD/yWSbrx+zQ3Qeas6O6ZncOIgjdC1eeNAq+4pqMFFIG/mraP2Waxb tRkvuMzpwSuyk9SSxbcghMIsHcnt49ZXeunJZyiJDPLl7RxjFDH9oIwyOFpHhh/6Hxbx TcmQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="i+cijJ+/"; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-32109-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32109-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id q69-20020a17090a17cb00b0028d9b5be596si6685243pja.28.2024.01.21.08.17.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 08:17:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32109-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="i+cijJ+/"; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-32109-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32109-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 56AE8B21D2E for ; Sun, 21 Jan 2024 16:17:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3CB2C381B4; Sun, 21 Jan 2024 16:17:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="i+cijJ+/" Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 351B8381AF for ; Sun, 21 Jan 2024 16:17:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705853858; cv=none; b=eE+aXalIPYjHE2ewXSaLbvNZWLSL2NmikAyOiZcWArsDVXb4RT1v8loFqCEO964qUuPBZDOJ8NqqhW6vKb2f2t6gkWr263YCHSNdoY3mjXE0IpQ8hUryQqAIUtfMN1CaaOlJxc23AQRm+hDxIHQTV+39IsvWQyQXDRVE1wad7Zg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705853858; c=relaxed/simple; bh=gvZEIS/jBXoNpzvSUbni12ioK+70WxbZ/7qDCGgisGU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MEXPd3O18+F+pyvwwFI2kCgQ58NX9GnMxuACgPwQ5kXs8QaBIlF6sibGGuG90gayJkpYBhlMh62pHb48ZVKgu8fVzWr3T/N2/lvxyM+IEMwzk49VFVL2sVQvfiqP1saShNaa1tbJFPxruVa5WjmJLrEm697zsnmY2fYtpmDUCqA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=i+cijJ+/; arc=none smtp.client-ip=95.215.58.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Sun, 21 Jan 2024 11:17:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1705853854; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=aBUz93f2jOBbyF/oUQgr9OHoouGr0fMqGUXTlJhH8ow=; b=i+cijJ+/iEse3wEqoLFRw0Z7YihzWsRXTkI3hPNaqKzJSedQgnHxJeLua7LGU/6vuyma3F RS1r/TSXRG/ySNsSSSKxMXYRq8zBdPUEWS9OPkmwxhiyFdJzqlM9ODvNmADwPsIVHpXwpF BBlmE1dnYIMmrQe8m2WfLCW03Y59kLg= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Kuan-Wei Chiu Cc: colyli@suse.de, bfoster@redhat.com, jserv@ccns.ncku.edu.tw, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org Subject: Re: [PATCH 2/5] bcachefs: Introduce parent function for sort_cmp_size() Message-ID: References: <20240121153649.2733274-1-visitorckw@gmail.com> <20240121153649.2733274-3-visitorckw@gmail.com> 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-Disposition: inline In-Reply-To: <20240121153649.2733274-3-visitorckw@gmail.com> X-Migadu-Flow: FLOW_OUT On Sun, Jan 21, 2024 at 11:36:46PM +0800, Kuan-Wei Chiu wrote: > When dealing with array indices, the parent's index can be obtained > using the formula (i - 1) / 2. However, when working with byte offsets, > this approach is not straightforward. To address this, we have > introduced a branch-free parent function that does not require any > division operations to calculate the parent's byte offset. This is a good commit message - but it would be even better if it was a function comment on parent() > > Signed-off-by: Kuan-Wei Chiu > --- > This patch has undergone unit testing using the following code [1]. > > [1]: > static int test(void) > { > size_t i, p, size, lsbit; > > for (i = 0; i < 10000; i++) { > size = get_random_u32() % (1U << 10); > lsbit = size & -size; > i = get_random_u32() % (1U << 20) * size + size; > p = parent(i, lsbit, size); > if (p != (i / size - 1) / 2 * size) > return -1; > } > > return 0; > } > > fs/bcachefs/util.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/fs/bcachefs/util.c b/fs/bcachefs/util.c > index bbc83b43162e..f5bbf96df2ce 100644 > --- a/fs/bcachefs/util.c > +++ b/fs/bcachefs/util.c > @@ -907,6 +907,13 @@ static inline void do_swap(void *base, size_t n, size_t size, > size); > } > > +static inline size_t parent(size_t i, size_t lsbit, size_t size) > +{ > + i -= size; > + i -= size & -(i & lsbit); > + return i >> 1; > +} > + > void eytzinger0_sort(void *base, size_t n, size_t size, > int (*cmp_func)(const void *, const void *, size_t), > void (*swap_func)(void *, void *, size_t)) > -- > 2.25.1 >