B
Beeeeeves
For the masm guys, which I hope will be able to make mincemeat out of this,
please read from the bottom up. It would make my day.
But this is where I'm up to with 'the pecker languages'.
ok, that's something to consider... thanks
eh?
a) the string won't be 4-byte aligned, and
b) it should still work if it is, shouldn't it?
Give me an example of how to call it with some 4-byte aligned strings
(including how to create the 4-byte aligned strings in the first place)
that shows an incorrectness.
Or better still how to write it in assembly language...
please read from the bottom up. It would make my day.
But this is where I'm up to with 'the pecker languages'.
1. tcslen - You are first finding the string lengths of the two strings.
Now, that is going to walk thru the string a character at a time looking for
zero termination. That is an overkill. As long as you're doing this,
there's no point in optimizing. You could very well compare the strings
right then. Try a loop method with character comparisons and time it.
You'll be surprised.
ok, that's something to consider... thanks
2. Your algorithm does the 4-byte boundary alignment. However, if the
string is already 4-byte aligned, your skipper will still go ahead and skip
4 bytes.
eh?
a) the string won't be 4-byte aligned, and
b) it should still work if it is, shouldn't it?
Give me an example of how to call it with some 4-byte aligned strings
(including how to create the 4-byte aligned strings in the first place)
that shows an incorrectness.
Or better still how to write it in assembly language...
Beeeeeves said:I've written a couple of mean functions to do this, they seem to be
working
pretty well (0.6 seconds to execute each of them on strings of 749KB,
where
something had been deleted out of the middle, on a 1.3MHz Duron. That was
with the DLL in release mode.)
Anyone able to beat this with some assembly language?
#define SCALE (long)(sizeof(long) / sizeof(_TCHAR))
long __cdecl FirstDifferentIndex(_TCHAR* str1, _TCHAR* str2)
{
long minlen = min((long)_tcslen(str1), (long)_tcslen(str2)),
minlongs = minlen / SCALE,
i = 0, t = 0;
long* skipper[] = {(long*)str1, (long*)str2};
for(;(i < minlongs) && (skipper[0] == skipper[1]); i++);
for(t = i * SCALE; (t < minlen) && (str1[t] == str2[t]); t++);
return t == minlen ? -1 : t;
}
long _cdecl AmountSameAtEnd(_TCHAR* str1, _TCHAR* str2)
{
long len[] = {(long)_tcslen(str1), (long)_tcslen(str2)},
off[] = {len[0] % SCALE, len[1] % SCALE},
pos[] = {(len[0] - off[0])/SCALE, (len[1] - off[1])/SCALE},
t = 0,
* skipper[] = {
(long*)(str1 + SCALE - off[0]),
(long*)(str2 + SCALE - off[1])};
for(;(pos[0] >= 0) && (pos[1] >= 0) &&
(skipper[0][pos[0]] == skipper[1][pos[1]]);
pos[0]--, pos[1]--);
long fin[] = {
min((pos[0] + 1) * SCALE - 1, len[0] - 1),
min((pos[1] + 1) * SCALE - 1, len[1] - 1)};
for(;(fin[0] >= 0) && (fin[1] >= 0) && (str1[fin[0]] == str2[fin[1]]);
fin[0]--, fin[1]--);
return len[0] - fin[0] - 1; //should be the same as len[1] - fin[1] - 1
}
Vijaye Raji said:In a 32 bit environment, the fastest approach would be find the 4-byte
boundary and start reading and comparing 4 bytes at a time. When a mismatch
is found, you'll dig in and compare them at byte/word level (depending on if
your encoding is 8-bit or 16-bit)
In x86 asm, you should use the string optimized lodsb, cmpsb
instructions.
-vJ