[fpc-devel] Pure function Wiki page
J. Gareth Moreton
gareth at moreton-family.com
Sun Jul 8 19:22:42 CEST 2018
At the same time, I would argue that, really, the programmer should code
something like this:
c := sin(x);if c > y then b := c;
There probably isn't any harm in supporting such optimisation though.
Gareth
On Sun 08/07/18 18:19 , "J. Gareth Moreton" gareth at moreton-family.com
sent:
The concept of pure functions is that it allows for their evaluation at
compile time for constant arguments. In your example, function purity
wouldn't play into it because x, I assume, is a variable whose value is
unknown at compile time. However, advanced data flow analysis might be
able to spot that x hasn't changed, and marking 'sin' as pure would indeed
be an advantage because the first call's result can be stored for later
use, and the second function call removed, knowing it won't cause
side-effects.
I would say that that is currently beyond the scope of the proposal, but
given that I've been working on a deep optimizer with the intention of
optimising code where div and mod are used together with the same arguments
(in this case, the pure 'function' is the div operator), that is definitely
something I would like to come back to - thanks for the idea!
Gareth
On Sun 08/07/18 19:00 , Martin fpc at mfriebe.de sent: On 08/07/2018 16:50,
J. Gareth Moreton wrote:
Hi everyone,
With some blessing from Florian on the concept, I've set up a Wiki page
discussing the design proposals for the support of pure functions, as well
as some explanation on what they actually are.
wiki.freepascal.org/Pure_functions
I hope it proves useful to explain what I'm doing. How do the proposals
look so far, Florian?
I seem to be missing a part of the feature (or did I misunderstand (part
of) the side?
It is all about the compiler evaluating pure functions at compile time,
and placing the result as a constant in the code. (Or at least that's how I
read it).
That's all fine.
But what about re-using results at runtime.
b := sin(x);
If x is unknown this can not be computed at compile time.
But
if sin(x) > y then b := sin(x);
Knowing that sin is pure AND knowing that x did not change, means only one
call is needed.
Is that also planed?
_______________________________________________
fpc-devel maillist - fpc-devel at lists.freepascal.org [1]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
[2]">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
_______________________________________________
fpc-devel maillist - fpc-devel at lists.freepascal.org [3]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
[4]">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Links:
------
[1] mailto:fpc-devel at lists.freepascal.org
[2] http://secureweb.fast.net.uk/ http:=
[3] mailto:fpc-devel at lists.freepascal.org
[4] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180708/f37d6e27/attachment.html>
More information about the fpc-devel
mailing list