Bug 1475 – allow to use intrinsic math functions (e.g. trigonometry) in CTFEs
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-09-05T15:05:00Z
Last change time
2014-02-17T22:50:29Z
Assigned to
bugzilla
Creator
dlang-bugzilla
Comments
Comment #0 by dlang-bugzilla — 2007-09-05T15:05:08Z
The compiler already supports some math functions, like the trigonometric ones, as intrinsics, since it inlines them even when -inline is not specified. It would greatly add to CTFEs if it were possible to call such functions in compile-time functions. For example, Pi could be calculated at compile-time using:
const double PI = atan(1)*4;
Sine tables could also be pre-calculated at compile time, etcetera.
Comment #1 by dlang-bugzilla — 2007-09-05T15:06:22Z
*** Bug 1476 has been marked as a duplicate of this bug. ***
Comment #2 by wbaxter — 2007-09-05T16:10:51Z
Of course you can already do this by writing your own CTFE functions to approximate those via Taylor series and the like. One of the first CTFE demos was a compile-time sqrt function.
But yeh, the compiler has access to a better way to evaluate those functions than running what amounts to an interpreted language, so it would be nice to be able to access it. I can imagine some sort of plugin system where you'd write some D-code, compile it to a dll, and expose new compile-time functions to the compiler via that dll (much as you write extensions for other scripting languages). And of course those initial CTFE compiler extensions could be used to compile further CTFE compiler extensions... and so on.
Comment #3 by sean — 2007-09-05T17:20:22Z
[email protected] wrote:
>
> ------- Comment #2 from [email protected] 2007-09-05 16:10 -------
> Of course you can already do this by writing your own CTFE functions to
> approximate those via Taylor series and the like. One of the first CTFE demos
> was a compile-time sqrt function.
>
> But yeh, the compiler has access to a better way to evaluate those functions
> than running what amounts to an interpreted language, so it would be nice to be
> able to access it.
It was my impression that CTFE relies on a fancy version of constant
folding, which doesn't make support for any special routines or plugins
a really natural fit. And while it seems like a potentially interesting
feature to add, I wonder whether there might be issues related to cross
compilation.
Comment #4 by wbaxter — 2007-09-05T19:46:58Z
> Sine tables could also be pre-calculated at compile time, etcetera.
The ironic thing is that if you wanted to implement a CTFE sine function right now, table lookup is probably the most reasonable way to do it.
Anyway, I like the general idea, and I think the same thing goes for many other things, like string formatting functions. There's a perfectly fine string formatter in std.string.format. It's a shame the compiler isn't able to invoke it during compile time.
Comment #5 by clugdbug — 2009-04-06T03:11:01Z
The intrinsics were included in constant folding in DMD2.005.
It'd be good to have a way of detecting CTFE inside a function, so that the other math functions could be made CTFE-able (many of them use asm) -- but that's a different issue.