mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Linux Kernel <linux-kernel@vger.kernel.org>,
	Michal Nazarewicz <mina86@mina86.com>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	john.stultz@linaro.org,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Ingo Molnar <mingo@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree
Date: Wed, 23 Sep 2015 13:04:15 -0700	[thread overview]
Message-ID: <20150923130415.561f5f13ed8fa64e2c79e2f2@linux-foundation.org> (raw)
In-Reply-To: <CACVxJT_0KRtZyhsUh9P6SisQ2qwDtWki0xQW-RsGzHnobq0R4Q@mail.gmail.com>

On Wed, 23 Sep 2015 16:44:29 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote:

> On Fri, Sep 18, 2015 at 12:36 AM,  <akpm@linux-foundation.org> wrote:
> 
> 
> > -#define abs(x) ({                                              \
> > -               long ret;                                       \
> > -               if (sizeof(x) == sizeof(long)) {                \
> > -                       long __x = (x);                         \
> > -                       ret = (__x < 0) ? -__x : __x;           \
> > -               } else {                                        \
> > -                       int __x = (x);                          \
> > -                       ret = (__x < 0) ? -__x : __x;           \
> > -               }                                               \
> > -               ret;                                            \
> > -       })
> > +#define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(s64), ({      \
> > +               s64 __x = (x);                                          \
> > +               (__x < 0) ? -__x : __x;                                 \
> > +       }), ({                                                          \
> > +               long ret;                                               \
> > +               if (sizeof(x) == sizeof(long)) {                        \
> > +                       long __x = (x);                                 \
> > +                       ret = (__x < 0) ? -__x : __x;                   \
> > +               } else {                                                \
> > +                       int __x = (x);                                  \
> > +                       ret = (__x < 0) ? -__x : __x;                   \
> > +               }                                                       \
> > +               ret;                                                    \
> > +       }))
> 
> 1. "char" should be banned, because it's signedness is not well defined.
> 2. there is unnecessary expansion to long.
> 
> I've sent kabs() before which didn't go in because it didn't work for
> INT_MAX et al
> (don't worry, this abs() doens't as well) but it is nicer that this
> version in other aspects
> (hopefully).
> 
> [PATCH v2] Add kabs()
> http://marc.info/?l=linux-kernel&m=133518745522740&w=4

Looks nice.  Care to send along a patch which updates the abs() in
linux-next?

> As for INT_MIN, it pretty impossible to make compiler to not generate a branch
> despite generating correct result without a branch.

  reply	other threads:[~2015-09-23 20:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <55fb3266.yHuY6+DaYTJdIL75%akpm@linux-foundation.org>
2015-09-23 13:44 ` Alexey Dobriyan
2015-09-23 20:04   ` Andrew Morton [this message]
2015-09-24 13:32   ` Michal Nazarewicz
2015-09-24 14:26     ` Alexey Dobriyan
2015-09-24 18:03   ` Linus Torvalds
2015-09-25 16:36     ` Michal Nazarewicz

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=20150923130415.561f5f13ed8fa64e2c79e2f2@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=adobriyan@gmail.com \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mina86@mina86.com \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=torvalds@linux-foundation.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

Powered by JetHome