Skip to contentSkip to conversation

Regarding min/max

RFC Richard Knutsson 5 participants 7 messages Feb 3, 2007 Kernel janitors

lore
Share & tools
lore
#0 lore raw

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
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#1 lore raw
Quoted 11 lines · 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.

Quoted 4 lines

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
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#2 lore raw
In reply to Richard Knutsson
Quoted 10 lines · Richard Knutsson

Hi

Regarding converting:

	a = x < y ? x : y;

with

	a = min(x, y);

and etc.

Quoted 8 lines

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

Signature · 11 lines
====================================
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
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
#3 lore raw
In reply to Richard Knutsson
Quoted 11 lines · Robert P. J. Day
Quoted 10 lines · Richard Knutsson

Hi

Regarding converting:

	a = x < y ? x : y;

with

	a = min(x, y);

and etc.

Quoted 8 lines
Quoted 8 lines

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
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#4 lore raw
In reply to Richard Knutsson
Quoted 43 lines · Jaco Kroon
Quoted 18 lines · Robert P. J. Day
Quoted 16 lines · Richard Knutsson

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.

Quoted 3 lines
Quoted 3 lines

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; })
Quoted 4 lines

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
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#5 lore raw
In reply to Richard Knutsson

Hi,

Quoted 7 lines · Jaco Kroon

from include/linux/kernel.h:

/*
* min()/max() macros that also do
* strict type-checking.. See the
* "unnecessary" pointer comparison.
*/

Quoted 5 lines
#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
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#6 lore raw
In reply to Richard Knutsson
Quoted 18 lines · Srdjan Todorovic

Hi,

Quoted 7 lines · Jaco Kroon

from include/linux/kernel.h:

/*
* min()/max() macros that also do
* strict type-checking.. See the
* "unnecessary" pointer comparison.
*/

Quoted 5 lines
#define min(x,y) ({ \
   typeof(x) _x = (x); \
   typeof(y) _y = (y); \
   (void) (&_x = &_y);        \
   _x < _y ? _x : _y; })
Quoted 4 lines

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.

Quoted 3 lines

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
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors