chiark / gitweb /
windows: expose iterate() to .Net callers
authorRichard Kettlewell <rjk@terraraq.org.uk>
Sat, 22 Dec 2012 16:37:24 +0000 (16:37 +0000)
committerRichard Kettlewell <rjk@terraraq.org.uk>
Sat, 22 Dec 2012 16:37:24 +0000 (16:37 +0000)
Lightly tested.

lib/arith.cc
lib/arith.h
windows/mandycs/Fixed128.cs
windows/tests/IterationTest.cs [new file with mode: 0644]
windows/tests/tests.csproj

index be28bb8..c78a96d 100644 (file)
@@ -49,3 +49,11 @@ count_t iterate(arith_t zx, arith_t zy, arith_t cx, arith_t cy,
     abort();
   }
 }
+
+count_t iterate_cs(const Fixed128 *zx, const Fixed128 *zy,
+                   const Fixed128 *cx, const Fixed128 *cy,
+                   int maxiters, int arith) {
+  return iterate(fixed128(*zx), fixed128(*zy),
+                 fixed128(*cx), fixed128(*cy),
+                 maxiters, (arith_type)arith);
+}
\ No newline at end of file
index 2a0cc44..e636bc1 100644 (file)
@@ -235,6 +235,13 @@ public:
 LIBMANDY_API count_t iterate(arith_t zx, arith_t zy, arith_t cx, arith_t cy,
                              int maxiters, arith_type arith);
 
+// C#-friendly interface
+extern "C" {
+  LIBMANDY_API count_t iterate_cs(const Fixed128 *zx, const Fixed128 *zy,
+                                  const Fixed128 *cx, const Fixed128 *cy,
+                                  int maxiters, int arith);
+}
+
 #endif /* ARITH_H */
 
 /*
index 341265e..aa6015f 100644 (file)
@@ -8,6 +8,17 @@ using System.Runtime.InteropServices;
 namespace uk.org.greenend.mandy
 {
   /// <summary>
+  /// Possible precisions
+  /// </summary>
+  public enum Precision
+  {
+    Double,
+    LongDouble,
+    Fixed64,
+    Fixed128,
+  };
+
+  /// <summary>
   /// 128-bit fixed point arithmetic
   /// </summary>
   /// <remarks><para>You get 32 bits of integer part and 96 bit of fractional part.</para></remarks>
@@ -85,7 +96,7 @@ namespace uk.org.greenend.mandy
 
     #endregion
 
-    #region Operations
+    #region Arithmetic Operations
 
     public static Fixed128 operator +(Fixed128 a, Fixed128 b)
     {
@@ -161,6 +172,17 @@ namespace uk.org.greenend.mandy
 
     #endregion
 
+    #region Algorithms
+
+    public static double iterate(Fixed128 zx, Fixed128 zy,
+                                 Fixed128 cx, Fixed128 cy,
+                                 int maxiters, Precision precision)
+    {
+      return iterate_cs(ref zx, ref zy, ref cx, ref cy, maxiters, (int)precision);
+    }
+
+    #endregion
+
     #region Unmanaged code
 
     [DllImport("libmandy.dll")]
@@ -212,6 +234,11 @@ namespace uk.org.greenend.mandy
     [DllImport("libmandy.dll")]
     private static extern double Fixed128_2double(ref Fixed128 a);
 
+    [DllImport("libmandy.dll")]
+    private static extern double iterate_cs(ref Fixed128 zx, ref Fixed128 zy,
+                                            ref Fixed128 cx, ref Fixed128 cy,
+                                            int maxiters, int arith); 
+
     #endregion
 
   }
diff --git a/windows/tests/IterationTest.cs b/windows/tests/IterationTest.cs
new file mode 100644 (file)
index 0000000..fb2bb84
--- /dev/null
@@ -0,0 +1,37 @@
+\feffusing System;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using uk.org.greenend.mandy;
+
+namespace tests
+{
+  [TestClass]
+  public class IterationTest
+  {
+    /// <summary>
+    /// Tests for Fix128
+    /// </summary>
+    /// <remarks>
+    /// <para>Only the 64-bit configuration is testable.</para></remarks>
+    [TestMethod]
+    [DeploymentItem(@"..\..\..\..\x64\Debug\libmandy.dll")]
+    public void IterateTest()
+    {
+      Fixed128 zx = 0, zy = 0;
+      Fixed128 cx = new Fixed128(0, 0xa6aaaaaau, 0xaaaaaaaau, 0xaaaaaaabu);
+      Fixed128 cy = new Fixed128(-1, 0xfd555555u, 0x55555555u, 0x55555555u);
+      Assert.AreEqual("0.651041666666666666666666666670873924827845396295529219014841526558257100987248122692108154296875",
+                      (string)cx);
+      Assert.AreEqual("-0.010416666666666666666666666670873924827845396295529219014841526558257100987248122692108154296875",
+                      (string)cy);
+      double count = Fixed128.iterate(zx, zy, cx, cy, 255, Precision.Double);
+      double expected = 1 + 5 - Math.Log(Math.Log(255.08471462316811, 2), 2);
+      Assert.AreEqual(count, expected);
+      count = Fixed128.iterate(zx, zy, cx, cy, 255, Precision.LongDouble);
+      Assert.AreEqual(count, expected);
+      count = Fixed128.iterate(zx, zy, cx, cy, 255, Precision.Fixed64);
+      Assert.AreEqual(count, expected);
+      count = Fixed128.iterate(zx, zy, cx, cy, 255, Precision.Fixed128);
+      Assert.AreEqual(count, expected);
+    }
+  }
+}
index 2109d12..9f206ac 100644 (file)
@@ -55,6 +55,7 @@
     <Compile Include="JobQueueTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Fixed128Test.cs" />
+    <Compile Include="IterationTest.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\mandycs\mandycs.csproj">