mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Matt Turner <mattst88@gmail.com>
Cc: Magnus Lindholm <linmag7@gmail.com>,
	Richard Henderson <richard.henderson@linaro.org>,
	linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org,
	kernel test robot <lkp@intel.com>,
	oe-kbuild-all@lists.linux.dev
Subject: Re: [PATCH] alpha: add 128-bit shift helpers
Date: Mon, 12 Oct 2026 13:37:53 +0100	[thread overview]
Message-ID: <20261012133753.3a5868e9@pumpkin> (raw)
In-Reply-To: <20261010022039.1610210-1-mattst88@gmail.com>

On Fri,  9 Oct 2026 22:20:39 -0400
Matt Turner <mattst88@gmail.com> wrote:

> Selecting ARCH_SUPPORTS_INT128 lets generic code shift __int128 values by
> a variable count. gcc open-codes such a shift when optimizing for speed,
> but with CONFIG_CC_OPTIMIZE_FOR_SIZE=y it calls the libgcc routines
> __ashlti3, __ashrti3 and __lshrti3 instead. The kernel does not link
> against libgcc and Alpha does not provide them, so the link fails:
> 
>   alpha-linux-ld: lib/ubsan.o: in function `get_signed_val':
>   (.text+0x1c0): undefined reference to `__ashlti3'
>   (.text+0x1f8): undefined reference to `__ashrti3'
>   alpha-linux-ld: kernel/time/timekeeping.o: in function `delta_to_ns_safe':
>   (.text+0x2ac): undefined reference to `__lshrti3'
> 
> Provide the three helpers in C, as s390 does, and export them for
> modules. They are built into the kernel proper rather than lib.a so the
> exports are present even when nothing built in references them.

Do they need an explicit (shift &= 63)?
I've seen gcc/clang remove an explicit '&= 63' because they know the
hardware instruction ignores the high bits.
But I'm not sure what happens here.
Adding it won't make much difference - unless it matters.

David

> 
> The 64x64 to 128-bit multiply is always expanded inline with mulq/umulh,
> so no __multi3 is needed.
> 
> Fixes: fd04433a5bf3 ("alpha: select ARCH_SUPPORTS_INT128")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202610090831.DSxrGQRc-lkp@intel.com/
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Matt Turner <mattst88@gmail.com>
> ---
> Magnus, this is an alternative to dropping the select. It applies on top
> of for-next (496e328c6107). If you would rather drop "alpha: select
> ARCH_SUPPORTS_INT128" for now, I will resend this without the Fixes tag,
> followed by the select, as a two-patch series.
> 
> Tested with gcc 16: defconfig plus CONFIG_CC_OPTIMIZE_FOR_SIZE=y and
> CONFIG_UBSAN=y builds and links (vmlinux and modules, W=1), and the
> helpers match the compiler's inline shifts for every count from 0 to 127
> in a userspace test. Not boot tested.
> 
>  arch/alpha/include/asm/asm-prototypes.h |  6 +++
>  arch/alpha/lib/Makefile                 |  2 +
>  arch/alpha/lib/tishift.c                | 70 +++++++++++++++++++++++++
>  3 files changed, 78 insertions(+)
>  create mode 100644 arch/alpha/lib/tishift.c
> 
> diff --git a/arch/alpha/include/asm/asm-prototypes.h b/arch/alpha/include/asm/asm-prototypes.h
> index c8ae46fc2e74..672fea15032f 100644
> --- a/arch/alpha/include/asm/asm-prototypes.h
> +++ b/arch/alpha/include/asm/asm-prototypes.h
> @@ -17,3 +17,9 @@ extern void __remlu(void);
>  extern void __divqu(void);
>  extern void __remqu(void);
>  extern unsigned long __udiv_qrnnd(unsigned long *, unsigned long, unsigned long , unsigned long);
> +
> +#ifdef CONFIG_ARCH_SUPPORTS_INT128
> +extern __int128_t __ashlti3(__int128_t a, int shift);
> +extern __int128_t __ashrti3(__int128_t a, int shift);
> +extern __int128_t __lshrti3(__int128_t a, int shift);
> +#endif
> diff --git a/arch/alpha/lib/Makefile b/arch/alpha/lib/Makefile
> index 84046e730e6d..f77d4a46f2e6 100644
> --- a/arch/alpha/lib/Makefile
> +++ b/arch/alpha/lib/Makefile
> @@ -35,6 +35,8 @@ lib-y =	__divqu.o __remqu.o __divlu.o __remlu.o \
>  	callback_srm.o srm_puts.o srm_printk.o \
>  	fls.o
>  
> +obj-$(CONFIG_ARCH_SUPPORTS_INT128) += tishift.o
> +
>  # The division routines are built from single source, with different defines.
>  AFLAGS___divqu.o = -DDIV
>  AFLAGS___remqu.o =       -DREM
> diff --git a/arch/alpha/lib/tishift.c b/arch/alpha/lib/tishift.c
> new file mode 100644
> index 000000000000..488e1e483d48
> --- /dev/null
> +++ b/arch/alpha/lib/tishift.c
> @@ -0,0 +1,70 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * 128-bit shift helpers.
> + *
> + * gcc open-codes a variable 128-bit shift when optimizing for speed, but
> + * calls these libgcc routines when optimizing for size.
> + */
> +
> +#include <linux/export.h>
> +#include <linux/types.h>
> +#include <asm/asm-prototypes.h>
> +
> +union ti {
> +	__int128_t val;
> +	struct {
> +		u64 low;
> +		u64 high;
> +	};
> +};
> +
> +__int128_t __ashlti3(__int128_t a, int shift)
> +{
> +	union ti ti = { .val = a };
> +
> +	if (!shift)
> +		return ti.val;
> +	if (shift < 64) {
> +		ti.high = (ti.high << shift) | (ti.low >> (64 - shift));
> +		ti.low <<= shift;
> +	} else {
> +		ti.high = ti.low << (shift - 64);
> +		ti.low = 0;
> +	}
> +	return ti.val;
> +}
> +EXPORT_SYMBOL(__ashlti3);
> +
> +__int128_t __ashrti3(__int128_t a, int shift)
> +{
> +	union ti ti = { .val = a };
> +
> +	if (!shift)
> +		return ti.val;
> +	if (shift < 64) {
> +		ti.low = (ti.low >> shift) | (ti.high << (64 - shift));
> +		ti.high = (s64)ti.high >> shift;
> +	} else {
> +		ti.low = (s64)ti.high >> (shift - 64);
> +		ti.high = (s64)ti.high >> 63;
> +	}
> +	return ti.val;
> +}
> +EXPORT_SYMBOL(__ashrti3);
> +
> +__int128_t __lshrti3(__int128_t a, int shift)
> +{
> +	union ti ti = { .val = a };
> +
> +	if (!shift)
> +		return ti.val;
> +	if (shift < 64) {
> +		ti.low = (ti.low >> shift) | (ti.high << (64 - shift));
> +		ti.high >>= shift;
> +	} else {
> +		ti.low = ti.high >> (shift - 64);
> +		ti.high = 0;
> +	}
> +	return ti.val;
> +}
> +EXPORT_SYMBOL(__lshrti3);


  parent reply	other threads:[~2026-10-12 12:37 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-05 19:29 [PATCH v2 00/22] alpha: catch up on architecture Kconfig options Matt Turner
2026-09-05 19:29 ` [PATCH v2 01/22] alpha: enter hardirq context before looking up the irq descriptor Matt Turner
2026-09-06  8:53   ` Magnus Lindholm
2026-09-05 19:29 ` [PATCH v2 02/22] alpha: select ARCH_HAS_UBSAN Matt Turner
2026-09-05 19:29 ` [PATCH v2 03/22] alpha: select ARCH_HAS_GCOV_PROFILE_ALL Matt Turner
2026-09-05 19:29 ` [PATCH v2 04/22] alpha: select HAVE_DEBUG_KMEMLEAK Matt Turner
2026-09-05 19:29 ` [PATCH v2 05/22] alpha: select EDAC_SUPPORT Matt Turner
2026-09-05 19:29 ` [PATCH v2 06/22] alpha: select ARCH_SUPPORTS_ATOMIC_RMW Matt Turner
2026-09-05 19:29 ` [PATCH v2 07/22] alpha: select ARCH_HAS_FAST_MULTIPLIER Matt Turner
2026-09-05 19:29 ` [PATCH v2 08/22] alpha: select ARCH_SUPPORTS_INT128 Matt Turner
2026-10-09  6:52   ` Magnus Lindholm
2026-10-10  2:20     ` [PATCH] alpha: add 128-bit shift helpers Matt Turner
2026-10-10 15:55       ` Magnus Lindholm
2026-10-12 12:37       ` David Laight [this message]
2026-10-13  3:24         ` Matt Turner
2026-09-05 19:29 ` [PATCH v2 09/22] alpha: select HAVE_ARCH_COMPILER_H Matt Turner
2026-09-05 19:29 ` [PATCH v2 10/22] alpha: select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE Matt Turner
2026-09-05 19:29 ` [PATCH v2 11/22] alpha: add HAVE_ARCH_THREAD_STRUCT_WHITELIST support Matt Turner
2026-09-05 19:29 ` [PATCH v2 12/22] alpha: add ARCH_HAS_PTE_SPECIAL support Matt Turner
2026-09-05 19:29 ` [PATCH v2 13/22] alpha: add ARCH_SUPPORTS_PAGE_TABLE_CHECK support Matt Turner
2026-09-06 19:53   ` Magnus Lindholm
2026-09-05 19:29 ` [PATCH v2 14/22] alpha: select ARCH_HAS_DEBUG_VM_PGTABLE Matt Turner
2026-09-05 19:29 ` [PATCH v2 15/22] alpha: select HAVE_GUP_FAST Matt Turner
2026-09-05 19:29 ` [PATCH v2 16/22] alpha: select ARCH_USE_MEMTEST Matt Turner
2026-09-05 19:29 ` [PATCH v2 17/22] alpha: select SYSCTL_EXCEPTION_TRACE Matt Turner
2026-09-05 19:29 ` [PATCH v2 18/22] alpha: discard .eh_frame and the relocation sections Matt Turner
2026-09-05 19:29 ` [PATCH v2 19/22] alpha: select ARCH_WANT_LD_ORPHAN_WARN Matt Turner
2026-09-05 19:29 ` [PATCH v2 20/22] alpha: select ARCH_WANT_IRQS_OFF_ACTIVATE_MM Matt Turner
2026-09-05 19:29 ` [PATCH v2 21/22] alpha: add HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP support Matt Turner
2026-09-06 20:44   ` Magnus Lindholm
2026-09-06 20:48     ` Matt Turner
2026-09-05 19:29 ` [PATCH v2 22/22] alpha: select SPARSE_IRQ Matt Turner
2026-09-06 21:18 ` [PATCH v2 00/22] alpha: catch up on architecture Kconfig options Magnus Lindholm

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=20261012133753.3a5868e9@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=linmag7@gmail.com \
    --cc=linux-alpha@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=mattst88@gmail.com \
    --cc=oe-kbuild-all@lists.linux.dev \
    --cc=richard.henderson@linaro.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

all inboxes | Powered by JetHome®