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

Maciej W. Rozycki macro@orcam.me.uk
Wed Mar 11 23:53:04 GMT 2026


On Thu, 12 Mar 2026, Alan Modra wrote:

> > 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.

 That's not a regression, neither old code did handle cases where bits to 
be sign-extended into are nonzero.  I do believe all call sites take this 
into account, either by masking at the exact invocation place or earlier 
on, e.g. by fetching only the relevant bits from the source.

 We could add explicit masking in the function, but I'm not sure if that 
wouldn't pessimise code.  Perhaps worth investigating.  NB in my setup, 
i.e. `powerpc64le-linux' the function was already always inlined anyway.

  Maciej


More information about the Binutils mailing list