Example #1
0
File: smv.cs Project: Sciumo/gaigen
        /// <summary>
        /// Writes functions to copy GMVs to SMVs
        /// </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 WriteGMVtoSMVcopy(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, G25.SMV smv)
        {
            StringBuilder defSB = cgd.m_defSB;

            G25.GMV gmv = S.m_GMV;
            string srcClassName = FT.GetMangledName(S, gmv.Name);

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

            bool dstPtr = false;
            string[] smvAccessStr = G25.CG.Shared.CodeUtil.GetAccessStr(S, smv, G25.CG.Shared.SmvUtil.THIS, dstPtr);

            string funcName = GMV.GetSetFuncName(S);

            string FINAL = (S.OutputJava()) ? "final " : "";
            string funcDecl = "\tpublic " + FINAL + "void " + funcName + "(" + FINAL + srcClassName + " src)";

            defSB.Append(funcDecl);
            {
                defSB.AppendLine(" {");

                // get a dictionary which tells you for each basis blade of 'smv' where it is in 'gmv'
                // A dictionary from <smv group, smv element> to <gmv group, gmv element>
                Dictionary<Tuple<int, int>, Tuple<int, int>> D = G25.MV.GetCoordMap(smv, gmv);

                // what is the highest group of the 'gmv' that must be (partially) copied to the 'smv'
                int highestGroup = -1;
                foreach (KeyValuePair<Tuple<int, int>, Tuple<int, int>> KVP in D)
                    if (KVP.Value.Value1 > highestGroup) highestGroup = KVP.Value.Value1;

                // generate code for each group
                for (int g = 0; g <= highestGroup; g++)
                {
                    // determine if group 'g' is to be copied to smv:
                    bool groupIsUsedBySMV = false;
                    foreach (KeyValuePair<Tuple<int, int>, Tuple<int, int>> KVP in D)
                    {
                        // KVP.Key = SMV<group, element>
                        // KVP.Value = GMV<group, element>
                        if (KVP.Value.Value1 == g)
                        {
                            if (!smv.IsCoordinateConstant(KVP.Key.Value2))
                            {
                                groupIsUsedBySMV = true;
                                break;
                            }
                        }
                    }

                    // if group is present in GMV:
                    if (groupIsUsedBySMV)
                    {
                        defSB.AppendLine("\t\tif (src.c()[" + g + "] != null) {");
                        defSB.AppendLine("\t\t\t" + FT.type + "[] ptr = src.c()[" + g + "];");
                        bool mustCast = false;
                        bool srcPtr = true;
                        int nbTabs = 3;
                        RefGA.Multivector[] value = G25.CG.Shared.Symbolic.GMVtoSymbolicMultivector(S, gmv, "ptr", srcPtr, g);
                        bool writeZeros = false;
                        string str = G25.CG.Shared.CodeUtil.GenerateSMVassignmentCode(S, FT, mustCast, smv, G25.CG.Shared.SmvUtil.THIS, dstPtr, value[g], nbTabs, writeZeros);
                        defSB.Append(str);
                        defSB.AppendLine("\t\t}");

                        defSB.AppendLine("\t\telse {");
                        foreach (KeyValuePair<Tuple<int, int>, Tuple<int, int>> KVP in D)
                        {
                            if ((KVP.Value.Value1 == g) && (!smv.IsCoordinateConstant(KVP.Key.Value2)))
                            {
                                // translate KVP.Key.Value2 to non-const idx, because the accessStrs are only about non-const blades blades!
                                int bladeIdx = smv.BladeIdxToNonConstBladeIdx(KVP.Key.Value2);
                                defSB.AppendLine("\t\t\t" + smvAccessStr[bladeIdx] + " = " + FT.DoubleToString(S, 0.0) + ";");
                            }
                        }
                        defSB.AppendLine("\t\t}");
                    }
                }
                defSB.AppendLine("\t}");
            }
        }
Example #2
0
File: xml.cs Project: Sciumo/gaigen
        /// <summary>
        /// Converts a G25.SMV to an XML string representation.
        /// </summary>
        /// <param name="S"></param>
        /// <param name="smv"></param>
        /// <returns>XML string representation of 'smv'.</returns>
        public static string SMVtoXmlString(Specification S, G25.SMV smv)
        {
            StringBuilder SB = new StringBuilder();
            bool constant = smv.IsConstant() && (S.GetMatchingConstant(smv) != null);
            string name = smv.Name;

            // remove the extra constant suffix?
            if (constant && name.EndsWith(Specification.CONSTANT_TYPE_SUFFIX))
                name = name.Substring(0, name.Length - Specification.CONSTANT_TYPE_SUFFIX.Length);

            SB.Append("<" + XML_SMV);

            // name
            SB.Append(" " + XML_NAME + "=\"" + name + "\"");

            // constant?
            if (constant)
                SB.Append(" " + XML_CONST + "=\"" + XML_TRUE + "\"");

            // type
            SB.Append(" " + XML_TYPE + "=\"" + smv.MvTypeString + "\"");

            // end of XML_SMV tag
            SB.Append(">");

            { // emit coordinate order:
                string[] bvNames = (string[])S.m_basisVectorNames.ToArray();
                // loop over all basis blades
                for (int b = 0; b < smv.Group(0).Length; b++)
                {
                    if (b > 0) SB.Append(" ");
                    string bbStr = BasisBladeToString(smv.BasisBlade(0, b), bvNames);
                    SB.Append(bbStr);

                    // if constant, add '=....'
                    if (smv.IsCoordinateConstant(b))
                        SB.Append("=" + smv.ConstBasisBladeValue(smv.BladeIdxToConstBladeIdx(b)).ToString());
                }
                if (smv.Comment.Length > 0)
                    SB.Append("  <" + XML_COMMENT + ">" + smv.Comment + "</" + XML_COMMENT + ">");
            }

            SB.Append("</" + XML_SMV + ">\n");

            return SB.ToString();
        }