9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Kemal <kemalinanc8@gmail.com>
To: 9front@9front.org
Subject: [9front] [PATCH] branchless assembly abs() and labs() for amd64
Date: Fri, 25 Dec 2020 19:36:22 +0300	[thread overview]
Message-ID: <CABO6shdLiqQd_L4nSvD6NwN9h56yz7+kabh0Q1q=GSF5yVhu-g@mail.gmail.com> (raw)

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\

             reply	other threads:[~2020-12-25 16:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-25 16:36 Kemal [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABO6shdLiqQd_L4nSvD6NwN9h56yz7+kabh0Q1q=GSF5yVhu-g@mail.gmail.com' \
    --to=kemalinanc8@gmail.com \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).