Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp2105390rdb; Sun, 21 Jan 2024 07:37:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IFMz6qRpRMEv4PGi3MAfL+qgjZl/kFWRUoUA6m9O7BUxp7DYQRkUQKNHllrQNVNzd4Geh9l X-Received: by 2002:a17:906:9c91:b0:a30:53f1:1a19 with SMTP id fj17-20020a1709069c9100b00a3053f11a19mr58348ejc.26.1705851464169; Sun, 21 Jan 2024 07:37:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705851464; cv=pass; d=google.com; s=arc-20160816; b=XAlOG5LyuJQpdN1TjvbdEDnEfanBYZkdR9UZOaYmxcankVjoFJpdLZdeuB6Rz3/jgO 4G25jH78Rq4rZGEnO++6x8VbesVNuj9jfT/D334NiPi6IQHijT06iy0PcqrzqKGg8XiM DmA8hSICfLSJcTUZLL9avGzQZICfZFAllVJ1AhZwXS5hNwmx90Y47D0W8OOqY8Bup5Az lU7B3X+hj6lZHXbMm/K3DAWYEhSDct8JaEOHgAHC09CtleoRTf/utvlPAc6bOnBvxUzM 1xbH0Zwd5hGGdhuVBEMauRHx7Cpj9tTLixIsL1cq9ZEHPh0umvFt5tMT8tYq20390QxQ eLYg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=2pqHvvCphlALlajToOUOu8ABe7Bhki2MXmZlVOF4qBQ=; fh=bIdfD9MSLywtXqsGRXj2khWM+rA/5tnwX1wJ2K6XY0o=; b=CCX0eHDxQq6YLL4smVv7L2BMtmv9SfpK8RUbbV1fOSaBIUJ8CK4Q4n2PX6ldPpJFsB 8p5fDoL4U3F56WB/2ASh5hhGImfXxPvL+jKI+EtZ81Jsxx6VKYrjJ49tFNpIyEHIQwBX TheWlgFU+3QBUN9VsY2D1Ee7+u6qx2SE2+e+pF5kESp0ls54xcJHaknijiRVOSKIPLQR cahgWnxm1PVf6e7jE765r1iXNVqQvJsmIou2SYvQB6Nz6ECC6bPAA67Nj9Yj5YyzGM6V 8HgsU//il0TLWUWoZ5FFazCpbU/l0gPUYjU27p8qxUIZombtnc2ECq0TVjxkZbjhucqr AGng== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="F/uylxvv"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-32091-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32091-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id l21-20020a170906079500b00a27a70807adsi9883058ejc.560.2024.01.21.07.37.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 07:37:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32091-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="F/uylxvv"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-32091-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32091-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id EB21A1F22EB6 for ; Sun, 21 Jan 2024 15:37:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8B1EC37702; Sun, 21 Jan 2024 15:37:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F/uylxvv" Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 87C9F3771F; Sun, 21 Jan 2024 15:37:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705851425; cv=none; b=g136HdmKrKhwih3yT2cuEJv4ACBG9eDxwgWEdWXVjEhJP3Th6NKc7sNk4fOGwjD+Og8YhMV2TvDASOivdi98fUyXKUqP3WpRcPOj0QM3ybjluIjNGkiFrPYMgffljEDCNXkLVWeJirqV6idpOGAdBe9sdK5+ohQ84gLGKOJN+VM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705851425; c=relaxed/simple; bh=td7eYq5RRdZjgP7IOM9O5bSI3K4VazA2jQDoOaruLEY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lr8wtKlkoBRQR7rjvjrcSHLBVaqCznk8ECnXgImJ1hBfpD05Cm/S2HJIa9QUqWwiEIZvtuFktSEssvubqnt9vDV7+37NjEk/BoCayBjdox6nInu8iDyfPvLrO3L9u+A7gRFYzeYSGyk17Fy4/Hqkw2TSDfqaPqyDpERU7oKtlLw= 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=F/uylxvv; arc=none smtp.client-ip=209.85.210.180 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 Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6d9b41a3cb7so828814b3a.0; Sun, 21 Jan 2024 07:37:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705851424; x=1706456224; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2pqHvvCphlALlajToOUOu8ABe7Bhki2MXmZlVOF4qBQ=; b=F/uylxvvWe45LqSORYN5ugmKL87jo+XNva6oXhtSANRpkM5TDGds2oAcUG5Ifa1P8a N8qZNUEZ4XG+FTo8srme4S9gcBHBGhDMb06F3wTk4tts5+HLQ26/bweMC5soMwABPDLP 4IVCxBnYcQtnkQLOoUnUQbMqUuJM6G4NOVwArSxHw55WdNjsj3Wq+DfB/3aUoZ40XVBw NYysfNSdTQPLVHzijOmMTw4MxCS9sIsrugHqpwkMPjhC+oKWNUyS8OojSqjbzlNrqbU6 3Q8wljLamC/soitqdk2h22wBcP0yC0tnzQvYildMNULop9qUZHU0gSputMlkRtrbaA4K TZkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705851424; x=1706456224; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2pqHvvCphlALlajToOUOu8ABe7Bhki2MXmZlVOF4qBQ=; b=dvCPvYuHyKNvQTUsRhVIC4lFNFTaHWi/HwFQ1aRDujbKJ1EIDWjCj4YJ2C5XdtJeVD FPtp20wXhu9oY8H6myI8d68yP+oxsMrT1lXAf4MDVjcHvGCL8EN67bRuu1Pcba5FHXL8 pY/FhwYJ13mU5tDnbOkmJb5PKhkBf/gm5VjlziFrXqnHsF5+1LyRSN/4qMRNFqIN9J8c 4wCFpMIkHeOlvgl+OxMEanLpmwy0ZOboyTrvd6k6E4Q1wVb6KALs63qvu7h22pBtR3l3 r6DJvveZ8FaqkHhgPW4nnjbJhhGV5KjzSx0TcA2JuMN4Hgt40wBZYrdOgRPdhTyma8lN QNjg== X-Gm-Message-State: AOJu0YzAzHVQN0uViyhOO8RLZygQO7LCEwdEU3QrzFSbObkTs870a0b5 yaNKVolek/BtOlwjfTd3J+DRoruea49EnZFcADUBgLgugNWTSKhi X-Received: by 2002:a17:90a:c286:b0:290:19c1:103f with SMTP id f6-20020a17090ac28600b0029019c1103fmr3578520pjt.1.1705851423879; Sun, 21 Jan 2024 07:37:03 -0800 (PST) Received: from localhost.localdomain ([140.116.154.65]) by smtp.gmail.com with ESMTPSA id sv13-20020a17090b538d00b0028d8fa0171asm7744347pjb.35.2024.01.21.07.37.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 07:37:03 -0800 (PST) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev Cc: bfoster@redhat.com, jserv@ccns.ncku.edu.tw, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH 2/5] bcachefs: Introduce parent function for sort_cmp_size() Date: Sun, 21 Jan 2024 23:36:46 +0800 Message-Id: <20240121153649.2733274-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240121153649.2733274-1-visitorckw@gmail.com> References: <20240121153649.2733274-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. 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