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


Groups > linux.kernel > #1225572

Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values

From Michal Nazarewicz <mina86@mina86.com>
Newsgroups linux.kernel
Subject Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values
Date 2015-09-16 01:00 +0200
Message-ID <q97mG-2Ru-1@gated-at.bofh.it> (permalink)
References <q8MUV-6nq-5@gated-at.bofh.it> <q8NHj-7hi-3@gated-at.bofh.it> <q8P6q-136-7@gated-at.bofh.it> <q8PpM-1pA-11@gated-at.bofh.it> <q95XB-UJ-31@gated-at.bofh.it>
Organization http://mina86.com/

Show all headers | View raw


On Tue, Sep 15 2015, Andrew Morton wrote:
> On Mon, 14 Sep 2015 23:46:32 -0400 Tejun Heo <tj@kernel.org> wrote:
>
>> Anyways, let's please get abs() working for all types, one way or the
>> other.
>
> That would be by far the best solution, of course.
>
> This seems to work OK:
>
> --- a/include/linux/kernel.h~a
> +++ a/include/linux/kernel.h
> @@ -207,8 +207,11 @@ extern int _cond_resched(void);
>   * for those.
>   */
>  #define abs(x) ({						\
> -		long ret;					\
> -		if (sizeof(x) == sizeof(long)) {		\
> +		s64 ret;					\
> +		if (sizeof(x) == sizeof(s64)) {			\
> +			s64 __x = (x);				\
> +			ret = (__x < 0) ? -__x : __x;		\
> +		} else if (sizeof(x) == sizeof(long)) {		\
>  			long __x = (x);				\
>  			ret = (__x < 0) ? -__x : __x;		\
>  		} else {					\

If the return type is an issue, we can use __builtin_choose_expr, no?

#define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(s64), abs64(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;						\
	}))

This is awkward but will make even printk happy.

>
> Test case:
>
> --- /dev/null
> +++ a/lib/xx.c
> @@ -0,0 +1,33 @@
> +#include <linux/kernel.h>
> +
> +#define newabs(x) ({						\
> +		s64 ret;					\
> +		if (sizeof(x) == sizeof(s64)) {			\
> +			s64 __x = (x);				\
> +			ret = (__x < 0) ? -__x : __x;		\
> +		} else if (sizeof(x) == sizeof(long)) {		\
> +			long __x = (x);				\
> +			ret = (__x < 0) ? -__x : __x;		\
> +		} else {					\
> +			int __x = (x);				\
> +			ret = (__x < 0) ? -__x : __x;		\
> +		}						\
> +		ret;						\
> +	})
> +
> +#define oldabs(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;						\
> +	})
> +
> +int foo(int x)
> +{
> +	return oldabs(x);
> +}
> diff -puN lib/Makefile~b lib/Makefile
> --- a/lib/Makefile~b
> +++ a/lib/Makefile
> @@ -13,7 +13,7 @@ lib-y := ctype.o string.o vsprintf.o cmd
>  	 sha1.o md5.o irq_regs.o argv_split.o \
>  	 proportions.o flex_proportions.o ratelimit.o show_mem.o \
>  	 is_single_threaded.o plist.o decompress.o kobject_uevent.o \
> -	 earlycpio.o seq_buf.o nmi_backtrace.o
> +	 earlycpio.o seq_buf.o nmi_backtrace.o xx.o
>  
>  obj-$(CONFIG_ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS) += usercopy.o
>  lib-$(CONFIG_MMU) += ioremap.o
>
>
> on i386, xx.o's text is 68 bytes with either newabs() or oldabs().
>
>
> lib/percpu_counter.o's text does get larger with newabs().  That's
> because __percpu_counter_compare() is doing abs() on an s64, doh.
>

-- 
Best regards,                                            _     _
.o. | Liege of Serenely Enlightened Majesty of         o' \,=./ `o
..o | Computer Science,  ミハウ “mina86” ナザレヴイツ  (o o)
ooo +--<mpn@google.com>--<xmpp:mina86@jabber.org>-----ooO--(_)--Ooo--
--
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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[RFC][PATCH 0/5] Fixes for abs() usage on 64bit values John Stultz <john.stultz@linaro.org> - 2015-09-15 03:10 +0200
  [RFC][PATCH 1/5] clocksource: Fix abs() usage w/ 64bit values John Stultz <john.stultz@linaro.org> - 2015-09-15 03:10 +0200
  [RFC][PATCH 2/5] time: Fix abs() usage with 64-bit values. John Stultz <john.stultz@linaro.org> - 2015-09-15 03:10 +0200
  Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values Tejun Heo <tj@kernel.org> - 2015-09-15 04:00 +0200
    Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values John Stultz <john.stultz@linaro.org> - 2015-09-15 05:30 +0200
      Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values Tejun Heo <tj@kernel.org> - 2015-09-15 05:50 +0200
        Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values Jeff Epler <jepler@unpythonic.net> - 2015-09-15 14:50 +0200
        Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values Andrew Morton <akpm@linux-foundation.org> - 2015-09-15 23:30 +0200
          Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values Michal Nazarewicz <mina86@mina86.com> - 2015-09-16 01:00 +0200
      Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values Ingo Molnar <mingo@kernel.org> - 2015-09-15 07:30 +0200
        Re: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values Linus Torvalds <torvalds@linux-foundation.org> - 2015-09-16 01:50 +0200

csiph-web