private static string GetAddSubtractHpCode_CSharp_Java(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, ADD_SUB_HP_TYPE funcType, G25.CG.Shared.FuncArgInfo[] FAI, string resultName) { G25.GMV gmv = S.m_GMV; bool NOT_HP = (!((funcType == ADD_SUB_HP_TYPE.HP) || (funcType == ADD_SUB_HP_TYPE.IHP))); StringBuilder SB= new StringBuilder(); // get number of groups, and possible assurances that a group is always present: int nbGroups = gmv.NbGroups; bool resultIsScalar = false, initResultToZero = false; SB.Append(GPparts.GetExpandCode(S, cgd, FT, FAI, resultIsScalar, initResultToZero)); // for each group // test if present in both-> then add both, etc for (int g = 0; g < nbGroups; g++) { SB.AppendLine(""); string funcName1_1 = GetCopyPartFunctionName(S, FT, g); string funcName2 = null; string funcName1_2 = null; switch (funcType) { case ADD_SUB_HP_TYPE.ADD: funcName2 = GetAdd2PartFunctionName(S, FT, g); funcName1_2 = GetCopyPartFunctionName(S, FT, g); break; case ADD_SUB_HP_TYPE.SUB: funcName2 = GetSub2PartFunctionName(S, FT, g); funcName1_2 = GetNegPartFunctionName(S, FT, g); break; case ADD_SUB_HP_TYPE.HP: funcName2 = GetHadamardProductPartFunctionName(S, FT, g); funcName1_2 = null; break; case ADD_SUB_HP_TYPE.IHP: funcName2 = GetInverseHadamardProductPartFunctionName(S, FT, g); funcName1_2 = null; break; } string allocCcode = "cc[" + g + "] = new " + FT.type + "[" + gmv.Group(g).Length + "];"; SB.AppendLine("if (ac[" + g + "] != null) {"); if (NOT_HP) SB.AppendLine("\t" + allocCcode); SB.AppendLine("\tif (bc[" + g + "] != null) {"); if (!NOT_HP) SB.AppendLine("\t\t" + allocCcode); SB.AppendLine("\t\t" + funcName2 + "(ac[" + g + "], bc[" + g + "], cc[" + g + "]);"); SB.AppendLine("\t}"); if (NOT_HP) SB.AppendLine("\telse " + funcName1_1 + "(ac[" + g + "], cc[" + g + "]);"); SB.AppendLine("}"); if (NOT_HP) { SB.AppendLine("else if (bc[" + g + "] != null) {"); SB.AppendLine("\t" + allocCcode); SB.AppendLine("\t" + funcName1_2 + "(bc[" + g + "], cc[" + g + "]);"); SB.AppendLine("}"); } } // return result SB.AppendLine("return new " + FT.GetMangledName(S, gmv.Name) + "(cc);"); return SB.ToString(); }
private static string GetAddSubtractHpCode_C_CPP(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, ADD_SUB_HP_TYPE funcType, G25.CG.Shared.FuncArgInfo[] FAI, string resultName) { G25.GMV gmv = S.m_GMV; bool NOT_HP = (!((funcType == ADD_SUB_HP_TYPE.HP) || (funcType == ADD_SUB_HP_TYPE.IHP))); StringBuilder SB= new StringBuilder(); SB.AppendLine("int aidx = 0, bidx = 0, cidx = 0;"); string agu = (S.OutputC()) ? FAI[0].Name + "->gu" : FAI[0].Name + ".gu()"; string ac = (S.OutputC()) ? FAI[0].Name + "->c" : FAI[0].Name + ".getC()"; string bgu = (S.OutputC()) ? FAI[1].Name + "->gu" : FAI[1].Name + ".gu()"; string bc = (S.OutputC()) ? FAI[1].Name + "->c" : FAI[1].Name + ".getC()"; string resultCoordPtr = (S.OutputC()) ? resultName + "->c" : "c"; string guSymbol = (NOT_HP) ? "|" : "&"; if (S.OutputC()) { SB.AppendLine(resultName + "->gu = " + agu + " " + guSymbol + " " + bgu + ";"); } else { SB.AppendLine("int gu = " + agu + " " + guSymbol + " " + bgu + ";"); SB.AppendLine(FT.type + " c[" + (1 << S.m_dimension) + "];"); } // get number of groups, and possible assurances that a group is always present: int nbGroups = gmv.NbGroups; // for each group // test if present in both-> then add both, etc for (int g = 0; g < nbGroups; g++) { SB.AppendLine(""); string funcName1_1 = GetCopyPartFunctionName(S, FT, g); string funcName2 = null; string funcName1_2 = null; switch (funcType) { case ADD_SUB_HP_TYPE.ADD: funcName2 = GetAdd2PartFunctionName(S, FT, g); funcName1_2 = GetCopyPartFunctionName(S, FT, g); break; case ADD_SUB_HP_TYPE.SUB: funcName2 = GetSub2PartFunctionName(S, FT, g); funcName1_2 = GetNegPartFunctionName(S, FT, g); break; case ADD_SUB_HP_TYPE.HP: funcName2 = GetHadamardProductPartFunctionName(S, FT, g); funcName1_2 = null; break; case ADD_SUB_HP_TYPE.IHP: funcName2 = GetInverseHadamardProductPartFunctionName(S, FT, g); funcName1_2 = null; break; } SB.AppendLine("if (" + agu + " & " + (1 << g) + ") {"); SB.AppendLine("\tif (" + bgu + " & " + (1 << g) + ") {"); SB.AppendLine("\t\t" + funcName2 + "(" + ac + " + aidx, " + bc + " + bidx, " + resultCoordPtr + " + cidx);"); if (g < (nbGroups - 1)) { SB.AppendLine("\t\tbidx += " + gmv.Group(g).Length + ";"); if (!NOT_HP) SB.AppendLine("\t\tcidx += " + gmv.Group(g).Length + ";"); } SB.AppendLine("\t}"); if (NOT_HP) SB.AppendLine("\telse " + funcName1_1 + "(" + ac + " + aidx, " + resultCoordPtr + " + cidx);"); if (g < (nbGroups - 1)) SB.AppendLine("\taidx += " + gmv.Group(g).Length + ";"); if (NOT_HP) SB.AppendLine("\tcidx += " + gmv.Group(g).Length + ";"); SB.AppendLine("}"); SB.AppendLine("else if (" + bgu + " & " + (1 << g) + ") {"); if (NOT_HP) SB.AppendLine("\t" + funcName1_2 + "(" + bc + " + bidx, " + resultCoordPtr + " + cidx);"); if (g < (nbGroups - 1)) SB.AppendLine("\tbidx += " + gmv.Group(g).Length + ";"); if (NOT_HP) SB.AppendLine("\tcidx += " + gmv.Group(g).Length + ";"); SB.AppendLine("}"); } // return result if (S.m_outputLanguage != OUTPUT_LANGUAGE.C) { SB.AppendLine("return " + FT.GetMangledName(S, gmv.Name) + "(gu, c);"); } return SB.ToString(); }
/// <summary> /// Returns the code addition/subtraction/hadamard product of two general multivectors. /// The code is composed of calls to functions generated by <c>WriteCASNparts()</c>. /// /// The returned code is only the body. The function declaration is not included. /// </summary> /// <param name="S">Specification of algebra (used for general multivector type, output language).</param> /// <param name="cgd">Currently not used.</param> /// <param name="FT">Floating point type.</param> /// <param name="funcType">Whether to generate addition, subtraction or Hadamard product code.</param> /// <param name="FAI">Info about function arguments.</param> /// <param name="resultName">Name of variable where the result goes (in the generated code).</param> /// <returns>code for the requested product type.</returns> public static string GetAddSubtractHpCode(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, ADD_SUB_HP_TYPE funcType, G25.CG.Shared.FuncArgInfo[] FAI, string resultName) { if (S.OutputCppOrC()) return GetAddSubtractHpCode_C_CPP(S, cgd, FT, funcType, FAI, resultName); else return GetAddSubtractHpCode_CSharp_Java(S, cgd, FT, funcType, FAI, resultName); }