Received: by 10.223.185.116 with SMTP id b49csp8594075wrg; Fri, 2 Mar 2018 04:43:52 -0800 (PST) X-Google-Smtp-Source: AG47ELtXwsqrYHTfAw3NlUDEx+jfUjnL8CDlOZ/luwPSyhKlg4VZCCOUyNIV2RFziOv67r0aqDx2 X-Received: by 2002:a17:902:4581:: with SMTP id n1-v6mr5318359pld.135.1519994632813; Fri, 02 Mar 2018 04:43:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519994632; cv=none; d=google.com; s=arc-20160816; b=vqhAR9hA60Tqh5i4xGPHDIR8crGs9aEezDc4bnhcIiC8eKOd1EP+umoBoEmvJ+75y7 uJ/Rncak9TI0Px6RiWQDh2yCLtdS4YZLhhreYozsE6GXoXvlEGZbw/urhvSofs2J7NyS Q8KiAfIPTPCqX93mXH/PjCq/uxvquJH4gMURg7zJx703gSVr0GofTTcQcj03T64R7e2z uWY/MWuut1QCSRFt9zn5NQ/vtZaNJW9u9I3d6s6EqIsjXMeu5EZroA2D7BCtqFwsB+aZ cT7mh7XGYZO4oSZc7yiv3HPXlbYWECFRkKvAaU+h8voJkNmfV0GZI5su+QRsGJZgRO0/ Zfdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=Jvzgl5aO0L6Ep1FxE6IC/5ky38vTiCCILhFsGnojsI4=; b=FSWBFm/bSiET4VSXNYG1FnAaMt16IejtpWO2WmrPBY8ZlNIxeVvu/y5inTJYM6OnPm Ed0o1NBA8Pi3zYZgytFfm78wAQ7HJqcbzhbqLoB9o9gCDwF73xQWc+rWpZXfcJT9wetI USLrRMMwiRC0/1nfkjs1is0b8YVWo1+Zn8Y76X0PVd8VmrsZTO5zq6MeS9qUmZS/TVdB vF9HFzois2NKP5hHKvadOocDCCndI+kANe9fg3sGjy3J65M+B3/FX7ZKzvsv8iSuB1f2 gHGdnt+DrZo8jd2OuYZERvo4cphAy1oxfB0FDmFvUPExvgQigDD/VYv2AKV/NCFtXVHv RYsQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 68-v6si4760663pla.767.2018.03.02.04.43.38; Fri, 02 Mar 2018 04:43:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1423777AbeCBI4A (ORCPT + 99 others); Fri, 2 Mar 2018 03:56:00 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:50932 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1423748AbeCBIzz (ORCPT ); Fri, 2 Mar 2018 03:55:55 -0500 Received: from localhost (clnet-b04-243.ikbnet.co.at [83.175.124.243]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id E17991249; Fri, 2 Mar 2018 08:55:54 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thomas Petazzoni , James Hogan , Waldemar Brodkorb , Ralf Baechle , "Maciej W. Rozycki" , Matthew Fortune , Florian Fainelli , linux-mips@linux-mips.org, Guenter Roeck Subject: [PATCH 4.4 34/34] MIPS: Implement __multi3 for GCC7 MIPS64r6 builds Date: Fri, 2 Mar 2018 09:51:30 +0100 Message-Id: <20180302084438.206438236@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180302084435.842679610@linuxfoundation.org> References: <20180302084435.842679610@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: James Hogan commit ebabcf17bcd7ce968b1631ebe08236275698f39b upstream. GCC7 is a bit too eager to generate suboptimal __multi3 calls (128bit multiply with 128bit result) for MIPS64r6 builds, even in code which doesn't explicitly use 128bit types, such as the following: unsigned long func(unsigned long a, unsigned long b) { return a > (~0UL) / b; } Which GCC rearanges to: return (unsigned __int128)a * (unsigned __int128)b > 0xffffffffffffffff; Therefore implement __multi3, but only for MIPS64r6 with GCC7 as under normal circumstances we wouldn't expect any calls to __multi3 to be generated from kernel code. Reported-by: Thomas Petazzoni Signed-off-by: James Hogan Tested-by: Waldemar Brodkorb Cc: Ralf Baechle Cc: Maciej W. Rozycki Cc: Matthew Fortune Cc: Florian Fainelli Cc: linux-mips@linux-mips.org Patchwork: https://patchwork.linux-mips.org/patch/17890/ Cc: Guenter Roeck Signed-off-by: Greg Kroah-Hartman --- arch/mips/lib/Makefile | 3 +- arch/mips/lib/libgcc.h | 17 +++++++++++++++ arch/mips/lib/multi3.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) --- a/arch/mips/lib/Makefile +++ b/arch/mips/lib/Makefile @@ -15,4 +15,5 @@ obj-$(CONFIG_CPU_R3000) += r3k_dump_tlb obj-$(CONFIG_CPU_TX39XX) += r3k_dump_tlb.o # libgcc-style stuff needed in the kernel -obj-y += ashldi3.o ashrdi3.o bswapsi.o bswapdi.o cmpdi2.o lshrdi3.o ucmpdi2.o +obj-y += ashldi3.o ashrdi3.o bswapsi.o bswapdi.o cmpdi2.o lshrdi3.o multi3.o \ + ucmpdi2.o --- a/arch/mips/lib/libgcc.h +++ b/arch/mips/lib/libgcc.h @@ -9,10 +9,18 @@ typedef int word_type __attribute__ ((mo struct DWstruct { int high, low; }; + +struct TWstruct { + long long high, low; +}; #elif defined(__LITTLE_ENDIAN) struct DWstruct { int low, high; }; + +struct TWstruct { + long long low, high; +}; #else #error I feel sick. #endif @@ -22,4 +30,13 @@ typedef union { long long ll; } DWunion; +#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPSR6) +typedef int ti_type __attribute__((mode(TI))); + +typedef union { + struct TWstruct s; + ti_type ti; +} TWunion; +#endif + #endif /* __ASM_LIBGCC_H */ --- /dev/null +++ b/arch/mips/lib/multi3.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +#include "libgcc.h" + +/* + * GCC 7 suboptimally generates __multi3 calls for mips64r6, so for that + * specific case only we'll implement it here. + * + * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82981 + */ +#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPSR6) && (__GNUC__ == 7) + +/* multiply 64-bit values, low 64-bits returned */ +static inline long long notrace dmulu(long long a, long long b) +{ + long long res; + + asm ("dmulu %0,%1,%2" : "=r" (res) : "r" (a), "r" (b)); + return res; +} + +/* multiply 64-bit unsigned values, high 64-bits of 128-bit result returned */ +static inline long long notrace dmuhu(long long a, long long b) +{ + long long res; + + asm ("dmuhu %0,%1,%2" : "=r" (res) : "r" (a), "r" (b)); + return res; +} + +/* multiply 128-bit values, low 128-bits returned */ +ti_type notrace __multi3(ti_type a, ti_type b) +{ + TWunion res, aa, bb; + + aa.ti = a; + bb.ti = b; + + /* + * a * b = (a.lo * b.lo) + * + 2^64 * (a.hi * b.lo + a.lo * b.hi) + * [+ 2^128 * (a.hi * b.hi)] + */ + res.s.low = dmulu(aa.s.low, bb.s.low); + res.s.high = dmuhu(aa.s.low, bb.s.low); + res.s.high += dmulu(aa.s.high, bb.s.low); + res.s.high += dmulu(aa.s.low, bb.s.high); + + return res.ti; +} +EXPORT_SYMBOL(__multi3); + +#endif /* 64BIT && CPU_MIPSR6 && GCC7 */