lkml.org 
[lkml]   [2015]   [Sep]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
SubjectRe: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree
From
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.


\
 
 \ /
  Last update: 2015-09-23 16:01    [W:0.048 / U:0.148 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site