[EXT] [PATCH 1/1] riscv: Add Zbkb optimized repeat_bytes helper

Peter Bergner bergner@tenstorrent.com
Thu Sep 11 17:03:08 GMT 2025


On 9/11/25 10:48 AM, Pincheng Wang wrote:
> +/* Miscellaneous functions used in string implementations.  Generic C version.
> +   Copyright (C) 2023-2025 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.

Others can correct me if I'm wrong, but since this is a new file, I think
you should be using "2025" for the copyright year, rather than "2023-2025".




> +/* Setup an word with each byte being c_in.  For instance, on a 64 bits
> +   machine with input as 0xce the functions returns 0xcececececececece.  */
> +static __always_inline op_t
> +repeat_bytes (unsigned char c_in)
> +{
> +  op_t c = c_in;
> +#if defined __riscv_zbkb && __riscv_xlen == 64
> +  /* 8bit -> 16bit -> 32bit -> 64bit pattern replication */
> +  __asm__ ("packh %0, %1, %1\n\t"
> +           "packw %0, %0, %0\n\t"
> +           "pack %0, %0, %0"
> +           : "=r"(c) : "r"(c));
> +#elif defined __riscv_zbkb && __riscv_xlen == 32
> +  /* 8bit -> 16bit -> 32bit pattern replication */
> +  __asm__ ("packh %0, %1, %1\n\t"
> +           "pack %0, %0, %0"
> +           : "=r"(c) : "r"(c));
> +#else
> +  c = ((op_t)-1 / 0xff) * c_in;
> +#endif
> +  return c;
> +}

It seems this inline asm creates a lot of unneeded data dependencies,
which may affect low end cores???  Maybe rewrite it to remove those using
some temp variables and a separate output variable?

...or maybe people don't think we should care?

Peter




More information about the Libc-alpha mailing list