<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="moz-cite-prefix">Am 17.02.2024 um 20:18 schrieb Florian
Klämpfl via fpc-pascal:<br>
</div>
<blockquote type="cite"
cite="mid:733BB5AC-34E7-4F82-BAB8-16E42BAB23EB@freepascal.org">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<br>
<div>
<blockquote type="cite">
<div>
<div>
<p> const Xconst : single = 1440.0; </p>
<p>var y1, y2 : real; </p>
<p>y1 := 33.0 / 1440.0; </p>
</div>
</div>
</blockquote>
<blockquote type="cite">
<div>
<div>
<p>y2 := 33.0 / Xconst; <br>
</p>
</div>
</div>
</blockquote>
<div>
<blockquote type="cite">
<p>the division in the first assignment (to y1) should be
done at maximum precision, that is, <br>
both constants should be converted by the compiler to the
maximum available precision and <br>
the division should be done (best at compile time) using
this precision. <br>
</p>
</blockquote>
<div>
<p>Constant folding is an optimization technique, so the
first expression could be also evaluated at run time in
case of a simple compiler (constant folding is not
something which is mandatory) which means that we have to
use always full precision (what full means depends on the
host and target platform thought) for real operations. So
either: always full precision with the result all
operations get bloated or some approach to assign a
precision to real constants.</p>
</div>
</div>
</div>
</blockquote>
<p>no problem here; the result of y1 must be the same, no matter if
the computation is done at compile time or at run time. <br>
the result should always be computed at the best precision
available, IMO (maybe controlled by a compiler option, <br>
which I personally would set). <br>
</p>
<p>y2: the computation could be done using single precision, because
the second operand says so. <br>
IMO: even if the first operand was a literal constant which
cannont be represented exactly in a single FP field <br>
</p>
<blockquote type="cite"
cite="mid:733BB5AC-34E7-4F82-BAB8-16E42BAB23EB@freepascal.org">
<div>
<div>
<div>
<p>It gets even more hairy if more advanced optimization
techniques are involved:</p>
</div>
</div>
Consider</div>
<div><br>
</div>
<div>var</div>
<div> y1,y2 : single;</div>
<div><br>
</div>
<div> y1 := 1440.0</div>
<div> y2 := 33.0 / y1;</div>
<div><br>
</div>
<div>When constant propagation and constant folding are on (both
are optimizations), y2 can be calculated at compile time and
everything reduced to one assignment to y2. So with your
proposal the value of y2 would differ depending on the
optimization level.</div>
</blockquote>
<p>if y2 is computed at compile time (which is fine), then the
result IMO is determined by the way the source code is written. <br>
A possible optimization must not change the meaning of the
program, given by the source code. <br>
So in this case, the compiler would have to do a single precision
division (if we could agree on the rules that we discussed so
far), <br>
and the meaning of the program may not be changed by optimization
techniques (that is: optimization may not change the <br>
result to a double or extended precision division ... otherwise
the optimization is wrong). <br>
</p>
<p>BTW: many of the ideas about what a compiler should do come from
my 30+ years experience with PL/1. <br>
That may be a sort of "deformation professionelle", as the French
call it, but that's how it is. <br>
</p>
<p>Apart from the proper handling of literal FP constants (which is
what we discuss here, IMO), there is another topic, <br>
which is IMO also part of the debate: <br>
</p>
<p>does <br>
</p>
<p> y2 := 33.1 / y1;</p>
<p>require the division to be done at single precision or not? <br>
</p>
<p>We have here a literal constant, which is NOT single (33.1) and a
single variable operand. <br>
I understood from some postings here, that some people want the
divisions with singles carried out using <br>
single arithmetic, for performance reasons, so I asked for a
single division here (in my previous postings). <br>
But IMO that's different in the current implementation ... what do
others think about this? <br>
</p>
<p>I, for my part, would find it strange, if the precision of the
division in this case would depend on the (implicit) <br>
type of the operand, that is: <br>
</p>
<p> y2 := 33.015625 / y1; { single precision, because constant is
single - 33 + 1 / 64 }<br>
y2 := 33.1 / y1; { extended precision, because constant is
extended } <br>
</p>
<p>IMO, both of these divisions should be done at single precision,
controlled by the type of y1. <br>
But this could be controlled by ANOTHER new option, if someone
asks for it. <br>
</p>
<p>Kind regards</p>
<p>Bernd <br>
</p>
<blockquote type="cite"
cite="mid:733BB5AC-34E7-4F82-BAB8-16E42BAB23EB@freepascal.org">
<div>
<blockquote type="cite">
<div>
<div>
<p> </p>
<span style="white-space: pre-wrap">
</span></div>
</div>
</blockquote>
</div>
</blockquote>
</body>
</html>