cvs commit: src/include _ctype.h

Andrey Chernov ache at nagual.pp.ru
Wed Oct 31 15:12:16 PDT 2007


On Tue, Oct 30, 2007 at 10:03:31AM -1000, Juli Mallett wrote:
> * "Andrey A. Chernov" <ache at FreeBSD.org> [ 2007-10-27 ]
> 	[ cvs commit: src/include _ctype.h ]
> > ache        2007-10-27 22:32:28 UTC
> > 
> >   FreeBSD src repository
> > 
> >   Modified files:
> >     include              _ctype.h 
> >   Log:
> >   Micro-optimization of prev. commit, change
> >   (_c < 0 || _c >= 128) to (_c & ~0x7F)
> 
> Isn't that a non-optimization in code and a minor pessimization of readability?
> Maybe I'm getting rusty, but those seem to result in nearly identical code on
> i386 with a relatively modern GCC.  Did you look at the compiler output for this
> optimization?  Is there a specific expensive instruction you're trying to avoid?
> For such thoroughyl bit-aligned range checks, you shouldn't even get a branch
> for the former case.  Is there a platform other than i386 I should look at where
> the previous expression is more clearly pessimized?  Or a different compiler
> than GCC?

For ones who doubts there two tests compiled with -O2. As you may see the 
result is almost identical (andl vs cmpl):
-------------------- a.c --------------------
main () {

	int c;

	return (c & ~0x7f) ? 0 : c * 2;
}
-------------------- a.s --------------------
	.file	"a.c"
	.text
	.p2align 4,,15
.globl main
	.type	main, @function
main:
	leal	4(%esp), %ecx
	andl	$-16, %esp
	pushl	-4(%ecx)
	movl	%eax, %edx
	andl	$-128, %edx
	addl	%eax, %eax
	cmpl	$1, %edx
	sbbl	%edx, %edx
	pushl	%ebp
	andl	%edx, %eax
	movl	%esp, %ebp
	pushl	%ecx
	popl	%ecx
	popl	%ebp
	leal	-4(%ecx), %esp
	ret
	.size	main, .-main
	.ident	"GCC: (GNU) 4.2.1 20070719  [FreeBSD]"
-------------------- a1.c --------------------
main () {

	int c;

	return (c < 0 || c >= 128) ? 0 : c * 2;


}
-------------------- a1.s --------------------
	.file	"a1.c"
	.text
	.p2align 4,,15
.globl main
	.type	main, @function
main:
	leal	4(%esp), %ecx
	andl	$-16, %esp
	pushl	-4(%ecx)
	addl	%eax, %eax
	cmpl	$128, %eax
	sbbl	%edx, %edx
	andl	%edx, %eax
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ecx
	popl	%ecx
	popl	%ebp
	leal	-4(%ecx), %esp
	ret
	.size	main, .-main
	.ident	"GCC: (GNU) 4.2.1 20070719  [FreeBSD]"



-- 
http://ache.pp.ru/


More information about the cvs-src mailing list