glsl: Implement IADD CC

This commit is contained in:
ameerj 2021-05-28 15:51:50 -04:00
parent 453cd25da5
commit 21797efa54
2 changed files with 17 additions and 2 deletions

View File

@ -183,6 +183,8 @@ std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info, IR
for (size_t index = 0; index < ctx.reg_alloc.num_used_registers; ++index) { for (size_t index = 0; index < ctx.reg_alloc.num_used_registers; ++index) {
ctx.header += fmt::format("{} R{};", ctx.reg_alloc.reg_types[index], index); ctx.header += fmt::format("{} R{};", ctx.reg_alloc.reg_types[index], index);
} }
// TODO: track CC usage
ctx.header += "uint carry;";
ctx.code.insert(0, ctx.header); ctx.code.insert(0, ctx.header);
ctx.code += "}"; ctx.code += "}";
fmt::print("\n{}\n", ctx.code); fmt::print("\n{}\n", ctx.code);

View File

@ -29,9 +29,22 @@ void SetSignFlag(EmitContext& ctx, IR::Inst& inst, std::string_view result) {
} // Anonymous namespace } // Anonymous namespace
void EmitIAdd32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) { void EmitIAdd32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
const auto result{ctx.reg_alloc.Define(inst, Type::U32)}; const auto result{ctx.reg_alloc.Define(inst, Type::U32)};
if (IR::Inst* const carry{inst.GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp)}) {
ctx.Add("{}=uaddCarry({},{},carry);", result, a, b);
ctx.AddU1("{}=carry!=0;", *carry, result);
carry->Invalidate();
} else {
ctx.Add("{}={}+{};", result, a, b); ctx.Add("{}={}+{};", result, a, b);
}
SetZeroFlag(ctx, inst, result); SetZeroFlag(ctx, inst, result);
SetSignFlag(ctx, inst, result); SetSignFlag(ctx, inst, result);
if (IR::Inst * overflow{inst.GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp)}) {
// https://stackoverflow.com/questions/55468823/how-to-detect-integer-overflow-in-c
constexpr u32 s32_max{static_cast<u32>(std::numeric_limits<s32>::max())};
ctx.AddU1("{}=int({})>=0?int({})>int({}-{}):int({})<int({}-{});", *overflow, a, b, s32_max,
a, b, s32_max, a);
overflow->Invalidate();
}
} }
void EmitIAdd64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) { void EmitIAdd64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
@ -179,7 +192,7 @@ void EmitSLessThan(EmitContext& ctx, IR::Inst& inst, std::string_view lhs, std::
} }
void EmitULessThan(EmitContext& ctx, IR::Inst& inst, std::string_view lhs, std::string_view rhs) { void EmitULessThan(EmitContext& ctx, IR::Inst& inst, std::string_view lhs, std::string_view rhs) {
ctx.AddU1("{}=uint({})<uint({)};", inst, lhs, rhs); ctx.AddU1("{}=uint({})<uint({});", inst, lhs, rhs);
} }
void EmitIEqual(EmitContext& ctx, IR::Inst& inst, std::string_view lhs, std::string_view rhs) { void EmitIEqual(EmitContext& ctx, IR::Inst& inst, std::string_view lhs, std::string_view rhs) {