Andes Workshop
http://forum.andestech.com/

避免某些程式碼被optimize的方法
http://forum.andestech.com/viewtopic.php?f=16&t=225
Page 1 of 1

Author:  cindy [ Wed Dec 28, 2011 5:55 pm ]
Post subject:  避免某些程式碼被optimize的方法

參考語法:
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,最佳化,優化

Page 1 of 1 All times are UTC + 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/