SPO Project Function Pick

I picked the function STRCSPN.  This function scans str1 for the first occurrence of any of the characters that are part of str2, returning the number of characters of str1 read before this first occurrence. The search includes the terminating null-characters. Therefore, the function will return the length of str1 if none of the characters of str2 are found in str1.

Here is the code in the GNU Library:

STRCSPN (const char *str, const char *reject)

 {

 if (__glibc_unlikely (reject[0] == '\0') ||
 __glibc_unlikely (reject[1] == '\0'))

 return __strchrnul (str, reject [0]) - str;



 /* Use multiple small memsets to enable inlining on most targets. */

unsigned char table[256];
 unsigned char *p = memset (table, 0, 64);
 memset (p + 64, 0, 64);
 memset (p + 128, 0, 64);
 memset (p + 192, 0, 64);




 unsigned char *s = (unsigned char*) reject;
 unsigned char tmp;
 do
 p[tmp = *s++] = 1;
while (tmp);
 s = (unsigned char*) str;
if (p[s[0]]) return 0;
if (p[s[1]]) return 1;
if (p[s[2]]) return 2;
if (p[s[3]]) return 3;

 s = (unsigned char *) PTR_ALIGN_DOWN (s, 4);

unsigned int c0, c1, c2, c3;
 do
{
s += 4;
c0 = p[s[0]];
c1 = p[s[1]];
c2 = p[s[2]];
c3 = p[s[3]];
}

while ((c0 | c1 | c2 | c3) == 0);
size_t count = s - (unsigned char *) str;
return (c0 | c1) != 0 ? count - c0 + 1 : count - c2 + 3;
}
My plan Is to rewrite this code, and test to see If I can improve the run time of this function or make it more optimal with inline assembly for an arm processors.
I am not sure on how I could rewrite this function 100%
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s