All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@domain.hid>
To: Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>
Cc: xenomai-core <xenomai@xenomai.org>
Subject: Re: [Xenomai-core] [rfc] Jumpless *llimd.
Date: Sun, 19 Jul 2009 10:15:04 +0200	[thread overview]
Message-ID: <4A62D608.2090802@domain.hid> (raw)
In-Reply-To: <4A60E4FF.9020307@domain.hid>

[-- Attachment #1: Type: text/plain, Size: 3423 bytes --]

Gilles Chanteperdrix wrote:
> 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).
> 

The approach is generally fine, but you have to take measures to avoid
using cmove on CPUs that don't support it. They may be still around
(older VIA C3 e.g.).

Jan


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 257 bytes --]

  reply	other threads:[~2009-07-19  8:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-17 20:54 [Xenomai-core] [rfc] Jumpless *llimd Gilles Chanteperdrix
2009-07-19  8:15 ` Jan Kiszka [this message]
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=4A62D608.2090802@domain.hid \
    --to=jan.kiszka@domain.hid \
    --cc=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.