<#@ 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 typeAliases = new Dictionary() { { 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 WidenableTypes { get; private set; } = new HashSet() { typeof(byte), typeof(ushort), typeof(uint), typeof(sbyte), typeof(short), typeof(int), typeof(float), }; private static Dictionary s_widenTargets = new Dictionary() { { 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 NarrowableTypes { get; private set; } = new HashSet() { typeof(ushort), typeof(uint), typeof(ulong), typeof(short), typeof(int), typeof(long), typeof(double), }; private static Dictionary s_narrowTargets = new Dictionary() { { 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> SameSizeConversionPairs = new List>() { new KeyValuePair(typeof(int), typeof(float)), new KeyValuePair(typeof(uint), typeof(float)), new KeyValuePair(typeof(long), typeof(double)), new KeyValuePair(typeof(ulong), typeof(double)), new KeyValuePair(typeof(float), typeof(int)), new KeyValuePair(typeof(float), typeof(uint)), new KeyValuePair(typeof(double), typeof(long)), new KeyValuePair(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 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 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(); } #>