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

"Maciej W. Rozycki via Binutils-cvs" <[email protected]>
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=822ac59036ffb2adc64f8833766cab9aa9bc28bc

commit 822ac59036ffb2adc64f8833766cab9aa9bc28bc
Author: Maciej W. Rozycki <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.