Constant expression optimization

H

Harald van Dijk

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.

7.6p1:
"A /floating-point status flag/ is a system variable whose value is set
(but never cleared) when a /floating-point exception/ is raised, which
occurs as a side effect of exceptional floating-point arithmetic to
provide auxiliary information."

Floating-point exception is in italics, meaning this is the definition of
the term, from which you can conclude that when a fp exception is raised,
the only thing that happens is a status flag is set.

Raising SIGFPE cannot be done by a conforming implementation (ignoring
calls to raise) unless the behaviour is already outside the scope of the
standard, in which case there is no requirement to raise SIGFPE.

("Warning: The posting profile's server doesn't carry newsgroup
"comp.compilers.lcc"." I've simply dropped it.)
 
D

Default User

Nate said:
Ah, indeed.

Well done, sir. Treat yourself to a richly-deserved defenestration.
;-)

Heyyyy. That was my joke. I deserve the . . . er, never mind.




Brian
 
C

CBFalconer

christian.bau said:
If the right standard header file <math.h> is included, then I
think the compiler can assume that sqrt () is the actual
standard library function, and if the programmer replaces it
with something else, it's their own fault. If the programmer
just adds a declaration himself like

Whether or not <math.h> is included, strictly speaking the function
sqrt() may not be replaced. In practice, most systems will allow
it.
 
M

Mark L Pappin

Flash Gordon said:
something like...
double d1 = complex_but_constant_expression;

To help avoid misunderstandings it might be best to use the phrase
"non-trivial" in place of "complex" when that is the meaning you
intend (as I believe it is here) as "complex" has another meaning
which is also relevant in this specific context.

mlp
 
R

robertwessel2

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.


That sort of optimization can be pretty straight-forward if you do
link time code generation. If the functions are in C, constant
propagation/evaluation will naturally reduce sqrt(2) to a constant.
Even if you have the functions implemented in assembler, if you can
tag their object in a recognizable way (perhaps a switch in
the .drectve section*) they can be evaluated during the code
generation step.


*you’re using PE, right?
 
T

toby

...
     (Personal note: Yes, I've overridden an implementation's sqrt()

For the record I've had to do it with vendor qsort() as well (crashing
bug - crash on entry iirc).

--T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top