All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Nazarewicz <mina86@mina86.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
	Alexey Dobriyan <adobriyan@gmail.com>
Cc: Linux Kernel <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	John Stultz <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>
Subject: Re: + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree
Date: Fri, 25 Sep 2015 18:36:01 +0200	[thread overview]
Message-ID: <xa1tr3lm78y6.fsf@mina86.com> (raw)
In-Reply-To: <CA+55aFz8TVP7mwwbx0QVZvW+pybfRQt8-=LG9hzwLzkgeTMUUA@mail.gmail.com>

On Thu, Sep 24 2015, Linus Torvalds wrote:
> One thing that *is* interesting is "what if 'long' and 's64' are the
> same size?" In particular, it means that right now Michal's patch
> *always* returns "long" on a 64-bit architecture, but will return
> "long" or "s64" on a 32-bit one.

That’s not accurate.  s64 is defined as long long so:
- on 64-bit architectures, the macro will return s64 (i.e. long long)
  for long arguments (because sizeof(long) == 8 == sizeof(s64) and the
  first path is taken), but
- on 32-bit architectures, it will return long for long arguments (since
  sizeof(long) == 4 != 8 == sizeof(long long) and the second path is
  taken).
But yes, the point remains, depending on architecture, the macro returns
different type for long arguments.

> The reason that is somewhat interesting is that while the sizes and
> values are the same, and the resulting C type expansions are
> "equivalent" types, i people *print* things, you have to use different
> modifiers for the two cases. So you might get warnings on 32-bit
> architectures and not get them on 64-bit, or vice versa.
>
> However, I don't see a good solution for that. And assuming we don't
> use "abs()" in an expression to printk(), I guess it doesn't much
> matter either.

This should do the trick:

#define abs(x) __builtin_choose_expr(				\
	__builtin_types_compatible_p(typeof(x), s64) ||		\
	__builtin_types_compatible_p(typeof(x), u64),		\
	({ s64 __x = (x); __x < 0 ? -__x : __x; }),		\
	__builtin_choose_expr(sizeof(x) <= sizeof(long), ({	\
		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;						\
	}), (void)(x)))

It’ll return s64 for s64 and u64 (i.e. long long and unsigned long long)
types, long for anything whose sizeof <= sizeof(long) and will bail out
with compile time error if used for any other type (if return value is
used).

I dunno whether added complexity is worth solving the problem though.

-- 
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--

      reply	other threads:[~2015-09-25 16:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-17 21:36 + kernelh-make-abs-work-with-64-bit-types.patch added to -mm tree akpm
2015-09-23 13:44 ` Alexey Dobriyan
2015-09-23 20:04   ` Andrew Morton
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 [this message]

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=xa1tr3lm78y6.fsf@mina86.com \
    --to=mina86@mina86.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.