digitalmars.D.learn - Compile-time evaluation of real expressions?
- H. S. Teoh (22/22) Jan 06 2012 Hi All,
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (5/25) Jan 06 2012 Most likely those functions are just implemented using inline assembly,
- Jonathan M Davis (13/15) Jan 06 2012 Yeah, several functions in std.math use inline assembly. So, for them to...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/4) Jan 06 2012 http://d.puremagic.com/issues/
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (7/22) Jan 06 2012 Allowing asm in CTFE would probably be way more work than it's worth.
- H. S. Teoh (13/32) Jan 06 2012 function is probably the least painful way to go.
- Robert Clipsham (7/12) Jan 06 2012 Doing this would mean you can't do cross-compilation, eg using x86 to
- H. S. Teoh (10/20) Jan 06 2012 [...]
- Robert Clipsham (13/31) Jan 06 2012 version(X86) asm
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (4/34) Jan 07 2012 Executing asm at compile time is also a security risk.
- Timon Gehr (2/12) Jan 06 2012 If the if condition is a constant, there is no runtime overhead.
- Jonathan M Davis (13/29) Jan 06 2012 em to
Hi All, As I understand it, compile-time execution *should* be able to evaluate floating-point functions, correct? Currently, I have this code: private static real cross_angles[6] = [ real.nan, real.nan, real.nan, atan(sqrt(5)), PI_4, atan(sqrt(5)-2) ]; But the compiler is complaining: /mnt/1/usr/include/d2/4.6/std/math.d:623: Error: asm statements cannot be interpreted at compile time /mnt/1/usr/include/d2/4.6/std/math.d:591: Error: cannot evaluate atan2(x,1.0e+0L) at compile time Is this an artifact of using gdc-4.6 instead of dmd? Or are certain floating-point functions not allowed in compile-time expressions? Thanks! P.S. I just starting learning and using D recently -- and totally loving it. T -- Knowledge is that area of ignorance that we arrange and classify. -- Ambrose Bierce
Jan 06 2012
On 06-01-2012 23:21, H. S. Teoh wrote:Hi All, As I understand it, compile-time execution *should* be able to evaluate floating-point functions, correct? Currently, I have this code: private static real cross_angles[6] = [ real.nan, real.nan, real.nan, atan(sqrt(5)), PI_4, atan(sqrt(5)-2) ]; But the compiler is complaining: /mnt/1/usr/include/d2/4.6/std/math.d:623: Error: asm statements cannot be interpreted at compile time /mnt/1/usr/include/d2/4.6/std/math.d:591: Error: cannot evaluate atan2(x,1.0e+0L) at compile time Is this an artifact of using gdc-4.6 instead of dmd? Or are certain floating-point functions not allowed in compile-time expressions? Thanks! P.S. I just starting learning and using D recently -- and totally loving it. TMost likely those functions are just implemented using inline assembly, therefore not usable in CTFE. -- - Alex
Jan 06 2012
On Saturday, January 07, 2012 00:03:39 Alex Rønne Petersen wrote:Most likely those functions are just implemented using inline assembly, therefore not usable in CTFE.Yeah, several functions in std.math use inline assembly. So, for them to be able to be used at compile time, either the compiler must be expanded to be able to run asm statements at compile time (which may or may not be planned and may or may not be reasonable), or those functions need another branch (using __cfte in an if condition) which doesn't use assembly. Or I suppose that if the extra check for __ctfe isn't considered particularly acceptable (after all, they're already using assembly), then separate functions meant specifically for CTFE would be necessary. I don't know what Don's preferred approach would be, but presumably, it would be up to him since he's the primary maintainer of both CTFE and std.math. An enhancement request for it should probably be opened: d.puremagi.com/issues - Jonathan M Davis
Jan 06 2012
On 01/06/2012 03:37 PM, Jonathan M Davis wrote:An enhancement request for it should probably be opened: d.puremagi.com/issueshttp://d.puremagic.com/issues/ Ali
Jan 06 2012
On 07-01-2012 00:37, Jonathan M Davis wrote:On Saturday, January 07, 2012 00:03:39 Alex Rønne Petersen wrote:Allowing asm in CTFE would probably be way more work than it's worth. You'd basically need full-blown analysis of x86 assembly plus an interpreter. Even then, x86 is not typed, so it's going to be a major pain... -- - AlexMost likely those functions are just implemented using inline assembly, therefore not usable in CTFE.Yeah, several functions in std.math use inline assembly. So, for them to be able to be used at compile time, either the compiler must be expanded to be able to run asm statements at compile time (which may or may not be planned and may or may not be reasonable), or those functions need another branch (using __cfte in an if condition) which doesn't use assembly. Or I suppose that if the extra check for __ctfe isn't considered particularly acceptable (after all, they're already using assembly), then separate functions meant specifically for CTFE would be necessary. I don't know what Don's preferred approach would be, but presumably, it would be up to him since he's the primary maintainer of both CTFE and std.math. An enhancement request for it should probably be opened: d.puremagi.com/issues - Jonathan M Davis
Jan 06 2012
On Sat, Jan 07, 2012 at 12:49:46AM +0100, Alex Rønne Petersen wrote:On 07-01-2012 00:37, Jonathan M Davis wrote:[...]On Saturday, January 07, 2012 00:03:39 Alex Rønne Petersen wrote:Most likely those functions are just implemented using inline assembly, therefore not usable in CTFE.Yeah, several functions in std.math use inline assembly. So, for them to be able to be used at compile time, either the compiler must be expanded to be able to run asm statements at compile time (which may or may not be planned and may or may not be reasonable), or those functions need another branch (using __cfte in an if condition) which doesn't use assembly. Or I suppose that if the extra check for __ctfe isn't considered particularly acceptable (after all, they're already using assembly), then separate functions meant specifically for CTFE would be necessary.From my limited experience, I'd say that having two versions of thefunction is probably the least painful way to go. [...]Allowing asm in CTFE would probably be way more work than it's worth. You'd basically need full-blown analysis of x86 assembly plus an interpreter. Even then, x86 is not typed, so it's going to be a major pain...[...] I admit I've no idea how the D compiler implements compile-time evaluation, but is it possible for the compiler to actually emit code for compile-time functions containing asm blocks and, say, execute it in a sandbox, and read the values out from the machine registers? Or does this create more problems than it solves? T -- Verbing weirds language. -- Calvin (& Hobbes)
Jan 06 2012
On 07/01/2012 00:31, H. S. Teoh wrote:I admit I've no idea how the D compiler implements compile-time evaluation, but is it possible for the compiler to actually emit code for compile-time functions containing asm blocks and, say, execute it in a sandbox, and read the values out from the machine registers? Or does this create more problems than it solves?Doing this would mean you can't do cross-compilation, eg using x86 to compile for ARM. Which means you'd need to use a virtual machine for it, which is almost certainly more effort than it's worth. -- Robert http://octarineparrot.com/
Jan 06 2012
On Sat, Jan 07, 2012 at 02:15:39AM +0000, Robert Clipsham wrote:On 07/01/2012 00:31, H. S. Teoh wrote:[...] But doesn't the use of asm{} already prevent cross-compilation in the first place? Or does the D compiler actually translate the instructions into the target platform? In which case, doesn't it already know enough to be able to interpret it? I'm pretty sure I'm missing something obvious. T -- Customer support: the art of getting your clients to pay for your own incompetence.I admit I've no idea how the D compiler implements compile-time evaluation, but is it possible for the compiler to actually emit code for compile-time functions containing asm blocks and, say, execute it in a sandbox, and read the values out from the machine registers? Or does this create more problems than it solves?Doing this would mean you can't do cross-compilation, eg using x86 to compile for ARM. Which means you'd need to use a virtual machine for it, which is almost certainly more effort than it's worth.
Jan 06 2012
On 07/01/2012 02:28, H. S. Teoh wrote:On Sat, Jan 07, 2012 at 02:15:39AM +0000, Robert Clipsham wrote:version(X86) asm { // X86 ASM } else version(ARM) asm { // ARM ASM } // etc -- Robert http://octarineparrot.com/On 07/01/2012 00:31, H. S. Teoh wrote:[...] But doesn't the use of asm{} already prevent cross-compilation in the first place? Or does the D compiler actually translate the instructions into the target platform? In which case, doesn't it already know enough to be able to interpret it? I'm pretty sure I'm missing something obvious. TI admit I've no idea how the D compiler implements compile-time evaluation, but is it possible for the compiler to actually emit code for compile-time functions containing asm blocks and, say, execute it in a sandbox, and read the values out from the machine registers? Or does this create more problems than it solves?Doing this would mean you can't do cross-compilation, eg using x86 to compile for ARM. Which means you'd need to use a virtual machine for it, which is almost certainly more effort than it's worth.
Jan 06 2012
On 07-01-2012 01:31, H. S. Teoh wrote:On Sat, Jan 07, 2012 at 12:49:46AM +0100, Alex Rønne Petersen wrote:Executing asm at compile time is also a security risk. -- - AlexOn 07-01-2012 00:37, Jonathan M Davis wrote:[...]On Saturday, January 07, 2012 00:03:39 Alex Rønne Petersen wrote:Most likely those functions are just implemented using inline assembly, therefore not usable in CTFE.Yeah, several functions in std.math use inline assembly. So, for them to be able to be used at compile time, either the compiler must be expanded to be able to run asm statements at compile time (which may or may not be planned and may or may not be reasonable), or those functions need another branch (using __cfte in an if condition) which doesn't use assembly. Or I suppose that if the extra check for __ctfe isn't considered particularly acceptable (after all, they're already using assembly), then separate functions meant specifically for CTFE would be necessary.From my limited experience, I'd say that having two versions of thefunction is probably the least painful way to go. [...]Allowing asm in CTFE would probably be way more work than it's worth. You'd basically need full-blown analysis of x86 assembly plus an interpreter. Even then, x86 is not typed, so it's going to be a major pain...[...] I admit I've no idea how the D compiler implements compile-time evaluation, but is it possible for the compiler to actually emit code for compile-time functions containing asm blocks and, say, execute it in a sandbox, and read the values out from the machine registers? Or does this create more problems than it solves? T
Jan 07 2012
On 01/07/2012 12:37 AM, Jonathan M Davis wrote:On Saturday, January 07, 2012 00:03:39 Alex Rønne Petersen wrote:If the if condition is a constant, there is no runtime overhead.Most likely those functions are just implemented using inline assembly, therefore not usable in CTFE.Yeah, several functions in std.math use inline assembly. So, for them to be able to be used at compile time, either the compiler must be expanded to be able to run asm statements at compile time (which may or may not be planned and may or may not be reasonable), or those functions need another branch (using __cfte in an if condition) which doesn't use assembly. Or I suppose that if the extra check for __ctfe isn't considered particularly acceptable (after all, they're already using assembly) [snip.]
Jan 06 2012
On Saturday, January 07, 2012 04:49:27 Timon Gehr wrote:On 01/07/2012 12:37 AM, Jonathan M Davis wrote:te:On Saturday, January 07, 2012 00:03:39 Alex R=C3=B8nne Petersen wro=em toMost likely those functions are just implemented using inline assembly, therefore not usable in CTFE.=20 Yeah, several functions in std.math use inline assembly. So, for th=y orbe able to be used at compile time, either the compiler must be expanded to be able to run asm statements at compile time (which ma=chmay not be planned and may or may not be reasonable), or those functions need another branch (using __cfte in an if condition) whi=fedoesn't use assembly. Or I suppose that if the extra check for __ct=yisn't considered particularly acceptable (after all, they're alread=Ah, good point - though depending on what the compiler does, that may o= nly be=20 the case with -O. But anyone who really cares about that level of perfo= rmance=20 would be compiling with -O anyway. - Jonathan M Davisusing assembly) [snip.]=20 If the if condition is a constant, there is no runtime overhead.
Jan 06 2012