2 using System.Reflection;
3 using System.Reflection.Emit;
7 private delegate TReturn OneParameter<TReturn, TParameter0> (TParameter0 p0);
9 private static Type[] SDivisionArgs = {typeof(int)};
10 private static Type[] UDivisionArgs = {typeof(uint)};
12 public static int Main (string[] args) {
13 if (TestSDivision (2000, 10) != 0)
16 if (TestUDivision (2000, 10) != 0)
22 private static int TestSDivision (int divisions, int invokes)
25 Random rand = new Random ();
26 for (i = 0; i < divisions; i++) {
27 int divisor = rand.Next (Int32.MinValue, Int32.MaxValue);
29 if (divisor == 0 || divisor == -1)
32 DynamicMethod SDivision = new DynamicMethod(
33 String.Format ("SDivision{0}", i),
38 ILGenerator il = SDivision.GetILGenerator();
39 il.Emit(OpCodes.Ldarg_0);
40 il.Emit(OpCodes.Ldc_I4, divisor);
44 OneParameter<int, int> invokeSDivision =
45 (OneParameter<int, int>)
46 SDivision.CreateDelegate(typeof(OneParameter<int, int>));
48 for (j = 0; j < invokes; j++) {
49 int dividend = rand.Next (Int32.MinValue, Int32.MaxValue);
52 result = invokeSDivision (dividend);
53 expected = dividend / divisor;
55 if (result != expected) {
56 Console.WriteLine("{0} / {1} = {2} != {3})", dividend, divisor, expected, result);
65 private static int TestUDivision (int divisions, int invokes)
68 Random rand = new Random ();
69 for (i = 0; i < divisions; i++) {
70 uint divisor = (uint)rand.Next (Int32.MinValue, Int32.MaxValue);
75 DynamicMethod UDivision = new DynamicMethod(
76 String.Format ("UDivision{0}", i),
81 ILGenerator il = UDivision.GetILGenerator();
82 il.Emit(OpCodes.Ldarg_0);
83 il.Emit(OpCodes.Ldc_I4, divisor);
84 il.Emit(OpCodes.Div_Un);
87 OneParameter<uint, uint> invokeUDivision =
88 (OneParameter<uint, uint>)
89 UDivision.CreateDelegate(typeof(OneParameter<uint, uint>));
91 for (j = 0; j < invokes; j++) {
92 uint dividend = (uint)rand.Next (Int32.MinValue, Int32.MaxValue);
93 uint result, expected;
95 result = invokeUDivision (dividend);
96 expected = dividend / divisor;
98 if (result != expected) {
99 Console.WriteLine("{0} / {1} = {2} != {3})", dividend, divisor, expected, result);