linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matthias Kaehlcke <mka@chromium.org>
To: hpa@zytor.com
Cc: Andy Lutomirski <luto@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	x86@kernel.org, linux-kernel@vger.kernel.org,
	Nick Desaulniers <ndesaulniers@google.com>,
	Manoj Gupta <manojgupta@chromium.org>,
	Tiancong Wang <tcwang@chromium.org>,
	Stephen Hines <srhines@google.com>,
	clang-built-linux@googlegroups.com
Subject: Re: [PATCH] lib: Add shared copy of __lshrti3 from libgcc
Date: Mon, 18 Mar 2019 15:16:39 -0700	[thread overview]
Message-ID: <20190318221639.GK112750@google.com> (raw)
In-Reply-To: <25133BBA-9121-4B3A-865B-085BFC5F154C@zytor.com>

On Mon, Mar 18, 2019 at 02:50:44PM -0700, hpa@zytor.com wrote:
> On March 18, 2019 2:31:13 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> >On Fri, Mar 15, 2019 at 01:54:50PM -0700, Matthias Kaehlcke wrote:
> >> The compiler may emit calls to __lshrti3 from the compiler runtime
> >> library, which results in undefined references:
> >> 
> >> arch/x86/kvm/x86.o: In function `mul_u64_u64_shr':
> >>   include/linux/math64.h:186: undefined reference to `__lshrti3'
> >> 
> >> Add a copy of the __lshrti3 libgcc routine (from gcc v4.9.2).
> >> 
> >> Include the function for x86 builds with clang, which is the
> >> environment where the above error was observed.
> >> 
> >> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> >
> >With "Revert "kbuild: use -Oz instead of -Os when using clang"
> >(https://lore.kernel.org/patchwork/patch/1051932/) the above
> >error is fixed, a few comments inline for if the patch is
> >resurrected in the future because __lshrti3 is emitted in a
> >different context.
> >
> >> diff --git a/include/linux/libgcc.h b/include/linux/libgcc.h
> >> index 32e1e0f4b2d0..a71036471838 100644
> >> --- a/include/linux/libgcc.h
> >> +++ b/include/linux/libgcc.h
> >> @@ -22,15 +22,26 @@
> >>  #include <asm/byteorder.h>
> >> 
> >>  typedef int word_type __attribute__ ((mode (__word__)));
> >> +typedef int TItype __attribute__ ((mode (TI)));
> >
> >Consider using __int128 instead. Definition and use need a
> >'defined(__SIZEOF_INT128__)' guard  (similar for mode (TI)), since
> >these 128 bit types aren't supported on all platforms.
> >
> >>  #ifdef __BIG_ENDIAN
> >>  struct DWstruct {
> >>  	int high, low;
> >>  };
> >> +
> >> +struct DWstruct128 {
> >> +	long long high, low;
> >> +};
> >
> >This struct isn't needed, struct DWstruct can be used.
> >
> >> diff --git a/lib/lshrti3.c b/lib/lshrti3.c
> >> new file mode 100644
> >> index 000000000000..2d2123bb3030
> >> --- /dev/null
> >> +++ b/lib/lshrti3.c
> >> @@ -0,0 +1,31 @@
> >> +// SPDX-License-Identifier: GPL-2.0
> >> +
> >> +#include <linux/export.h>
> >> +#include <linux/libgcc.h>
> >> +
> >> +long long __lshrti3(long long u, word_type b)
> >
> >use TItype for input/output, which is what gcc does, though the above
> >matches the interface in the documentation.
> >
> >> +{
> >> +	DWunion128 uu, w;
> >> +	word_type bm;
> >> +
> >> +	if (b == 0)
> >> +		return u;
> >> +
> >> +	uu.ll = u;
> >> +	bm = 64 - b;
> >> +
> >> +	if (bm <= 0) {
> >> +		w.s.high = 0;
> >> +		w.s.low = (unsigned long long) uu.s.high >> -bm;
> >
> >include <linux/types.h> and use u64 instead of unsigned long long.
> 
> Ok, now I'm really puzzled.
> 
> How could we need a 128-bit shift when the prototype only has 64 bits of input?!

Good question, this is the code from libgcc:

TItype
__lshrti3 (TItype u, shift_count_type b)
{
  if (b == 0)
    return u;

  const DWunion uu = {.ll = u};
  const shift_count_type bm = (8 * (8)) - b;
  DWunion w;

  if (bm <= 0)
    {
      w.s.high = 0;
      w.s.low = (UDItype) uu.s.high >> -bm;
    }
  else
    {
      const UDItype carries = (UDItype) uu.s.high << bm;

      w.s.high = (UDItype) uu.s.high >> b;
      w.s.low = ((UDItype) uu.s.low >> b) | carries;
    }

  return w.ll;
}


My compiler knowledge is limited, my guess is that the function is a
generic implementation, and while a long long is 64-bit wide under
Linux it could be 128-bit on other platforms.

  reply	other threads:[~2019-03-18 22:16 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-15 20:54 [PATCH] lib: Add shared copy of __lshrti3 from libgcc Matthias Kaehlcke
2019-03-15 22:06 ` Nick Desaulniers
2019-03-15 22:08   ` hpa
2019-03-15 23:34     ` Matthias Kaehlcke
2019-03-15 23:51       ` hpa
2019-03-15 22:12   ` hpa
2019-03-15 23:47     ` Matthias Kaehlcke
2019-03-15 23:53       ` hpa
2019-03-18 16:38         ` Matthias Kaehlcke
2019-03-15 23:29   ` Matthias Kaehlcke
2019-03-18  9:14   ` Peter Zijlstra
2019-03-18 14:43     ` David Laight
2019-03-18 14:54       ` Peter Zijlstra
2019-03-18 21:31 ` Matthias Kaehlcke
2019-03-18 21:50   ` hpa
2019-03-18 22:16     ` Matthias Kaehlcke [this message]
2019-03-18 23:44       ` hpa
2019-03-18 23:52         ` Matthias Kaehlcke
2019-03-18 23:54           ` hpa
2019-03-18 23:55           ` hpa
2019-03-19 17:10             ` Matthias Kaehlcke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190318221639.GK112750@google.com \
    --to=mka@chromium.org \
    --cc=bp@alien8.de \
    --cc=clang-built-linux@googlegroups.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=manojgupta@chromium.org \
    --cc=mingo@redhat.com \
    --cc=ndesaulniers@google.com \
    --cc=srhines@google.com \
    --cc=tcwang@chromium.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).