2019-10-13 07:02:07 +01:00
|
|
|
using Ryujinx.Graphics.Shader.Decoders;
|
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
|
|
|
|
using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
|
|
|
|
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.Instructions
|
|
|
|
{
|
|
|
|
static partial class InstEmit
|
|
|
|
{
|
2019-11-08 20:29:41 +00:00
|
|
|
private enum MemoryRegion
|
|
|
|
{
|
|
|
|
Local,
|
|
|
|
Shared
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:06:46 +00:00
|
|
|
public static void Atom(EmitterContext context)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstAtom op = context.GetOp<InstAtom>();
|
2020-11-10 00:06:46 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
int sOffset = (op.Imm20 << 12) >> 12;
|
2020-11-10 00:06:46 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
(Operand addrLow, Operand addrHigh) = Get40BitsAddress(context, new Register(op.SrcA, RegisterType.Gpr), op.E, sOffset);
|
2020-11-10 00:06:46 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand value = GetSrcReg(context, op.SrcB);
|
2020-11-10 00:06:46 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand res = EmitAtomicOp(context, Instruction.MrGlobal, op.Op, op.Size, addrLow, addrHigh, value);
|
2020-11-10 00:06:46 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
context.Copy(GetDest(op.Dest), res);
|
2020-11-10 00:06:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 20:29:41 +00:00
|
|
|
public static void Atoms(EmitterContext context)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstAtoms op = context.GetOp<InstAtoms>();
|
2019-11-08 20:29:41 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand offset = context.ShiftRightU32(GetSrcReg(context, op.SrcA), Const(2));
|
2019-11-08 20:29:41 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
int sOffset = (op.Imm22 << 10) >> 10;
|
2020-11-10 00:06:46 +00:00
|
|
|
|
|
|
|
offset = context.IAdd(offset, Const(sOffset));
|
2019-11-08 20:29:41 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand value = GetSrcReg(context, op.SrcB);
|
2020-10-13 01:40:50 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
AtomSize size = op.AtomsSize switch
|
2020-04-03 01:20:47 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
AtomsSize.S32 => AtomSize.S32,
|
|
|
|
AtomsSize.U64 => AtomSize.U64,
|
|
|
|
AtomsSize.S64 => AtomSize.S64,
|
|
|
|
_ => AtomSize.U32
|
|
|
|
};
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand res = EmitAtomicOp(context, Instruction.MrShared, op.AtomOp, size, offset, Const(0), value);
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
context.Copy(GetDest(op.Dest), res);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Ldc(EmitterContext context)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstLdc op = context.GetOp<InstLdc>();
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
if (op.LsSize > LsSize2.B64)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid LDC size: {op.LsSize}.");
|
|
|
|
return;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
bool isSmallInt = op.LsSize < LsSize2.B32;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
int count = op.LsSize == LsSize2.B64 ? 2 : 1;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand slot = Const(op.CbufSlot);
|
|
|
|
Operand srcA = GetSrcReg(context, op.SrcA);
|
2020-10-13 01:40:50 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
if (op.AddressMode == AddressMode.Is || op.AddressMode == AddressMode.Isl)
|
2020-10-13 01:40:50 +01:00
|
|
|
{
|
|
|
|
slot = context.IAdd(slot, context.BitfieldExtractU32(srcA, Const(16), Const(16)));
|
|
|
|
srcA = context.BitwiseAnd(srcA, Const(0xffff));
|
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand addr = context.IAdd(srcA, Const(Imm16ToSInt(op.CbufOffset)));
|
2020-02-14 00:48:07 +00:00
|
|
|
Operand wordOffset = context.ShiftRightU32(addr, Const(2));
|
|
|
|
Operand bitOffset = GetBitOffset(context, addr);
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
for (int index = 0; index < count; index++)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
Register dest = new Register(op.Dest + index, RegisterType.Gpr);
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
if (dest.IsRZ)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand offset = context.IAdd(wordOffset, Const(index));
|
2020-10-13 01:40:50 +01:00
|
|
|
Operand value = context.LoadConstant(slot, offset);
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
if (isSmallInt)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
value = ExtractSmallInt(context, (LsSize)op.LsSize, bitOffset, value);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
context.Copy(Register(dest), value);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Ldg(EmitterContext context)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstLdg op = context.GetOp<InstLdg>();
|
2019-11-08 20:29:41 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
EmitLdg(context, op.LsSize, op.SrcA, op.Dest, Imm24ToSInt(op.Imm24), op.E);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
public static void Ldl(EmitterContext context)
|
2019-12-14 17:51:00 +00:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstLdl op = context.GetOp<InstLdl>();
|
2019-12-14 17:51:00 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
EmitLoad(context, MemoryRegion.Local, op.LsSize, GetSrcReg(context, op.SrcA), op.Dest, Imm24ToSInt(op.Imm24));
|
2019-12-14 17:51:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
public static void Lds(EmitterContext context)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstLds op = context.GetOp<InstLds>();
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
EmitLoad(context, MemoryRegion.Shared, op.LsSize, GetSrcReg(context, op.SrcA), op.Dest, Imm24ToSInt(op.Imm24));
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 20:29:41 +00:00
|
|
|
public static void Red(EmitterContext context)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstRed op = context.GetOp<InstRed>();
|
|
|
|
|
|
|
|
(Operand addrLow, Operand addrHigh) = Get40BitsAddress(context, new Register(op.SrcA, RegisterType.Gpr), op.E, op.Imm20);
|
|
|
|
|
|
|
|
EmitAtomicOp(context, Instruction.MrGlobal, (AtomOp)op.RedOp, op.RedSize, addrLow, addrHigh, GetDest(op.SrcB));
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
public static void Stg(EmitterContext context)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstStg op = context.GetOp<InstStg>();
|
|
|
|
|
|
|
|
EmitStg(context, op.LsSize, op.SrcA, op.Dest, Imm24ToSInt(op.Imm24), op.E);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
public static void Stl(EmitterContext context)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstStl op = context.GetOp<InstStl>();
|
|
|
|
|
|
|
|
EmitStore(context, MemoryRegion.Local, op.LsSize, GetSrcReg(context, op.SrcA), op.Dest, Imm24ToSInt(op.Imm24));
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Sts(EmitterContext context)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
InstSts op = context.GetOp<InstSts>();
|
|
|
|
|
|
|
|
EmitStore(context, MemoryRegion.Shared, op.LsSize, GetSrcReg(context, op.SrcA), op.Dest, Imm24ToSInt(op.Imm24));
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 20:29:41 +00:00
|
|
|
private static Operand EmitAtomicOp(
|
|
|
|
EmitterContext context,
|
2021-10-12 21:35:31 +01:00
|
|
|
Instruction mr,
|
|
|
|
AtomOp op,
|
|
|
|
AtomSize type,
|
|
|
|
Operand addrLow,
|
|
|
|
Operand addrHigh,
|
|
|
|
Operand value)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-11-19 13:45:46 +00:00
|
|
|
Operand res = Const(0);
|
2019-11-08 20:29:41 +00:00
|
|
|
|
|
|
|
switch (op)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case AtomOp.Add:
|
|
|
|
if (type == AtomSize.S32 || type == AtomSize.U32)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
res = context.AtomicAdd(mr, addrLow, addrHigh, value);
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-06 02:02:28 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid reduction type: {type}.");
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-10-12 21:35:31 +01:00
|
|
|
case AtomOp.And:
|
|
|
|
if (type == AtomSize.S32 || type == AtomSize.U32)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
res = context.AtomicAnd(mr, addrLow, addrHigh, value);
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-06 02:02:28 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid reduction type: {type}.");
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-10-12 21:35:31 +01:00
|
|
|
case AtomOp.Xor:
|
|
|
|
if (type == AtomSize.S32 || type == AtomSize.U32)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
res = context.AtomicXor(mr, addrLow, addrHigh, value);
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-06 02:02:28 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid reduction type: {type}.");
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-10-12 21:35:31 +01:00
|
|
|
case AtomOp.Or:
|
|
|
|
if (type == AtomSize.S32 || type == AtomSize.U32)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
res = context.AtomicOr(mr, addrLow, addrHigh, value);
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-06 02:02:28 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid reduction type: {type}.");
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-10-12 21:35:31 +01:00
|
|
|
case AtomOp.Max:
|
|
|
|
if (type == AtomSize.S32)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
res = context.AtomicMaxS32(mr, addrLow, addrHigh, value);
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
2021-10-12 21:35:31 +01:00
|
|
|
else if (type == AtomSize.U32)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
res = context.AtomicMaxU32(mr, addrLow, addrHigh, value);
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-06 02:02:28 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid reduction type: {type}.");
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-10-12 21:35:31 +01:00
|
|
|
case AtomOp.Min:
|
|
|
|
if (type == AtomSize.S32)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
res = context.AtomicMinS32(mr, addrLow, addrHigh, value);
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
2021-10-12 21:35:31 +01:00
|
|
|
else if (type == AtomSize.U32)
|
2019-11-08 20:29:41 +00:00
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
res = context.AtomicMinU32(mr, addrLow, addrHigh, value);
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-06 02:02:28 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid reduction type: {type}.");
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
private static void EmitLoad(
|
|
|
|
EmitterContext context,
|
|
|
|
MemoryRegion region,
|
|
|
|
LsSize2 size,
|
|
|
|
Operand srcA,
|
|
|
|
int rd,
|
|
|
|
int offset)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
if (size > LsSize2.B128)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid load size: {size}.");
|
|
|
|
return;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
bool isSmallInt = size < LsSize2.B32;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
int count = 1;
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
switch (size)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case LsSize2.B64: count = 2; break;
|
|
|
|
case LsSize2.B128: count = 4; break;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand baseOffset = context.IAdd(srcA, Const(offset));
|
|
|
|
Operand wordOffset = context.ShiftRightU32(baseOffset, Const(2)); // Word offset = byte offset / 4 (one word = 4 bytes).
|
2019-10-13 07:02:07 +01:00
|
|
|
Operand bitOffset = GetBitOffset(context, baseOffset);
|
|
|
|
|
|
|
|
for (int index = 0; index < count; index++)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
Register dest = new Register(rd + index, RegisterType.Gpr);
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
if (dest.IsRZ)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand elemOffset = context.IAdd(wordOffset, Const(index));
|
2019-11-08 20:29:41 +00:00
|
|
|
Operand value = null;
|
|
|
|
|
|
|
|
switch (region)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case MemoryRegion.Local: value = context.LoadLocal(elemOffset); break;
|
|
|
|
case MemoryRegion.Shared: value = context.LoadShared(elemOffset); break;
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
if (isSmallInt)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
value = ExtractSmallInt(context, (LsSize)size, bitOffset, value);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
context.Copy(Register(dest), value);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
private static void EmitLdg(
|
|
|
|
EmitterContext context,
|
|
|
|
LsSize size,
|
|
|
|
int ra,
|
|
|
|
int rd,
|
|
|
|
int offset,
|
|
|
|
bool extended)
|
2019-12-01 02:53:09 +00:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
bool isSmallInt = size < LsSize.B32;
|
2019-12-01 02:53:09 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
int count = GetVectorCount(size);
|
2019-12-01 02:53:09 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
(Operand addrLow, Operand addrHigh) = Get40BitsAddress(context, new Register(ra, RegisterType.Gpr), extended, offset);
|
2019-12-01 02:53:09 +00:00
|
|
|
|
|
|
|
Operand bitOffset = GetBitOffset(context, addrLow);
|
|
|
|
|
|
|
|
for (int index = 0; index < count; index++)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
Register dest = new Register(rd + index, RegisterType.Gpr);
|
2019-12-01 02:53:09 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
if (dest.IsRZ)
|
2019-12-01 02:53:09 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand value = context.LoadGlobal(context.IAdd(addrLow, Const(index * 4)), addrHigh);
|
|
|
|
|
|
|
|
if (isSmallInt)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
value = ExtractSmallInt(context, size, bitOffset, value);
|
2019-12-01 02:53:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
context.Copy(Register(dest), value);
|
2019-12-01 02:53:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
private static void EmitStore(
|
|
|
|
EmitterContext context,
|
|
|
|
MemoryRegion region,
|
|
|
|
LsSize2 size,
|
|
|
|
Operand srcA,
|
|
|
|
int rd,
|
|
|
|
int offset)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
if (size > LsSize2.B128)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
context.Config.GpuAccessor.Log($"Invalid store size: {size}.");
|
|
|
|
return;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
bool isSmallInt = size < LsSize2.B32;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
int count = 1;
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
switch (size)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case LsSize2.B64: count = 2; break;
|
|
|
|
case LsSize2.B128: count = 4; break;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand baseOffset = context.IAdd(srcA, Const(offset));
|
2019-10-13 07:02:07 +01:00
|
|
|
Operand wordOffset = context.ShiftRightU32(baseOffset, Const(2));
|
|
|
|
Operand bitOffset = GetBitOffset(context, baseOffset);
|
|
|
|
|
|
|
|
for (int index = 0; index < count; index++)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
bool isRz = rd + index >= RegisterConsts.RegisterZeroIndex;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand value = Register(isRz ? rd : rd + index, RegisterType.Gpr);
|
|
|
|
Operand elemOffset = context.IAdd(wordOffset, Const(index));
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
if (isSmallInt)
|
|
|
|
{
|
2019-11-08 20:29:41 +00:00
|
|
|
Operand word = null;
|
|
|
|
|
|
|
|
switch (region)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case MemoryRegion.Local: word = context.LoadLocal(elemOffset); break;
|
|
|
|
case MemoryRegion.Shared: word = context.LoadShared(elemOffset); break;
|
2019-11-08 20:29:41 +00:00
|
|
|
}
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
value = InsertSmallInt(context, (LsSize)size, bitOffset, word, value);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 20:29:41 +00:00
|
|
|
switch (region)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case MemoryRegion.Local: context.StoreLocal(elemOffset, value); break;
|
|
|
|
case MemoryRegion.Shared: context.StoreShared(elemOffset, value); break;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
private static void EmitStg(
|
|
|
|
EmitterContext context,
|
|
|
|
LsSize2 size,
|
|
|
|
int ra,
|
|
|
|
int rd,
|
|
|
|
int offset,
|
|
|
|
bool extended)
|
2019-12-01 02:53:09 +00:00
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
if (size > LsSize2.B128)
|
|
|
|
{
|
|
|
|
context.Config.GpuAccessor.Log($"Invalid store size: {size}.");
|
|
|
|
return;
|
|
|
|
}
|
2019-12-01 02:53:09 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
bool isSmallInt = size < LsSize2.B32;
|
2019-12-01 02:53:09 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
int count = GetVectorCount((LsSize)size);
|
2019-12-01 02:53:09 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
(Operand addrLow, Operand addrHigh) = Get40BitsAddress(context, new Register(ra, RegisterType.Gpr), extended, offset);
|
2019-12-01 02:53:09 +00:00
|
|
|
|
|
|
|
Operand bitOffset = GetBitOffset(context, addrLow);
|
|
|
|
|
|
|
|
for (int index = 0; index < count; index++)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
bool isRz = rd + index >= RegisterConsts.RegisterZeroIndex;
|
2019-12-01 02:53:09 +00:00
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand value = Register(isRz ? rd : rd + index, RegisterType.Gpr);
|
2019-12-01 02:53:09 +00:00
|
|
|
|
|
|
|
if (isSmallInt)
|
|
|
|
{
|
|
|
|
Operand word = context.LoadGlobal(addrLow, addrHigh);
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
value = InsertSmallInt(context, (LsSize)size, bitOffset, word, value);
|
2019-12-01 02:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
context.StoreGlobal(context.IAdd(addrLow, Const(index * 4)), addrHigh, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
private static int GetVectorCount(LsSize size)
|
2019-12-01 02:53:09 +00:00
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case LsSize.B64:
|
2019-12-01 02:53:09 +00:00
|
|
|
return 2;
|
2021-10-12 21:35:31 +01:00
|
|
|
case LsSize.B128:
|
|
|
|
case LsSize.UB128:
|
2019-12-01 02:53:09 +00:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static (Operand, Operand) Get40BitsAddress(
|
|
|
|
EmitterContext context,
|
|
|
|
Register ra,
|
|
|
|
bool extended,
|
|
|
|
int offset)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
Operand addrLow = Register(ra);
|
2019-12-01 02:53:09 +00:00
|
|
|
Operand addrHigh;
|
|
|
|
|
|
|
|
if (extended && !ra.IsRZ)
|
|
|
|
{
|
|
|
|
addrHigh = Register(ra.Index + 1, RegisterType.Gpr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addrHigh = Const(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand offs = Const(offset);
|
|
|
|
|
|
|
|
addrLow = context.IAdd(addrLow, offs);
|
|
|
|
|
|
|
|
if (extended)
|
|
|
|
{
|
|
|
|
Operand carry = context.ICompareLessUnsigned(addrLow, offs);
|
|
|
|
|
|
|
|
addrHigh = context.IAdd(addrHigh, context.ConditionalSelect(carry, Const(1), Const(0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (addrLow, addrHigh);
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
private static Operand GetBitOffset(EmitterContext context, Operand baseOffset)
|
|
|
|
{
|
2019-12-01 02:53:09 +00:00
|
|
|
// Note: bit offset = (baseOffset & 0b11) * 8.
|
2019-10-13 07:02:07 +01:00
|
|
|
// Addresses should be always aligned to the integer type,
|
|
|
|
// so we don't need to take unaligned addresses into account.
|
|
|
|
return context.ShiftLeft(context.BitwiseAnd(baseOffset, Const(3)), Const(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Operand ExtractSmallInt(
|
|
|
|
EmitterContext context,
|
2021-10-12 21:35:31 +01:00
|
|
|
LsSize size,
|
|
|
|
Operand bitOffset,
|
|
|
|
Operand value)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
|
|
|
value = context.ShiftRightU32(value, bitOffset);
|
|
|
|
|
|
|
|
switch (size)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case LsSize.U8: value = ZeroExtendTo32(context, value, 8); break;
|
|
|
|
case LsSize.U16: value = ZeroExtendTo32(context, value, 16); break;
|
|
|
|
case LsSize.S8: value = SignExtendTo32(context, value, 8); break;
|
|
|
|
case LsSize.S16: value = SignExtendTo32(context, value, 16); break;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Operand InsertSmallInt(
|
|
|
|
EmitterContext context,
|
2021-10-12 21:35:31 +01:00
|
|
|
LsSize size,
|
|
|
|
Operand bitOffset,
|
|
|
|
Operand word,
|
|
|
|
Operand value)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
2021-10-12 21:35:31 +01:00
|
|
|
case LsSize.U8:
|
|
|
|
case LsSize.S8:
|
2019-10-13 07:02:07 +01:00
|
|
|
value = context.BitwiseAnd(value, Const(0xff));
|
|
|
|
value = context.BitfieldInsert(word, value, bitOffset, Const(8));
|
|
|
|
break;
|
|
|
|
|
2021-10-12 21:35:31 +01:00
|
|
|
case LsSize.U16:
|
|
|
|
case LsSize.S16:
|
2019-10-13 07:02:07 +01:00
|
|
|
value = context.BitwiseAnd(value, Const(0xffff));
|
|
|
|
value = context.BitfieldInsert(word, value, bitOffset, Const(16));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|