Linked by Amjith Ramanujam on Fri 21st Nov 2008 03:38 UTC
General Development The Linux kernel uses several special capabilities of the GNU Compiler Collection (GCC) suite. These capabilities range from giving you shortcuts and simplifications to providing the compiler with hints for optimization. Discover some of these special GCC features and learn how to use them in the Linux kernel.
Order by: Score:
Comment by Yamin
by Yamin on Fri 21st Nov 2008 04:58 UTC
Yamin
Member since:
2006-01-10

----------
#define min(x, y) ({ \
typeof(x) _min1 = (x); \
typeof(y) _min2 = (y); \
(void) (&_min1 == &_min2); \
_min1 < _min2 ? _min1 : _min2; })
-------------

anyone know what why this line is there:
void) (&_min1 == &_min2);
?

Reply Score: 1

RE: Comment by Yamin
by irasnyd on Fri 21st Nov 2008 05:34 UTC in reply to "Comment by Yamin"
irasnyd Member since:
2005-07-06

It has to do with typechecking.

Making a simple program:
int x = 10;
long y = 20;
long r = min(x, y);

Gives the following warning:
warning: comparison of distinct pointer types lacks a cast

Reply Score: 3

RE[2]: Comment by Yamin
by drahca on Fri 21st Nov 2008 13:34 UTC in reply to "RE: Comment by Yamin"
drahca Member since:
2006-02-23

Could you please explain further. For me the line (void) (&_min1 == &_min2); is still somewhat inexplicable, since it does not seem to do anything other than compare the two pointers of the temporary variables (which are always distinct) and cast the result to void. So it seems to me as an addendum to the macro (they must have first written it without this line) to keep GCC from generating warnings. Why GCC refrains from the warning after this line is not completely clear to me.

Reply Score: 1

RE[3]: Comment by Yamin
by theosib on Fri 21st Nov 2008 13:45 UTC in reply to "RE[2]: Comment by Yamin"
theosib Member since:
2006-03-02

Maybe it's in there in order to generate a warning.

Reply Score: 2

RE[4]: Comment by Yamin
by drahca on Fri 21st Nov 2008 13:54 UTC in reply to "RE[3]: Comment by Yamin"
drahca Member since:
2006-02-23

Ah. That would make more sense. ;) Thank you.

Reply Score: 1

v Too much hacks
by sbenitezb on Fri 21st Nov 2008 14:05 UTC
JonathanBThompson
Member since:
2006-05-26

And having special optimizations in the compiler is nice. But, isn't it a goal to have the OS as portable as possible to make it easier to port? Granted, GCC is readily available for most any platform, and readily enough ported to any other platform as needed, but so much for the claim of Linux using open standards, while at the same time purposely using compiler hacks that no other compiler has, making the code less portable.

Reply Score: 3

braddock Member since:
2005-07-08

Has Linux EVER been compiled with a non-GNU compiler?

Reply Score: 1

braddock Member since:
2005-07-08

To answer my own question...

In 2004 a German group apparently got the Linux 2.6 Kernel to compile using the Intel Compiler. It required patches and a custom pre-processor to convert some of the GCC directives. Interesting...

http://www.pyrillion.org/index.html?showframe=linuxkernelpatch.html

They claimed performance improvements of up to 40% for some limited areas of the kernel, and 8-9% overall performance improvement in general.

Reply Score: 1

siki_miki Member since:
2006-01-17

It is good to have such useful extensions, because it makes the code faster, and decreases chances of errors in the code. Pure C standard isn't enough for the kernel
(and a small part of it is still platform-dependent ASM, anyway).

Portability of the Linux kernel is not in danger because gcc supports enough platforms.

Reply Score: 2