* [KJ] [RFC] Regarding abs() and labs()
@ 2007-02-03 1:13 Richard Knutsson
2007-02-03 1:44 ` Randy Dunlap
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Richard Knutsson @ 2007-02-03 1:13 UTC (permalink / raw)
To: kernel-janitors
Hello all
Just took a look at abs() and can not understand why it seem so
overcomplicated. Why not just:
#define abs(x) ((x) < 0) ? -(x) : (x))
and skip labs() (not used anyway)
+ it gives a warning with -W if x is of a unsigned type ("condition is
always false" or similar).
But remember to use %u when printing an unsigned, took me a while before
I understood how an unsigned could contain a "negative" value :)
Happy weekend
Richard Knutsson
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [KJ] [RFC] Regarding abs() and labs()
2007-02-03 1:13 [KJ] [RFC] Regarding abs() and labs() Richard Knutsson
@ 2007-02-03 1:44 ` Randy Dunlap
2007-02-03 2:22 ` Richard Knutsson
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Randy Dunlap @ 2007-02-03 1:44 UTC (permalink / raw)
To: kernel-janitors
On Sat, 03 Feb 2007 02:13:44 +0100 Richard Knutsson wrote:
> Hello all
>
> Just took a look at abs() and can not understand why it seem so
> overcomplicated. Why not just:
>
> #define abs(x) ((x) < 0) ? -(x) : (x))
It's almost that simple. It just adds a local __x so that
'x' won't be evaluated multiple times as it would in the simpler
version. Multiple evaluations (of an expression) (that could
have side effects) would take more CPU horsepower and could
have bad side effects as well.
> and skip labs() (not used anyway)
> + it gives a warning with -W if x is of a unsigned type ("condition is
> always false" or similar).
> But remember to use %u when printing an unsigned, took me a while before
> I understood how an unsigned could contain a "negative" value :)
---
~Randy
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [KJ] [RFC] Regarding abs() and labs()
2007-02-03 1:13 [KJ] [RFC] Regarding abs() and labs() Richard Knutsson
2007-02-03 1:44 ` Randy Dunlap
@ 2007-02-03 2:22 ` Richard Knutsson
2007-02-03 2:34 ` Randy Dunlap
2007-02-03 2:52 ` Richard Knutsson
3 siblings, 0 replies; 5+ messages in thread
From: Richard Knutsson @ 2007-02-03 2:22 UTC (permalink / raw)
To: kernel-janitors
Randy Dunlap wrote:
> On Sat, 03 Feb 2007 02:13:44 +0100 Richard Knutsson wrote:
>
>
>> Hello all
>>
>> Just took a look at abs() and can not understand why it seem so
>> overcomplicated. Why not just:
>>
>> #define abs(x) ((x) < 0) ? -(x) : (x))
>>
>
> It's almost that simple. It just adds a local __x so that
> 'x' won't be evaluated multiple times as it would in the simpler
> version. Multiple evaluations (of an expression) (that could
> have side effects) would take more CPU horsepower and could
> have bad side effects as well.
>
You mean if x is ex a + b? In such case; why not use typeof instead of int?
What kind of side-effects are you talking about?
Richard Knutsson
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [KJ] [RFC] Regarding abs() and labs()
2007-02-03 1:13 [KJ] [RFC] Regarding abs() and labs() Richard Knutsson
2007-02-03 1:44 ` Randy Dunlap
2007-02-03 2:22 ` Richard Knutsson
@ 2007-02-03 2:34 ` Randy Dunlap
2007-02-03 2:52 ` Richard Knutsson
3 siblings, 0 replies; 5+ messages in thread
From: Randy Dunlap @ 2007-02-03 2:34 UTC (permalink / raw)
To: kernel-janitors
On Sat, 03 Feb 2007 03:22:29 +0100 Richard Knutsson wrote:
> Randy Dunlap wrote:
> > On Sat, 03 Feb 2007 02:13:44 +0100 Richard Knutsson wrote:
> >
> >
> >> Hello all
> >>
> >> Just took a look at abs() and can not understand why it seem so
> >> overcomplicated. Why not just:
> >>
> >> #define abs(x) ((x) < 0) ? -(x) : (x))
> >>
> >
> > It's almost that simple. It just adds a local __x so that
> > 'x' won't be evaluated multiple times as it would in the simpler
> > version. Multiple evaluations (of an expression) (that could
> > have side effects) would take more CPU horsepower and could
> > have bad side effects as well.
> >
> You mean if x is ex a + b? In such case; why not use typeof instead of int?
I suppose that would make sense if we have a need for abs(various types),
but until we do, just using: int __x; is good enough.
> What kind of side-effects are you talking about?
The usual kind that affect computer software. E.g.:
a = abs(var1++);
a = abs(fseek(File, -offset, whence));
but I don't want to limit the possibilities of side effects.
---
~Randy
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [KJ] [RFC] Regarding abs() and labs()
2007-02-03 1:13 [KJ] [RFC] Regarding abs() and labs() Richard Knutsson
` (2 preceding siblings ...)
2007-02-03 2:34 ` Randy Dunlap
@ 2007-02-03 2:52 ` Richard Knutsson
3 siblings, 0 replies; 5+ messages in thread
From: Richard Knutsson @ 2007-02-03 2:52 UTC (permalink / raw)
To: kernel-janitors
Randy Dunlap wrote:
> On Sat, 03 Feb 2007 03:22:29 +0100 Richard Knutsson wrote:
>
>
>> Randy Dunlap wrote:
>>
>>> On Sat, 03 Feb 2007 02:13:44 +0100 Richard Knutsson wrote:
>>>
>>>
>>>
>>>> Hello all
>>>>
>>>> Just took a look at abs() and can not understand why it seem so
>>>> overcomplicated. Why not just:
>>>>
>>>> #define abs(x) ((x) < 0) ? -(x) : (x))
>>>>
>>>>
>>> It's almost that simple. It just adds a local __x so that
>>> 'x' won't be evaluated multiple times as it would in the simpler
>>> version. Multiple evaluations (of an expression) (that could
>>> have side effects) would take more CPU horsepower and could
>>> have bad side effects as well.
>>>
>>>
>> You mean if x is ex a + b? In such case; why not use typeof instead of int?
>>
>
> I suppose that would make sense if we have a need for abs(various types),
> but until we do, just using: int __x; is good enough.
>
Mm, true. Don't mess with something that is not broken (if not for the
fun of it).
But that really leaves labs() hanging. I guess it was added because it
is in gcc's stdlib.h.
>> What kind of side-effects are you talking about?
>>
>
> The usual kind that affect computer software. E.g.:
>
> a = abs(var1++);
>
Oh thanks, good "eye-opener" :)
Thanks for clearing things up
Richard Knutsson
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-02-03 2:52 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-02-03 1:13 [KJ] [RFC] Regarding abs() and labs() Richard Knutsson
2007-02-03 1:44 ` Randy Dunlap
2007-02-03 2:22 ` Richard Knutsson
2007-02-03 2:34 ` Randy Dunlap
2007-02-03 2:52 ` Richard Knutsson
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.