GenerationConfig.ttinclude 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <#@ import namespace="System.Linq" #>
  2. <#@ import namespace="System.Collections.Generic" #>
  3. <#@ import namespace="System.Text" #>
  4. <#+
  5. /* This file includes static data used as compilation configuration for the rest of the code generation.
  6. It is shared here to ensure that all generated code compiles with the same constants and configurations. */
  7. // The set of supported numeric types to compile
  8. public static Type[] supportedTypes = new[]
  9. {
  10. typeof(byte), typeof(sbyte), typeof(ushort), typeof(short),
  11. typeof(uint), typeof(int), typeof(ulong), typeof(long),
  12. typeof(float), typeof(double)
  13. };
  14. // The set of unsigned types, a subset of the above. Used for excluding from certain methods, i.e. Abs and Negate
  15. public static Type[] unsignedTypes = new[]
  16. {
  17. typeof(byte), typeof(ushort), typeof(uint), typeof(ulong)
  18. };
  19. public static Type[] integralTypes = new[]
  20. {
  21. typeof(byte), typeof(sbyte), typeof(ushort), typeof(short),
  22. typeof(uint), typeof(int), typeof(ulong), typeof(long)
  23. };
  24. public static Type[] nonClsCompliantTypes = new[]
  25. {
  26. typeof(sbyte), typeof(ushort), typeof(uint), typeof(ulong)
  27. };
  28. public static Dictionary<Type, string> typeAliases = new Dictionary<Type, string>()
  29. {
  30. { typeof(byte), "byte" },
  31. { typeof(sbyte), "sbyte" },
  32. { typeof(ushort), "ushort" },
  33. { typeof(short), "short" },
  34. { typeof(uint), "uint" },
  35. { typeof(int), "int" },
  36. { typeof(ulong), "ulong" },
  37. { typeof(long), "long" },
  38. { typeof(float), "float" },
  39. { typeof(double), "double" }
  40. };
  41. // The total register size, in bytes. 16 for SSE2, 32 for AVX, 64 for AVX512
  42. public static int totalSize = 16;
  43. /* General template helper procedures */
  44. // Returns the constructed register field name for the given type and index.
  45. public string GetRegisterFieldName(Type t, int index)
  46. {
  47. return "register." + t.Name.ToLowerInvariant() + "_" + index;
  48. }
  49. // Returns the number of fields for a given type, based on the current configuration's register size
  50. public int GetNumFields(Type t, int totalSize)
  51. {
  52. return totalSize / Marshal.SizeOf(t);
  53. }
  54. public static HashSet<Type> WidenableTypes { get; private set; } = new HashSet<Type>()
  55. {
  56. typeof(byte),
  57. typeof(ushort),
  58. typeof(uint),
  59. typeof(sbyte),
  60. typeof(short),
  61. typeof(int),
  62. typeof(float),
  63. };
  64. private static Dictionary<Type, Type> s_widenTargets = new Dictionary<Type, Type>()
  65. {
  66. { typeof(byte), typeof(ushort) },
  67. { typeof(ushort), typeof(uint) },
  68. { typeof(uint), typeof(ulong) },
  69. { typeof(sbyte), typeof(short) },
  70. { typeof(short), typeof(int) },
  71. { typeof(int), typeof(long) },
  72. { typeof(float), typeof(double) },
  73. };
  74. public Type GetWidenTarget(Type t)
  75. {
  76. Type target;
  77. if (!s_widenTargets.TryGetValue(t, out target))
  78. {
  79. throw new InvalidOperationException("No widen target for " + t.Name);
  80. }
  81. return target;
  82. }
  83. public static HashSet<Type> NarrowableTypes { get; private set; } = new HashSet<Type>()
  84. {
  85. typeof(ushort),
  86. typeof(uint),
  87. typeof(ulong),
  88. typeof(short),
  89. typeof(int),
  90. typeof(long),
  91. typeof(double),
  92. };
  93. private static Dictionary<Type, Type> s_narrowTargets = new Dictionary<Type, Type>()
  94. {
  95. { typeof(ulong), typeof(uint) },
  96. { typeof(uint), typeof(ushort) },
  97. { typeof(ushort), typeof(byte) },
  98. { typeof(long), typeof(int) },
  99. { typeof(int), typeof(short) },
  100. { typeof(short), typeof(sbyte) },
  101. { typeof(double), typeof(float) },
  102. };
  103. public Type GetNarrowTarget(Type t)
  104. {
  105. Type target;
  106. if (!s_narrowTargets.TryGetValue(t, out target))
  107. {
  108. throw new InvalidOperationException("No narrow target for " + t.Name);
  109. }
  110. return target;
  111. }
  112. public static List<KeyValuePair<Type, Type>> SameSizeConversionPairs = new List<KeyValuePair<Type, Type>>()
  113. {
  114. new KeyValuePair<Type, Type>(typeof(int), typeof(float)),
  115. new KeyValuePair<Type, Type>(typeof(uint), typeof(float)),
  116. new KeyValuePair<Type, Type>(typeof(long), typeof(double)),
  117. new KeyValuePair<Type, Type>(typeof(ulong), typeof(double)),
  118. new KeyValuePair<Type, Type>(typeof(float), typeof(int)),
  119. new KeyValuePair<Type, Type>(typeof(float), typeof(uint)),
  120. new KeyValuePair<Type, Type>(typeof(double), typeof(long)),
  121. new KeyValuePair<Type, Type>(typeof(double), typeof(ulong)),
  122. };
  123. public void GenerateCopyrightHeader()
  124. {
  125. #>// Licensed to the .NET Foundation under one or more agreements.
  126. // The .NET Foundation licenses this file to you under the MIT license.
  127. // See the LICENSE file in the project root for more information.
  128. <#+
  129. }
  130. public string GenerateIfStatementHeader(Type type)
  131. {
  132. string keyword = (type == supportedTypes[0]) ? "if" : "else if";
  133. return string.Format("{0} (typeof(T) == typeof({1}))", keyword, typeAliases[type]);
  134. }
  135. public string GenerateIfStatementHeader(Type type, IEnumerable<Type> allTypes)
  136. {
  137. string keyword = (type == allTypes.ToArray()[0]) ? "if" : "else if";
  138. return string.Format("{0} (typeof(T) == typeof({1}))", keyword, typeAliases[type]);
  139. }
  140. public string MakeTypeComparisonCondition(Type type)
  141. {
  142. return string.Format("(typeof(T) == typeof({0}))", typeAliases[type]);
  143. }
  144. public string GenerateIfConditionAllTypes(IEnumerable<Type> allTypes)
  145. {
  146. StringBuilder sbuilder = new StringBuilder();
  147. bool firstTime = true;
  148. foreach (var type in allTypes)
  149. {
  150. if (firstTime)
  151. {
  152. sbuilder.Append("if (").Append(MakeTypeComparisonCondition(type));
  153. firstTime = false;
  154. }
  155. else
  156. {
  157. sbuilder.AppendLine().Append(" || ").Append(MakeTypeComparisonCondition(type));
  158. }
  159. }
  160. sbuilder.Append(")");
  161. return sbuilder.ToString();
  162. }
  163. #>