* [KJ] [RFC] Regarding min/max
@ 2007-02-03 1:23 Richard Knutsson
2007-02-03 1:45 ` Randy Dunlap
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: Richard Knutsson @ 2007-02-03 1:23 UTC (permalink / raw)
To: kernel-janitors
Hi
Regarding converting:
a = x < y ? x : y;
with
a = min(x, y);
and etc.
What about variables in structs? It seem to not like the multiple lines in min()/max(),
so why not introduce something like:
#define __min(x,y) ((x) < (y) ? (x) : (y))
(and use __min() in min() also)? Then there would be no problem to clean up in the structs
+ the underscores should make people aware it does not type-check.
Just a thought
/Richard Knutsson
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [KJ] [RFC] Regarding min/max
2007-02-03 1:23 [KJ] [RFC] Regarding min/max Richard Knutsson
@ 2007-02-03 1:45 ` Randy Dunlap
2007-02-03 7:42 ` Robert P. J. Day
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Randy Dunlap @ 2007-02-03 1:45 UTC (permalink / raw)
To: kernel-janitors
On Sat, 03 Feb 2007 02:23:14 +0100 Richard Knutsson wrote:
> Hi
>
> Regarding converting:
>
> a = x < y ? x : y;
>
> with
> a = min(x, y);
> and etc.
>
> What about variables in structs? It seem to not like the multiple lines in min()/max(),
Can you give an example of this "not like"?
Thanks.
> so why not introduce something like:
> #define __min(x,y) ((x) < (y) ? (x) : (y))
> (and use __min() in min() also)? Then there would be no problem to clean up in the structs
> + the underscores should make people aware it does not type-check.
---
~Randy
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [KJ] [RFC] Regarding min/max
2007-02-03 1:23 [KJ] [RFC] Regarding min/max Richard Knutsson
2007-02-03 1:45 ` Randy Dunlap
@ 2007-02-03 7:42 ` Robert P. J. Day
2007-02-03 8:38 ` Jaco Kroon
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Robert P. J. Day @ 2007-02-03 7:42 UTC (permalink / raw)
To: kernel-janitors
On Sat, 3 Feb 2007, Richard Knutsson wrote:
> Hi
>
> Regarding converting:
>
> a = x < y ? x : y;
>
> with
> a = min(x, y);
> and etc.
>
> What about variables in structs? It seem to not like the multiple
> lines in min()/max(), so why not introduce something like: #define
> __min(x,y) ((x) < (y) ? (x) : (y)) (and use __min() in min() also)?
> Then there would be no problem to clean up in the structs + the
> underscores should make people aware it does not type-check.
>
> Just a thought
> /Richard Knutsson
as hard as it may be to believe, i'm going to sit this one out. :-)
i don't really have any strong opinions on how to implement min/max
routines, but it *is* obvious that there's a lot of that kind of
cleanup that could be done.
rday
--
====================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA
http://www.fsdev.dreamhosters.com/wiki/index.php?title=Main_Page
====================================
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [KJ] [RFC] Regarding min/max
2007-02-03 1:23 [KJ] [RFC] Regarding min/max Richard Knutsson
2007-02-03 1:45 ` Randy Dunlap
2007-02-03 7:42 ` Robert P. J. Day
@ 2007-02-03 8:38 ` Jaco Kroon
2007-02-03 19:01 ` Richard Knutsson
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Jaco Kroon @ 2007-02-03 8:38 UTC (permalink / raw)
To: kernel-janitors
Robert P. J. Day wrote:
> On Sat, 3 Feb 2007, Richard Knutsson wrote:
>>Hi
>>
>>Regarding converting:
>>
>> a = x < y ? x : y;
>>
>>with
>> a = min(x, y);
>>and etc.
>>
>>What about variables in structs? It seem to not like the multiple
>>lines in min()/max(), so why not introduce something like: #define
>>__min(x,y) ((x) < (y) ? (x) : (y)) (and use __min() in min() also)?
>>Then there would be no problem to clean up in the structs + the
>>underscores should make people aware it does not type-check.
>>
>>Just a thought
>>/Richard Knutsson
from include/linux/kernel.h:
/*
* min()/max() macros that also do
* strict type-checking.. See the
* "unnecessary" pointer comparison.
*/
#define min(x,y) ({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
(void) (&_x = &_y); \
_x < _y ? _x : _y; })
#define max(x,y) ({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
(void) (&_x = &_y); \
_x > _y ? _x : _y; })
So one just need to convert to using these :).
Jaco
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [KJ] [RFC] Regarding min/max
2007-02-03 1:23 [KJ] [RFC] Regarding min/max Richard Knutsson
` (2 preceding siblings ...)
2007-02-03 8:38 ` Jaco Kroon
@ 2007-02-03 19:01 ` Richard Knutsson
2007-02-03 20:04 ` Srdjan Todorovic
2007-02-03 20:10 ` Jaco Kroon
5 siblings, 0 replies; 7+ messages in thread
From: Richard Knutsson @ 2007-02-03 19:01 UTC (permalink / raw)
To: kernel-janitors
Jaco Kroon wrote:
> Robert P. J. Day wrote:
>
>> On Sat, 3 Feb 2007, Richard Knutsson wrote:
>>
>>> Hi
>>>
>>> Regarding converting:
>>>
>>> a = x < y ? x : y;
>>>
>>> with
>>> a = min(x, y);
>>> and etc.
>>>
>>> What about variables in structs? It seem to not like the multiple
>>> lines in min()/max(), so why not introduce something like: #define
>>> __min(x,y) ((x) < (y) ? (x) : (y)) (and use __min() in min() also)?
>>> Then there would be no problem to clean up in the structs + the
>>> underscores should make people aware it does not type-check.
>>>
>>> Just a thought
>>> /Richard Knutsson
>>>
>
> from include/linux/kernel.h:
>
> /*
> * min()/max() macros that also do
> * strict type-checking.. See the
> * "unnecessary" pointer comparison.
> */
> #define min(x,y) ({ \
> typeof(x) _x = (x); \
> typeof(y) _y = (y); \
> (void) (&_x = &_y); \
> _x < _y ? _x : _y; })
>
> #define max(x,y) ({ \
> typeof(x) _x = (x); \
> typeof(y) _y = (y); \
> (void) (&_x = &_y); \
> _x > _y ? _x : _y; })
>
> So one just need to convert to using these :).
>
> Jaco
>
Hmm, seems I pushed the "Reply" instead of "Reply all"-button when
answering Randy.
I tried to recreate the compile-failure (was some time ago since it
happened) when using min()/max() in a struct but it happily compiled it
so (thankfully) you are correct, just need to use min()/max(). :)
Richard Knutsson
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [KJ] [RFC] Regarding min/max
2007-02-03 1:23 [KJ] [RFC] Regarding min/max Richard Knutsson
` (3 preceding siblings ...)
2007-02-03 19:01 ` Richard Knutsson
@ 2007-02-03 20:04 ` Srdjan Todorovic
2007-02-03 20:10 ` Jaco Kroon
5 siblings, 0 replies; 7+ messages in thread
From: Srdjan Todorovic @ 2007-02-03 20:04 UTC (permalink / raw)
To: kernel-janitors
Hi,
On Sat, 3 Feb 2007, Jaco Kroon wrote:
> from include/linux/kernel.h:
>
> /*
> * min()/max() macros that also do
> * strict type-checking.. See the
> * "unnecessary" pointer comparison.
> */
> #define min(x,y) ({ \
> typeof(x) _x = (x); \
> typeof(y) _y = (y); \
> (void) (&_x = &_y); \
> _x < _y ? _x : _y; })
I don't quite understand that macro, specificaly the (void) (&_x = &_y); line.
The comment says that this macro does strict type-checking. From what I
can see, it does a comparison on the two automatic variables, but throws
away the result. So thus is essentially a no-op.
If x and y are two different types, the compiler would complain with a
warning/error because there was no cast. Is that the strict type-checking
that the comment mentions, and did I understand this correctly?
Thanks,
Srdjan
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [KJ] [RFC] Regarding min/max
2007-02-03 1:23 [KJ] [RFC] Regarding min/max Richard Knutsson
` (4 preceding siblings ...)
2007-02-03 20:04 ` Srdjan Todorovic
@ 2007-02-03 20:10 ` Jaco Kroon
5 siblings, 0 replies; 7+ messages in thread
From: Jaco Kroon @ 2007-02-03 20:10 UTC (permalink / raw)
To: kernel-janitors
Srdjan Todorovic wrote:
> Hi,
>
> On Sat, 3 Feb 2007, Jaco Kroon wrote:
>
>
>>from include/linux/kernel.h:
>>
>>/*
>> * min()/max() macros that also do
>> * strict type-checking.. See the
>> * "unnecessary" pointer comparison.
>> */
>>#define min(x,y) ({ \
>> typeof(x) _x = (x); \
>> typeof(y) _y = (y); \
>> (void) (&_x = &_y); \
>> _x < _y ? _x : _y; })
>
> I don't quite understand that macro, specificaly the (void) (&_x = &_y); line.
> The comment says that this macro does strict type-checking. From what I
> can see, it does a comparison on the two automatic variables, but throws
> away the result. So thus is essentially a no-op.
Exactly. But it verifies that a pointer to each of the two can be
compared, and that can only happen if the pointer types are identical.
Whilst a int can automatically be "upgraded" to long (for example), int*
and long* are two distinct types. So the pointer comparison is thrown
away, since that really isn't the comparison we're after, then _x < _y
actually compares the data and returns the appropriate one.
> If x and y are two different types, the compiler would complain with a
> warning/error because there was no cast. Is that the strict type-checking
> that the comment mentions, and did I understand this correctly?
Jaco
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
https://lists.osdl.org/mailman/listinfo/kernel-janitors
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2007-02-03 20:10 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-03 1:23 [KJ] [RFC] Regarding min/max Richard Knutsson
2007-02-03 1:45 ` Randy Dunlap
2007-02-03 7:42 ` Robert P. J. Day
2007-02-03 8:38 ` Jaco Kroon
2007-02-03 19:01 ` Richard Knutsson
2007-02-03 20:04 ` Srdjan Todorovic
2007-02-03 20:10 ` Jaco Kroon
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.