Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1231452

Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree

From Alexey Dobriyan <adobriyan@gmail.com>
Newsgroups linux.kernel
Subject Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree
Date 2015-09-23 15:50 +0200
Message-ID <qbSAP-5Me-29@gated-at.bofh.it> (permalink)
References <qbSAP-5Me-31@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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

As for INT_MIN, it pretty impossible to make compiler to not generate a branch
despite generating correct result without a branch.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree Alexey Dobriyan <adobriyan@gmail.com> - 2015-09-23 15:50 +0200
  Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm  tree Andrew Morton <akpm@linux-foundation.org> - 2015-09-23 22:10 +0200
  Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree Michal Nazarewicz <mina86@mina86.com> - 2015-09-24 15:40 +0200
    Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree Alexey Dobriyan <adobriyan@gmail.com> - 2015-09-24 16:30 +0200
  Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree Linus Torvalds <torvalds@linux-foundation.org> - 2015-09-24 20:10 +0200
    Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree Michal Nazarewicz <mina86@mina86.com> - 2015-09-25 18:40 +0200

csiph-web