All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>
To: xenomai-core <xenomai@xenomai.org>
Subject: [Xenomai-core] [rfc] Jumpless *llimd.
Date: Fri, 17 Jul 2009 22:54:23 +0200	[thread overview]
Message-ID: <4A60E4FF.9020307@domain.hid> (raw)


Hi,

we discussed this issue several times already I believe: it would be 
fine if llimd (and nodiv_llimd) could work without jumps. 32 bits 
compiler are unable to generate code without jumps for the following 
sequence:

union u64 {
	long long ll;
	unsigned l, h;
};

long long llimd(union u64 x, unsigned m, unsigned d)
{
	unsigned s = x.h & 0x80000000;
	if (s)
		x.ll = -x.ll;
	x.ll = ullimd(x.ll, m, d);
	if (s)
		x.ll = -x.ll;
}

even though this works for x86_64 compiler.

So, I thought, we might help a bit with inline assembly (after all, 
ullimd is already inline assembly). For instance, we could define macros
with the following semantic:

#define sign_split(s, x)                        \
        s = x.l & (1 << 31);                    \
        if (s)                                  \
                x.ll = -x.ll;

#define sign_apply(s, x)                        \
        if (s)                                  \
                x.ll = -x.ll


Jumpless versions on x86_32, using the cmov instruction, would give us:

#define x86_sign_split(s, x)                                            \
        ({                                                              \
                unsigned tmpl = 0, tmph = 0;                            \
                s = x.h;                                        	\
                asm ("sub %[tmpl], %[xl]\n\t"                           \
                     "sbb %[tmph], %[xh]\n\t"                           \
                     "andl $0x80000000, %[s]\n\t"                       \
                     "cmovnz %[tmpl], %[xl]\n\t"                        \
                     "cmovnz %[tmph], %[xh]\n\n"                        \
                     : [s]"+m"(s), [tmph]"+rm?"(tmph), [tmpl]"+rm?"(tmpl), \
                       [xh]"=r"(x.h), [xl]"=r"(x.l));                 	\
         })

#define x86_sign_apply(s, x)                                            \
        ({                                                              \
                unsigned tmpl = 0, tmph = 0;                            \
                asm ("sub %[tmpl], %[xl]\n\t"                           \
                     "sbb %[tmph], %[xh]\n\t"                           \
                     "cmpl $0x80000000, %[s]\n\t" 			\
                     "cmove %[tmpl], %[xl]\n\t"                 	\
                     "cmove %[tmph], %[xh]\n\n"                 	\
                     : [tmph]"+rm?"(tmph), [tmpl]"+rm?"(tmpl), 		\
                       [xh]"=r"(x.h), [xl]"=r"(x.l)                     \
                     : [s]"m"(s));                      		\
         })

What do you think? I am out of my mind? Would you see llimd defined
locally in each asm/arith.h using these macros? Or should we make this 
yet another macro defined by asm/arith.h and used by 
asm-generic/arith.h? 

Note that on ARM, the inline assembly would be shorter (maybe there are
shorter solutions on x86_32, but as usual, they are probably not natural).

-- 
					    Gilles.


             reply	other threads:[~2009-07-17 20:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-17 20:54 Gilles Chanteperdrix [this message]
2009-07-19  8:15 ` [Xenomai-core] [rfc] Jumpless *llimd Jan Kiszka
2009-07-19 13:47   ` Gilles Chanteperdrix

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=4A60E4FF.9020307@domain.hid \
    --to=gilles.chanteperdrix@xenomai.org \
    --cc=xenomai@xenomai.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.