前のページ | 次のページ
リスト


〔リスト1〕浮動小数点演算のための最適化を行う例(test213.c)
//結果が「非数値」あるいは「無限」ではないと仮定して浮動小数点演算のための最適化を行う例
#define _GNU_SOURCE 
#include <stdio.h>
#include <math.h>
const float f1 = 3.1212312312312312f;
const float f2 = 6.5432165432165432f;
float	x;
float	y;

float func(float a)
{
	x	=	10;
	x	=	NAN	*	x;
	y	=	x	* f1;
    return a * f1 / f2;
}


〔リスト2〕結果が「非数値」あるいは「無限」ではないと仮定して浮動小数点演算のための最適化を行うオプションを付けて生成されたアセンブラ・ソース(test213a.s)
	.file	"test213.c"
.globl f1
	.section	.rodata
	.align 4
	.type	f1, @object
	.size	f1, 4
f1:
	.long	1078444609
.globl f2
	.align 4
	.type	f2, @object
	.size	f2, 4
f2:
	.long	1087463944
	.align 4
.LC1:
	.long	2143289344
	.text
.globl func
	.type	func, @function
func:
	pushl	%ebp
	movl		%esp, %ebp
	movl		$0x41200000, %eax
	movl		%eax, x
	flds		x
	flds		.LC1
	fmulp	%st, %st(1)
	fstps	x
	flds		x
	fmuls	f1
	fstps	y
	flds		8(%ebp)
	fmuls	f1
	fdivs	f2
	leave
	ret
	.size	func, .-func
	.comm	x,4,4
	.comm	y,4,4
	.section	.note.GNU-stack,"",@progbits
	.ident	"GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"

〔リスト3〕オプションなしで生成されたアセンブラ・ソース(test213.s)
	.file	"test213.c"
.globl f1
	.section	.rodata
	.align 4
	.type	f1, @object
	.size	f1, 4
f1:
	.long	1078444609
.globl f2
	.align 4
	.type	f2, @object
	.size	f2, 4
f2:
	.long	1087463944
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC2:
	.long	1078444609
	.align 4
.LC3:
	.long	1087463944
	.text
	.p2align 2,,3
.globl func
	.type	func, @function
func:
	pushl	%ebp
	movl		%esp, %ebp
	flds		8(%ebp)
	movl		$0x7fc00000, %eax
	fmuls	.LC2
	fdivs	.LC3
	movl		%eax, x
	movl		%eax, y
	leave
	ret
	.size	func, .-func
	.comm	x,4,4
	.comm	y,4,4
	.section	.note.GNU-stack,"",@progbits
	.ident	"GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"

〔リスト4〕メモリ中のアドレス定数を,算術演算で使用する前にレジスタにコピーする例(test214.c)
//メモリ中のアドレス定数を、算術演算で使用する前にレジスタにコピーする例
#include <stdio.h>
int main()
{
	int p = 1;
	long	  adr = (long)&p;
	adr	= adr+2;
	printf ("%x\n",adr);
	adr	= adr+2;
	printf ("%x\n",adr);
	adr	= adr+2;
	printf ("%x\n",adr);
	adr	= adr+2;
	printf ("%x\n",adr);
	return 0;
}


〔リスト5〕オプションを付けて生成されたアセンブラ・ソース(test214a.s)
	.file	"test214.c"
	.section	.rodata
.LC0:
	.string	"%x\n"
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl		%esp, %ebp
	subl		$8, %esp
	andl		$-16, %esp
	movl		$0, %eax
	subl		%eax, %esp
	movl		$1, -4(%ebp)
	leal		-4(%ebp), %eax
	movl		%eax, -8(%ebp)
	movl		-8(%ebp), %eax
	movl		%eax, -8(%ebp)
	leal		-8(%ebp), %eax
	addl		$2, (%eax)
	subl		$8, %esp
	pushl	-8(%ebp)
	movl		$.LC0, %eax
	pushl	%eax
	call		printf
	addl		$16, %esp
	leal		-8(%ebp), %eax
	addl		$2, (%eax)
	subl		$8, %esp
	pushl	-8(%ebp)
	movl		$.LC0, %eax
	pushl		%eax
	call		printf
	addl		$16, %esp
	leal		-8(%ebp), %eax
	addl		$2, (%eax)
	subl		$8, %esp
	pushl	-8(%ebp)
	movl		$.LC0, %eax
	pushl	%eax
	call		printf
	addl		$16, %esp
	leal		-8(%ebp), %eax
	addl		$2, (%eax)
	subl		$8, %esp
	pushl	-8(%ebp)
	movl		$.LC0, %eax
	pushl	%eax
	call		printf
	addl		$16, %esp
	movl		$0, %eax
	leave
	ret
	.size	main, .-main
	.section	.note.GNU-stack,"",@progbits
	.ident	"GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"

〔リスト6〕オプションなしで生成されたアセンブラ・ソース(test214.s)
	.file	"test214.c"
	.section	.rodata
.LC0:
	.string	"%x\n"
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl		%esp, %ebp
	subl		$8, %esp
	andl		$-16, %esp
	movl		$0, %eax
	subl		%eax, %esp
	movl		$1, -4(%ebp)
	leal		-4(%ebp), %eax
	movl		%eax, -8(%ebp)
	leal		-8(%ebp), %eax
	addl		$2, (%eax)
	subl		$8, %esp
	pushl	-8(%ebp)
	pushl	$.LC0
	call		printf
	addl		$16, %esp
	leal		-8(%ebp), %eax
	addl		$2, (%eax)
	subl		$8, %esp
	pushl	-8(%ebp)
	pushl	$.LC0
	call		printf
	addl		$16, %esp
	leal		-8(%ebp), %eax
	addl		$2, (%eax)
	subl		$8, %esp
	pushl	-8(%ebp)
	pushl	$.LC0
	call		printf
	addl		$16, %esp
	leal		-8(%ebp), %eax
	addl		$2, (%eax)
	subl		$8, %esp
	pushl	-8(%ebp)
	pushl	$.LC0
	call		printf
	addl		$16, %esp
	movl		$0, %eax
	leave
	ret
	.size	main, .-main
	.section	.note.GNU-stack,"",@progbits
	.ident	"GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"

〔リスト7〕-fforce-memオプションを付けて生成されたアセンブラ・ソース(test215a.s)
	.file	"test215.c"
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%x\n"
	.text
	.p2align 2,,3
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl		%esp, %ebp
	subl		$8, %esp
	andl		$-16, %esp
	subl		$8, %esp
	leal		-2(%ebp), %eax
	pushl	%eax
	pushl	$.LC0
	movl		$1, -4(%ebp)
	call		printf
	popl		%edx
	popl		%ecx
	pushl	%ebp
	pushl	$.LC0
	call		printf
	popl		%edx
	popl		%ecx
	leal		2(%ebp), %eax
	pushl	%eax
	pushl	$.LC0
	call		printf
	popl		%edx
	popl		%ecx
	leal		4(%ebp), %eax
	pushl	%eax
	pushl	$.LC0
	call		printf
	xorl		%eax, %eax
	leave
	ret
	.size	main, .-main
	.section	.note.GNU-stack,"",@progbits
	.ident	"GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"

〔リスト8〕フレーム・ポインタを使用しない例(test216.c)
//フレームポインタをレジスタに持たない例
#include <stdio.h>
void sub1(void);
void sub2(void);
void sub3(void);
int main()
{
	sub1();
	sub2();
	sub3();
	return 0;
}
void sub1(void)
{
	int	a;
	int	b;
	a	=	10;
	b	=	20;
	return;
}
void sub2(void)
{
	int	a;
	int	b;
	a	=	11;
	b	=	21;
	return;
}
void sub3(void)
{
	int	a;
	int	b;
	a	=	12;
	b	=	22;
	return;
}

〔リスト9〕オプションを付けて生成されたアセンブラ・ソース(test216a.s)
	.file	"test216.c"
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl		%esp, %ebp
	subl		$8, %esp
	andl		$-16, %esp
	movl		$0, %eax
	subl		%eax, %esp
	call		sub1
	call		sub2
	call		sub3
	movl		$0, %eax
	leave
	ret
	.size	main, .-main
.globl sub1
	.type	sub1, @function
sub1:
	subl		$8, %esp
	movl		$10, 4(%esp)
	movl		$20, (%esp)
	addl		$8, %esp
	ret
	.size	sub1, .-sub1
.globl sub2
	.type	sub2, @function
sub2:
	subl		$8, %esp
	movl		$11, 4(%esp)
	movl		$21, (%esp)
	addl		$8, %esp
	ret
	.size	sub2, .-sub2
.globl sub3
	.type	sub3, @function
sub3:
	subl		$8, %esp
	movl		$12, 4(%esp)
	movl		$22, (%esp)
	addl		$8, %esp
	ret
	.size	sub3, .-sub3
	.section	.note.GNU-stack,"",@progbits
	.ident	"GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"

〔リスト10〕オプションなしで生成されたアセンブラ・ソース(test216.s)
	.file	"test216.c"
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl		%esp, %ebp
	subl		$8, %esp
	andl		$-16, %esp
	movl		$0, %eax
	subl		%eax, %esp
	call		sub1
	call		sub2
	call		sub3
	movl		$0, %eax
	leave
	ret
	.size	main, .-main
.globl sub1
	.type	sub1, @function
sub1:
	pushl	%ebp
	movl		%esp, %ebp
	subl		$8, %esp
	movl		$10, -4(%ebp)
	movl		$20, -8(%ebp)
	leave
	ret
	.size	sub1, .-sub1
.globl sub2
	.type	sub2, @function
sub2:
	pushl	%ebp
	movl		%esp, %ebp
	subl		$8, %esp
	movl		$11, -4(%ebp)
	movl		$21, -8(%ebp)
	leave
	ret
	.size	sub2, .-sub2
.globl sub3
	.type	sub3, @function
sub3:
	pushl	%ebp
	movl		%esp, %ebp
	subl		$8, %esp
	movl		$12, -4(%ebp)
	movl		$22, -8(%ebp)
	leave
	ret
	.size	sub3, .-sub3
	.section	.note.GNU-stack,"",@progbits
	.ident	"GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"
前のページ | 次のページ
NEW記事内インデックス    連載インデックスはこちら   Interfaceのトップ
最適化オプション
◆リスト

Copyright 2004 岸 哲夫

Copyright 1997-2004 CQ Publishing Co.,Ltd.