| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <#@ import namespace="System.Linq" #>
- <#@ import namespace="System.Collections.Generic" #>
- <#@ import namespace="System.Text" #>
- <#+
- /* This file includes static data used as compilation configuration for the rest of the code generation.
- It is shared here to ensure that all generated code compiles with the same constants and configurations. */
- // The set of supported numeric types to compile
- public static Type[] supportedTypes = new[]
- {
- typeof(byte), typeof(sbyte), typeof(ushort), typeof(short),
- typeof(uint), typeof(int), typeof(ulong), typeof(long),
- typeof(float), typeof(double)
- };
- // The set of unsigned types, a subset of the above. Used for excluding from certain methods, i.e. Abs and Negate
- public static Type[] unsignedTypes = new[]
- {
- typeof(byte), typeof(ushort), typeof(uint), typeof(ulong)
- };
- public static Type[] integralTypes = new[]
- {
- typeof(byte), typeof(sbyte), typeof(ushort), typeof(short),
- typeof(uint), typeof(int), typeof(ulong), typeof(long)
- };
- public static Type[] nonClsCompliantTypes = new[]
- {
- typeof(sbyte), typeof(ushort), typeof(uint), typeof(ulong)
- };
- public static Dictionary<Type, string> typeAliases = new Dictionary<Type, string>()
- {
- { typeof(byte), "byte" },
- { typeof(sbyte), "sbyte" },
- { typeof(ushort), "ushort" },
- { typeof(short), "short" },
- { typeof(uint), "uint" },
- { typeof(int), "int" },
- { typeof(ulong), "ulong" },
- { typeof(long), "long" },
- { typeof(float), "float" },
- { typeof(double), "double" }
- };
- // The total register size, in bytes. 16 for SSE2, 32 for AVX, 64 for AVX512
- public static int totalSize = 16;
- /* General template helper procedures */
- // Returns the constructed register field name for the given type and index.
- public string GetRegisterFieldName(Type t, int index)
- {
- return "register." + t.Name.ToLowerInvariant() + "_" + index;
- }
- // Returns the number of fields for a given type, based on the current configuration's register size
- public int GetNumFields(Type t, int totalSize)
- {
- return totalSize / Marshal.SizeOf(t);
- }
- public static HashSet<Type> WidenableTypes { get; private set; } = new HashSet<Type>()
- {
- typeof(byte),
- typeof(ushort),
- typeof(uint),
- typeof(sbyte),
- typeof(short),
- typeof(int),
- typeof(float),
- };
- private static Dictionary<Type, Type> s_widenTargets = new Dictionary<Type, Type>()
- {
- { typeof(byte), typeof(ushort) },
- { typeof(ushort), typeof(uint) },
- { typeof(uint), typeof(ulong) },
- { typeof(sbyte), typeof(short) },
- { typeof(short), typeof(int) },
- { typeof(int), typeof(long) },
- { typeof(float), typeof(double) },
- };
- public Type GetWidenTarget(Type t)
- {
- Type target;
- if (!s_widenTargets.TryGetValue(t, out target))
- {
- throw new InvalidOperationException("No widen target for " + t.Name);
- }
- return target;
- }
- public static HashSet<Type> NarrowableTypes { get; private set; } = new HashSet<Type>()
- {
- typeof(ushort),
- typeof(uint),
- typeof(ulong),
- typeof(short),
- typeof(int),
- typeof(long),
- typeof(double),
- };
- private static Dictionary<Type, Type> s_narrowTargets = new Dictionary<Type, Type>()
- {
- { typeof(ulong), typeof(uint) },
- { typeof(uint), typeof(ushort) },
- { typeof(ushort), typeof(byte) },
- { typeof(long), typeof(int) },
- { typeof(int), typeof(short) },
- { typeof(short), typeof(sbyte) },
- { typeof(double), typeof(float) },
- };
- public Type GetNarrowTarget(Type t)
- {
- Type target;
- if (!s_narrowTargets.TryGetValue(t, out target))
- {
- throw new InvalidOperationException("No narrow target for " + t.Name);
- }
- return target;
- }
- public static List<KeyValuePair<Type, Type>> SameSizeConversionPairs = new List<KeyValuePair<Type, Type>>()
- {
- new KeyValuePair<Type, Type>(typeof(int), typeof(float)),
- new KeyValuePair<Type, Type>(typeof(uint), typeof(float)),
- new KeyValuePair<Type, Type>(typeof(long), typeof(double)),
- new KeyValuePair<Type, Type>(typeof(ulong), typeof(double)),
- new KeyValuePair<Type, Type>(typeof(float), typeof(int)),
- new KeyValuePair<Type, Type>(typeof(float), typeof(uint)),
- new KeyValuePair<Type, Type>(typeof(double), typeof(long)),
- new KeyValuePair<Type, Type>(typeof(double), typeof(ulong)),
- };
- public void GenerateCopyrightHeader()
- {
- #>// Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- <#+
- }
- public string GenerateIfStatementHeader(Type type)
- {
- string keyword = (type == supportedTypes[0]) ? "if" : "else if";
- return string.Format("{0} (typeof(T) == typeof({1}))", keyword, typeAliases[type]);
- }
- public string GenerateIfStatementHeader(Type type, IEnumerable<Type> allTypes)
- {
- string keyword = (type == allTypes.ToArray()[0]) ? "if" : "else if";
- return string.Format("{0} (typeof(T) == typeof({1}))", keyword, typeAliases[type]);
- }
- public string MakeTypeComparisonCondition(Type type)
- {
- return string.Format("(typeof(T) == typeof({0}))", typeAliases[type]);
- }
- public string GenerateIfConditionAllTypes(IEnumerable<Type> allTypes)
- {
- StringBuilder sbuilder = new StringBuilder();
- bool firstTime = true;
- foreach (var type in allTypes)
- {
- if (firstTime)
- {
- sbuilder.Append("if (").Append(MakeTypeComparisonCondition(type));
- firstTime = false;
- }
- else
- {
- sbuilder.AppendLine().Append(" || ").Append(MakeTypeComparisonCondition(type));
- }
- }
- sbuilder.Append(")");
- return sbuilder.ToString();
- }
- #>
|