Skip to contentSkip to conversation

Possible solution for min()/max() war

IDEA+RFC Brad Chapman 6 participants 8 messages Aug 28, 2001 – Aug 29, 2001 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Everyone,

From reading this thread, I believe I have come up with several reasons,
IMHO, why the old min()/max() macros were not usable:

- They did not take into account non-typesafe comparisons
- They were too generic
- Some versions, IIRC, relied on typeof()
- They did not take into account signed/unsigned conversions

I have also discovered one problem with the new three-arg min()/max()
macro: it forces both arguments to be the same, thus preventing signed/unsigned comparisons.

	Thus, I have a humble idea: add another type argument!

	Below is a coding example (just an example!):

	#define min(ta, a, tb, b)	((ta)(a) < (tb)(b) ? (a) : (b))
	#define max(ta, a, tb, b)	((ta)(a) > (tb)(b) ? (a) : (b))

	.....
	long int number[] = { 878593, 786831 };
	short int num[] = { 878, 786 };

	.....
	long_min = min(long int, num[0], long int, number[0]);
	short_min = min(short int, num[0], short int, number[0]);

	long_max = max(long int, num[1], long int, number[1]);
	short_max = max(short int, num[1], short int, number[1]);

	Thus, people can now cast different number types up or down, depending

on what they need, and properly compare their sizes. The new argument also makes comparisons between signed/unsigned more flexible, because now you can cast in both directions - get a signed minimum and an unsigned minimum.

This is an RFC. Flames, anyone? Honestly, this was one of those
"lightbulb" ideas. If people object, I don't mind......

Brad

P.S: /me also wonders if I will need firehoses in my e-mail client ;)

Quoted 69 lines · Andreas Schwab
Quoted 11 lines

Roman Zippel <[email protected]> writes:

|> Hi,
|> 
|> Linus Torvalds wrote:
[...]
|> > You just fixed the "re-use arguments" bug - which is a bug, but doesn't
|> > address the fact that most of the min/max bugs are due to the programmer
|> > _indending_ a unsigned compare because he didn't even think about the
|> > type.

#if 0
If the comparison was made unsigned, cast to the largest natural value on the target, while keeping the types and sizes of the input variables the same, this macro does about what 99.9999999 percent of what everybody wants:
#endif

#include <stdio.h>

#define min(a,b) ((size_t)(a) < (size_t)(b) ? (a) : (b))

int main()

{
    printf("%d\n", min(1,2));
    printf("%d\n", min(-1,2));
    printf("%d\n", min(0xffffffff,3));
    printf("%d\n", min(0x8000,4));
    printf("%d\n", min(0x7fff,5));
    printf("%d\n", min(0x80000000,6));
    printf("%d\n", min(0x7fffffff,7));

    printf("%ld\n", min(1L,2L));
    printf("%ld\n", min(-1L,2L));
    printf("%ld\n", min(0xffffffff,3L));
    printf("%ld\n", min(0x8000,4L));
    printf("%ld\n", min(0x7fff,5L));
    printf("%ld\n", min(0x80000000,6L));
    printf("%ld\n", min(0x7fffffff,7L));

    printf("%lu\n", min(1L,2LU));
    printf("%lu\n", min(-1L,2LU));
    printf("%lu\n", min(0xffffffff,3LU));
    printf("%lu\n", min(0x8000,4LU));
    printf("%lu\n", min(0x7fff,5LU));
    printf("%lu\n", min(0x80000000,6LU));
    printf("%lu\n", min(0x7fffffff,7LU));

    printf("%d\n", min(-1, -2));
    printf("%d\n", min(-1, 0));
    printf("%p\n", min((void *)0x80000000, (void *)0x7fffffff));
    return 0;
}

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be attributed to Microsoft. Once I realized that Ctrl-Alt-Del was handled in the BIOS, I found that there aren't any.

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

=====
Brad Chapman

Permanent e-mail: [email protected]
Current e-mail: [email protected]
Alternate e-mail: [email protected]

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

#2 lore raw

Brad, the three arg min/max changes both values to the common type and then compares them.

there isn't any need to have two different types becouse you then still need to decide what the common type you are going to use for the comparison is.

David Lang

Quoted 142 lines · Brad Chapman

Date: Tue, 28 Aug 2001 12:33:17 -0700 (PDT)
From: Brad Chapman <[email protected]>
To: [email protected]
Subject: Re: [IDEA+RFC] Possible solution for min()/max() war

Everyone,

From reading this thread, I believe I have come up with several reasons,
IMHO, why the old min()/max() macros were not usable:

- They did not take into account non-typesafe comparisons
- They were too generic
- Some versions, IIRC, relied on typeof()
- They did not take into account signed/unsigned conversions

I have also discovered one problem with the new three-arg min()/max()
macro: it forces both arguments to be the same, thus preventing signed/unsigned comparisons.

	Thus, I have a humble idea: add another type argument!

	Below is a coding example (just an example!):

	#define min(ta, a, tb, b)	((ta)(a) < (tb)(b) ? (a) : (b))
	#define max(ta, a, tb, b)	((ta)(a) > (tb)(b) ? (a) : (b))

	.....
	long int number[] = { 878593, 786831 };
	short int num[] = { 878, 786 };

	.....
	long_min = min(long int, num[0], long int, number[0]);
	short_min = min(short int, num[0], short int, number[0]);

	long_max = max(long int, num[1], long int, number[1]);
	short_max = max(short int, num[1], short int, number[1]);

	Thus, people can now cast different number types up or down, depending

on what they need, and properly compare their sizes. The new argument also makes comparisons between signed/unsigned more flexible, because now you can cast in both directions - get a signed minimum and an unsigned minimum.

This is an RFC. Flames, anyone? Honestly, this was one of those
"lightbulb" ideas. If people object, I don't mind......

Brad

P.S: /me also wonders if I will need firehoses in my e-mail client ;)

Quoted 69 lines · Andreas Schwab
Quoted 11 lines

Roman Zippel <[email protected]> writes:

|> Hi,
|>
|> Linus Torvalds wrote:
[...]
|> > You just fixed the "re-use arguments" bug - which is a bug, but doesn't
|> > address the fact that most of the min/max bugs are due to the programmer
|> > _indending_ a unsigned compare because he didn't even think about the
|> > type.

#if 0
If the comparison was made unsigned, cast to the largest natural value on the target, while keeping the types and sizes of the input variables the same, this macro does about what 99.9999999 percent of what everybody wants:
#endif

#include <stdio.h>

#define min(a,b) ((size_t)(a) < (size_t)(b) ? (a) : (b))

int main()

{
    printf("%d\n", min(1,2));
    printf("%d\n", min(-1,2));
    printf("%d\n", min(0xffffffff,3));
    printf("%d\n", min(0x8000,4));
    printf("%d\n", min(0x7fff,5));
    printf("%d\n", min(0x80000000,6));
    printf("%d\n", min(0x7fffffff,7));

    printf("%ld\n", min(1L,2L));
    printf("%ld\n", min(-1L,2L));
    printf("%ld\n", min(0xffffffff,3L));
    printf("%ld\n", min(0x8000,4L));
    printf("%ld\n", min(0x7fff,5L));
    printf("%ld\n", min(0x80000000,6L));
    printf("%ld\n", min(0x7fffffff,7L));

    printf("%lu\n", min(1L,2LU));
    printf("%lu\n", min(-1L,2LU));
    printf("%lu\n", min(0xffffffff,3LU));
    printf("%lu\n", min(0x8000,4LU));
    printf("%lu\n", min(0x7fff,5LU));
    printf("%lu\n", min(0x80000000,6LU));
    printf("%lu\n", min(0x7fffffff,7LU));

    printf("%d\n", min(-1, -2));
    printf("%d\n", min(-1, 0));
    printf("%p\n", min((void *)0x80000000, (void *)0x7fffffff));
    return 0;
}

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be attributed to Microsoft. Once I realized that Ctrl-Alt-Del was handled in the BIOS, I found that there aren't any.

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

=====
Brad Chapman

Permanent e-mail: [email protected]
Current e-mail: [email protected]
Alternate e-mail: [email protected]

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#3 lore raw

Mr. Lang,

Quoted 2 lines · --- David Lang

Brad, the three arg min/max changes both values to the common type and then compares them.

That's the problem. It prevents you from doing varied conversions.
My new macros allow you to do all three basic conversions (AFAICT) :

- signed with signed
- unsigned with unsigned
- signed with unsigned
^^^^^^^^^^^^^^^^^^^^

This is the one type of conversion that the three-arg macro DOES NOT
cover. When you provide the ability to do multiple signed/unsigned conversions on different sizes (signed, unsigned, long, short), then stuff becomes much easier.

Quoted 4 lines

there isn't any need to have two different types becouse you then still need to decide what the common type you are going to use for the comparison is.

What if you don't _want_ a common type? See above. IIRC, this was a major
sticking point when Linus first changedthe macros to use a type argument; the new macro prevented signed/unsigned conversions. My macro defs allow that.

Quoted 2 lines

David Lang

Brad

Quoted 56 lines · Brad Chapman
Quoted 54 lines

Date: Tue, 28 Aug 2001 12:33:17 -0700 (PDT)
From: Brad Chapman <[email protected]>
To: [email protected]
Subject: Re: [IDEA+RFC] Possible solution for min()/max() war

Everyone,

From reading this thread, I believe I have come up with several reasons,
IMHO, why the old min()/max() macros were not usable:

- They did not take into account non-typesafe comparisons
- They were too generic
- Some versions, IIRC, relied on typeof()
- They did not take into account signed/unsigned conversions

I have also discovered one problem with the new three-arg min()/max()
macro: it forces both arguments to be the same, thus preventing signed/unsigned comparisons.

	Thus, I have a humble idea: add another type argument!

	Below is a coding example (just an example!):

	#define min(ta, a, tb, b)	((ta)(a) < (tb)(b) ? (a) : (b))
	#define max(ta, a, tb, b)	((ta)(a) > (tb)(b) ? (a) : (b))

	.....
	long int number[] = { 878593, 786831 };
	short int num[] = { 878, 786 };

	.....
	long_min = min(long int, num[0], long int, number[0]);
	short_min = min(short int, num[0], short int, number[0]);

	long_max = max(long int, num[1], long int, number[1]);
	short_max = max(short int, num[1], short int, number[1]);

	Thus, people can now cast different number types up or down, depending

on what they need, and properly compare their sizes. The new argument also makes comparisons between signed/unsigned more flexible, because now you can cast in both directions - get a signed minimum and an unsigned minimum.

This is an RFC. Flames, anyone? Honestly, this was one of those
"lightbulb" ideas. If people object, I don't mind......

Brad

P.S: /me also wonders if I will need firehoses in my e-mail client ;)

Quoted 4 lines · Andreas Schwab
Quoted 3 lines

Roman Zippel <[email protected]> writes:

Quoted 90 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
|> Hi,
|>
|> Linus Torvalds wrote:

[...]
|> > You just fixed the "re-use arguments" bug - which is a bug, but doesn't |> > address the fact that most of the min/max bugs are due to the

programmer

Quoted 83 lines
Quoted 59 lines
Quoted 2 lines

|> > _indending_ a unsigned compare because he didn't even think about the |> > type.

#if 0
If the comparison was made unsigned, cast to the largest natural value on the target, while keeping the types and sizes of the input variables the same, this macro does about what 99.9999999 percent of what everybody wants:
#endif

#include <stdio.h>

#define min(a,b) ((size_t)(a) < (size_t)(b) ? (a) : (b))

int main()

{
    printf("%d\n", min(1,2));
    printf("%d\n", min(-1,2));
    printf("%d\n", min(0xffffffff,3));
    printf("%d\n", min(0x8000,4));
    printf("%d\n", min(0x7fff,5));
    printf("%d\n", min(0x80000000,6));
    printf("%d\n", min(0x7fffffff,7));

    printf("%ld\n", min(1L,2L));
    printf("%ld\n", min(-1L,2L));
    printf("%ld\n", min(0xffffffff,3L));
    printf("%ld\n", min(0x8000,4L));
    printf("%ld\n", min(0x7fff,5L));
    printf("%ld\n", min(0x80000000,6L));
    printf("%ld\n", min(0x7fffffff,7L));

    printf("%lu\n", min(1L,2LU));
    printf("%lu\n", min(-1L,2LU));
    printf("%lu\n", min(0xffffffff,3LU));
    printf("%lu\n", min(0x8000,4LU));
    printf("%lu\n", min(0x7fff,5LU));
    printf("%lu\n", min(0x80000000,6LU));
    printf("%lu\n", min(0x7fffffff,7LU));

    printf("%d\n", min(-1, -2));
    printf("%d\n", min(-1, 0));
    printf("%p\n", min((void *)0x80000000, (void *)0x7fffffff));
    return 0;
}

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be attributed to Microsoft. Once I realized that Ctrl-Alt-Del was handled in the BIOS, I found that there aren't any.

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

=====
Brad Chapman

Permanent e-mail: [email protected]
Current e-mail: [email protected]
Alternate e-mail: [email protected]

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

=====
Brad Chapman

Permanent e-mail: [email protected]
Current e-mail: [email protected]
Alternate e-mail: [email protected]

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

#4 lore raw
Quoted 14 lines · Brad Chapman

Mr. Lang,

Quoted 2 lines · --- David Lang

Brad, the three arg min/max changes both values to the common type and then compares them.

That's the problem. It prevents you from doing varied conversions.
My new macros allow you to do all three basic conversions (AFAICT) :

- signed with signed
- unsigned with unsigned
- signed with unsigned
^^^^^^^^^^^^^^^^^^^^

Quoted 4 lines

This is the one type of conversion that the three-arg macro DOES NOT
cover. When you provide the ability to do multiple signed/unsigned conversions on different sizes (signed, unsigned, long, short), then stuff becomes much easier.

there isn't such a thing as a signed with unsigned comparison at the machine level (as andreas points out).

if you are thinking that there is please try and give a low level (i.e. binary) example of how the comparison can work.

David Lang

Quoted 174 lines
Quoted 4 lines

there isn't any need to have two different types becouse you then still need to decide what the common type you are going to use for the comparison is.

What if you don't _want_ a common type? See above. IIRC, this was a major
sticking point when Linus first changedthe macros to use a type argument; the new macro prevented signed/unsigned conversions. My macro defs allow that.

Quoted 2 lines

David Lang

Brad

Quoted 56 lines · Brad Chapman
Quoted 54 lines

Date: Tue, 28 Aug 2001 12:33:17 -0700 (PDT)
From: Brad Chapman <[email protected]>
To: [email protected]
Subject: Re: [IDEA+RFC] Possible solution for min()/max() war

Everyone,

From reading this thread, I believe I have come up with several reasons,
IMHO, why the old min()/max() macros were not usable:

- They did not take into account non-typesafe comparisons
- They were too generic
- Some versions, IIRC, relied on typeof()
- They did not take into account signed/unsigned conversions

I have also discovered one problem with the new three-arg min()/max()
macro: it forces both arguments to be the same, thus preventing signed/unsigned comparisons.

	Thus, I have a humble idea: add another type argument!

	Below is a coding example (just an example!):

	#define min(ta, a, tb, b)	((ta)(a) < (tb)(b) ? (a) : (b))
	#define max(ta, a, tb, b)	((ta)(a) > (tb)(b) ? (a) : (b))

	.....
	long int number[] = { 878593, 786831 };
	short int num[] = { 878, 786 };

	.....
	long_min = min(long int, num[0], long int, number[0]);
	short_min = min(short int, num[0], short int, number[0]);

	long_max = max(long int, num[1], long int, number[1]);
	short_max = max(short int, num[1], short int, number[1]);

	Thus, people can now cast different number types up or down, depending

on what they need, and properly compare their sizes. The new argument also makes comparisons between signed/unsigned more flexible, because now you can cast in both directions - get a signed minimum and an unsigned minimum.

This is an RFC. Flames, anyone? Honestly, this was one of those
"lightbulb" ideas. If people object, I don't mind......

Brad

P.S: /me also wonders if I will need firehoses in my e-mail client ;)

Quoted 4 lines · Andreas Schwab
Quoted 3 lines

Roman Zippel <[email protected]> writes:

Quoted 90 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
|> Hi,
|>
|> Linus Torvalds wrote:

[...]
|> > You just fixed the "re-use arguments" bug - which is a bug, but doesn't |> > address the fact that most of the min/max bugs are due to the

programmer

Quoted 83 lines
Quoted 59 lines
Quoted 2 lines

|> > _indending_ a unsigned compare because he didn't even think about the |> > type.

#if 0
If the comparison was made unsigned, cast to the largest natural value on the target, while keeping the types and sizes of the input variables the same, this macro does about what 99.9999999 percent of what everybody wants:
#endif

#include <stdio.h>

#define min(a,b) ((size_t)(a) < (size_t)(b) ? (a) : (b))

int main()

{
    printf("%d\n", min(1,2));
    printf("%d\n", min(-1,2));
    printf("%d\n", min(0xffffffff,3));
    printf("%d\n", min(0x8000,4));
    printf("%d\n", min(0x7fff,5));
    printf("%d\n", min(0x80000000,6));
    printf("%d\n", min(0x7fffffff,7));

    printf("%ld\n", min(1L,2L));
    printf("%ld\n", min(-1L,2L));
    printf("%ld\n", min(0xffffffff,3L));
    printf("%ld\n", min(0x8000,4L));
    printf("%ld\n", min(0x7fff,5L));
    printf("%ld\n", min(0x80000000,6L));
    printf("%ld\n", min(0x7fffffff,7L));

    printf("%lu\n", min(1L,2LU));
    printf("%lu\n", min(-1L,2LU));
    printf("%lu\n", min(0xffffffff,3LU));
    printf("%lu\n", min(0x8000,4LU));
    printf("%lu\n", min(0x7fff,5LU));
    printf("%lu\n", min(0x80000000,6LU));
    printf("%lu\n", min(0x7fffffff,7LU));

    printf("%d\n", min(-1, -2));
    printf("%d\n", min(-1, 0));
    printf("%p\n", min((void *)0x80000000, (void *)0x7fffffff));
    return 0;
}

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be attributed to Microsoft. Once I realized that Ctrl-Alt-Del was handled in the BIOS, I found that there aren't any.

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

=====
Brad Chapman

Permanent e-mail: [email protected]
Current e-mail: [email protected]
Alternate e-mail: [email protected]

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

=====
Brad Chapman

Permanent e-mail: [email protected]
Current e-mail: [email protected]
Alternate e-mail: [email protected]

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

#5 lore raw
In reply to Brad Chapman

Brad Chapman <[email protected]> writes:

|> Everyone,
|> 
|> 	From reading this thread, I believe I have come up with several reasons,
|> IMHO, why the old min()/max() macros were not usable:
|> 
|> 	- They did not take into account non-typesafe comparisons
|> 	- They were too generic
|> 	- Some versions, IIRC, relied on typeof()
|> 	- They did not take into account signed/unsigned conversions
|> 
|> 	I have also discovered one problem with the new three-arg min()/max()
|> macro: it forces both arguments to be the same, thus preventing signed/unsigned
|> comparisons.

There is no such thing as signed/unsigned comparision in C. Any comparison is either signed or unsigned, depending on whether the common type of arguments after applying the usual arithmetic conversions is signed or unsigned.

|> Thus, I have a humble idea: add another type argument!

This does not bye you anything because the there can only be one common type anyway.

Andreas.

Signature · 5 lines
Andreas Schwab                                  "And now for something
SuSE Labs                                        completely different."
[email protected]
SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
#6 lore raw

Mr. Schwab,

Quoted 16 lines · --- Andreas Schwab

Brad Chapman <[email protected]> writes:

|> Everyone,
|> 
|> 	From reading this thread, I believe I have come up with several reasons,
|> IMHO, why the old min()/max() macros were not usable:
|> 
|> 	- They did not take into account non-typesafe comparisons
|> 	- They were too generic
|> 	- Some versions, IIRC, relied on typeof()
|> 	- They did not take into account signed/unsigned conversions
|> 
|> 	I have also discovered one problem with the new three-arg min()/max()
|> macro: it forces both arguments to be the same, thus preventing signed/unsigned
|> comparisons.
Quoted 4 lines

There is no such thing as signed/unsigned comparision in C. Any comparison is either signed or unsigned, depending on whether the common type of arguments after applying the usual arithmetic conversions is signed or unsigned.

Then why, IIRC, are the kernel hackers so upset about how the three-arg
macros _prevent_ signed/unsigned comparisons? Apparently the ability to compare signed/unsigned variables must have _some_ significance....

Quoted 5 lines

|> Thus, I have a humble idea: add another type argument!

This does not bye you anything because the there can only be one common type anyway.

IDGT. What if you don't want a common type? What if you explicitly
_want_ to cast signed "up" to unsigned or unsigned "down" to signed?

Quoted 2 lines

Andreas.

Brad

Quoted 7 lines
Signature · 6 lines
-- 
Andreas Schwab                                  "And now for something
SuSE Labs                                        completely different."
[email protected]
SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5

=====
Brad Chapman

Permanent e-mail: [email protected]
Current e-mail: [email protected]
Alternate e-mail: [email protected]

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

#7 lore raw
Quoted 20 lines · Brad Chapman

Mr. Schwab,

Quoted 16 lines · --- Andreas Schwab

Brad Chapman <[email protected]> writes:

|> Everyone,
|>
|>    From reading this thread, I believe I have come up with several reasons,
|> IMHO, why the old min()/max() macros were not usable:
|>
|>    - They did not take into account non-typesafe comparisons
|>    - They were too generic
|>    - Some versions, IIRC, relied on typeof()
|>    - They did not take into account signed/unsigned conversions
|>
|>    I have also discovered one problem with the new three-arg min()/max()
|> macro: it forces both arguments to be the same, thus preventing signed/unsigned
|> comparisons.
Quoted 8 lines
Quoted 4 lines

There is no such thing as signed/unsigned comparision in C. Any comparison is either signed or unsigned, depending on whether the common type of arguments after applying the usual arithmetic conversions is signed or unsigned.

Then why, IIRC, are the kernel hackers so upset about how the three-arg
macros _prevent_ signed/unsigned comparisons? Apparently the ability to compare signed/unsigned variables must have _some_ significance....

Are they? The most salient objections I've seen are to do with compatibility and code maintenance.

Since the underlying hardware has to compare either signed or unsigned numbers, you will always have to eventually cast to some common type. The three arg macro simply makes it explicit *what* that common type is.

Signature · 4 lines
Chris Friesen                    | MailStop: 043/33/F10  
Nortel Networks                  | work: (613) 765-0557
3500 Carling Avenue              | fax:  (613) 765-2986
Nepean, ON K2H 8E9 Canada        | email: [email protected]
#8 lore raw
In reply to Brad Chapman
Quoted 3 lines · Brad Chapman

Mr. Schwab,

[...]

Quoted 8 lines
Quoted 4 lines

There is no such thing as signed/unsigned comparision in C. Any comparison is either signed or unsigned, depending on whether the common type of arguments after applying the usual arithmetic conversions is signed or unsigned.

Then why, IIRC, are the kernel hackers so upset about how the three-arg
macros _prevent_ signed/unsigned comparisons? Apparently the ability to compare signed/unsigned variables must have _some_ significance....

You are misunderstanding this.
It is possible to write something that _looks like_ a signed to unsigned comparison, i.e.:
int a;
unsigned int b;
...
x = min(a,b);

Looks like a signed to unsigned comparison - but it isn't!

You see, the compiler always does an _implicit_ cast in cases like this. So the actual code generated by the compiler might become a signed to signed
comparison, or a unsigned to unsigned comparison.

One of the arguments gets changed invisibly, and that is what kernel developers
are so upset about. You don't really know which one without thinking hard
about it, and that is a source of many hard-to-find bugs.

A min function with a single type argument force you to select a type, instead of blindly let the compiler choose one or the other.

Your two-type min has exactly the same problem as the no-type min:
You can specify two different types, but the compiler will then force it to be only one anyway. And you may wonder which of the two it will choose. And there will be bugs as people assumed signed when it turned out to be unsigned.

The single-type min let you specify exactly which type will be used in the comparison. You may use the type of one of the arguments, or something different from both. The type you select will be the one used anyway, there is no room for doubt or false assumptions.

Quoted 9 lines
Quoted 6 lines

|> Thus, I have a humble idea: add another type argument!

This does not bye you anything because the there can only be one common type anyway.

IDGT. What if you don't want a common type? What if you explicitly
_want_ to cast signed "up" to unsigned or unsigned "down" to signed?

What you ask for is impossible. You can write code that looks like it is comparing different types, but that won't actually happen. The cpu always compares two quantities of the same type, no matter what you write. The compiler will force a single type anyway, so it is nice to state explicit what that single type will be.

Stating two types buys you nothing, as the compiler will force a single one again. I don't even know about a cpu that will do signed to unigned compares - this is why the compiler works that way. Common cpu's only do two
kindes of comparisons - signed to signed, and unigned to unsigned. Which one depends on which comparison instruction the compiler inserts.

A variable don't really have a type on common cpu's. That's just a programming language convention.
The machine instructions are typed though, and the single-typed min() in the kernel mirrors this.

Helge Hafting