Esempio n. 1
0
File: smv.cs Progetto: Sciumo/gaigen
        /// <summary>
        /// Writes code for abs largest coordinate
        /// </summary>
        /// <param name="S"></param>
        /// <param name="cgd">Results go here. Also intermediate data for code generation. Also contains plugins and cog.</param>
        public static void WriteLargestCoordinateFunctions(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, G25.SMV smv)
        {
            StringBuilder defSB = cgd.m_defSB;
            defSB.AppendLine("");

            const string smvName = G25.CG.Shared.SmvUtil.THIS;
            const bool ptr = false;

            string fabsFunc = G25.CG.Shared.CodeUtil.OpNameToLangString(S, FT, RefGA.Symbolic.UnaryScalarOp.ABS);

            string[] AS = G25.CG.Shared.CodeUtil.GetAccessStr(S, smv, smvName, ptr);

            RefGA.BasisBlade maxBasisBlade = smv.AbsoluteLargestConstantBasisBlade();

            //string className = FT.GetMangledName(S, smv.Name);

            for (int _returnBitmap = 0; _returnBitmap <= 1; _returnBitmap++)
            {
                bool returnBitmap = (_returnBitmap != 0);

                // write comment
                int nbTabs = 1;
                if (returnBitmap)
                    new G25.CG.Shared.Comment("Returns the absolute largest coordinate,\nand the corresponding basis blade bitmap.").Write(defSB, S, nbTabs);
                else new G25.CG.Shared.Comment("Returns the absolute largest coordinate.").Write(defSB, S, nbTabs);

                string funcName = Util.GetFunctionName(S, ((returnBitmap) ? "largestBasisBlade" : "largestCoordinate"));

                string funcDecl;
                if ((S.OutputCSharp()) && returnBitmap)
                {
                    funcDecl = FT.type + " " + funcName + "(int bm) ";
                }
                else if ((S.OutputJava()) && returnBitmap)
                {
                    funcDecl = FT.type + "[] " + funcName + "() ";
                }
                else
                {
                    funcDecl = FT.type + " " + funcName + "()";
                }

                string FINAL = (S.OutputJava()) ? "final " : "";
                defSB.Append("\tpublic " + FINAL + funcDecl);
                {
                    defSB.AppendLine(" {");

                    if ((S.OutputJava()) && returnBitmap)
                        defSB.AppendLine("\t\tint bm;");

                    int startIdx = 0;
                    if (maxBasisBlade != null)
                    {
                        defSB.AppendLine("\t\t" + FT.type + " maxValue = " + FT.DoubleToString(S, Math.Abs(maxBasisBlade.scale)) + ";");
                        if (returnBitmap)
                            defSB.AppendLine("\t\tbm = " + maxBasisBlade.bitmap + ";");
                    }
                    else
                    {
                        defSB.AppendLine("\t\t" + FT.type + " maxValue = " + fabsFunc + "(" + AS[0] + ");");
                        if (returnBitmap)
                            defSB.AppendLine("\t\tbm = 0;");
                        startIdx = 1;
                    }

                    for (int c = startIdx; c < smv.NbNonConstBasisBlade; c++)
                    {
                        defSB.Append("\t\tif (" + fabsFunc + "(" + AS[c] + ") > maxValue) { maxValue = " + fabsFunc + "(" + AS[c] + "); ");
                        if (returnBitmap) defSB.Append("bm = " + smv.NonConstBasisBlade(c).bitmap + "; ");
                        defSB.AppendLine("}");
                    }

                    if ((S.OutputJava()) && returnBitmap)
                    {
                        defSB.AppendLine("\t\treturn new " + FT.type + "[]{maxValue, (" + FT.type + ")bm};");
                    }
                    else
                    {
                        defSB.AppendLine("\t\treturn maxValue;");
                    }
                    defSB.AppendLine("\t}");
                }
            }
        }