Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Wed, 12 Aug 2020 02:10:08 +0300
From: Alexander Monakov <amonakov@...ras.ru>
To: musl@...ts.openwall.com
Subject: [PATCH] setjmp: optimize x86-64 longjmp prologues

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(-)

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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.