lkml.org 
[lkml]   [2015]   [Sep]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [RFC][PATCH 0/5] Fixes for abs() usage on 64bit values
Hello,

On Mon, Sep 14, 2015 at 08:27:08PM -0700, John Stultz wrote:
> Yea. The above make sense to me, but I suspect there's some very
> subtle reason for the existing separated logic.
> But I'd have to defer to akpm for hints on that.

Hmmm... people could be using it for calculating the distance between
two unsigned values and in that case the original behavior would be
the correct one. e.g.

unsigned diff, a, b;
diff = abs(a - b);

u8 and u16 being treated differently from u32 and u64 makes sense too
because u6 and u16 are always casted to unsigned for calculations
anyway. Maintaining the current behavior and combining the two isn't
difficult tho. Sth like the following could work.

#define abs(x) \
({ \
typeof(x) __ret; \
if (sizeof(x) <= sizeof(s32)) { \
s32 __x = (x); \
__ret = __x < 0 ? -__x : __x; \
} else { \
s64 __x = (x); \
__ret = __x < 0 ? -__x : __x; \
}
__ret;
})

It might trigger some printf format warnings due to the change in
return type but I think the end results would be the same as the
combination of the current abs() and abs64().

Anyways, let's please get abs() working for all types, one way or the
other.

Thanks.

--
tejun


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