From patchwork Thu Mar 16 22:16:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Packham X-Patchwork-Id: 1758014 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=PVPMzWzZ; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Pd1pH4jcgz1yWs for ; Fri, 17 Mar 2023 09:17:11 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4B95F85F11; Thu, 16 Mar 2023 23:16:57 +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="PVPMzWzZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 001EA85ECF; Thu, 16 Mar 2023 23:16:45 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) (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 917A485E6B for ; Thu, 16 Mar 2023 23:16:42 +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-pl1-x62a.google.com with SMTP id o11so3339179ple.1 for ; Thu, 16 Mar 2023 15:16:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679005000; 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=APhr/Xk3Lb0KWdKGYBBayY/JgKPYC2iuWfvlL3cJAN0=; b=PVPMzWzZ1U6tPebAS5rjGX4TjxcmMJUXdDsLvPsXsjVPb0QXJEAtTKPt2awFmSFVk2 /u4bMhDiPSLXwR0x2NHHVq6yrZCCdVtIAyNsL9Yhg50abq0lEC+q82d4myfFJwAneVMy WoBvXBs3VcC2Rfsla5wbTUb6U7q+6s4FwdLvplKiSZN1egYTWhlyknq79B6pc8OJmzC6 oZqSsQB1vYS0ssyMF0tfIqGba3wkOmAaE19prIxW0BLlw4h5IXD/HuMW9jl3S2dhA2lP JO3jcQMf4/WKD3XL2ST4TE5NfxQyKq7E8C+68fXDiMlGG1pzBALXzGkOhER2djA0Ghm0 9uIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679005000; 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=APhr/Xk3Lb0KWdKGYBBayY/JgKPYC2iuWfvlL3cJAN0=; b=AV2/2koY2Pfdr3emgeD+FeNRoyhH9zJ8FFy0mm1TrhuVuYDxo8+ybV6Ux9XbVEZzFf LGcKKU/I+Lcu2PRfRAc59smPlcHOT7YwcDv8FjHz/eNj5G9eN6P6MopW0Eh1/mqIOrvx ulsG4A71oq6h+E9N6mYTC9rfozBSGprUroG00uWR6yDFB0Sm5Imc4P5BMB3qSxfTaNW6 NWExUCs5vFq0Ihg4sEAttNaQKb3spuTJXA8YeCrm6SEN71jkpIPXjdmjecmQ95Jh19U8 5IHoK8EUG/PLaH56T7njgQs8xfUziWUnW5FclB/g4GwYdQ4q++UeWnwF8pJRKlfdunat rSmQ== X-Gm-Message-State: AO0yUKWGRufJc+NL6CWnxRXOktJhBZX0nVMK0yB8roZhZPQ350L9psIq J1Zq8AOywL7Tm6WyJAu1Bszt2R8CjD0= X-Google-Smtp-Source: AK7set81TAigdYP1T+m/XhtWBD0JfkEBwWi4rEY2oGfdfM0cur3LASwfINSr0HFJTCPmGQTzug92qQ== X-Received: by 2002:a05:6a21:99a6:b0:d6:532:6671 with SMTP id ve38-20020a056a2199a600b000d605326671mr916664pzb.14.1679005000088; Thu, 16 Mar 2023 15:16:40 -0700 (PDT) Received: from chrisp-dl.atlnz.lc ([2001:df5:b000:22:f841:1af3:451b:dee3]) by smtp.gmail.com with ESMTPSA id 64-20020a630843000000b004b1fef0bf16sm125307pgi.73.2023.03.16.15.16.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 15:16:39 -0700 (PDT) From: Chris Packham To: u-boot@lists.denx.de Cc: Chris Packham Subject: [PATCH 1/2] include: kernel.h: port find_closest() from Linux Date: Fri, 17 Mar 2023 11:16:24 +1300 Message-Id: <20230316221626.1043438-2-judge.packham@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230316221626.1043438-1-judge.packham@gmail.com> References: <20230316221626.1043438-1-judge.packham@gmail.com> MIME-Version: 1.0 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. Signed-off-by: Chris Packham Reviewed-by: Simon Glass --- 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