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

From: Tejun Heo
Date: Mon Sep 14 2015 - 23:46:41 EST


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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/