From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C1B44C6FD1F for ; Sun, 19 Mar 2023 21:24:23 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 05D2E8594F; Sun, 19 Mar 2023 22:24:12 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="MdmV50WB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 651F285936; Sun, 19 Mar 2023 22:24:03 +0100 (CET) Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1119B85283 for ; Sun, 19 Mar 2023 22:23:59 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=judge.packham@gmail.com Received: by mail-pj1-x102a.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso14525200pjb.0 for ; Sun, 19 Mar 2023 14:23:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679261037; 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=9sSCcoCNV6N1dPLxgfYCtpbTtWatL5CtYE+8iuMRx5Q=; b=MdmV50WBdzoy7ceB5UFpMu0NoGBskrvseeS4fu8v8/3qfCQ/Dzsygby0jmFKYtoSSf DVLfZ3EHtrNcje/6S2Ux6cvn2taPAnvquC4sall0yub99n9CUc7P97t4Vj7NzJMxOcC+ U3jlJLF+VsA/Vw2cfXG/W3wS4h53N6RClECxf6kFCDpq1s9G32cIxLiZ6VtjgjHUz4aQ dfqL83aII1ENUahPyIXkANYWVZBV+1eL8UAMAG6UzSXNU289svLP4d26UANYnx2rj2KE D331tCK0J4iu3DrmtRZ40Lsb0JDCfsueMnF8F6PSfiF+8/+DMNpwpiLnCVfIcGfOS4dL qCkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679261037; 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=9sSCcoCNV6N1dPLxgfYCtpbTtWatL5CtYE+8iuMRx5Q=; b=wcCmoyu1gZwtTZK4yQ0wWircP9f1i6GwFodG9WzYmnjjwMGsx0QoboBMjw6DvEIg0M VTVfR1AT6PPpKS8RjGSzsVp4jBPmRTHOlplPTpYgmlt74i8sf/91iy6OcNKW6BT+lk29 629AmXlB2ypXhbndfi0G4JvMmrekL7qaXbjlwFkryzsoSlAH94kS8oj2UTevaHzY1lzf ZLC0RvtXsNTzXmcVhTaGcdSiz16khMrzE0fE4U/W0LZ9MVwzWY1DwLcllalDAcJHOW8c 9f/AFhx2jenR+5Fds/enD8bqtaD5nRmF/NgsFh05krw+c598MEjMlLjSmG/f0frpHCP7 jYMw== X-Gm-Message-State: AO0yUKUAifG7TRmm9Hsjya/1vx+87CIJV6BzjMApXRL/bIkFHPc4hL6C H73nSC4/4bGGQW+Q7V/UfDVCkTzBAjI= X-Google-Smtp-Source: AK7set/M07NmYuW9vcfsvRcSiFW79aM+GpS6SQu54np7Z8/nMJI2IQIE++z9CeofN5v6xMzZUkXrBA== X-Received: by 2002:a17:902:f351:b0:1a0:4ebd:15d5 with SMTP id q17-20020a170902f35100b001a04ebd15d5mr10947084ple.67.1679261036830; Sun, 19 Mar 2023 14:23:56 -0700 (PDT) Received: from chrisp-dl.atlnz.lc ([2001:df5:b000:22:7258:28bf:27ad:5c43]) by smtp.gmail.com with ESMTPSA id x10-20020a1709027c0a00b00198f36a8941sm3176174pll.221.2023.03.19.14.23.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Mar 2023 14:23:56 -0700 (PDT) From: Chris Packham To: u-boot@lists.denx.de Cc: Chris Packham , Simon Glass Subject: [PATCH v2 1/2] include: kernel.h: port find_closest() from Linux Date: Mon, 20 Mar 2023 10:23:43 +1300 Message-Id: <20230319212344.1691814-2-judge.packham@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230319212344.1691814-1-judge.packham@gmail.com> References: <20230319212344.1691814-1-judge.packham@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean The find_closest() macro can be used to find an element in a sorted array that is closest to an input value. Bring in this macro from Linux v6.3-rc1-2-g8ca09d5fa354. Signed-off-by: Chris Packham Reviewed-by: Simon Glass --- Changes in v2: - Add note on which Linux version this came from - Collect review from Simon include/linux/kernel.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 3e71d61074b6..5cd6c9dc8219 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -284,4 +284,28 @@ offsetof(struct structure, member) == (offset), \ "`struct " #structure "` offset for `" #member "` is not " #offset) +#define __find_closest(x, a, as, op) \ +({ \ + typeof(as) __fc_i, __fc_as = (as) - 1; \ + typeof(x) __fc_x = (x); \ + typeof(*a) const *__fc_a = (a); \ + for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \ + if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] + \ + __fc_a[__fc_i + 1], 2)) \ + break; \ + } \ + (__fc_i); \ +}) + +/** + * find_closest - locate the closest element in a sorted array + * @x: The reference value. + * @a: The array in which to look for the closest element. Must be sorted + * in ascending order. + * @as: Size of 'a'. + * + * Returns the index of the element closest to 'x'. + */ +#define find_closest(x, a, as) __find_closest(x, a, as, <=) + #endif -- 2.40.0