mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Randy Dunlap <rdunlap@xenotime.net>
To: Alexey Dobriyan <adobriyan@gmail.com>
Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org,
	behlendorf1@llnl.gov, oleg@redhat.com
Subject: Re: [PATCH] remove abs64()
Date: Tue, 12 Apr 2011 14:07:26 -0700	[thread overview]
Message-ID: <20110412140726.3f4f2a7b.rdunlap@xenotime.net> (raw)
In-Reply-To: <20110412210045.GA19901@p183>

On Wed, 13 Apr 2011 00:00:45 +0300 Alexey Dobriyan wrote:

> We don't need no stinking abs64() given some GCC extensions
> (especially __builtin_choose_expr()).
> 
> One abs() implementation is better than two abs() implementations.

questionable.

> New abs() doesn't expand type needlessly.
> 
> Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
> ---
> 
>  drivers/gpu/drm/drm_irq.c |    4 ++--
>  include/linux/kernel.h    |   43 +++++++++++++++++++++----------------------
>  lib/div64.c               |    2 +-
>  3 files changed, 24 insertions(+), 25 deletions(-)


> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -143,28 +143,27 @@ extern int _cond_resched(void);
>  
>  #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
>  
> -/*
> - * abs() handles unsigned and signed longs, ints, shorts and chars.  For all
> - * input types abs() returns a signed long.
> - * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
> - * for those.
> - */
> -#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 abs64(x) ({				\
> -		s64 __x = (x);			\
> -		(__x < 0) ? -__x : __x;		\
> -	})
> +#define abs(x)								\
> +({									\
> +	typeof(x) _x = (x);						\
> +									\
> +	__builtin_choose_expr(						\
> +		__builtin_types_compatible_p(typeof(_x), signed char),	\
> +		(unsigned char)({ _x < 0 ? -_x : _x; }),		\
> +	__builtin_choose_expr(						\
> +		__builtin_types_compatible_p(typeof(_x), short),	\
> +		(unsigned short)({ _x < 0 ? -_x : _x; }),		\
> +	__builtin_choose_expr(						\
> +		__builtin_types_compatible_p(typeof(_x), int),		\
> +		(unsigned int)({ _x < 0 ? -_x : _x; }),			\
> +	__builtin_choose_expr(						\
> +		__builtin_types_compatible_p(typeof(_x), long),		\
> +		(unsigned long)({ _x < 0 ? -_x : _x; }),		\
> +	__builtin_choose_expr(						\
> +		__builtin_types_compatible_p(typeof(_x), long long),	\
> +		(unsigned long long)({ _x < 0 ? -_x : _x; }),		\
> +	_x)))));							\
> +})

that is better?

---
~Randy

  reply	other threads:[~2011-04-12 21:07 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-12 21:00 Alexey Dobriyan
2011-04-12 21:07 ` Randy Dunlap [this message]
2011-04-12 21:10   ` Andrew Morton
2011-04-12 21:16     ` Alexey Dobriyan
2011-04-12 21:33       ` Andrew Morton
2011-04-12 21:40         ` Randy Dunlap
2011-04-12 21:14   ` Alexey Dobriyan
2011-04-13 14:27 ` Oleg Nesterov
2011-04-13 18:48   ` Andrew Morton
2011-04-13 20:20     ` Oleg Nesterov
2011-04-13 20:26       ` Andrew Morton
2011-04-13 20:36         ` Alexey Dobriyan

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=20110412140726.3f4f2a7b.rdunlap@xenotime.net \
    --to=rdunlap@xenotime.net \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=behlendorf1@llnl.gov \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    /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

all inboxes | Powered by JetHome®