Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Constant expression optimization

Reply
Thread Tools

Constant expression optimization

 
 
jacob navia
Guest
Posts: n/a
 
      10-20-2008
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.

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?

Thanks in advance for your attention.



--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      10-20-2008
On Oct 20, 8:19 pm, jacob navia <(E-Mail Removed)> wrote:
> 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>

> What other functions would you add?


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

 
Reply With Quote
 
 
 
 
Walter Banks
Guest
Posts: n/a
 
      10-20-2008


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.


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..


 
Reply With Quote
 
Nate Eldredge
Guest
Posts: n/a
 
      10-20-2008
jacob navia <(E-Mail Removed)> writes:

> 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.


What will sqrt(-1.0) do?

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-20-2008
Nate Eldredge wrote:

> jacob navia <(E-Mail Removed)> writes:
>
> > 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.

>
> What will sqrt(-1.0) do?


What would you imagine?




Brian
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      10-20-2008
On Oct 20, 9:00 pm, Walter Banks <(E-Mail Removed)> wrote:
> 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.

>
> There are two potential problems with this.
>


<snip>

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


If the library is conforming, it should be the same result as I
understand it.
Also, if he redefines the function, he's invoking undefined behavior
and anything can happend.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-20-2008
Walter Banks wrote:
>
> 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.

>
> 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
>

These optimisations are already common.

--
Ian Collins
 
Reply With Quote
 
Nate Eldredge
Guest
Posts: n/a
 
      10-20-2008
"Default User" <(E-Mail Removed)> writes:

> Nate Eldredge wrote:
>
>> jacob navia <(E-Mail Removed)> writes:
>>
>> > 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.

>>
>> What will sqrt(-1.0) do?

>
> What would you imagine?


Well, by my reading of 7.12 (9) and 7.12.7.5 (2), it should either set
errno to EDOM or raise a floating-point exception, or both, depending on
the value of math_errhandling.

Incidentally, where in the standard is "floating-point exception"
defined, and is it stated exactly what happens when a floating-point
exception is raised? A natural assumption would be that it raises the
signal SIGFPE but I can't find that explicitly stated.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-20-2008
Nate Eldredge wrote:

> "Default User" <(E-Mail Removed)> writes:
>
> > Nate Eldredge wrote:


> >> What will sqrt(-1.0) do?

> >
> > What would you imagine?

>
> Well, by my reading of 7.12 (9) and 7.12.7.5 (2)


You aren't much fun.




Brian
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      10-20-2008
Eric Sosman wrote:
> Are you doing anything with pow(any, integer)? Changing
> pow(x, 2.0) into x*x, that sort of thing?
>


When the second argument is an integer I call ipow.
I did not think about that specific optimization. Thanks.

>> 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?

>
> 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?
>



Well it can be more complicated than that...

When I extend constant folding into doubles (I do now
only integers constant folding) I will be able
to use this too, enhancing constant folding across
function calls.


--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Zero Optimization and Sign Optimization??? Ravikiran C Programming 22 11-24-2008 03:19 AM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Case expression must be constant expression Philipp Java 26 11-25-2007 10:10 PM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM



Advertisments