-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitOperations.tt
More file actions
85 lines (78 loc) · 2.86 KB
/
BitOperations.tt
File metadata and controls
85 lines (78 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".autogen.cs" #>
<#
var ops = new []
{
new Operation("And", "And", false),
new Operation("Complement", "Not", true), new Operation("Not", "Not", true),
new Operation("Or", "Or", false),
new Operation("ShiftLeft", "Shl", false, "By"),
new Operation("ShiftRight", "Shr", false, "By"),
new Operation("ShiftRightUnsigned", "Shr_Un", false, "By"),
new Operation("Xor", "Xor", false)
};
#>
using System;
using System.Reflection.Emit;
using JetBrains.Annotations;
namespace ILGeneratorExtensions
{
/// <summary>
/// Contains extension methods for perofmring bitwise operations on integers
/// </summary>
public static class BitOperations
{
<# foreach (var op in ops) { #>
#region <#= op.Name #>
<# if (op.Unary) { #>
/// <summary>
/// Pop an integer value from the evaluation stack and perform a bitwise <#= op.Name.ToLower() #> operation on it
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator <#= op.Name #>(this ILGenerator generator) => generator.FluentEmit(OpCodes.<#= op.Op #>);
<# } else { #>
/// <summary>
/// Pop two integer values from the evaluation stack and perform a bitwise <#= op.Name.ToLower() #> operation on them
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator <#= op.Name #>(this ILGenerator generator) => generator.FluentEmit(OpCodes.<#= op.Op #>);
<# foreach (var type in new [] { typeof(int), typeof(uint), typeof(long), typeof(ulong) }) { #>
/// <summary>
/// Pop an integer value from the evaluation stack and perform a bitwise <#= op.Name.ToLower() #> operation <#= op.Suffix.ToLower() #> the given value
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="value">The value to bitwise <#= op.Name.ToLower() #> the evaluation stack value <#= op.Suffix.ToLower() #></param>
[PublicAPI]
public static ILGenerator <#= op.Name #><#= op.Suffix #>(this ILGenerator generator, <#= type.Name #> value)
{
return generator.LoadConstant(value)
.<#= op.Name #>();
}
<# } // if #>
<# } // foreach #>
#endregion
<# } // foreach #>
}
}
<#+
public class Operation
{
public string Name;
public string Op;
public bool Unary;
public string Suffix;
public Operation(string name, string op, bool unary, string suffix = "With")
{
Name = name;
Op = op;
Unary = unary;
Suffix = suffix;
}
}
#>