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

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 +--<>--<>-----ooO--(_)--Ooo--

 \ /
  Last update: 2015-09-25 19:01    [W:0.079 / U:1.896 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site