Conditional compilation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux