aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Instruction/AInstEmitHash.cs
blob: 69bdbc480db00915d1d315fd533cd4c75f305bcb (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using ChocolArm64.Decoder;
using ChocolArm64.State;
using ChocolArm64.Translation;
using System;
using System.Reflection.Emit;
using System.Runtime.Intrinsics.X86;

namespace ChocolArm64.Instruction
{
    static partial class AInstEmit
    {
        public static void Crc32b(AILEmitterCtx Context)
        {
            EmitCrc32(Context, nameof(ASoftFallback.Crc32b));
        }

        public static void Crc32h(AILEmitterCtx Context)
        {
            EmitCrc32(Context, nameof(ASoftFallback.Crc32h));
        }

        public static void Crc32w(AILEmitterCtx Context)
        {
            EmitCrc32(Context, nameof(ASoftFallback.Crc32w));
        }

        public static void Crc32x(AILEmitterCtx Context)
        {
            EmitCrc32(Context, nameof(ASoftFallback.Crc32x));
        }

        public static void Crc32cb(AILEmitterCtx Context)
        {
            if (AOptimizations.UseSse42)
            {
                EmitSse42Crc32(Context, typeof(uint), typeof(byte));
            }
            else
            {
                EmitCrc32(Context, nameof(ASoftFallback.Crc32cb));
            }
        }

        public static void Crc32ch(AILEmitterCtx Context)
        {
            if (AOptimizations.UseSse42)
            {
                EmitSse42Crc32(Context, typeof(uint), typeof(ushort));
            }
            else
            {
                EmitCrc32(Context, nameof(ASoftFallback.Crc32ch));
            }
        }

        public static void Crc32cw(AILEmitterCtx Context)
        {
            if (AOptimizations.UseSse42)
            {
                EmitSse42Crc32(Context, typeof(uint), typeof(uint));
            }
            else
            {
                EmitCrc32(Context, nameof(ASoftFallback.Crc32cw));
            }
        }

        public static void Crc32cx(AILEmitterCtx Context)
        {
            if (AOptimizations.UseSse42)
            {
                EmitSse42Crc32(Context, typeof(ulong), typeof(ulong));
            }
            else
            {
                EmitCrc32(Context, nameof(ASoftFallback.Crc32cx));
            }
        }

        private static void EmitSse42Crc32(AILEmitterCtx Context, Type TCrc, Type TData)
        {
            AOpCodeAluRs Op = (AOpCodeAluRs)Context.CurrOp;

            Context.EmitLdintzr(Op.Rn);
            Context.EmitLdintzr(Op.Rm);

            Context.EmitCall(typeof(Sse42).GetMethod(nameof(Sse42.Crc32), new Type[] { TCrc, TData }));

            Context.EmitStintzr(Op.Rd);
        }

        private static void EmitCrc32(AILEmitterCtx Context, string Name)
        {
            AOpCodeAluRs Op = (AOpCodeAluRs)Context.CurrOp;

            Context.EmitLdintzr(Op.Rn);

            if (Op.RegisterSize != ARegisterSize.Int32)
            {
                Context.Emit(OpCodes.Conv_U4);
            }

            Context.EmitLdintzr(Op.Rm);

            ASoftFallback.EmitCall(Context, Name);

            if (Op.RegisterSize != ARegisterSize.Int32)
            {
                Context.Emit(OpCodes.Conv_U8);
            }

            Context.EmitStintzr(Op.Rd);
        }
    }
}