Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp35880617rwd; Mon, 10 Jul 2023 14:14:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlEP1HBc+q0/D104eTLcKFh94TkhTlw1N+2YnPRu1SExZvv8WmAzqwEKRTVfegIWx6u3Iku5 X-Received: by 2002:a05:6a20:7d84:b0:11d:8a8f:655d with SMTP id v4-20020a056a207d8400b0011d8a8f655dmr19485349pzj.4.1689023649763; Mon, 10 Jul 2023 14:14:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689023649; cv=none; d=google.com; s=arc-20160816; b=cfa+smNkXcFvvzOSNpvJmRVZdBVisoW0/PPBcD9El7hjPIfdkyGPicMPfq2HCdIqkL Sdq3qzrKmtGkIeEEwclRadO2oI6XSDxKeVOl6b4J2+53KRQ0xu6bcf/S1sf5g4OqwEhQ PZyyTgbDJMy+5wi5rMev5daRwgTOb+FQnSuYVE1meX63NB8Nbm3hXuFa6lc7WbkDm5ha PKAo8pq4vWHpPYuEzOUwX2drLAafNU0xzT0K1BhSm8gejNaf52V4fA4wSQd2Zxxe3+Hl B/+9yM6EYTqZQ8ExwGI6xGjz/eci02MI0LztxW3Rtj9Bqf1nZmfqTAkIt2T77n3AhFy0 8vuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=A7RcetqmNW3btMImHl/BZfdcqRl9coUa9L9XZZv1508=; fh=XtKYrrV24qpJDEOQ96O/Lmd5LL6GP9mEWUOmxLG8cXI=; b=HDmxJnh2nxB4VrSFpPKVcu5oIWd/m6EMoMCvxjgeRjORnRofsO2aeKEi/AgHHyHsDY mABldx3Zqqo5uicsPrZaBnZoeo8gg05iYvcrQ9H8UcKPfyF2qv6JvtONm6oy27eSYKB5 zxUaV9ElHX6caisvgeuYhkDjs5AXsExiw5p6UiC62jqh97BO+5ejjO9Jl0ols5Kok7qS X5Z6zj5XBXT8/PjzM1YqcOwMVMEC1T66/JPZ1UGEWyWgl7HxYUIFPNjpocxY/tdFBeDc SZ7a1dIaaU9lSZpNo/iCiIIEV4s/ohAjOizwmMkjSnI7IJuWm7rpMnEc7n4Vc5Hz1OBC 1GFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=WgSSkXyo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z34-20020a056a001da200b0063b7c4435c0si296910pfw.54.2023.07.10.14.13.57; Mon, 10 Jul 2023 14:14:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=WgSSkXyo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233382AbjGJUqc (ORCPT + 99 others); Mon, 10 Jul 2023 16:46:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231656AbjGJUpA (ORCPT ); Mon, 10 Jul 2023 16:45:00 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C5A31BF; Mon, 10 Jul 2023 13:43:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=A7RcetqmNW3btMImHl/BZfdcqRl9coUa9L9XZZv1508=; b=WgSSkXyo6wHIO77DHamsAtL5yS QPw+3CNlMjYUWUL/cmn1npceGfaf+29pfTh1AWihDxltm3qKIFPVy5zVYjvclsnFpXKYetM/YBD+t pDQiNiNl9LpEoGB8KmBRwWmkDxMJDuHj5GlmENIK0nNp8MTh/wrwhCI2z1kpRHwF5n0P8/2o0TOIw lnX0H/DLnXPWpzmVouo1J7c0g3n5+cegiGdOuSGV97zLi9NShKpz20OELw2TBUrUBocDgTJYn7oY0 d4t9+1dGZsxpyK/6oTDLRRXJ10U4eXy3U5L8PQIq7FLntXSPG6i3HPw2LOBx3gqBtNDv1YMvvQTgb wQnxk6dA==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qIxjR-00EuoE-3L; Mon, 10 Jul 2023 20:43:41 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 01/38] minmax: Add in_range() macro Date: Mon, 10 Jul 2023 21:43:02 +0100 Message-Id: <20230710204339.3554919-2-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230710204339.3554919-1-willy@infradead.org> References: <20230710204339.3554919-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Determine if a value lies within a range more efficiently (subtraction + comparison vs two comparisons and an AND). It also has useful (under some circumstances) behaviour if the range exceeds the maximum value of the type. Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/minmax.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 396df1121bff..028069a1f7ef 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -158,6 +158,32 @@ */ #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) +static inline bool in_range64(u64 val, u64 start, u64 len) +{ + return (val - start) < len; +} + +static inline bool in_range32(u32 val, u32 start, u32 len) +{ + return (val - start) < len; +} + +/** + * in_range - Determine if a value lies within a range. + * @val: Value to test. + * @start: First value in range. + * @len: Number of values in range. + * + * This is more efficient than "if (start <= val && val < (start + len))". + * It also gives a different answer if @start + @len overflows the size of + * the type by a sufficient amount to encompass @val. Decide for yourself + * which behaviour you want, or prove that start + len never overflow. + * Do not blindly replace one form with the other. + */ +#define in_range(val, start, len) \ + sizeof(start) <= sizeof(u32) ? in_range32(val, start, len) : \ + in_range64(val, start, len) + /** * swap - swap values of @a and @b * @a: first value -- 2.39.2