strcmp is too heavy for its everyday usage...
strcmp is _mostly_ used here and there to check strings for equality only, not for finding their ordering. And if we have a function for this equality check only, it would be a reasonable benefit:
a) for a long strings this function can operate with a whole words vs. bytes, which is much faster;
b) for a very short strings, when strcmp and this function can be unrolled into a series of a direct bytes comparing, this defun don't have to cope with (unsigned char) -> (int) casting for the each pair of bytes, so it will be shorter, faster etc.
memcmp is the subject of the question, too.
Any comments?
The optimized string functions already do word comparisons when that is possible and advantageous. The comparisons to extract the ordering vs just equality/nonequality are only on the first nonmatching byte.
Quoted 4 lines · Roland McGrath
The optimized string functions already do word comparisons when that is possible and advantageous. The comparisons to extract the ordering vs just equality/nonequality are only on the first nonmatching byte.
But it's an overhead anyway. Then, it's bad enough for the inlining. And then, where is such a strcmp in GLIBC?
Quoted 8 lines · Denis Zaitsev
Quoted 4 lines · Roland McGrath
The optimized string functions already do word comparisons when that is possible and advantageous. The comparisons to extract the ordering vs just equality/nonequality are only on the first nonmatching byte.
But it's an overhead anyway.
Rather neglectable, IMHO.
Quoted 1 line
Then, it's bad enough for the inlining.
If it's inlined then the compiler should be smart enough to discard the unneded bits. If not, and the difference is measurable, then the compiler should be fixed.
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux AG, Maxfeldstraße 5, 90409 Nürnberg, Germany Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Quoted 4 lines · Andreas Schwab
Quoted 2 lines · Denis Zaitsev
On Wed, Jan 07, 2004 at 05:13:11PM -0800, Roland McGrath wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
The optimized string functions already do word comparisons when that is possible and advantageous. The comparisons to extract the ordering vs just equality/nonequality are only on the first nonmatching byte.
But it's an overhead anyway.
Rather neglectable, IMHO.
Nearly agreed.
Quoted 5 lines
Quoted 1 line
Then, it's bad enough for the inlining.
If it's inlined then the compiler should be smart enough to discard the unneded bits. If not, and the difference is measurable, then the compiler should be fixed.
GCC is smart enough. It doesn't do the job thru the best possible way, but this should and (important!) can really be fixed. So, generally I agree again. But suppose such an example:
extern inlines(const unsigned char *a, const unsigned char *b)
{
int r;
(r= a[0] - b[0]) &&
(r= a[1] - b[1]) &&
(r= a[2] - b[2]) &&
(r= a[3] - b[3]);
return r;
}It's a typical inline code for compare 4-byte of mem. When it is used, say, in such a context
s(a,b) ? A() : B();
GCC discards the value of r perfectly, leaving the only code needed for compare bytes for eq/neq. But GCC doesn't merge the 4 byte comparing into single word comparing. And, as I understand, it will never do that, as it's not asked to. Or this kind of optimization is assumed ok for compiler, but just still unimplemented?
Quoted 10 lines · Denis Zaitsev
(r= a[0] - b[0]) && (r= a[1] - b[1]) && (r= a[2] - b[2]) && (r= a[3] - b[3]); return r; }It's a typical inline code for compare 4-byte of mem. When it is used, say, in such a context
Quoted 7 lines
s(a,b) ? A() : B();GCC discards the value of r perfectly, leaving the only code needed for compare bytes for eq/neq. But GCC doesn't merge the 4 byte comparing into single word comparing. And, as I understand, it will never do that, as it's not asked to. Or this kind of optimization is assumed ok for compiler, but just still unimplemented?
Certainly it's ok if it converts.
However, on most targets you'd have to know that a and b are aligned. Worse, even for targets like x86 that support unaligned loads you have to know for certain that neither a[3] nor b[3] could possibly segv when a[0] and b[0] won't. That condition is trivial when a and b are aligned, but otherwise...
Adding compensation code to deal with the extra conditions that must be satisfied will probably negate whatever you're hoping to gain here.
r~
Quoted 19 lines · Richard Henderson
Quoted 10 lines · Denis Zaitsev
(r= a[0] - b[0]) && (r= a[1] - b[1]) && (r= a[2] - b[2]) && (r= a[3] - b[3]); return r; }It's a typical inline code for compare 4-byte of mem. When it is used, say, in such a context
Quoted 7 lines
s(a,b) ? A() : B();GCC discards the value of r perfectly, leaving the only code needed for compare bytes for eq/neq. But GCC doesn't merge the 4 byte comparing into single word comparing. And, as I understand, it will never do that, as it's not asked to. Or this kind of optimization is assumed ok for compiler, but just still unimplemented?
Quoted 6 lines
Certainly it's ok if it converts.
However, on most targets you'd have to know that a and b are aligned. Worse, even for targets like x86 that support unaligned loads you have to know for certain that neither a[3] nor b[3] could possibly segv when a[0] and b[0] won't.
Yes, and therefore this code is for memcmp, not for strcmp. BTW, do I understand right, that memcmp is free of that problem, as it has the explicit length argument? And this arg says implicitly, that the access for the mem thru all this length is ok?
Quoted 11 lines · Richard Henderson
Quoted 9 lines · Denis Zaitsev
(r= a[0] - b[0]) && (r= a[1] - b[1]) && (r= a[2] - b[2]) && (r= a[3] - b[3]); return r; }never do that, as it's not asked to. Or this kind of optimization is assumed ok for compiler, but just still unimplemented?
Quoted 7 lines
Certainly it's ok if it converts.
However, on most targets you'd have to know that a and b are aligned. Worse, even for targets like x86 that support unaligned loads you have to know for certain that neither a[3] nor b[3] could possibly segv when a[0] and b[0] won't. That condition is trivial when a and b are aligned, but otherwise...
Fair enough, but...
extern ints(const unsigned char a[static 4], const unsigned char b[static 4])
{
int r;
(r= a[0] - b[0]) &&
(r= a[1] - b[1]) &&
(r= a[2] - b[2]) &&
(r= a[3] - b[3]);
return r;
}...produces the same code.
Signature · 4 lines
# James Antill -- [email protected] :0: * ^From: .*james@and\.org /dev/null