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

Maciej W. Rozycki macro@sourceware.org
Wed Mar 11 16:32:09 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=822ac59036ffb2adc64f8833766cab9aa9bc28bc

commit 822ac59036ffb2adc64f8833766cab9aa9bc28bc
Author: Maciej W. Rozycki <macro@orcam.me.uk>
Date:   Wed Mar 11 16:30:29 2026 +0000

    MIPS/BFD: Use branchless code for sign-extension
    
    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.

Diff:
---
 bfd/elfxx-mips.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c
index 6feaa706029..24a08436d05 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -5239,11 +5239,8 @@ mips_elf_local_relocation_p (bfd *input_bfd,
 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


More information about the Binutils-cvs mailing list