[committed] MIPS/BFD: Use branchless code for sign-extension

Alan Modra amodra@gmail.com
Wed Mar 11 22:15:22 GMT 2026


On Wed, Mar 11, 2026 at 04:32:17PM +0000, Maciej W. Rozycki wrote:
> Use common branchless code for `_bfd_mips_elf_sign_extend', typically 
> avoiding the branch penalty and also reducing code size when inlined in 
> elfxx-mips.c.
> ---
>  bfd/elfxx-mips.c |    7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> binutils-bfd-mips-elf-sign-extend.diff
> Index: binutils-gdb/bfd/elfxx-mips.c
> ===================================================================
> --- binutils-gdb.orig/bfd/elfxx-mips.c
> +++ binutils-gdb/bfd/elfxx-mips.c
> @@ -5239,11 +5239,8 @@ mips_elf_local_relocation_p (bfd *input_
>  bfd_vma
>  _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
>  {
> -  if (value & ((bfd_vma) 1 << (bits - 1)))
> -    /* VALUE is negative.  */
> -    value |= ((bfd_vma) - 1) << bits;
> -
> -  return value;
> +  bfd_vma sign = (bfd_vma) 1 << (bits - 1);
> +  return (value ^ sign) - sign;
>  }
>  
>  /* Return non-zero if the indicated VALUE has overflowed the maximum

Is there no possibility that the value being sign extended has
non-zero bits above the sign bit?  Some calls to
_bfd_mips_elf_sign_extend mask the value.  Many don't.

-- 
Alan Modra


More information about the Binutils mailing list