Andes Workshop

It is currently Sun Nov 17, 2019 3:07 pm

All times are UTC + 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: 避免某些程式碼被optimize的方法
PostPosted: Wed Dec 28, 2011 5:55 pm 
Offline
User avatar

Joined: Fri Mar 04, 2011 9:36 pm
Posts: 500
參考語法:
http://gcc.gnu.org/onlinedocs/gcc/Funct ... agmas.html
下面提供2個寫法,
其中寫法2會有warning出現,說這個語法在這個machine不支援,其實它的結果是正確的。

寫法1: 只針對1個function。
__attribute__((optimize("O0")))
int add (int a, int b )
{
int x = a;
int y = b;
return x + y;
}

int main ()
{
int r = 1;
int a = r;
int b = r;
func ();
return 0;
}

寫法2:包含在裡面的code都不會被optimize。
注意!這個寫法不一定要以function為範圍,
可以任意選取一段code。

#pragma GCC push_options
#pragma GCC optimize ("O0")

int add (int a, int b )
{
int x = a;
int y = b;
return x + y;
}

#pragma GCC pop_options
int main ()
{
int r = 1;
int a = r;
int b = r;
func ();
return 0;
}

#pragma GCC push_options
#pragma GCC pop_options
這2行是表示會先把原來的option push進去,
例如是-Os,
後面再把-Os pop回來,恢復原來的optimize設定。

關鍵字
optimize, optimization,最佳化,優化


Top
 Profile Send private message E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group