setjmp: optimize x86-64 longjmp prologues

Submitted by Alexander Monakov on Aug. 11, 2020, 11:10 p.m.

Details

Message ID 20200811231008.8896-1-amonakov@ispras.ru
State New
Series "Series without cover letter"
Headers show

Commit Message

Alexander Monakov Aug. 11, 2020, 11:10 p.m.
Use a branchless sequence that is one byte shorter as well.
---
 src/setjmp/x32/longjmp.s    | 9 ++++-----
 src/setjmp/x86_64/longjmp.s | 9 ++++-----
 2 files changed, 8 insertions(+), 10 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/setjmp/x32/longjmp.s b/src/setjmp/x32/longjmp.s
index bb88afa1..c8ad86fa 100644
--- a/src/setjmp/x32/longjmp.s
+++ b/src/setjmp/x32/longjmp.s
@@ -5,11 +5,10 @@ 
 .type longjmp,@function
 _longjmp:
 longjmp:
-	mov %esi,%eax           /* val will be longjmp return */
-	test %esi,%esi
-	jnz 1f
-	inc %eax                /* if val==0, val=1 per longjmp semantics */
-1:
+	xor %eax,%eax
+	cmp %esi,%eax           /* CF==0 iff val==0 */
+	cmc                     /* CF = !val        */
+	adc %esi,%eax           /* eax = val + !val */
 	mov (%rdi),%rbx         /* rdi is the jmp_buf, restore regs from it */
 	mov 8(%rdi),%rbp
 	mov 16(%rdi),%r12
diff --git a/src/setjmp/x86_64/longjmp.s b/src/setjmp/x86_64/longjmp.s
index bb88afa1..c8ad86fa 100644
--- a/src/setjmp/x86_64/longjmp.s
+++ b/src/setjmp/x86_64/longjmp.s
@@ -5,11 +5,10 @@ 
 .type longjmp,@function
 _longjmp:
 longjmp:
-	mov %esi,%eax           /* val will be longjmp return */
-	test %esi,%esi
-	jnz 1f
-	inc %eax                /* if val==0, val=1 per longjmp semantics */
-1:
+	xor %eax,%eax
+	cmp %esi,%eax           /* CF==0 iff val==0 */
+	cmc                     /* CF = !val        */
+	adc %esi,%eax           /* eax = val + !val */
 	mov (%rdi),%rbx         /* rdi is the jmp_buf, restore regs from it */
 	mov 8(%rdi),%rbp
 	mov 16(%rdi),%r12