private static string GetDivCodeCSharpOrJava(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, G25.Metric M, G25.CG.Shared.FuncArgInfo[] FAI, string resultName, DIVCODETYPE funcType) { G25.GMV gmv = S.m_GMV; int nbGroups = gmv.NbGroups; StringBuilder SB = new StringBuilder(); string normFuncName = G25.CG.Shared.Dependencies.GetDependency(S, cgd, (funcType == DIVCODETYPE.UNIT) ? "norm" : "norm2", new String[] { gmv.Name }, FT, M.m_name); string normVarName = (funcType == DIVCODETYPE.UNIT) ? "n" : "n2"; if ((funcType == DIVCODETYPE.UNIT) || (funcType == DIVCODETYPE.VERSOR_INVERSE)) { // compute norm SB.AppendLine(FT.type + " " + normVarName + " = " + normFuncName + G25.CG.Shared.CANSparts.RETURNS_SCALAR + "(" + FAI[0].Name + ".to_" + FAI[0].MangledTypeName + "());"); } bool resultIsScalar = false, initResultToZero = false; SB.Append(GPparts.GetExpandCode(S, cgd, FT, new FuncArgInfo[]{FAI[0]}, resultIsScalar, initResultToZero)); // for each group present, copy and scale (for versor inverse, modulate with reverse) // for each group, test if present for (int g = 0; g < nbGroups; g++) { SB.AppendLine(""); // get func name string funcName = GetCopyDivPartFunctionName(S, FT, g); // get multiplier, normVarName string (depends on whether this is unit or versor inverse) string normString; if ((funcType == DIVCODETYPE.UNIT) || (funcType == DIVCODETYPE.VERSOR_INVERSE)) { double m = (funcType == DIVCODETYPE.UNIT) ? 1 : gmv.Group(g)[0].Reverse().scale / gmv.Group(g)[0].scale; normString = ((m < 0) ? "-" : "") + normVarName; } else normString = FAI[1].Name; //string allocCcode = SB.AppendLine("if (ac[" + g + "] != null) {"); SB.AppendLine("\tcc[" + g + "] = new " + FT.type + "[" + gmv.Group(g).Length + "];"); SB.AppendLine("\t" + funcName + "(ac[" + g + "], cc[" + g + "], " + normString + ");"); SB.AppendLine("}"); } SB.AppendLine("return new " + FT.GetMangledName(S, gmv.Name) + "(cc);"); return SB.ToString(); }
/// <summary> /// Returns the code for unit or versorInverse using metric <c>M</c>. /// The code is composed of calls to the <c>norm2()</c> function, /// and to functions generated by <c>WriteCANSparts()</c>. /// /// The generated function first compute the norm squared using the requested metric, /// and then divides the input multivector by it. /// /// The returned code is only the body. The function declaration is not included. /// </summary> /// <param name="S">Specification of algebra (used for output language).</param> /// <param name="cgd">Used to resolve dependecy (<c>norm2()</c> function).</param> /// <param name="FT">Floating point type.</param> /// <param name="M">The metric of the dual.</param> /// <param name="FAI">Info about function arguments</param> /// <param name="resultName">Name of variable where the result goes (in the generated code).</param> /// <param name="funcType">What type of function (UNIT, VERSOR_INVERSE or DIV)</param> /// <returns>code for the requested product type.</returns> public static string GetDivCode(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, G25.Metric M, G25.CG.Shared.FuncArgInfo[] FAI, string resultName, DIVCODETYPE funcType) { if (S.OutputCppOrC()) return GetDivCodeCppOrC(S, cgd, FT, M, FAI, resultName, funcType); else return GetDivCodeCSharpOrJava(S, cgd, FT, M, FAI, resultName, funcType); }
private static string GetDivCodeCppOrC(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, G25.Metric M, G25.CG.Shared.FuncArgInfo[] FAI, string resultName, DIVCODETYPE funcType) { G25.GMV gmv = S.m_GMV; StringBuilder SB = new StringBuilder(); string normFuncName = G25.CG.Shared.Dependencies.GetDependency(S, cgd, (funcType == DIVCODETYPE.UNIT) ? "norm" : "norm2", new String[] { gmv.Name }, FT, M.m_name); string normVarName = (funcType == DIVCODETYPE.UNIT) ? "n" : "n2"; 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 resultCoordPtr = (S.OutputC()) ? resultName + "->c" : "c"; SB.AppendLine("int idx = 0;"); if ((funcType == DIVCODETYPE.UNIT) || (funcType == DIVCODETYPE.VERSOR_INVERSE)) { // compute norm SB.AppendLine(FT.type + " " + normVarName + " = " + normFuncName + G25.CG.Shared.CANSparts.RETURNS_SCALAR + "(" + FAI[0].Name + ");"); } // copy group usage if (S.OutputC()) { SB.AppendLine(resultName + "->gu = " + agu + ";"); } else { SB.AppendLine("int gu = " + agu + ";"); SB.AppendLine(FT.type + " c[" + (1 << S.m_dimension) + "];"); } // for each group present, copy and scale (for versor inverse, modulate with reverse) // for each group, test if present int nbGroups = gmv.NbGroups; for (int g = 0; g < nbGroups; g++) { SB.AppendLine(""); // get func name string funcName = GetCopyDivPartFunctionName(S, FT, g); // get multiplier, normVarName string (depends on whether this is unit or versor inverse) string normString; if ((funcType == DIVCODETYPE.UNIT) || (funcType == DIVCODETYPE.VERSOR_INVERSE)) { double m = (funcType == DIVCODETYPE.UNIT) ? 1 : gmv.Group(g)[0].Reverse().scale / gmv.Group(g)[0].scale; normString = ((m < 0) ? "-" : "") + normVarName; } else normString = FAI[1].Name; SB.AppendLine("if (" + agu + " & " + (1 << g) + ") {"); SB.AppendLine("\t" + funcName + "(" + ac + " + idx, " + resultCoordPtr + " + idx, " + normString + ");"); if (g < (nbGroups - 1)) SB.AppendLine("\tidx += " + 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(); }