9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] [PATCH] branchless assembly abs() and labs() for amd64
@ 2020-12-25 16:36 Kemal
  2020-12-25 18:13 ` ori
  0 siblings, 1 reply; 6+ messages in thread
From: Kemal @ 2020-12-25 16:36 UTC (permalink / raw)
  To: 9front

hello,

i remembered a method to do branchless abs.
can i send it as a patch?

regards.

diff -r d9e940a768d1 sys/src/libc/amd64/abs.s
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/src/libc/amd64/abs.s Fri Dec 25 15:54:42 2020 +0300
@@ -0,0 +1,16 @@
+/*
+this method of abs works this way:
+first it gets the sign bit and extends it with cdq
+sign bit is the most significant bit of an integer showing it's sign
+lets say we have a decimal number -127. according to two's
complement, this is "10000001" in binary. the sign bit here is the
first "1" cdq gets the ax's
+sign bit and extends it like "11111111" then puts it to dx. now we are going to
+do a little math trick here. if we xor dx and ax we will have
"01111110" which is 126. according to two's complement, anything goes
like "11...11" is -1, so to recover the missing 1 we can substract dx
from ax which will
+result in ax-(-1) which would be ax+1. we have 127, the abs value.
+i don't remember the source where i got this method from so i can't
cite them :-(
+*/
+TEXT abs(SB),$0
+ MOVL RARG, AX
+ CDQ
+ XORL DX, AX
+ SUBL DX, AX
+ RET
diff -r d9e940a768d1 sys/src/libc/amd64/labs.s
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/src/libc/amd64/labs.s Fri Dec 25 15:54:42 2020 +0300
@@ -0,0 +1,7 @@
+/* check abs.s for explanation */
+TEXT labs(SB),$0
+ MOVL RARG, AX
+ CDQ
+ XORL DX, AX
+ SUBL DX, AX
+ RET
diff -r d9e940a768d1 sys/src/libc/amd64/mkfile
--- a/sys/src/libc/amd64/mkfile Mon Oct 19 01:20:29 2020 +0200
+++ b/sys/src/libc/amd64/mkfile Fri Dec 25 15:54:42 2020 +0300
@@ -3,10 +3,12 @@

 LIB=/$objtype/lib/libc.a
 SFILES=\
+ abs.s\
  argv0.s\
  atom.s\
  cycles.s\
  getfcr.s\
+ labs.s\
  main9.s\
  main9p.s\
  memccpy.s\

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-12-26 14:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-25 16:36 [9front] [PATCH] branchless assembly abs() and labs() for amd64 Kemal
2020-12-25 18:13 ` ori
2020-12-25 18:28   ` boehm.igor
2020-12-25 22:30     ` Kemal
2020-12-26  1:38       ` ori
2020-12-26 14:12         ` Kemal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).