Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp714518pxf; Wed, 10 Mar 2021 16:10:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJwSAYPGIYgwSGal3BxsP2ZKFgpnX9Lv/HnK7pqcu8VYlHd2NMixRkZUZhsdmNWU0FPwwVq3 X-Received: by 2002:a17:907:2661:: with SMTP id ci1mr404581ejc.403.1615421426893; Wed, 10 Mar 2021 16:10:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615421426; cv=none; d=google.com; s=arc-20160816; b=OLmLp3cu+7d0nwmlj064pPUtAm5Y1Kh1Kf0v2bFLES8IRvv7ADst08X+c62JJ7c1m+ eKyIgoHs1go6957lDNWCXpassNh+rHrRrH1bdeXoix13Fpyb+8m1ECQwDNHEILI42C8m 1jNBLwkPKLsXJ2lTMaUIWsF8Ba4Q/BJkjap93Xe1d+PofiVrj63Lkj2o7fqJna7Nod2s H2AeJd35azNdpCV29I9eLf9nsEUAFbdikPZA4sbmtxj9uAAs/kouXT0ldaXXKCJLblRv 9c2Z26CX9leMWarGh0LvwJtwJoRhIULwYWjT8lVY/0oK76vn+LoMJ4MlgfP2AfdcFUAq wiyw== 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; bh=DjdlDe1r8PWcI8CJTWfGrTFm/qYeuvm3S8MlLSmS6yU=; b=f2qlKrv53KTC7xdJ3MSzaANmNW9RaO9B6KMjSAfJ0UjbjZqej0D8S+H2wWig9RmXhm FlP0mx/fbfHFi2bb9FX+l/pIz5HyuIyfs+oErfXoiEUp1SL32mj6vGD3vgHSva4AVtoz u+Noch8LGrf6mnfB8KeivDFcjUuUCzgr0Y63GUj4P97/KAKrMTEXeWrmB9EBaQRelxF5 0JpTsHilAgWrLRaL+ZdgFchcat5xvhIJuA573tWMTgIgvNFepH6Z8YghVh7UBdStvyuP gUmILleYq93YJe6My8ud5fvW6j3wpFWp8SXt00nD1pd7rnXWKhETZgMUADzfde/wsfyR 8jYQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce13si651303edb.43.2021.03.10.16.10.04; Wed, 10 Mar 2021 16:10:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229656AbhCKAJM (ORCPT + 99 others); Wed, 10 Mar 2021 19:09:12 -0500 Received: from mail-io1-f42.google.com ([209.85.166.42]:39463 "EHLO mail-io1-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229675AbhCKAIv (ORCPT ); Wed, 10 Mar 2021 19:08:51 -0500 Received: by mail-io1-f42.google.com with SMTP id o9so19994742iow.6 for ; Wed, 10 Mar 2021 16:08:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DjdlDe1r8PWcI8CJTWfGrTFm/qYeuvm3S8MlLSmS6yU=; b=Jpaa2zXbIFgb+l5i91xlzLRbrRIA2whNXVd/bfHSEtKGIfYuypzCNJh7eE1zuxnPPZ 1wo0NrxgkPMILYLsyIHgdqaw4+AXvsgUDR341bw7wkh7xyWXmmWSTQSe7LDLnltjzq/D T55SxtBc0WdhYNF0ebQnNXM898SIXovg2yB5JpAnnT0DI61tl1anSyiZjse6Rk3ENGd2 uxP1aZLc0mW4bM9xpOemKCNezgOquZFwySBpgFHtZFNO9+Y0IHOo+pNkFeijifFLpOHL y7yCQzoFePdxwz02uAVlYxKHzqSlS+H/D1+uQBjG1Z8sRoD10COYZbBeUV84XM0FEQ0h /IJw== X-Gm-Message-State: AOAM53281oCuEXqUaIQfNmkQSso0vIVvdoVF9q3DUOfI2KyLSAM8yu/G 4DAK76pfKpfIAoG0icufdQ== X-Received: by 2002:a5d:97c9:: with SMTP id k9mr4303295ios.45.1615421330477; Wed, 10 Mar 2021 16:08:50 -0800 (PST) Received: from xps15.herring.priv ([64.188.179.253]) by smtp.googlemail.com with ESMTPSA id x17sm484351ilm.40.2021.03.10.16.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Mar 2021 16:08:49 -0800 (PST) From: Rob Herring To: Will Deacon , Catalin Marinas , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Jiri Olsa , Mark Rutland Cc: Ian Rogers , Alexander Shishkin , honnappa.nagarahalli@arm.com, Zachary.Leaf@arm.com, Raphael Gault , Jonathan Cameron , Namhyung Kim , Itaru Kitayama , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 03/10] tools/include: Add an initial math64.h Date: Wed, 10 Mar 2021 17:08:30 -0700 Message-Id: <20210311000837.3630499-4-robh@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210311000837.3630499-1-robh@kernel.org> References: <20210311000837.3630499-1-robh@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add an initial math64.h similar to linux/math64.h with functions mul_u64_u64_div64() and mul_u64_u32_shr(). This isn't a direct copy of include/linux/math64.h as that doesn't define mul_u64_u64_div64(). Implementation was written by Peter Zilkstra based on linux/math64.h and div64.h[1]. The original implementation was not optimal on arm64 as __int128 division is not optimal with a call out to __udivti3, so I dropped the __int128 variant of mul_u64_u64_div64(). [1] https://lore.kernel.org/lkml/20200322101848.GF2452@worktop.programming.kicks-ass.net/ Cc: Peter Zijlstra Signed-off-by: Rob Herring --- tools/include/linux/math64.h | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tools/include/linux/math64.h diff --git a/tools/include/linux/math64.h b/tools/include/linux/math64.h new file mode 100644 index 000000000000..4ad45d5943dc --- /dev/null +++ b/tools/include/linux/math64.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MATH64_H +#define _LINUX_MATH64_H + +#include + +#ifdef __x86_64__ +static inline u64 mul_u64_u64_div64(u64 a, u64 b, u64 c) +{ + u64 q; + + asm ("mulq %2; divq %3" : "=a" (q) + : "a" (a), "rm" (b), "rm" (c) + : "rdx"); + + return q; +} +#define mul_u64_u64_div64 mul_u64_u64_div64 +#endif + +#ifdef __SIZEOF_INT128__ +static inline u64 mul_u64_u32_shr(u64 a, u32 b, unsigned int shift) +{ + return (u64)(((unsigned __int128)a * b) >> shift); +} + +#else + +#ifdef __i386__ +static inline u64 mul_u32_u32(u32 a, u32 b) +{ + u32 high, low; + + asm ("mull %[b]" : "=a" (low), "=d" (high) + : [a] "a" (a), [b] "rm" (b) ); + + return low | ((u64)high) << 32; +} +#else +static inline u64 mul_u32_u32(u32 a, u32 b) +{ + return (u64)a * b; +} +#endif + +static inline u64 mul_u64_u32_shr(u64 a, u32 b, unsigned int shift) +{ + u32 ah, al; + u64 ret; + + al = a; + ah = a >> 32; + + ret = mul_u32_u32(al, b) >> shift; + if (ah) + ret += mul_u32_u32(ah, b) << (32 - shift); + + return ret; +} + +#endif /* __SIZEOF_INT128__ */ + +#ifndef mul_u64_u64_div64 +static inline u64 mul_u64_u64_div64(u64 a, u64 b, u64 c) +{ + u64 quot, rem; + + quot = a / c; + rem = a % c; + + return quot * b + (rem * b) / c; +} +#endif + +#endif /* _LINUX_MATH64_H */ -- 2.27.0