Thread overview
[phobos] Purity of C Math Functions
Dec 05, 2010
David Simcha
Dec 05, 2010
Brad Roberts
Dec 05, 2010
Jonathan M Davis
Dec 05, 2010
Simen Kjaeraas
Dec 06, 2010
Jonathan M Davis
Dec 06, 2010
Robert Jacques
December 05, 2010
I'm trying to test out Brad's 64 work and I can't compile Phobos because in 64 mode std.math calls some C math functions declared in druntime w/o purity, safety, etc. tags.  I now remember that this was one of the few areas I meant to come back to when I was cleaning up 64 support in Phobos, but I forgot about it.  Is it safe to assume that C core.stdc.math functions are @safe pure nothrow?
December 05, 2010
On 12/5/2010 8:55 AM, David Simcha wrote:
> I'm trying to test out Brad's 64 work and I can't compile Phobos because in 64
> mode std.math calls some C math functions declared in druntime w/o purity,
> safety, etc. tags.  I now remember that this was one of the few areas I meant to
> come back to when I was cleaning up 64 support in Phobos, but I forgot about
> it.  Is it safe to assume that C core.stdc.math functions are @safe pure nothrow?

I'd asked about this a long time ago and recall the answer being 'no', since they tend to mess with errno and other global state like rounding modes.

I've got email out to Don and Walter to see if they're interested in implementing the right asm code for the few functions that need it.  Basically, this diff needs to be made less bogus:

Index: std/math.d ===================================================================
--- std/math.d  (revision 2206)
+++ std/math.d  (working copy)
@@ -66,6 +66,8 @@
     // GDC can't actually do inline asm.
 } else version(D_InlineAsm_X86) {
     version = Naked_D_InlineAsm_X86;
+} else version(D_InlineAsm_X86_64) {
+    version = Naked_D_InlineAsm_X86_64;
 }
 version(LDC) {
     import ldc.intrinsics;
@@ -427,6 +429,9 @@

 Lret:
     ;
+    } else version(Naked_D_InlineAsm_X86_64) {
+        pragma(msg, "Hey Don.. wanna code this for us?");
+        return 0;
     } else {
         return core.stdc.math.tanl(x);
     }
@@ -574,6 +579,11 @@
             fpatan;
         }
     }
+    else version(D_InlineAsm_X86_64)
+    {
+        pragma(msg, "Needs to be written");
+        return 0;
+    }
     else
     {
         return core.stdc.math.atan2l(y,x);
@@ -917,6 +927,13 @@
         // (This is valid because the overflow & underflow limits for exp
         // and exp2 are so similar).
         return exp2(LOG2E*x);
+    }
+    else version(Naked_D_InlineAsm_X86_64)
+    {
+        //  e^^x = 2^^(LOG2E*x)
+        // (This is valid because the overflow & underflow limits for exp
+        // and exp2 are so similar).
+        return exp2(LOG2E*x);
     } else {
         return core.stdc.math.exp(x);
     }
@@ -1013,6 +1030,9 @@
             add ESP,12+8;
             ret PARAMSIZE;
         }
+    } else version(Naked_D_InlineAsm_X86_64) {
+        pragma(msg, "Needs to be written");
+        return 0;
     } else {
         return core.stdc.math.expm1(x);
     }
@@ -1114,6 +1134,9 @@
             add ESP,12+8;
             ret PARAMSIZE;
         }
+    } else version(Naked_D_InlineAsm_X86_64) {
+        pragma(msg, "Needs to be written");
+        return 0;
     } else {
         return core.stdc.math.exp2(x);
     }
@@ -1826,6 +1849,9 @@
             fistp n;
         }
         return n;
+    } else version(D_InlineAsm_X86_64) {
+        pragma(msg, "needs to be written");
+        return 0;
     } else {
         return core.stdc.math.llrintl(x);
     }

December 05, 2010
On Sunday 05 December 2010 08:55:54 David Simcha wrote:
> I'm trying to test out Brad's 64 work and I can't compile Phobos because in 64 mode std.math calls some C math functions declared in druntime w/o purity, safety, etc. tags.  I now remember that this was one of the few areas I meant to come back to when I was cleaning up 64 support in Phobos, but I forgot about it.  Is it safe to assume that C core.stdc.math functions are @safe pure nothrow?

Shouldn't _all_ C function be considered nothrow? C doesn't have exceptions, so C functions can't possibly throw.

- Jonathan M Davis
December 06, 2010
Jonathan M Davis <jmdavisProg at gmx.com> wrote:

> On Sunday 05 December 2010 08:55:54 David Simcha wrote:
>> I'm trying to test out Brad's 64 work and I can't compile Phobos because in 64 mode std.math calls some C math functions declared in druntime w/o purity, safety, etc. tags.  I now remember that this was one of the few areas I meant to come back to when I was cleaning up 64 support in Phobos, but I forgot about it.  Is it safe to assume that C core.stdc.math functions are @safe pure nothrow?
>
> Shouldn't _all_ C function be considered nothrow? C doesn't have
> exceptions, so
> C functions can't possibly throw.

A C function could call a D function (e.g. passed as a function pointer), no?

-- 
Simen
December 05, 2010
On Sunday 05 December 2010 15:51:07 Simen Kjaeraas wrote:
> Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > On Sunday 05 December 2010 08:55:54 David Simcha wrote:
> >> I'm trying to test out Brad's 64 work and I can't compile Phobos because in 64 mode std.math calls some C math functions declared in druntime w/o purity, safety, etc. tags.  I now remember that this was one of the few areas I meant to come back to when I was cleaning up 64 support in Phobos, but I forgot about it.  Is it safe to assume that C core.stdc.math functions are @safe pure nothrow?
> > 
> > Shouldn't _all_ C function be considered nothrow? C doesn't have
> > exceptions, so
> > C functions can't possibly throw.
> 
> A C function could call a D function (e.g. passed as a function pointer),
> no?

Well, I wouldn't expect a D function which threw an exception to be properly handled by the C function, such that it would be an extremely bad idea to call a D function which throws from C code. I certainly wouldn't expect calling a C function which calls a D function to throw an exception even if the D function threw one. I'd expect things to go seriously south if a D function ever threw an exception and a C function called it. Maybe it's able to handle it somehow. I don't know. But I wouldn't expect it to work, since C has no concept of exceptions.

- Jonathan M Davis
December 05, 2010
On Sun, 05 Dec 2010 18:43:23 -0500, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> On Sunday 05 December 2010 08:55:54 David Simcha wrote:
>> I'm trying to test out Brad's 64 work and I can't compile Phobos because in 64 mode std.math calls some C math functions declared in druntime w/o purity, safety, etc. tags.  I now remember that this was one of the few areas I meant to come back to when I was cleaning up 64 support in Phobos, but I forgot about it.  Is it safe to assume that C core.stdc.math functions are @safe pure nothrow?
>
> Shouldn't _all_ C function be considered nothrow? C doesn't have
> exceptions, so
> C functions can't possibly throw.
>
> - Jonathan M Davis

The C abi is the lingua franca of programming languages: just because the ABI is C, doesn't mean you're not actually calling C++, Python, Java, etc code.