digitalmars.D - Compile time code paths
- Jeremie Pelletier (1/1) Aug 08 2009 If a function has both an asm and D implementations inside its body, and...
- Daniel Keep (6/7) Aug 08 2009 Not that I know of. There's no way to switch based on run time/compile
- David Gileadi (2/11) Aug 10 2009 Is this a case for version(CompileTime){}?
- Jeremie Pelletier (10/22) Aug 10 2009 No because it would also compile this block into the binary.
- Don (10/34) Aug 11 2009 I think something like this might work:
- Jeremie Pelletier (2/29) Aug 11 2009 Good idea, I agree with the use of __traits here. The syntax looks great...
If a function has both an asm and D implementations inside its body, and the D version can be executed at compile time, but the asm one is much faster at runtime. Is it possible to have the compiler use the D code path at compile time (ie to fill in enums and whatnot), and have the asm version available at runtime.
Aug 08 2009
Jeremie Pelletier wrote:If a function has both an asm and D implementations inside its body, and the D version can be executed at compile time, but the asm one is much faster at runtime. Is it possible to have the compiler use the D code path at compile time (ie to fill in enums and whatnot), and have the asm version available at runtime.Not that I know of. There's no way to switch based on run time/compile time. This was going to be solved, at least in part, using static arguments, but that got dropped. As it stands, you just have to use a suffix or prefix or something to distinguish CTFE methods from runtime methods.
Aug 08 2009
Daniel Keep wrote:Jeremie Pelletier wrote:Is this a case for version(CompileTime){}?If a function has both an asm and D implementations inside its body, and the D version can be executed at compile time, but the asm one is much faster at runtime. Is it possible to have the compiler use the D code path at compile time (ie to fill in enums and whatnot), and have the asm version available at runtime.Not that I know of. There's no way to switch based on run time/compile time. This was going to be solved, at least in part, using static arguments, but that got dropped. As it stands, you just have to use a suffix or prefix or something to distinguish CTFE methods from runtime methods.
Aug 10 2009
David Gileadi Wrote:Daniel Keep wrote:No because it would also compile this block into the binary. The easy way is of course to have different symbols for compile-time and run-time. But this doesn't go well with generic programming where the function needing such a check can be deep in the compile-time call stack. For example: int foo() { return bar + 1; } int bar() { return foobar * 2; } int foobar() { static if(isCompileTime) return ...; /// asm cannot execute at compile time, needed to keep foo and bar able to do CTFE else asm { ...; } /// asm optimized for runtime }Jeremie Pelletier wrote:Is this a case for version(CompileTime){}?If a function has both an asm and D implementations inside its body, and the D version can be executed at compile time, but the asm one is much faster at runtime. Is it possible to have the compiler use the D code path at compile time (ie to fill in enums and whatnot), and have the asm version available at runtime.Not that I know of. There's no way to switch based on run time/compile time. This was going to be solved, at least in part, using static arguments, but that got dropped. As it stands, you just have to use a suffix or prefix or something to distinguish CTFE methods from runtime methods.
Aug 10 2009
Jeremie Pelletier wrote:David Gileadi Wrote:I think something like this might work: static if (__traits(CompileTime)) { ... // ctfe code } else { asm { ... } } Not sure what the exact syntax should be.Daniel Keep wrote:No because it would also compile this block into the binary.Jeremie Pelletier wrote:Is this a case for version(CompileTime){}?If a function has both an asm and D implementations inside its body, and the D version can be executed at compile time, but the asm one is much faster at runtime. Is it possible to have the compiler use the D code path at compile time (ie to fill in enums and whatnot), and have the asm version available at runtime.Not that I know of. There's no way to switch based on run time/compile time. This was going to be solved, at least in part, using static arguments, but that got dropped. As it stands, you just have to use a suffix or prefix or something to distinguish CTFE methods from runtime methods.The easy way is of course to have different symbols for compile-time and run-time. But this doesn't go well with generic programming where the function needing such a check can be deep in the compile-time call stack. For example: int foo() { return bar + 1; } int bar() { return foobar * 2; } int foobar() { static if(isCompileTime) return ...; /// asm cannot execute at compile time, needed to keep foo and bar able to do CTFE else asm { ...; } /// asm optimized for runtime }
Aug 11 2009
Don Wrote:Jeremie Pelletier wrote:Good idea, I agree with the use of __traits here. The syntax looks great to me as it is.David Gileadi Wrote:I think something like this might work: static if (__traits(CompileTime)) { ... // ctfe code } else { asm { ... } } Not sure what the exact syntax should be.Daniel Keep wrote:No because it would also compile this block into the binary.Jeremie Pelletier wrote:Is this a case for version(CompileTime){}?If a function has both an asm and D implementations inside its body, and the D version can be executed at compile time, but the asm one is much faster at runtime. Is it possible to have the compiler use the D code path at compile time (ie to fill in enums and whatnot), and have the asm version available at runtime.Not that I know of. There's no way to switch based on run time/compile time. This was going to be solved, at least in part, using static arguments, but that got dropped. As it stands, you just have to use a suffix or prefix or something to distinguish CTFE methods from runtime methods.
Aug 11 2009