[phobos] Purity of C Math Functions

Brad Roberts braddr at puremagic.com
Sun Dec 5 15:04:21 PST 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);
     }



More information about the phobos mailing list