|
| 1 | +using System; |
| 2 | +using Platform.Reflection; |
| 3 | +using Platform.Reflection.Sigil; |
| 4 | + |
| 5 | +// ReSharper disable StaticFieldInGenericType |
| 6 | + |
| 7 | +namespace Platform.Numbers |
| 8 | +{ |
| 9 | + public static class BitwiseHelpers<T> |
| 10 | + { |
| 11 | + public static readonly Func<T, T, int, int, T> PartialWrite; |
| 12 | + public static readonly Func<T, int, int, T> PartialRead; |
| 13 | + |
| 14 | + static BitwiseHelpers() |
| 15 | + { |
| 16 | + DelegateHelpers.Compile(out PartialWrite, emiter => |
| 17 | + { |
| 18 | + if (!CachedTypeInfo<T>.IsNumeric) |
| 19 | + throw new NotSupportedException(); |
| 20 | + |
| 21 | + var constants = GetConstants<T>(); |
| 22 | + var bitsNumber = constants.Item1; |
| 23 | + var numberFilledWithOnes = constants.Item2; |
| 24 | + |
| 25 | + ushort shiftArgument = 2; |
| 26 | + ushort limitArgument = 3; |
| 27 | + |
| 28 | + var checkLimit = emiter.DefineLabel(); |
| 29 | + var calculateSourceMask = emiter.DefineLabel(); |
| 30 | + |
| 31 | + // Check shift |
| 32 | + emiter.LoadArgument(shiftArgument); |
| 33 | + emiter.LoadConstant(0); |
| 34 | + emiter.BranchIfGreaterOrEqual(checkLimit); // Skip fix |
| 35 | + |
| 36 | + // Fix shift |
| 37 | + emiter.LoadConstant(bitsNumber); |
| 38 | + emiter.LoadArgument(shiftArgument); |
| 39 | + emiter.Add(); |
| 40 | + emiter.StoreArgument(shiftArgument); |
| 41 | + |
| 42 | + emiter.MarkLabel(checkLimit); |
| 43 | + // Check limit |
| 44 | + emiter.LoadArgument(limitArgument); |
| 45 | + emiter.LoadConstant(0); |
| 46 | + emiter.BranchIfGreaterOrEqual(calculateSourceMask); // Skip fix |
| 47 | + |
| 48 | + // Fix limit |
| 49 | + emiter.LoadConstant(bitsNumber); |
| 50 | + emiter.LoadArgument(limitArgument); |
| 51 | + emiter.Add(); |
| 52 | + emiter.StoreArgument(limitArgument); |
| 53 | + |
| 54 | + emiter.MarkLabel(calculateSourceMask); |
| 55 | + |
| 56 | + using (var sourceMask = emiter.DeclareLocal<T>()) |
| 57 | + using (var targetMask = emiter.DeclareLocal<T>()) |
| 58 | + { |
| 59 | + emiter.LoadConstant(typeof(T), numberFilledWithOnes); |
| 60 | + emiter.LoadArgument(limitArgument); |
| 61 | + emiter.ShiftLeft(); |
| 62 | + emiter.Not(); |
| 63 | + emiter.LoadConstant(typeof(T), numberFilledWithOnes); |
| 64 | + emiter.And(); |
| 65 | + emiter.StoreLocal(sourceMask); |
| 66 | + |
| 67 | + emiter.LoadLocal(sourceMask); |
| 68 | + emiter.LoadArgument(shiftArgument); |
| 69 | + emiter.ShiftLeft(); |
| 70 | + emiter.Not(); |
| 71 | + emiter.StoreLocal(targetMask); |
| 72 | + |
| 73 | + emiter.LoadArgument(0); // target |
| 74 | + emiter.LoadLocal(targetMask); |
| 75 | + emiter.And(); |
| 76 | + emiter.LoadArgument(1); // source |
| 77 | + emiter.LoadLocal(sourceMask); |
| 78 | + emiter.And(); |
| 79 | + emiter.LoadArgument(shiftArgument); |
| 80 | + emiter.ShiftLeft(); |
| 81 | + emiter.Or(); |
| 82 | + } |
| 83 | + |
| 84 | + emiter.Return(); |
| 85 | + }); |
| 86 | + |
| 87 | + DelegateHelpers.Compile(out PartialRead, emiter => |
| 88 | + { |
| 89 | + if (!CachedTypeInfo<T>.IsNumeric) |
| 90 | + throw new NotSupportedException(); |
| 91 | + |
| 92 | + var constants = GetConstants<T>(); |
| 93 | + var bitsNumber = constants.Item1; |
| 94 | + var numberFilledWithOnes = constants.Item2; |
| 95 | + |
| 96 | + ushort shiftArgument = 1; |
| 97 | + ushort limitArgument = 2; |
| 98 | + |
| 99 | + var checkLimit = emiter.DefineLabel(); |
| 100 | + var calculateSourceMask = emiter.DefineLabel(); |
| 101 | + |
| 102 | + // Check shift |
| 103 | + emiter.LoadArgument(shiftArgument); |
| 104 | + emiter.LoadConstant(0); |
| 105 | + emiter.BranchIfGreaterOrEqual(checkLimit); // Skip fix |
| 106 | + |
| 107 | + // Fix shift |
| 108 | + emiter.LoadConstant(bitsNumber); |
| 109 | + emiter.LoadArgument(shiftArgument); |
| 110 | + emiter.Add(); |
| 111 | + emiter.StoreArgument(shiftArgument); |
| 112 | + |
| 113 | + emiter.MarkLabel(checkLimit); |
| 114 | + // Check limit |
| 115 | + emiter.LoadArgument(limitArgument); |
| 116 | + emiter.LoadConstant(0); |
| 117 | + emiter.BranchIfGreaterOrEqual(calculateSourceMask); // Skip fix |
| 118 | + |
| 119 | + // Fix limit |
| 120 | + emiter.LoadConstant(bitsNumber); |
| 121 | + emiter.LoadArgument(limitArgument); |
| 122 | + emiter.Add(); |
| 123 | + emiter.StoreArgument(limitArgument); |
| 124 | + |
| 125 | + emiter.MarkLabel(calculateSourceMask); |
| 126 | + |
| 127 | + using (var sourceMask = emiter.DeclareLocal<T>()) |
| 128 | + using (var targetMask = emiter.DeclareLocal<T>()) |
| 129 | + { |
| 130 | + emiter.LoadConstant(typeof(T), numberFilledWithOnes); |
| 131 | + emiter.LoadArgument(limitArgument); // limit |
| 132 | + emiter.ShiftLeft(); |
| 133 | + emiter.Not(); |
| 134 | + emiter.LoadConstant(typeof(T), numberFilledWithOnes); |
| 135 | + emiter.And(); |
| 136 | + emiter.StoreLocal(sourceMask); |
| 137 | + |
| 138 | + emiter.LoadLocal(sourceMask); |
| 139 | + emiter.LoadArgument(shiftArgument); |
| 140 | + emiter.ShiftLeft(); |
| 141 | + emiter.StoreLocal(targetMask); |
| 142 | + |
| 143 | + emiter.LoadArgument(0); // target |
| 144 | + emiter.LoadLocal(targetMask); |
| 145 | + emiter.And(); |
| 146 | + emiter.LoadArgument(shiftArgument); |
| 147 | + emiter.ShiftRight(); |
| 148 | + } |
| 149 | + |
| 150 | + emiter.Return(); |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + private static Tuple<int, TElement> GetConstants<TElement>() |
| 155 | + { |
| 156 | + var type = typeof(T); |
| 157 | + if (type == typeof(ulong)) |
| 158 | + return new Tuple<int, TElement>(64, (TElement)(object)ulong.MaxValue); |
| 159 | + if (type == typeof(uint)) |
| 160 | + return new Tuple<int, TElement>(32, (TElement)(object)uint.MaxValue); |
| 161 | + if (type == typeof(ushort)) |
| 162 | + return new Tuple<int, TElement>(16, (TElement)(object)ushort.MaxValue); |
| 163 | + if (type == typeof(byte)) |
| 164 | + return new Tuple<int, TElement>(8, (TElement)(object)byte.MaxValue); |
| 165 | + throw new NotSupportedException(); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments