Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp575875pxk; Fri, 11 Sep 2020 14:53:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcN5s+LmV6XT47k1YxPvXpPihoRN2FOXsxYsYV6akWNJvd0+a1NrgIAiW49b51gWzVkiXv X-Received: by 2002:a17:906:c0ce:: with SMTP id bn14mr4101891ejb.422.1599861203829; Fri, 11 Sep 2020 14:53:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599861203; cv=none; d=google.com; s=arc-20160816; b=PcfXJ7nFxgiJSIkPA0Gv1YhNyXf+KvkcpjU6e9lpj01PihzfCFDEw4w0XB5bOaednA udombGtW/NLCIV8J7bqnkGYBkk/5D8rVCh8ITUntkMhgjDzI9VHik6f3zurhEKACejGF u0RyORQNB0XCDDHzfHWKy1aoPNgb+V7Ec73YVDvpqpDVGjEHKmmk1yklt6Gd8T5dWrkn lJvKVX/BbXgLO3n1J+hOWx/Cq2t3RR0UUojGAujqJdMHhqfP1VKUxfwFTAuGFERqOUPB 88uQ6heTHb27qpsWyy2bD7MoQ+WFpDxFqO0gAGFM1qo00C/GKVEmAeK0puYs6TAJo61l Uyog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=vUUj1yA5C8gIyhmtCGBnAf4j9JtfhjGcWKZGvC9mD28=; b=b8j6dARXGZVp/qi1UyHf0Mpx8U7AKsw6a5n/ZXdKTP3FsZi1b8mg+p3H3LMLVD5feX Sizn8mn2ezaB9q5R0wdCcdgcbEhPDFf9rZqNIjRWki86n3z8oq+N8tFYib0wwYj/9GMW 75RfxnTPKMTWOKd3izavOfJUDfCmJFbtL6OmlQvR3KP/dQ8wIXysMEq/Y5b3o2KFiCdj 7k7shxzz5ZH8gvnYV8sOyAia5MM7y0amjGui5bkuAxdKBFjkuT4fsHetJLFoqMX+vLke 1zhwPq47ECrI9dkwZ6LySBWMBfa9/+ZARPh/hRLQ0MXMPTx/dQrR1e7iw1DQWYhv/Nvl 5TGw== 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 p14si2357636edj.165.2020.09.11.14.53.01; Fri, 11 Sep 2020 14:53:23 -0700 (PDT) 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 S1725893AbgIKVwB (ORCPT + 99 others); Fri, 11 Sep 2020 17:52:01 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:32909 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725890AbgIKVv3 (ORCPT ); Fri, 11 Sep 2020 17:51:29 -0400 Received: by mail-io1-f68.google.com with SMTP id r25so12711054ioj.0 for ; Fri, 11 Sep 2020 14:51:27 -0700 (PDT) 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=vUUj1yA5C8gIyhmtCGBnAf4j9JtfhjGcWKZGvC9mD28=; b=Mz02Idu9ezuC6AildO42SVHY02NRH68qFo1TWwOx+5yU5doGvkJuKK5S/vPy68ZROf s5pfP8ZgbEclTz5oaBDgISEWwOaZQj99CtEbV0pCySF5JG+UB+2/y+qZrxiqZghR5Ruk iC6Ka2QO55zXuYoBKCHn8NpmRp2buIm7CVNR2+P/eSAHn8Gj0HFH3RPFyZwKg158UQNz 2I9IcezeBhTMB7BYSNqVRjew6MjMbLRlgVRp8irAUIlEM9X94UY1kHkbqjeH+E3U+LCH hVEnVrTylIyZ0FNHGtqtNxn6PAXZ+UVElkEY7DBW2UAeBWoQIcrEgu8h09NAG18oX/9r U42g== X-Gm-Message-State: AOAM533p4GWLwfOkNJrnMzom/rDOoi/NGW8NCwmHIPDfBFppPBVFF9jU r7bN8rJzXhnX+TAW+KXoPA== X-Received: by 2002:a5e:c70b:: with SMTP id f11mr3517630iop.140.1599861086588; Fri, 11 Sep 2020 14:51:26 -0700 (PDT) Received: from xps15.herring.priv ([64.188.179.251]) by smtp.googlemail.com with ESMTPSA id a20sm1927966ilq.57.2020.09.11.14.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2020 14:51:26 -0700 (PDT) From: Rob Herring To: Will Deacon , Catalin Marinas , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Jiri Olsa Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Alexander Shishkin , Namhyung Kim , Raphael Gault , Mark Rutland , Jonathan Cameron , Ian Rogers , honnappa.nagarahalli@arm.com Subject: [PATCH v3 04/10] tools/include: Add an initial math64.h Date: Fri, 11 Sep 2020 15:51:12 -0600 Message-Id: <20200911215118.2887710-5-robh@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200911215118.2887710-1-robh@kernel.org> References: <20200911215118.2887710-1-robh@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org 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.25.1