Conditional compilation

Stéphane FORNENGO sfornengo@gmail.com
Tue May 10 10:01:00 GMT 2011


Is the following idea possible ?
take for example the strcmp function with the standard following code:

int strcmp (const char *p1, const char *p2)
{
	register const unsigned char *s1 = (const unsigned char *) p1;
	register const unsigned char *s2 = (const unsigned char *) p2;
	unsigned char c1, c2;

	do {
		c1 = (unsigned char) *s1++;
		c2 = (unsigned char) *s2++;
		if (c1 == '\0') return c1 - c2;
    } while (c1 == c2);

	return c1 - c2;
}

We found different usages of this function as:
strcmp(a,b)
strcmp(a,"")
strcmp(a,"0123456789")
...

this usages could be detect at compile time and the function code
greatly optimized knowing this information !
for example, rewrite strcmp with conditionnal compilation based on
argument length at compilation time:

int strcmp (const char *p1, const char *p2)
#if arg2 is unknown // strcmp(a,b)
    ... here the classical code
#elif arg2 is const // strcmp(a,"hello")
    #if arg2.length == 0 // strcmp(a,"")
        ... here short code
    #elif arg2.length < 6
        ... here a code based on byte to byte comparison
    #else strcmp(a,"0123456789")
        ... here a code based on long to long comparison
#endif



More information about the Gcc-help mailing list