常量表达式优化 [英] Constant expression optimization

查看:101
本文介绍了常量表达式优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在为lcc-win添加一个优化:


sqrt(2.0)现在会激发常数1.4142。 ..等等将生成

而不是生成实际的电话。


详情:

-------


编译器以最高精度进行所有计算

可用(long double),然后将结果转换为

(可能)根据需要降低精度。


例如:

float f = sqrtf(2.0f);


编译器将输入常量转换为long double

精度常量,然后将结果转换为浮点数

常量。


这段代码:

long double ld = sqrt(2.0f);


将激活对sqrt(2.0f)的调用,转换为2.0 br />
从一个浮点常数变成一个long double,然后

结果再次从一个double转换成一个浮点数,然后

再次抛出做的将一个浮点数分配给一个长的

加倍。通过的实际价值将是

1.414213538169861

而不是
1.41421356237309504

因为它应该是长双精度。

我希望我没有忘记一些模糊的规则。如果你想到这个优化的一些问题,请



只需回答。


支持的功能是大多数功能math.h

(sqrt acos sin / cos)等


稍后我将添加常量字符串的strlen。 (strlen(你好))


你还会添加什么其他功能?


提前感谢您的关注。


-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique http://www.cs.virginia.edu/~lcc-win32

推荐答案

10月20日晚上8:19,jacob navia< ja ... @ nospam.comwrote:
On Oct 20, 8:19 pm, jacob navia <ja...@nospam.comwrote:




我正在为lcc-win添加优化:


sqrt( 2.0)现在将激发常量1.4142 ...等将生成
而不是生成实际调用。
Hi

I am adding an optimization to lcc-win:

sqrt(2.0) will provoke now that the constant 1.4142... etc will be
generated instead of generating an actual call.



< snip>

<snip>


您还要添加哪些其他功能?
What other functions would you add?



我会添加memset(& obj,0,sizeof obj);如T obj = {0};如果memset是obj的第一次操作。

I''d add memset(&obj, 0, sizeof obj); as T obj = {0}; if memset is the
first operation on obj.


jacob navia写道:
jacob navia wrote:




我正在为lcc-win添加优化:


sqrt(2.0)现在会激发常数1.4142 ...等将生成

而不是产生实际的通话。


详情:

---- ---


编译器以最高精度进行所有计算

可用(long double),然后将结果转换为

(可能)根据需要降低精度。


例如:

float f = sqrtf(2.0f);

编译器将输入常量转换为long double

精度常量,然后将结果转换为浮点数

常量。

此代码:

long double ld = sqrt(2.0f);


将激发对sqrt(2.0f)的调用2.0转换
从一个浮动常数变成一个长双,然后

结果再次从一个双重投入浮动,然后

再次抛出来做将一个浮点数分配给一个长的

加倍。通过的实际价值将是

1.414213538169861

而不是
1.41421356237309504

因为它应该是长双精度。


我希望我没有忘记一些模糊的规则。如果你想到这个优化的一些问题,请



只需回答。
Hi

I am adding an optimization to lcc-win:

sqrt(2.0) will provoke now that the constant 1.4142... etc will be
generated instead of generating an actual call.

Details:
-------

The compiler does all calculation in the highest precision
available (long double), and then transforms the result into
the (maybe) lower precision as required.

For instance:
float f = sqrtf(2.0f);

The compiler transforms the input constant in a long double
precision constant, then transforms the result into a float
constant.

This code:
long double ld = sqrt(2.0f);

will provoke a call to sqrt(2.0f) with the 2.0 transformed
into a long double from a float constant, then the
result cast again down into a float from a double, then
cast up again to do the assignment of a float into a long
double. The actual value passed will be
1.414213538169861
and not
1.41421356237309504
as it should be in long double precision.
I hope I did not forget some obscure rule in this. Please
if you think about some problems with this optimization
just answer.



我认为你已经确定了主要问题,即两个

舍入并不总是给出相同的结果一。但是,对于浮点数结果的准确性来说,很少有保证这对于b的
,你可能会因为轻微的邋get而侥幸逃脱。

如果有一个编译器选项来禁用捏造,可能

有助于保持数字作家的快乐。


你在做什么与pow(任何,整数)?改变

pow(x,2.0)到x * x,那种东西?

I think you''ve identified the main issue, which is that two
roundings don''t always give the same result as one. But there
are so few guarantees about the accuracy of floating-point results
in C that you can probably get away with the slight sloppiness.
If there''s a compiler option to disable the fudging, that might
help keep the numericists happy.

Are you doing anything with pow(any, integer)? Changing
pow(x, 2.0) into x*x, that sort of thing?


支持的函数是大多数函数math.h

(sqrt acos sin / cos)等


稍后我将添加常量字符串的strlen。 (strlen(你好))


你还会添加哪些其他功能?
The functions supported are most of the functions in math.h
(sqrt acos sin/cos) etc.

Later I will add strlen of constant strings. (strlen("hello"))

What other functions would you add?



如果你能获得代表的话收集程序

并对它们进行描述,你可能能够获得更好的指导,而不仅仅是通过询问来实现优化的价值。例如,你是否有理由相信某些程序经常评估sqrt(2.0)

足以用1.414替换它...会有很大的帮助吗? br />

-
Er ********* @ sun.com

If you can get hold of a "representative" collection of programs
and profile them, you may be able to get better guidance on what''s
worth optimizing than just by asking around. For example, do you
have reason to believe that some programs evaluate sqrt(2.0) often
enough that replacing it with 1.414... would be a big help?

--
Er*********@sun.com





jacob navia写道:


jacob navia wrote:

我希望我没有忘记一些模糊的规则。如果你想到这个优化的一些问题,请



只需回答。
I hope I did not forget some obscure rule in this. Please
if you think about some problems with this optimization
just answer.



这有两个潜在的问题。

1)你如何确保内部计算将是

与实际图书馆发生的情况相同

电话。


2)如果有人使用他们自己的库并且它会发生什么? >
产生与内部计算不同的结果

或完全重新定义函数

w ..

There are two potential problems with this.
1) How do you make sure the internal calculation will be the
same as what would have happened from an actual library
call.

2) What happens if someone uses their own library and it
produces a different result from your internal calculation
or redefines the function completely
w..


这篇关于常量表达式优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆