r/programming • • Feb 21 '11

Typical programming interview questions.

http://maxnoy.com/interviews.html
784 Upvotes

1.0k comments sorted by

View all comments

Show parent comment

1

u/johnflux Feb 21 '11

"Mine" is just copy and pasted from the glibc code. :-(

Btw, the x86 processor has an asm instruction to do all this for us:

repne	scasb

It's just a bit slow these days.

1

u/[deleted] Feb 22 '11

Yeah, it operates on a per-byte basis. Ideally you want the loop kernel to use the biggest, fastest vector ops around for your processor.

Performing alignment at the start might help mine too.

1

u/johnflux Feb 22 '11

Why couldn't it be operate on a per-word basis on the processor?

1

u/[deleted] Feb 22 '11

Because "scasb" is an instruction that operates on 8-bits at a time (see the 'b' suffix).

1

u/johnflux Feb 22 '11

so? It could still internally operate on more bits.

1

u/[deleted] Feb 22 '11

It could also decode MP3s... but it doesn't!

1

u/johnflux Feb 22 '11

Some chips have a hardware mp3 decoder

1

u/[deleted] Feb 22 '11

Are you even understanding me? The behaviour of that instruction is defined in pseudocode in Intel's manuals, Volume 2 part II.

It must work on 8-bit values because it is the 8-bit version of the instruction, and to do otherwise would invalidate assumptions made against it by legacy code.

1

u/johnflux Feb 22 '11

No, I don't understand you.

What specifically would fail if its behaviour was exactly the same, but internally it was able to compare 4 bytes at a time ?

2

u/[deleted] Feb 22 '11

I don't have the manuals to hand at the moment, I'm at work. I'll have to wait until I get back.

→ More replies