Skip to content

Commit

Permalink
Spoonfeeding the Mono JIT compiler seems to work, inlining does not w…
Browse files Browse the repository at this point in the history
…ork in any usefull way :(
  • Loading branch information
kenkendk committed Apr 30, 2015
1 parent 3ba73b2 commit ab3131c
Show file tree
Hide file tree
Showing 8 changed files with 446 additions and 118 deletions.
43 changes: 40 additions & 3 deletions bridge/NumCIL/NumCIL.Unsafe/Aggregate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
if ((typename == "Single" || typename == "Double") && (opname == "And" || opname == "Or" || opname == "Xor"))
continue;

string opcode_template = "(System.{0})op.Op({1}, {2})";

if (opname == "Add")
opcode_template = "(System.{0})({1} + {2})";
else if (opname == "Sub")
opcode_template = "(System.{0})({1} - {2})";
else if (opname == "Mul")
opcode_template = "(System.{0})({1} * {2})";
else if (opname == "Div")
opcode_template = "(System.{0})({1} / {2})";
else if (opname == "Mod")
opcode_template = "(System.{0})({1} % {2})";
else if (opname == "Min")
opcode_template = "(System.{0})Math.Min({1}, {2})";
else if (opname == "Max")
opcode_template = "(System.{0})Math.Max({1}, {2})";
else if (opname == "Pow")
opcode_template = "(System.{0})Math.Pow({1}, {2})";
else if (opname == "And")
opcode_template = "(System.{0})({1} & {2})";
else if (opname == "Or")
opcode_template = "(System.{0})({1} | {2})";
else if (opname == "Xor")
opcode_template = "(System.{0})({1} ^ {2})";
else if (opname == "Equal")
opcode_template = "(System.{0})({1} == {2})";
else if (opname == "NotEqual")
opcode_template = "(System.{0})({1} != {2})";
else if (opname == "GreaterThan")
opcode_template = "(System.{0})({1} > {2})";
else if (opname == "GreaterThanOrEqual")
opcode_template = "(System.{0})({1} >= {2})";
else if (opname == "LessThan")
opcode_template = "(System.{0})({1} < {2})";
else if (opname == "LessThanOrEqual")
opcode_template = "(System.{0})({1} <= {2})";


#>
/// <summary>
Expand Down Expand Up @@ -104,7 +141,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In

for (long i = 1; i < totalOps; i++)
{
result = op.Op(result, d1[ix1]);
result = <#=string.Format(opcode_template, typename, "result", "d1[ix1]")#>;
ix1 += stride1;
}
}
Expand All @@ -125,7 +162,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
{
for (long j = (i == 0 ? 1 : 0); j < opsInner; j++)
{
result = op.Op(result, d1[ix1]);
result = <#=string.Format(opcode_template, typename, "result", "d1[ix1]")#>;
ix1 += innerStride1;
}

Expand Down Expand Up @@ -172,7 +209,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
{
for (long k = (first ? 1 : 0); k < opsInnerInner; k++)
{
result = op.Op(result, d1[ix1]);
result = <#=string.Format(opcode_template, typename, "result", "d1[ix1]")#>;
ix1 += innerInnerStride1;
}
first = false;
Expand Down
121 changes: 80 additions & 41 deletions bridge/NumCIL/NumCIL.Unsafe/ApplyBinary.tt

Large diffs are not rendered by default.

66 changes: 59 additions & 7 deletions bridge/NumCIL/NumCIL.Unsafe/ApplyUnary.tt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,58 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In

}

string opcode_template = "op.Op({1})";

if (convertOps.Any(x => x == opname) || opname == "Copy")
opcode_template = "({0})({1})";


if (opname == "Ceiling")
opcode_template = "({0})(Math.Ceiling({1}))";
else if (opname == "Floor")
opcode_template = "({0})(Math.Floor({1}))";
else if (opname == "Round")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Sqrt")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Exp")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Abs")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Sin")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Cos")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Tan")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Asin")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Acos")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Atan")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Sinh")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Cosh")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Tanh")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Log")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Log10")
opcode_template = "({0})(Math." + opname + "({1}))";
else if (opname == "Invert")
opcode_template = "({0})(~({1}))";
else if (opname == "Not")
opcode_template = "({0})(!({1}))";
else if (opname == "Sign")
opcode_template = "({0})(Math." + opname + "({1}))";


string opcode_a = string.Format(opcode_template, "System." + typename_out, "d1[ix1]" + (typename == "Boolean" && typename_out != "Boolean" ? " ? 1 : 0 " : ""));
string opcode_s = string.Format(opcode_template, "System." + typename_out, "scalar" + (typename == "Boolean" && typename_out != "Boolean" ? " ? 1 : 0 " : ""));




#>
Expand Down Expand Up @@ -152,15 +204,15 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
//Best case, all are equal, just keep a single counter
for (long i = 0; i < totalOps; i++)
{
d2[ix1] = op.Op(d1[ix1]);
d2[ix1] = <#=opcode_a#>;
ix1 += stride1;
}
}
else
{
for (long i = 0; i < totalOps; i++)
{
d2[ix2] = op.Op(d1[ix1]);
d2[ix2] = <#=opcode_a#>;
ix1 += stride1;
ix2 += stride2;
}
Expand All @@ -187,7 +239,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
{
for (long j = 0; j < opsInner; j++)
{
d2[ix2] = op.Op(d1[ix1]);
d2[ix2] = <#=opcode_a#>;
ix1 += innerStride1;
ix2 += innerStride2;
}
Expand Down Expand Up @@ -241,7 +293,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
{
for (long k = 0; k < opsInnerInner; k++)
{
d2[ix3] = op.Op(d1[ix1]);
d2[ix3] = <#=opcode_a#>;
ix1 += innerInnerStride1;
ix3 += innerInnerStride3;
}
Expand Down Expand Up @@ -307,7 +359,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In

for (long i = 0; i < totalOps; i++)
{
d2[ix2] = op.Op(scalar);
d2[ix2] = <#=opcode_s#>;
ix2 += stride2;
}
}
Expand All @@ -325,7 +377,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
{
for (long j = 0; j < opsInner; j++)
{
d2[ix2] = op.Op(scalar);
d2[ix2] = <#=opcode_s#>;
ix2 += innerStride2;
}

Expand Down Expand Up @@ -366,7 +418,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
{
for (long k = 0; k < opsInnerInner; k++)
{
d2[ix3] = op.Op(scalar);
d2[ix3] = <#=opcode_s#>;
ix3 += innerInnerStride3;
}

Expand Down
43 changes: 40 additions & 3 deletions bridge/NumCIL/NumCIL.Unsafe/Reduce.tt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
if ((typename == "Single" || typename == "Double") && (opname == "And" || opname == "Or" || opname == "Xor"))
continue;

string opcode_template = "(System.{0})op.Op({1}, {2})";

if (opname == "Add")
opcode_template = "(System.{0})({1} + {2})";
else if (opname == "Sub")
opcode_template = "(System.{0})({1} - {2})";
else if (opname == "Mul")
opcode_template = "(System.{0})({1} * {2})";
else if (opname == "Div")
opcode_template = "(System.{0})({1} / {2})";
else if (opname == "Mod")
opcode_template = "(System.{0})({1} % {2})";
else if (opname == "Min")
opcode_template = "(System.{0})Math.Min({1}, {2})";
else if (opname == "Max")
opcode_template = "(System.{0})Math.Max({1}, {2})";
else if (opname == "Pow")
opcode_template = "(System.{0})Math.Pow({1}, {2})";
else if (opname == "And")
opcode_template = "(System.{0})({1} & {2})";
else if (opname == "Or")
opcode_template = "(System.{0})({1} | {2})";
else if (opname == "Xor")
opcode_template = "(System.{0})({1} ^ {2})";
else if (opname == "Equal")
opcode_template = "(System.{0})({1} == {2})";
else if (opname == "NotEqual")
opcode_template = "(System.{0})({1} != {2})";
else if (opname == "GreaterThan")
opcode_template = "(System.{0})({1} > {2})";
else if (opname == "GreaterThanOrEqual")
opcode_template = "(System.{0})({1} >= {2})";
else if (opname == "LessThan")
opcode_template = "(System.{0})({1} < {2})";
else if (opname == "LessThanOrEqual")
opcode_template = "(System.{0})({1} <= {2})";


#>
/// <summary>
Expand Down Expand Up @@ -118,7 +155,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
System.<#=typename#> value = d[ix];

for (long i = ix + stride; i < limit; i += stride)
value = op.Op(value, d[i]);
value = <#=string.Format(opcode_template, typename, "value", "d[i]")#>;

vd[@out.Shape.Offset] = value;
}
Expand All @@ -142,7 +179,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
for (long j = 1; j < outerCount; j++)
{
nx += strideOuter;
value = op.Op(value, d[nx]);
value = <#=string.Format(opcode_template, typename, "value", "d[nx]")#>;
}

vd[ox] = value;
Expand All @@ -168,7 +205,7 @@ foreach(string typename in new string[] {"SByte", "Byte", "Int16", "UInt16", "In
System.<#=typename#> value = d[ix];

for (long j = strideInner; j < limitInner; j += strideInner)
value = op.Op(value, d[j + ix]);
value = <#=string.Format(opcode_template, typename, "value", "d[j + ix]")#>;

vd[ox] = value;
ox += strideRes;
Expand Down
47 changes: 42 additions & 5 deletions bridge/NumCIL/NumCIL/UFunc/TypedAggregate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ foreach(string fulltypename in new string[] {"T", "System.SByte", "System.Byte",
break;
case "System.Numerics.Complex":
typename = "Complex128";
mathlib = "T";
mathlib = "System.Numerics.Complex";
break;
case "NumCIL.Complex64.DataType":
typename = "Complex64";
mathlib = "T";
mathlib = "NumCIL.Complex64.DataType";
break;
default:
if (typename.StartsWith("System."))
Expand Down Expand Up @@ -95,6 +95,43 @@ foreach(string fulltypename in new string[] {"T", "System.SByte", "System.Byte",
continue;
}

string opcode_template = "({0})op.Op({1}, {2})";

if (opname == "Add")
opcode_template = "({0})({1} + {2})";
else if (opname == "Sub")
opcode_template = "({0})({1} - {2})";
else if (opname == "Mul")
opcode_template = "({0})({1} * {2})";
else if (opname == "Div")
opcode_template = "({0})({1} / {2})";
else if (opname == "Mod")
opcode_template = "({0})({1} % {2})";
else if (opname == "Min")
opcode_template = "({0})Math.Min({1}, {2})";
else if (opname == "Max")
opcode_template = "({0})Math.Max({1}, {2})";
else if (opname == "Pow")
opcode_template = "({0})" + mathlib + ".Pow({1}, {2})";
else if (opname == "And")
opcode_template = "({0})({1} & {2})";
else if (opname == "Or")
opcode_template = "({0})({1} | {2})";
else if (opname == "Xor")
opcode_template = "({0})({1} ^ {2})";
else if (opname == "Equal")
opcode_template = "({0})({1} == {2})";
else if (opname == "NotEqual")
opcode_template = "({0})({1} != {2})";
else if (opname == "GreaterThan")
opcode_template = "({0})({1} > {2})";
else if (opname == "GreaterThanOrEqual")
opcode_template = "({0})({1} >= {2})";
else if (opname == "LessThan")
opcode_template = "({0})({1} < {2})";
else if (opname == "LessThanOrEqual")
opcode_template = "({0})({1} <= {2})";

#>
/// <summary>
/// Calculates the scalar result of applying the binary operation to all elements
Expand Down Expand Up @@ -137,7 +174,7 @@ foreach(string fulltypename in new string[] {"T", "System.SByte", "System.Byte",

for (long i = 1; i < totalOps; i++)
{
result = op.Op(result, d1[ix1]);
result = <#=string.Format(opcode_template, fulltypename, "result", "d1[ix1]")#>;
ix1 += stride1;
}
}
Expand All @@ -158,7 +195,7 @@ foreach(string fulltypename in new string[] {"T", "System.SByte", "System.Byte",
{
for (long j = (i == 0 ? 1 : 0); j < opsInner; j++)
{
result = op.Op(result, d1[ix1]);
result = <#=string.Format(opcode_template, fulltypename, "result", "d1[ix1]")#>;
ix1 += innerStride1;
}

Expand Down Expand Up @@ -205,7 +242,7 @@ foreach(string fulltypename in new string[] {"T", "System.SByte", "System.Byte",
{
for (long k = (first ? 1 : 0); k < opsInnerInner; k++)
{
result = op.Op(result, d1[ix1]);
result = <#=string.Format(opcode_template, fulltypename, "result", "d1[ix1]")#>;
ix1 += innerInnerStride1;
}
first = false;
Expand Down

0 comments on commit ab3131c

Please sign in to comment.