CONTACT
US
2018-08-29 Viewed: 1386 Tag: seo google  

As a software engineer, what's the most ingenious line of code you've seen,If you asked me to implement strlen, I’d probably write something like this,Tabpear

As a software engineer, what's the most ingenious line of code you've seen?

Gary Munnelly, Computer engineering graduate and PhD student now working in Digital Humanities.

Answered Aug 22 · Upvoted by Tim Broberg, 25 years a hardware and software developer, still finding new mistakes to make and Gideon Shavit, Professional Software Engineer for 25 years

A really, really cool piece of code I found is in the GNU glibc source code. It is the strlen function in the string.h header file.


If you asked me to implement strlen, I’d probably write something like this:


size_t strlen (const char *str) 

{

const char *p;

if (str == NULL)

return 0;

p = str;

while (*p != '\0') 

p++;

return p - str;

}

Nothing really wrong with that. Does the job and is about as complex as you’d expect.


But the folks who wrote glibc. Good lord this is cool!

Before I begin, you can find the full source here. I’ll be removing comments and changing indentation where I think it helps readability, so check the original for how it’s actually written.


So, the function begins by iterating over the start of the string one letter at a time until it finds a character that is aligned on a longword boundary. If it finds the end of the string in there, it bails out and returns the length


for (char_ptr = str;

((unsigned long int) char_ptr 

& (sizeof (longword) - 1)) != 0;

++char_ptr)

  if (*char_ptr == '\0')

return char_ptr - str;

Then (hold on to your britches), it casts the string from that point on to an array of unsigned long ints


longword_ptr = (unsigned long int *) char_ptr;

Now the function can leap over characters in groups of four or eight depending on the system architecture. It sets up some magic numbers so that when it encounters the end of the string it will know that a NULL occurred in the middle of the long int.


// 32 bit architecture

himagic = 0x80808080L;

lomagic = 0x01010101L;

 

// 64 bit architecture

if (sizeof (longword) > 4) {

himagic = ((himagic << 16) << 16) | himagic;

lomagic = ((lomagic << 16) << 16) | lomagic;

}

 

// Something is wrong

if (sizeof (longword) > 8)

abort ();

With that done, the main loop begins


for (;;) {

longword = *longword_ptr++;

On each iteration, the function copies a group of chars over to a longword variable and advances the string pointer. It then applies its bitmagic. If the result of the bitwise operation is non-zero, then the function has found the NULL somewhere in the last leap (Possibly. It can misfire):


if (((longword - lomagic) & ~longword & himagic) != 0)

If it thinks it has found the NULL, cast that part of the string back to chars and look for the zero entry:


// note we have to subtract 1 because the pointer is 1 step

// too far ahead

const char *cp = (const char *) (longword_ptr - 1);

 

// now just check which char is == NULL

if (cp[0] == 0)

return cp - str;

if (cp[1] == 0)

return cp - str + 1;

if (cp[2] == 0)

return cp - str + 2;

if (cp[3] == 0)

return cp - str + 3;...

Tabpear is a Software Technology Co., Ltd which has many domestic excellent  e-commerce platform R & D teams and mobile R & D teams. We divide the software engineer teams into several levels according to the project development experience. Level one is more than 3 years of development experience. Level two is more than five years of development experience. Level three is more than 10 years of development experience and Level four is expert team which has more than five participating in research projects, Invention patents and software copyrights of 6 or more, published in the core periodicals more than 10 papers. There is also a team ,working at cloud computing and service computing including four doctors, seven masters and 23 persons who have master's degree.




More blogs    


所有评论仅代表网友意见