2016-07-01 15:01:06 +02:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2016-12-31 12:17:47 +01:00
|
|
|
#include <dynarmic/coprocessor.h>
|
|
|
|
|
2016-09-01 01:06:40 +02:00
|
|
|
#include "backend_x64/abi.h"
|
2016-12-05 05:14:58 +01:00
|
|
|
#include "backend_x64/block_of_code.h"
|
2016-07-01 15:01:06 +02:00
|
|
|
#include "backend_x64/emit_x64.h"
|
2017-02-16 19:18:29 +01:00
|
|
|
#include "common/address_range.h"
|
2016-12-05 05:11:34 +01:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/bit_util.h"
|
2017-02-16 19:18:29 +01:00
|
|
|
#include "common/common_types.h"
|
2017-02-16 20:40:51 +01:00
|
|
|
#include "common/variant_util.h"
|
2016-09-03 22:48:03 +02:00
|
|
|
#include "frontend/ir/basic_block.h"
|
|
|
|
#include "frontend/ir/microinstruction.h"
|
2016-12-05 05:11:34 +01:00
|
|
|
#include "frontend/ir/opcodes.h"
|
2016-07-01 15:01:06 +02:00
|
|
|
|
|
|
|
// TODO: Have ARM flags in host flags and not have them use up GPR registers unless necessary.
|
|
|
|
// TODO: Actually implement that proper instruction selector you've always wanted to sweetheart.
|
|
|
|
|
|
|
|
namespace Dynarmic {
|
|
|
|
namespace BackendX64 {
|
|
|
|
|
2017-12-09 16:42:47 +01:00
|
|
|
using namespace Xbyak::util;
|
|
|
|
|
2017-03-18 18:20:21 +01:00
|
|
|
constexpr u64 f32_negative_zero = 0x80000000u;
|
|
|
|
constexpr u64 f32_nan = 0x7fc00000u;
|
|
|
|
constexpr u64 f32_non_sign_mask = 0x7fffffffu;
|
|
|
|
|
|
|
|
constexpr u64 f64_negative_zero = 0x8000000000000000u;
|
|
|
|
constexpr u64 f64_nan = 0x7ff8000000000000u;
|
|
|
|
constexpr u64 f64_non_sign_mask = 0x7fffffffffffffffu;
|
|
|
|
|
|
|
|
constexpr u64 f64_penultimate_positive_denormal = 0x000ffffffffffffeu;
|
|
|
|
constexpr u64 f64_min_s32 = 0xc1e0000000000000u; // -2147483648 as a double
|
|
|
|
constexpr u64 f64_max_s32 = 0x41dfffffffc00000u; // 2147483647 as a double
|
|
|
|
constexpr u64 f64_min_u32 = 0x0000000000000000u; // 0 as a double
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitContext::EmitContext(RegAlloc& reg_alloc, IR::Block& block)
|
|
|
|
: reg_alloc(reg_alloc), block(block) {}
|
|
|
|
|
|
|
|
void EmitContext::EraseInstruction(IR::Inst* inst) {
|
2016-08-26 21:38:59 +02:00
|
|
|
block.Instructions().erase(inst);
|
2016-12-22 19:43:11 +01:00
|
|
|
inst->Invalidate();
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
EmitX64<JST>::EmitX64(BlockOfCode* code, UserCallbacks cb, Jit* jit_interface)
|
2017-02-04 10:23:19 +01:00
|
|
|
: code(code), cb(cb), jit_interface(jit_interface) {
|
2016-12-05 05:22:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
EmitX64<JST>::~EmitX64() {}
|
2016-07-14 13:52:53 +02:00
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
boost::optional<typename EmitX64<JST>::BlockDescriptor> EmitX64<JST>::GetBasicBlock(IR::LocationDescriptor descriptor) const {
|
2018-01-01 23:34:05 +01:00
|
|
|
auto iter = block_descriptors.find(descriptor);
|
2016-12-19 16:01:49 +01:00
|
|
|
if (iter == block_descriptors.end())
|
2016-12-05 05:22:56 +01:00
|
|
|
return boost::none;
|
2017-11-28 21:56:49 +01:00
|
|
|
return iter->second;
|
2016-12-05 05:22:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitVoid(EmitContext&, IR::Inst*) {
|
2017-02-18 22:46:36 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitBreakpoint(EmitContext&, IR::Inst*) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->int3();
|
2016-08-05 15:07:27 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitIdentity(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
if (!args[0].IsImmediate()) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-08-05 15:11:27 +02:00
|
|
|
}
|
2016-08-02 12:51:05 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::PushRSBHelper(Xbyak::Reg64 loc_desc_reg, Xbyak::Reg64 index_reg, IR::LocationDescriptor target) {
|
2016-08-24 21:07:08 +02:00
|
|
|
using namespace Xbyak::util;
|
|
|
|
|
2018-01-01 23:34:05 +01:00
|
|
|
auto iter = block_descriptors.find(target);
|
2016-12-19 16:01:49 +01:00
|
|
|
CodePtr target_code_ptr = iter != block_descriptors.end()
|
2017-02-16 19:18:29 +01:00
|
|
|
? iter->second.entrypoint
|
2016-12-19 16:01:49 +01:00
|
|
|
: code->GetReturnFromRunCodeAddress();
|
2016-08-13 01:10:23 +02:00
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
code->mov(index_reg.cvt32(), dword[r15 + offsetof(JST, rsb_ptr)]);
|
2016-08-15 16:48:22 +02:00
|
|
|
|
2018-01-01 23:34:05 +01:00
|
|
|
code->mov(loc_desc_reg, target.Value());
|
2016-12-19 16:01:49 +01:00
|
|
|
|
2018-01-01 23:34:05 +01:00
|
|
|
patch_information[target].mov_rcx.emplace_back(code->getCurr());
|
2016-12-19 16:01:49 +01:00
|
|
|
EmitPatchMovRcx(target_code_ptr);
|
2016-08-15 16:48:22 +02:00
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
code->mov(qword[r15 + index_reg * 8 + offsetof(JST, rsb_location_descriptors)], loc_desc_reg);
|
|
|
|
code->mov(qword[r15 + index_reg * 8 + offsetof(JST, rsb_codeptrs)], rcx);
|
2016-08-15 16:48:22 +02:00
|
|
|
|
2017-11-27 21:29:19 +01:00
|
|
|
code->add(index_reg.cvt32(), 1);
|
2018-01-02 18:45:39 +01:00
|
|
|
code->and_(index_reg.cvt32(), u32(JST::RSBPtrMask));
|
|
|
|
code->mov(dword[r15 + offsetof(JST, rsb_ptr)], index_reg.cvt32());
|
2017-11-27 21:29:19 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPushRSB(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-11-27 21:29:19 +01:00
|
|
|
ASSERT(args[0].IsImmediate());
|
|
|
|
u64 unique_hash_of_target = args[0].GetImmediateU64();
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RCX});
|
|
|
|
Xbyak::Reg64 loc_desc_reg = ctx.reg_alloc.ScratchGpr();
|
|
|
|
Xbyak::Reg64 index_reg = ctx.reg_alloc.ScratchGpr();
|
2016-08-15 16:48:22 +02:00
|
|
|
|
2018-01-01 23:34:05 +01:00
|
|
|
PushRSBHelper(loc_desc_reg, index_reg, IR::LocationDescriptor{unique_hash_of_target});
|
2016-08-13 01:10:23 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitGetCarryFromOp(EmitContext&, IR::Inst*) {
|
2016-08-26 17:43:51 +02:00
|
|
|
ASSERT_MSG(false, "should never happen");
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitGetOverflowFromOp(EmitContext&, IR::Inst*) {
|
2016-08-26 17:43:51 +02:00
|
|
|
ASSERT_MSG(false, "should never happen");
|
2016-07-08 11:09:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitGetGEFromOp(EmitContext&, IR::Inst*) {
|
2016-12-04 21:52:06 +01:00
|
|
|
ASSERT_MSG(false, "should never happen");
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPack2x32To1x64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 lo = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
Xbyak::Reg64 hi = ctx.reg_alloc.UseScratchGpr(args[1]);
|
2016-08-05 16:27:29 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->shl(hi, 32);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->mov(lo.cvt32(), lo.cvt32()); // Zero extend to 64-bits
|
|
|
|
code->or_(lo, hi);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, lo);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitLeastSignificantWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitMostSignificantWord(EmitContext& ctx, IR::Inst* inst) {
|
2017-11-27 20:36:30 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->shr(result, 32);
|
2016-08-06 22:03:57 +02:00
|
|
|
|
|
|
|
if (carry_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
Xbyak::Reg64 carry = ctx.reg_alloc.ScratchGpr();
|
2016-08-24 21:07:08 +02:00
|
|
|
code->setc(carry.cvt8());
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-08-06 22:03:57 +02:00
|
|
|
}
|
2017-11-27 20:36:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitLeastSignificantHalf(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-07-12 00:06:35 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitLeastSignificantByte(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitMostSignificantBit(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2016-07-04 11:22:11 +02:00
|
|
|
// TODO: Flag optimization
|
2016-08-24 21:07:08 +02:00
|
|
|
code->shr(result, 31);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitIsZero(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2016-07-04 11:22:11 +02:00
|
|
|
// TODO: Flag optimization
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(result, result);
|
|
|
|
code->sete(result.cvt8());
|
|
|
|
code->movzx(result, result.cvt8());
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitIsZero64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2016-08-04 23:04:42 +02:00
|
|
|
// TODO: Flag optimization
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(result, result);
|
|
|
|
code->sete(result.cvt8());
|
|
|
|
code->movzx(result, result.cvt8());
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitLogicalShiftLeft(EmitContext& ctx, IR::Inst* inst) {
|
2016-08-25 22:08:47 +02:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
auto& carry_arg = args[2];
|
|
|
|
|
2016-07-01 15:01:06 +02:00
|
|
|
// TODO: Consider using BMI2 instructions like SHLX when arm-in-host flags is implemented.
|
|
|
|
|
|
|
|
if (!carry_inst) {
|
2016-07-23 00:55:00 +02:00
|
|
|
if (shift_arg.IsImmediate()) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift <= 31) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->shl(result, shift);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->xor_(result, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 zero = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
// The 32-bit x64 SHL instruction masks the shift count by 0x1F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count, so shifts above 31 result in zeros.
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->shl(result, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->xor_(zero, zero);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cmp(code->cl, 32);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cmovnb(result, zero);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(carry_inst);
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift_arg.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
// There is nothing more to do.
|
|
|
|
} else if (shift < 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bt(carry.cvt32(), 0);
|
|
|
|
code->shl(result, shift);
|
|
|
|
code->setc(carry.cvt8());
|
2016-07-23 00:55:00 +02:00
|
|
|
} else if (shift > 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->xor_(result, result);
|
|
|
|
code->xor_(carry, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->mov(carry, result);
|
|
|
|
code->xor_(result, result);
|
|
|
|
code->and_(carry, 1);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
// TODO: Optimize this.
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->inLocalLabel();
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cmp(code->cl, 32);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->ja(".Rs_gt32");
|
|
|
|
code->je(".Rs_eq32");
|
2016-07-23 00:55:00 +02:00
|
|
|
// if (Rs & 0xFF < 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bt(carry.cvt32(), 0); // Set the carry flag for correct behaviour in the case when Rs & 0xFF == 0
|
2017-02-24 22:25:31 +01:00
|
|
|
code->shl(result, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->setc(carry.cvt8());
|
|
|
|
code->jmp(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
// } else if (Rs & 0xFF > 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".Rs_gt32");
|
|
|
|
code->xor_(result, result);
|
|
|
|
code->xor_(carry, carry);
|
|
|
|
code->jmp(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
// } else if (Rs & 0xFF == 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".Rs_eq32");
|
|
|
|
code->mov(carry, result);
|
|
|
|
code->and_(carry, 1);
|
|
|
|
code->xor_(result, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
// }
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".end");
|
|
|
|
|
|
|
|
code->outLocalLabel();
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitLogicalShiftRight(EmitContext& ctx, IR::Inst* inst) {
|
2016-08-25 22:08:47 +02:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
auto& carry_arg = args[2];
|
|
|
|
|
2016-07-01 15:01:06 +02:00
|
|
|
if (!carry_inst) {
|
2016-07-23 00:55:00 +02:00
|
|
|
if (shift_arg.IsImmediate()) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift <= 31) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->shr(result, shift);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->xor_(result, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 zero = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
// The 32-bit x64 SHR instruction masks the shift count by 0x1F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count, so shifts above 31 result in zeros.
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->shr(result, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->xor_(zero, zero);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cmp(code->cl, 32);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cmovnb(result, zero);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(carry_inst);
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift_arg.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
// There is nothing more to do.
|
|
|
|
} else if (shift < 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->shr(result, shift);
|
|
|
|
code->setc(carry.cvt8());
|
2016-07-23 00:55:00 +02:00
|
|
|
} else if (shift == 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bt(result, 31);
|
|
|
|
code->setc(carry.cvt8());
|
|
|
|
code->mov(result, 0);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->xor_(result, result);
|
|
|
|
code->xor_(carry, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
// TODO: Optimize this.
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->inLocalLabel();
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cmp(code->cl, 32);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->ja(".Rs_gt32");
|
|
|
|
code->je(".Rs_eq32");
|
2016-07-23 00:55:00 +02:00
|
|
|
// if (Rs & 0xFF == 0) goto end;
|
2017-02-24 22:25:31 +01:00
|
|
|
code->test(code->cl, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->jz(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
// if (Rs & 0xFF < 32) {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->shr(result, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->setc(carry.cvt8());
|
|
|
|
code->jmp(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
// } else if (Rs & 0xFF > 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".Rs_gt32");
|
|
|
|
code->xor_(result, result);
|
|
|
|
code->xor_(carry, carry);
|
|
|
|
code->jmp(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
// } else if (Rs & 0xFF == 32) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".Rs_eq32");
|
|
|
|
code->bt(result, 31);
|
|
|
|
code->setc(carry.cvt8());
|
|
|
|
code->xor_(result, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
// }
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".end");
|
|
|
|
|
|
|
|
code->outLocalLabel();
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitLogicalShiftRight64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
2016-08-07 15:23:33 +02:00
|
|
|
|
|
|
|
ASSERT_MSG(shift_arg.IsImmediate(), "variable 64 bit shifts are not implemented");
|
2017-02-24 22:25:31 +01:00
|
|
|
ASSERT_MSG(shift_arg.GetImmediateU8() < 64, "shift width clamping is not implemented");
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2016-08-07 15:23:33 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->shr(result.cvt64(), shift);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-07 15:23:33 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitArithmeticShiftRight(EmitContext& ctx, IR::Inst* inst) {
|
2016-08-25 22:08:47 +02:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
2016-07-04 11:22:11 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
auto& carry_arg = args[2];
|
|
|
|
|
2016-07-04 11:22:11 +02:00
|
|
|
if (!carry_inst) {
|
2016-07-23 00:55:00 +02:00
|
|
|
if (shift_arg.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->sar(result, u8(shift < 31 ? shift : 31));
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.UseScratch(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 const31 = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
// The 32-bit x64 SAR instruction masks the shift count by 0x1F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count.
|
|
|
|
|
|
|
|
// We note that all shift values above 31 have the same behaviour as 31 does, so we saturate `shift` to 31.
|
2016-08-24 21:07:08 +02:00
|
|
|
code->mov(const31, 31);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->movzx(code->ecx, code->cl);
|
|
|
|
code->cmp(code->ecx, u32(31));
|
|
|
|
code->cmovg(code->ecx, const31);
|
|
|
|
code->sar(result, code->cl);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-04 11:22:11 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(carry_inst);
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift_arg.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt8();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
// There is nothing more to do.
|
|
|
|
} else if (shift <= 31) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->sar(result, shift);
|
|
|
|
code->setc(carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->sar(result, 31);
|
|
|
|
code->bt(result, 31);
|
|
|
|
code->setc(carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt8();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
// TODO: Optimize this.
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->inLocalLabel();
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cmp(code->cl, u32(31));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->ja(".Rs_gt31");
|
2016-07-23 00:55:00 +02:00
|
|
|
// if (Rs & 0xFF == 0) goto end;
|
2017-02-24 22:25:31 +01:00
|
|
|
code->test(code->cl, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->jz(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
// if (Rs & 0xFF <= 31) {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->sar(result, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->setc(carry);
|
|
|
|
code->jmp(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
// } else if (Rs & 0xFF > 31) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".Rs_gt31");
|
|
|
|
code->sar(result, 31); // 31 produces the same results as anything above 31
|
|
|
|
code->bt(result, 31);
|
|
|
|
code->setc(carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
// }
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".end");
|
|
|
|
|
|
|
|
code->outLocalLabel();
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-10 02:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitRotateRight(EmitContext& ctx, IR::Inst* inst) {
|
2016-08-25 22:08:47 +02:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
2016-07-10 02:18:17 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
auto& carry_arg = args[2];
|
|
|
|
|
2016-07-10 02:18:17 +02:00
|
|
|
if (!carry_inst) {
|
2016-07-23 00:55:00 +02:00
|
|
|
if (shift_arg.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
2016-07-10 02:18:17 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->ror(result, u8(shift & 0x1F));
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
// x64 ROR instruction does (shift & 0x1F) for us.
|
2017-02-24 22:25:31 +01:00
|
|
|
code->ror(result, code->cl);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-10 02:18:17 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(carry_inst);
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift_arg.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt8();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
// There is nothing more to do.
|
|
|
|
} else if ((shift & 0x1F) == 0) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bt(result, u8(31));
|
|
|
|
code->setc(carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->ror(result, shift);
|
|
|
|
code->setc(carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.UseScratch(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt8();
|
2016-07-23 00:55:00 +02:00
|
|
|
|
|
|
|
// TODO: Optimize
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->inLocalLabel();
|
|
|
|
|
2016-07-23 00:55:00 +02:00
|
|
|
// if (Rs & 0xFF == 0) goto end;
|
2017-02-24 22:25:31 +01:00
|
|
|
code->test(code->cl, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->jz(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->and_(code->ecx, u32(0x1F));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->jz(".zero_1F");
|
2016-07-23 00:55:00 +02:00
|
|
|
// if (Rs & 0x1F != 0) {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->ror(result, code->cl);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->setc(carry);
|
|
|
|
code->jmp(".end");
|
2016-07-23 00:55:00 +02:00
|
|
|
// } else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".zero_1F");
|
|
|
|
code->bt(result, u8(31));
|
|
|
|
code->setc(carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
// }
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(".end");
|
|
|
|
|
|
|
|
code->outLocalLabel();
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-04 11:22:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitRotateRightExtended(EmitContext& ctx, IR::Inst* inst) {
|
2016-08-25 22:08:47 +02:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
2016-07-31 20:07:35 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(args[1]).cvt8();
|
2016-07-31 20:07:35 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bt(carry.cvt32(), 0);
|
|
|
|
code->rcr(result, 1);
|
2016-07-31 20:07:35 +02:00
|
|
|
|
|
|
|
if (carry_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(carry_inst);
|
2016-12-22 19:43:11 +01:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->setc(carry);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-31 20:07:35 +02:00
|
|
|
}
|
2017-11-27 20:36:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-31 20:07:35 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
const Xbyak::Reg64 INVALID_REG = Xbyak::Reg64(-1);
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
static Xbyak::Reg8 DoCarry(RegAlloc& reg_alloc, Argument& carry_in, IR::Inst* carry_out) {
|
2016-07-23 00:55:00 +02:00
|
|
|
if (carry_in.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
return carry_out ? reg_alloc.ScratchGpr().cvt8() : INVALID_REG.cvt8();
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2017-02-24 22:25:31 +01:00
|
|
|
return carry_out ? reg_alloc.UseScratchGpr(carry_in).cvt8() : reg_alloc.UseGpr(carry_in).cvt8();
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitAddWithCarry(EmitContext& ctx, IR::Inst* inst) {
|
2016-08-25 22:08:47 +02:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
auto overflow_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp);
|
2016-07-23 00:55:00 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
auto& carry_in = args[2];
|
2016-07-08 11:09:18 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg8 carry = DoCarry(ctx.reg_alloc, carry_in, carry_inst);
|
|
|
|
Xbyak::Reg8 overflow = overflow_inst ? ctx.reg_alloc.ScratchGpr().cvt8() : INVALID_REG.cvt8();
|
2016-07-08 11:09:18 +02:00
|
|
|
|
|
|
|
// TODO: Consider using LEA.
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
2016-08-24 21:07:08 +02:00
|
|
|
if (carry_in.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
if (carry_in.GetImmediateU1()) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->stc();
|
|
|
|
code->adc(result, op_arg);
|
|
|
|
} else {
|
|
|
|
code->add(result, op_arg);
|
|
|
|
}
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bt(carry.cvt32(), 0);
|
|
|
|
code->adc(result, op_arg);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
2016-08-24 21:07:08 +02:00
|
|
|
op_arg.setBit(32);
|
|
|
|
if (carry_in.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
if (carry_in.GetImmediateU1()) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->stc();
|
|
|
|
code->adc(result, *op_arg);
|
|
|
|
} else {
|
|
|
|
code->add(result, *op_arg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
code->bt(carry.cvt32(), 0);
|
|
|
|
code->adc(result, *op_arg);
|
|
|
|
}
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-08 11:09:18 +02:00
|
|
|
|
|
|
|
if (carry_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(carry_inst);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->setc(carry);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-08 11:09:18 +02:00
|
|
|
}
|
|
|
|
if (overflow_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(overflow_inst);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->seto(overflow);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(overflow_inst, overflow);
|
2016-07-08 11:09:18 +02:00
|
|
|
}
|
2017-11-27 20:36:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-08 11:09:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitAdd64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-08-04 23:04:42 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
Xbyak::Reg64 op_arg = ctx.reg_alloc.UseGpr(args[1]);
|
2016-08-04 23:04:42 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->add(result, op_arg);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitSubWithCarry(EmitContext& ctx, IR::Inst* inst) {
|
2016-08-25 22:08:47 +02:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
auto overflow_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp);
|
2016-07-08 12:49:30 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
auto& carry_in = args[2];
|
2016-07-23 00:55:00 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg8 carry = DoCarry(ctx.reg_alloc, carry_in, carry_inst);
|
|
|
|
Xbyak::Reg8 overflow = overflow_inst ? ctx.reg_alloc.ScratchGpr().cvt8() : INVALID_REG.cvt8();
|
2016-07-08 12:49:30 +02:00
|
|
|
|
|
|
|
// TODO: Consider using LEA.
|
2016-07-23 00:55:00 +02:00
|
|
|
// TODO: Optimize CMP case.
|
2016-07-08 12:49:30 +02:00
|
|
|
// Note that x64 CF is inverse of what the ARM carry flag is here.
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
2016-08-24 21:07:08 +02:00
|
|
|
if (carry_in.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
if (carry_in.GetImmediateU1()) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->sub(result, op_arg);
|
|
|
|
} else {
|
|
|
|
code->stc();
|
|
|
|
code->sbb(result, op_arg);
|
|
|
|
}
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bt(carry.cvt32(), 0);
|
|
|
|
code->cmc();
|
|
|
|
code->sbb(result, op_arg);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
2016-08-24 21:07:08 +02:00
|
|
|
op_arg.setBit(32);
|
|
|
|
if (carry_in.IsImmediate()) {
|
2017-02-24 22:25:31 +01:00
|
|
|
if (carry_in.GetImmediateU1()) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->sub(result, *op_arg);
|
|
|
|
} else {
|
|
|
|
code->stc();
|
|
|
|
code->sbb(result, *op_arg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
code->bt(carry.cvt32(), 0);
|
|
|
|
code->cmc();
|
|
|
|
code->sbb(result, *op_arg);
|
|
|
|
}
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-08 12:49:30 +02:00
|
|
|
|
|
|
|
if (carry_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(carry_inst);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->setnc(carry);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
2016-07-08 12:49:30 +02:00
|
|
|
}
|
|
|
|
if (overflow_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(overflow_inst);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->seto(overflow);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(overflow_inst, overflow);
|
2016-07-08 12:49:30 +02:00
|
|
|
}
|
2017-11-27 20:36:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-08 12:49:30 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitSub64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-08-06 07:09:47 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
Xbyak::Reg64 op_arg = ctx.reg_alloc.UseGpr(args[1]);
|
2016-08-06 07:09:47 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->sub(result, op_arg);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-06 07:09:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitMul(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2017-02-24 22:25:31 +01:00
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
code->imul(result, result, args[1].GetImmediateU32());
|
2016-08-04 23:04:42 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
2016-08-24 21:07:08 +02:00
|
|
|
op_arg.setBit(32);
|
|
|
|
|
|
|
|
code->imul(result, *op_arg);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitMul64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-08-05 16:27:29 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
2016-08-05 16:27:29 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->imul(result, *op_arg);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitAnd(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-07-08 11:43:28 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2016-08-24 21:07:08 +02:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
2016-07-08 11:43:28 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->and_(result, op_arg);
|
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
2016-08-24 21:07:08 +02:00
|
|
|
op_arg.setBit(32);
|
2016-07-08 11:43:28 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->and_(result, *op_arg);
|
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-08 11:43:28 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitEor(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-07-08 12:14:50 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2016-08-24 21:07:08 +02:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
2016-07-08 12:14:50 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->xor_(result, op_arg);
|
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
2016-08-24 21:07:08 +02:00
|
|
|
op_arg.setBit(32);
|
2016-07-08 12:14:50 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->xor_(result, *op_arg);
|
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-08 12:14:50 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitOr(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-07-10 03:06:38 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2016-08-24 21:07:08 +02:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
2016-07-10 03:06:38 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->or_(result, op_arg);
|
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
2016-08-24 21:07:08 +02:00
|
|
|
op_arg.setBit(32);
|
2016-07-10 03:06:38 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
code->or_(result, *op_arg);
|
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-10 03:06:38 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitNot(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-07-10 04:44:45 +02:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
Xbyak::Reg32 result;
|
|
|
|
if (args[0].IsImmediate()) {
|
2018-01-02 00:40:34 +01:00
|
|
|
result = ctx.reg_alloc.ScratchGpr().cvt32();
|
2017-02-24 22:25:31 +01:00
|
|
|
code->mov(result, u32(~args[0].GetImmediateU32()));
|
2016-07-23 00:55:00 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2016-08-24 21:07:08 +02:00
|
|
|
code->not_(result);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-23 00:55:00 +02:00
|
|
|
}
|
2016-07-16 20:23:42 +02:00
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitSignExtendWordToLong(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->movsxd(result.cvt64(), result.cvt32());
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitSignExtendHalfToWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->movsx(result.cvt32(), result.cvt16());
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-16 20:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitSignExtendByteToWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->movsx(result.cvt32(), result.cvt8());
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-16 20:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitZeroExtendWordToLong(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->mov(result.cvt32(), result.cvt32()); // x64 zeros upper 32 bits on a 32-bit move
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-04 23:04:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitZeroExtendHalfToWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->movzx(result.cvt32(), result.cvt16());
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-16 20:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitZeroExtendByteToWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2017-02-24 22:25:31 +01:00
|
|
|
code->movzx(result.cvt32(), result.cvt8());
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-16 20:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitByteReverseWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bswap(result);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-16 20:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitByteReverseHalf(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg16 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt16();
|
2016-08-24 21:07:08 +02:00
|
|
|
code->rol(result, 8);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-16 20:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitByteReverseDual(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2016-08-24 21:07:08 +02:00
|
|
|
code->bswap(result);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-07-20 16:34:17 +02:00
|
|
|
}
|
2016-07-16 20:23:42 +02:00
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitCountLeadingZeros(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-11-02 20:58:40 +01:00
|
|
|
if (code->DoesCpuSupport(Xbyak::util::Cpu::tLZCNT)) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 source = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-12-15 23:33:20 +01:00
|
|
|
|
|
|
|
code->lzcnt(result, source);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-12-15 23:33:20 +01:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 source = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-12-15 23:33:20 +01:00
|
|
|
|
|
|
|
// The result of a bsr of zero is undefined, but zf is set after it.
|
|
|
|
code->bsr(result, source);
|
|
|
|
code->mov(source, 0xFFFFFFFF);
|
|
|
|
code->cmovz(result, source);
|
|
|
|
code->neg(result);
|
|
|
|
code->add(result, 31);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-12-15 23:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitSignedSaturatedAdd(EmitContext& ctx, IR::Inst* inst) {
|
2016-12-15 23:33:20 +01:00
|
|
|
auto overflow_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-15 23:33:20 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 addend = ctx.reg_alloc.UseGpr(args[1]).cvt32();
|
|
|
|
Xbyak::Reg32 overflow = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-12-15 23:33:20 +01:00
|
|
|
|
|
|
|
code->mov(overflow, result);
|
|
|
|
code->shr(overflow, 31);
|
|
|
|
code->add(overflow, 0x7FFFFFFF);
|
|
|
|
// overflow now contains 0x7FFFFFFF if a was positive, or 0x80000000 if a was negative
|
|
|
|
code->add(result, addend);
|
|
|
|
code->cmovo(result, overflow);
|
|
|
|
|
|
|
|
if (overflow_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(overflow_inst);
|
2016-12-15 23:33:20 +01:00
|
|
|
|
|
|
|
code->seto(overflow.cvt8());
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(overflow_inst, overflow);
|
2016-12-15 23:33:20 +01:00
|
|
|
}
|
2017-11-27 20:36:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-12-15 23:33:20 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitSignedSaturatedSub(EmitContext& ctx, IR::Inst* inst) {
|
2016-12-15 23:33:20 +01:00
|
|
|
auto overflow_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-15 23:33:20 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 subend = ctx.reg_alloc.UseGpr(args[1]).cvt32();
|
|
|
|
Xbyak::Reg32 overflow = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-12-15 23:33:20 +01:00
|
|
|
|
|
|
|
code->mov(overflow, result);
|
|
|
|
code->shr(overflow, 31);
|
|
|
|
code->add(overflow, 0x7FFFFFFF);
|
|
|
|
// overflow now contains 0x7FFFFFFF if a was positive, or 0x80000000 if a was negative
|
|
|
|
code->sub(result, subend);
|
|
|
|
code->cmovo(result, overflow);
|
|
|
|
|
|
|
|
if (overflow_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(overflow_inst);
|
2016-12-15 23:33:20 +01:00
|
|
|
|
|
|
|
code->seto(overflow.cvt8());
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(overflow_inst, overflow);
|
2016-12-15 23:33:20 +01:00
|
|
|
}
|
2017-11-27 20:36:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-12-15 23:33:20 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitUnsignedSaturation(EmitContext& ctx, IR::Inst* inst) {
|
2016-12-21 15:16:48 +01:00
|
|
|
auto overflow_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
size_t N = args[1].GetImmediateU8();
|
2016-12-21 15:16:48 +01:00
|
|
|
ASSERT(N <= 31);
|
|
|
|
|
|
|
|
u32 saturated_value = (1u << N) - 1;
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
Xbyak::Reg32 reg_a = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 overflow = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-12-21 15:16:48 +01:00
|
|
|
|
|
|
|
// Pseudocode: result = clamp(reg_a, 0, saturated_value);
|
|
|
|
code->xor_(overflow, overflow);
|
|
|
|
code->cmp(reg_a, saturated_value);
|
|
|
|
code->mov(result, saturated_value);
|
|
|
|
code->cmovle(result, overflow);
|
|
|
|
code->cmovbe(result, reg_a);
|
|
|
|
|
|
|
|
if (overflow_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(overflow_inst);
|
2016-12-21 15:16:48 +01:00
|
|
|
|
|
|
|
code->seta(overflow.cvt8());
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(overflow_inst, overflow);
|
2016-12-21 15:16:48 +01:00
|
|
|
}
|
2017-11-27 20:36:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-12-21 15:16:48 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitSignedSaturation(EmitContext& ctx, IR::Inst* inst) {
|
2016-12-21 15:16:48 +01:00
|
|
|
auto overflow_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
size_t N = args[1].GetImmediateU8();
|
2016-12-21 15:16:48 +01:00
|
|
|
ASSERT(N >= 1 && N <= 32);
|
|
|
|
|
|
|
|
if (N == 32) {
|
|
|
|
if (overflow_inst) {
|
|
|
|
auto no_overflow = IR::Value(false);
|
|
|
|
overflow_inst->ReplaceUsesWith(no_overflow);
|
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-12-21 15:16:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 mask = (1u << N) - 1;
|
|
|
|
u32 positive_saturated_value = (1u << (N - 1)) - 1;
|
|
|
|
u32 negative_saturated_value = 1u << (N - 1);
|
|
|
|
u32 sext_negative_satured_value = Common::SignExtend(N, negative_saturated_value);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
Xbyak::Reg32 reg_a = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 overflow = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
Xbyak::Reg32 tmp = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-12-21 15:16:48 +01:00
|
|
|
|
|
|
|
// overflow now contains a value between 0 and mask if it was originally between {negative,positive}_saturated_value.
|
|
|
|
code->lea(overflow, code->ptr[reg_a.cvt64() + negative_saturated_value]);
|
|
|
|
|
|
|
|
// Put the appropriate saturated value in result
|
|
|
|
code->cmp(reg_a, positive_saturated_value);
|
|
|
|
code->mov(tmp, positive_saturated_value);
|
|
|
|
code->mov(result, sext_negative_satured_value);
|
|
|
|
code->cmovg(result, tmp);
|
|
|
|
|
|
|
|
// Do the saturation
|
|
|
|
code->cmp(overflow, mask);
|
|
|
|
code->cmovbe(result, reg_a);
|
|
|
|
|
|
|
|
if (overflow_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(overflow_inst);
|
2016-12-21 15:16:48 +01:00
|
|
|
|
|
|
|
code->seta(overflow.cvt8());
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(overflow_inst, overflow);
|
2016-12-21 15:16:48 +01:00
|
|
|
}
|
2017-11-27 20:36:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-12-21 15:16:48 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedAddU8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-04 21:52:33 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-08-12 17:53:16 +02:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->paddb(xmm_a, xmm_b);
|
2016-08-12 17:53:16 +02:00
|
|
|
|
2016-12-04 21:52:33 +01:00
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2016-08-12 17:53:16 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Xmm ones = ctx.reg_alloc.ScratchXmm();
|
2017-11-25 20:34:30 +01:00
|
|
|
|
|
|
|
code->pcmpeqb(ones, ones);
|
2016-08-12 17:53:16 +02:00
|
|
|
|
2017-11-25 20:34:30 +01:00
|
|
|
code->movdqa(xmm_ge, xmm_a);
|
|
|
|
code->pminub(xmm_ge, xmm_b);
|
|
|
|
code->pcmpeqb(xmm_ge, xmm_b);
|
|
|
|
code->pxor(xmm_ge, ones);
|
2016-08-12 17:53:16 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedAddS8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm saturated_sum = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pxor(xmm_ge, xmm_ge);
|
2016-12-18 17:25:41 +01:00
|
|
|
code->movdqa(saturated_sum, xmm_a);
|
|
|
|
code->paddsb(saturated_sum, xmm_b);
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pcmpgtb(xmm_ge, saturated_sum);
|
|
|
|
code->pcmpeqb(saturated_sum, saturated_sum);
|
|
|
|
code->pxor(xmm_ge, saturated_sum);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2016-12-18 17:25:41 +01:00
|
|
|
code->paddb(xmm_a, xmm_b);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedAddU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->paddw(xmm_a, xmm_b);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
2017-11-20 00:14:56 +01:00
|
|
|
if (code->DoesCpuSupport(Xbyak::util::Cpu::tSSE41)) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Xmm ones = ctx.reg_alloc.ScratchXmm();
|
2017-11-25 20:34:30 +01:00
|
|
|
|
|
|
|
code->pcmpeqb(ones, ones);
|
|
|
|
|
|
|
|
code->movdqa(xmm_ge, xmm_a);
|
|
|
|
code->pminuw(xmm_ge, xmm_b);
|
|
|
|
code->pcmpeqw(xmm_ge, xmm_b);
|
|
|
|
code->pxor(xmm_ge, ones);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2017-11-20 00:14:56 +01:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm tmp_a = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Xmm tmp_b = ctx.reg_alloc.ScratchXmm();
|
2017-11-25 20:34:30 +01:00
|
|
|
|
|
|
|
// !(b <= a+b) == b > a+b
|
|
|
|
code->movdqa(tmp_a, xmm_a);
|
2017-11-20 00:14:56 +01:00
|
|
|
code->movdqa(tmp_b, xmm_b);
|
2017-11-25 20:34:30 +01:00
|
|
|
code->paddw(tmp_a, code->MConst(0x80008000));
|
2017-11-20 00:14:56 +01:00
|
|
|
code->paddw(tmp_b, code->MConst(0x80008000));
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pcmpgtw(tmp_b, tmp_a); // *Signed* comparison!
|
2016-12-18 17:25:41 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, tmp_b);
|
2017-11-25 20:34:30 +01:00
|
|
|
}
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedAddS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm saturated_sum = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pxor(xmm_ge, xmm_ge);
|
2016-12-18 17:25:41 +01:00
|
|
|
code->movdqa(saturated_sum, xmm_a);
|
|
|
|
code->paddsw(saturated_sum, xmm_b);
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pcmpgtw(xmm_ge, saturated_sum);
|
|
|
|
code->pcmpeqw(saturated_sum, saturated_sum);
|
|
|
|
code->pxor(xmm_ge, saturated_sum);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2016-12-18 17:25:41 +01:00
|
|
|
code->paddw(xmm_a, xmm_b);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-08-12 17:53:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSubU8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-05 01:27:59 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-12-05 01:27:59 +01:00
|
|
|
|
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2016-12-05 01:27:59 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
2016-12-05 01:27:59 +01:00
|
|
|
|
2016-12-05 01:56:56 +01:00
|
|
|
code->movdqa(xmm_ge, xmm_a);
|
2016-12-05 01:27:59 +01:00
|
|
|
code->pmaxub(xmm_ge, xmm_b);
|
|
|
|
code->pcmpeqb(xmm_ge, xmm_a);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2017-11-25 18:34:39 +01:00
|
|
|
code->psubb(xmm_a, xmm_b);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSubS8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2016-12-22 19:43:11 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm saturated_sum = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pxor(xmm_ge, xmm_ge);
|
|
|
|
code->movdqa(saturated_sum, xmm_a);
|
|
|
|
code->psubsb(saturated_sum, xmm_b);
|
|
|
|
code->pcmpgtb(xmm_ge, saturated_sum);
|
|
|
|
code->pcmpeqb(saturated_sum, saturated_sum);
|
|
|
|
code->pxor(xmm_ge, saturated_sum);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2016-12-18 17:25:41 +01:00
|
|
|
code->psubb(xmm_a, xmm_b);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSubU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
2017-11-20 00:14:56 +01:00
|
|
|
if (code->DoesCpuSupport(Xbyak::util::Cpu::tSSE41)) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
2017-11-25 20:34:30 +01:00
|
|
|
|
2017-11-20 00:14:56 +01:00
|
|
|
code->movdqa(xmm_ge, xmm_a);
|
|
|
|
code->pmaxuw(xmm_ge, xmm_b); // Requires SSE 4.1
|
|
|
|
code->pcmpeqw(xmm_ge, xmm_a);
|
2017-11-25 20:34:30 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2017-11-20 00:14:56 +01:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Xmm ones = ctx.reg_alloc.ScratchXmm();
|
2017-11-25 20:34:30 +01:00
|
|
|
|
2017-11-20 00:14:56 +01:00
|
|
|
// (a >= b) == !(b > a)
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pcmpeqb(ones, ones);
|
2017-11-20 00:14:56 +01:00
|
|
|
code->paddw(xmm_a, code->MConst(0x80008000));
|
|
|
|
code->paddw(xmm_b, code->MConst(0x80008000));
|
|
|
|
code->movdqa(xmm_ge, xmm_b);
|
|
|
|
code->pcmpgtw(xmm_ge, xmm_a); // *Signed* comparison!
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pxor(xmm_ge, ones);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2017-11-25 20:34:30 +01:00
|
|
|
}
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2017-11-25 18:34:39 +01:00
|
|
|
code->psubw(xmm_a, xmm_b);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSubS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2016-12-18 17:25:41 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm saturated_diff = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Xmm xmm_ge = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2017-11-25 20:34:30 +01:00
|
|
|
code->pxor(xmm_ge, xmm_ge);
|
|
|
|
code->movdqa(saturated_diff, xmm_a);
|
|
|
|
code->psubsw(saturated_diff, xmm_b);
|
|
|
|
code->pcmpgtw(xmm_ge, saturated_diff);
|
|
|
|
code->pcmpeqw(saturated_diff, saturated_diff);
|
|
|
|
code->pxor(xmm_ge, saturated_diff);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, xmm_ge);
|
2016-12-18 17:25:41 +01:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2016-12-18 17:25:41 +01:00
|
|
|
code->psubw(xmm_a, xmm_b);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-12-05 01:27:59 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingAddU8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-11-25 21:32:22 +01:00
|
|
|
|
2017-12-12 17:11:22 +01:00
|
|
|
if (args[0].IsInXmm() || args[1].IsInXmm()) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseScratchXmm(args[1]);
|
|
|
|
Xbyak::Xmm ones = ctx.reg_alloc.ScratchXmm();
|
2016-11-25 21:32:22 +01:00
|
|
|
|
2017-12-12 17:11:22 +01:00
|
|
|
// Since,
|
|
|
|
// pavg(a, b) == (a + b + 1) >> 1
|
|
|
|
// Therefore,
|
|
|
|
// ~pavg(~a, ~b) == (a + b) >> 1
|
|
|
|
|
|
|
|
code->pcmpeqb(ones, ones);
|
|
|
|
code->pxor(xmm_a, ones);
|
|
|
|
code->pxor(xmm_b, ones);
|
|
|
|
code->pavgb(xmm_a, xmm_b);
|
|
|
|
code->pxor(xmm_a, ones);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2017-12-12 17:11:22 +01:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 reg_a = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 reg_b = ctx.reg_alloc.UseGpr(args[1]).cvt32();
|
|
|
|
Xbyak::Reg32 xor_a_b = ctx.reg_alloc.ScratchGpr().cvt32();
|
2017-12-12 17:11:22 +01:00
|
|
|
Xbyak::Reg32 and_a_b = reg_a;
|
|
|
|
Xbyak::Reg32 result = reg_a;
|
|
|
|
|
|
|
|
// This relies on the equality x+y == ((x&y) << 1) + (x^y).
|
|
|
|
// Note that x^y always contains the LSB of the result.
|
|
|
|
// Since we want to calculate (x+y)/2, we can instead calculate (x&y) + ((x^y)>>1).
|
|
|
|
// We mask by 0x7F to remove the LSB so that it doesn't leak into the field below.
|
|
|
|
|
|
|
|
code->mov(xor_a_b, reg_a);
|
|
|
|
code->and_(and_a_b, reg_b);
|
|
|
|
code->xor_(xor_a_b, reg_b);
|
|
|
|
code->shr(xor_a_b, 1);
|
|
|
|
code->and_(xor_a_b, 0x7F7F7F7F);
|
|
|
|
code->add(result, xor_a_b);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2017-12-12 17:11:22 +01:00
|
|
|
}
|
2016-11-25 21:32:22 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingAddU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-11-26 12:28:20 +01:00
|
|
|
|
2017-12-12 16:57:26 +01:00
|
|
|
if (args[0].IsInXmm() || args[1].IsInXmm()) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
|
|
|
Xbyak::Xmm tmp = ctx.reg_alloc.ScratchXmm();
|
2017-12-12 16:57:26 +01:00
|
|
|
|
|
|
|
code->movdqa(tmp, xmm_a);
|
|
|
|
code->pand(xmm_a, xmm_b);
|
|
|
|
code->pxor(tmp, xmm_b);
|
|
|
|
code->psrlw(tmp, 1);
|
|
|
|
code->paddw(xmm_a, tmp);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2017-12-12 16:57:26 +01:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 reg_a = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 reg_b = ctx.reg_alloc.UseGpr(args[1]).cvt32();
|
|
|
|
Xbyak::Reg32 xor_a_b = ctx.reg_alloc.ScratchGpr().cvt32();
|
2017-12-12 16:57:26 +01:00
|
|
|
Xbyak::Reg32 and_a_b = reg_a;
|
|
|
|
Xbyak::Reg32 result = reg_a;
|
|
|
|
|
|
|
|
// This relies on the equality x+y == ((x&y) << 1) + (x^y).
|
|
|
|
// Note that x^y always contains the LSB of the result.
|
|
|
|
// Since we want to calculate (x+y)/2, we can instead calculate (x&y) + ((x^y)>>1).
|
|
|
|
// We mask by 0x7FFF to remove the LSB so that it doesn't leak into the field below.
|
|
|
|
|
|
|
|
code->mov(xor_a_b, reg_a);
|
|
|
|
code->and_(and_a_b, reg_b);
|
|
|
|
code->xor_(xor_a_b, reg_b);
|
|
|
|
code->shr(xor_a_b, 1);
|
|
|
|
code->and_(xor_a_b, 0x7FFF7FFF);
|
|
|
|
code->add(result, xor_a_b);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2017-12-12 16:57:26 +01:00
|
|
|
}
|
2016-11-26 12:28:20 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingAddS8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-11-26 19:12:29 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 reg_a = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 reg_b = ctx.reg_alloc.UseGpr(args[1]).cvt32();
|
|
|
|
Xbyak::Reg32 xor_a_b = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-11-26 19:12:29 +01:00
|
|
|
Xbyak::Reg32 and_a_b = reg_a;
|
|
|
|
Xbyak::Reg32 result = reg_a;
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-11-26 19:12:29 +01:00
|
|
|
|
|
|
|
// This relies on the equality x+y == ((x&y) << 1) + (x^y).
|
|
|
|
// Note that x^y always contains the LSB of the result.
|
|
|
|
// Since we want to calculate (x+y)/2, we can instead calculate (x&y) + ((x^y)>>1).
|
|
|
|
// We mask by 0x7F to remove the LSB so that it doesn't leak into the field below.
|
|
|
|
// carry propagates the sign bit from (x^y)>>1 upwards by one.
|
|
|
|
|
|
|
|
code->mov(xor_a_b, reg_a);
|
2017-06-12 02:17:04 +02:00
|
|
|
code->and_(and_a_b, reg_b);
|
|
|
|
code->xor_(xor_a_b, reg_b);
|
2016-11-26 19:12:29 +01:00
|
|
|
code->mov(carry, xor_a_b);
|
2017-06-12 02:17:04 +02:00
|
|
|
code->and_(carry, 0x80808080);
|
2016-11-26 19:12:29 +01:00
|
|
|
code->shr(xor_a_b, 1);
|
2017-06-12 02:17:04 +02:00
|
|
|
code->and_(xor_a_b, 0x7F7F7F7F);
|
2016-11-26 19:12:29 +01:00
|
|
|
code->add(result, xor_a_b);
|
2017-06-12 02:17:04 +02:00
|
|
|
code->xor_(result, carry);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-11-26 19:12:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingAddS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-11-26 19:12:29 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
|
|
|
Xbyak::Xmm tmp = ctx.reg_alloc.ScratchXmm();
|
2016-11-26 19:12:29 +01:00
|
|
|
|
|
|
|
// This relies on the equality x+y == ((x&y) << 1) + (x^y).
|
|
|
|
// Note that x^y always contains the LSB of the result.
|
2017-12-12 16:57:26 +01:00
|
|
|
// Since we want to calculate (x+y)/2, we can instead calculate (x&y) + ((x^y)>>>1).
|
|
|
|
// The arithmetic shift right makes this signed.
|
2016-11-26 19:12:29 +01:00
|
|
|
|
2017-12-12 16:57:26 +01:00
|
|
|
code->movdqa(tmp, xmm_a);
|
|
|
|
code->pand(xmm_a, xmm_b);
|
|
|
|
code->pxor(tmp, xmm_b);
|
|
|
|
code->psraw(tmp, 1);
|
|
|
|
code->paddw(xmm_a, tmp);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-11-26 19:12:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingSubU8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-11-26 19:27:21 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 minuend = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 subtrahend = ctx.reg_alloc.UseScratchGpr(args[1]).cvt32();
|
2016-11-26 19:27:21 +01:00
|
|
|
|
|
|
|
// This relies on the equality x-y == (x^y) - (((x^y)&y) << 1).
|
|
|
|
// Note that x^y always contains the LSB of the result.
|
|
|
|
// Since we want to calculate (x+y)/2, we can instead calculate ((x^y)>>1) - ((x^y)&y).
|
|
|
|
|
2017-06-12 02:17:04 +02:00
|
|
|
code->xor_(minuend, subtrahend);
|
|
|
|
code->and_(subtrahend, minuend);
|
2016-11-26 19:27:21 +01:00
|
|
|
code->shr(minuend, 1);
|
|
|
|
|
|
|
|
// At this point,
|
|
|
|
// minuend := (a^b) >> 1
|
|
|
|
// subtrahend := (a^b) & b
|
|
|
|
|
|
|
|
// We must now perform a partitioned subtraction.
|
|
|
|
// We can do this because minuend contains 7 bit fields.
|
|
|
|
// We use the extra bit in minuend as a bit to borrow from; we set this bit.
|
|
|
|
// We invert this bit at the end as this tells us if that bit was borrowed from.
|
2017-06-12 02:17:04 +02:00
|
|
|
code->or_(minuend, 0x80808080);
|
2016-11-26 19:27:21 +01:00
|
|
|
code->sub(minuend, subtrahend);
|
2017-06-12 02:17:04 +02:00
|
|
|
code->xor_(minuend, 0x80808080);
|
2016-11-26 19:27:21 +01:00
|
|
|
|
|
|
|
// minuend now contains the desired result.
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, minuend);
|
2016-11-26 19:27:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingSubS8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-22 13:02:24 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 minuend = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 subtrahend = ctx.reg_alloc.UseScratchGpr(args[1]).cvt32();
|
2016-12-22 13:02:24 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-12-22 13:02:24 +01:00
|
|
|
|
|
|
|
// This relies on the equality x-y == (x^y) - (((x^y)&y) << 1).
|
|
|
|
// Note that x^y always contains the LSB of the result.
|
|
|
|
// Since we want to calculate (x-y)/2, we can instead calculate ((x^y)>>1) - ((x^y)&y).
|
|
|
|
|
2017-06-12 02:17:04 +02:00
|
|
|
code->xor_(minuend, subtrahend);
|
|
|
|
code->and_(subtrahend, minuend);
|
2016-12-22 13:02:24 +01:00
|
|
|
code->mov(carry, minuend);
|
2017-06-12 02:17:04 +02:00
|
|
|
code->and_(carry, 0x80808080);
|
2016-12-22 13:02:24 +01:00
|
|
|
code->shr(minuend, 1);
|
|
|
|
|
|
|
|
// At this point,
|
|
|
|
// minuend := (a^b) >> 1
|
|
|
|
// subtrahend := (a^b) & b
|
|
|
|
// carry := (a^b) & 0x80808080
|
|
|
|
|
|
|
|
// We must now perform a partitioned subtraction.
|
|
|
|
// We can do this because minuend contains 7 bit fields.
|
|
|
|
// We use the extra bit in minuend as a bit to borrow from; we set this bit.
|
|
|
|
// We invert this bit at the end as this tells us if that bit was borrowed from.
|
|
|
|
// We then sign extend the result into this bit.
|
2017-06-12 02:17:04 +02:00
|
|
|
code->or_(minuend, 0x80808080);
|
2016-12-22 13:02:24 +01:00
|
|
|
code->sub(minuend, subtrahend);
|
2017-06-12 02:17:04 +02:00
|
|
|
code->xor_(minuend, 0x80808080);
|
|
|
|
code->xor_(minuend, carry);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, minuend);
|
2016-12-22 13:02:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingSubU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-11-26 19:27:21 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm minuend = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm subtrahend = ctx.reg_alloc.UseScratchXmm(args[1]);
|
2016-11-26 19:27:21 +01:00
|
|
|
|
|
|
|
// This relies on the equality x-y == (x^y) - (((x^y)&y) << 1).
|
|
|
|
// Note that x^y always contains the LSB of the result.
|
2017-12-14 16:04:20 +01:00
|
|
|
// Since we want to calculate (x-y)/2, we can instead calculate ((x^y)>>1) - ((x^y)&y).
|
2016-11-26 19:27:21 +01:00
|
|
|
|
2017-12-14 16:04:20 +01:00
|
|
|
code->pxor(minuend, subtrahend);
|
|
|
|
code->pand(subtrahend, minuend);
|
|
|
|
code->psrlw(minuend, 1);
|
2016-11-26 19:27:21 +01:00
|
|
|
|
|
|
|
// At this point,
|
|
|
|
// minuend := (a^b) >> 1
|
|
|
|
// subtrahend := (a^b) & b
|
|
|
|
|
2017-12-14 16:04:20 +01:00
|
|
|
code->psubw(minuend, subtrahend);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, minuend);
|
2016-12-22 13:02:24 +01:00
|
|
|
}
|
2016-11-26 19:27:21 +01:00
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingSubS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-22 13:02:24 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm minuend = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm subtrahend = ctx.reg_alloc.UseScratchXmm(args[1]);
|
2016-12-22 13:02:24 +01:00
|
|
|
|
|
|
|
// This relies on the equality x-y == (x^y) - (((x^y)&y) << 1).
|
|
|
|
// Note that x^y always contains the LSB of the result.
|
2017-12-14 16:04:20 +01:00
|
|
|
// Since we want to calculate (x-y)/2, we can instead calculate ((x^y)>>>1) - ((x^y)&y).
|
2016-12-22 13:02:24 +01:00
|
|
|
|
2017-12-14 16:04:20 +01:00
|
|
|
code->pxor(minuend, subtrahend);
|
|
|
|
code->pand(subtrahend, minuend);
|
|
|
|
code->psraw(minuend, 1);
|
2016-12-22 13:02:24 +01:00
|
|
|
|
|
|
|
// At this point,
|
2017-12-14 16:04:20 +01:00
|
|
|
// minuend := (a^b) >>> 1
|
2016-12-22 13:02:24 +01:00
|
|
|
// subtrahend := (a^b) & b
|
|
|
|
|
2017-12-14 16:04:20 +01:00
|
|
|
code->psubw(minuend, subtrahend);
|
2016-11-26 19:27:21 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, minuend);
|
2017-02-24 22:25:31 +01:00
|
|
|
}
|
2016-12-28 22:28:55 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
void EmitPackedSubAdd(BlockOfCode* code, EmitContext& ctx, IR::Inst* inst, bool hi_is_sum, bool is_signed, bool is_halving) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-03-24 16:56:24 +01:00
|
|
|
auto ge_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetGEFromOp);
|
2016-12-28 22:28:55 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 reg_a_hi = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 reg_b_hi = ctx.reg_alloc.UseScratchGpr(args[1]).cvt32();
|
|
|
|
Xbyak::Reg32 reg_a_lo = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
Xbyak::Reg32 reg_b_lo = ctx.reg_alloc.ScratchGpr().cvt32();
|
2017-03-24 16:56:24 +01:00
|
|
|
Xbyak::Reg32 reg_sum, reg_diff;
|
2016-12-28 22:28:55 +01:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
if (is_signed) {
|
|
|
|
code->movsx(reg_a_lo, reg_a_hi.cvt16());
|
|
|
|
code->movsx(reg_b_lo, reg_b_hi.cvt16());
|
|
|
|
code->sar(reg_a_hi, 16);
|
|
|
|
code->sar(reg_b_hi, 16);
|
|
|
|
} else {
|
|
|
|
code->movzx(reg_a_lo, reg_a_hi.cvt16());
|
|
|
|
code->movzx(reg_b_lo, reg_b_hi.cvt16());
|
|
|
|
code->shr(reg_a_hi, 16);
|
|
|
|
code->shr(reg_b_hi, 16);
|
|
|
|
}
|
2016-12-28 22:28:55 +01:00
|
|
|
|
2017-03-24 16:56:24 +01:00
|
|
|
if (hi_is_sum) {
|
2016-12-28 22:28:55 +01:00
|
|
|
code->sub(reg_a_lo, reg_b_hi);
|
|
|
|
code->add(reg_a_hi, reg_b_lo);
|
2017-03-24 16:56:24 +01:00
|
|
|
reg_diff = reg_a_lo;
|
|
|
|
reg_sum = reg_a_hi;
|
2016-12-28 22:28:55 +01:00
|
|
|
} else {
|
|
|
|
code->add(reg_a_lo, reg_b_hi);
|
|
|
|
code->sub(reg_a_hi, reg_b_lo);
|
2017-03-24 16:56:24 +01:00
|
|
|
reg_diff = reg_a_hi;
|
|
|
|
reg_sum = reg_a_lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ge_inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.EraseInstruction(ge_inst);
|
2017-03-24 16:56:24 +01:00
|
|
|
|
|
|
|
// The reg_b registers are no longer required.
|
|
|
|
Xbyak::Reg32 ge_sum = reg_b_hi;
|
|
|
|
Xbyak::Reg32 ge_diff = reg_b_lo;
|
|
|
|
|
|
|
|
code->mov(ge_sum, reg_sum);
|
|
|
|
code->mov(ge_diff, reg_diff);
|
|
|
|
|
|
|
|
if (!is_signed) {
|
|
|
|
code->shl(ge_sum, 15);
|
2017-11-25 18:34:39 +01:00
|
|
|
code->sar(ge_sum, 31);
|
2017-03-24 16:56:24 +01:00
|
|
|
} else {
|
2017-06-12 02:17:04 +02:00
|
|
|
code->not_(ge_sum);
|
2017-11-25 18:34:39 +01:00
|
|
|
code->sar(ge_sum, 31);
|
2017-03-24 16:56:24 +01:00
|
|
|
}
|
2017-06-12 02:17:04 +02:00
|
|
|
code->not_(ge_diff);
|
2017-11-25 18:34:39 +01:00
|
|
|
code->sar(ge_diff, 31);
|
|
|
|
code->and_(ge_sum, hi_is_sum ? 0xFFFF0000 : 0x0000FFFF);
|
|
|
|
code->and_(ge_diff, hi_is_sum ? 0x0000FFFF : 0xFFFF0000);
|
2017-03-24 16:56:24 +01:00
|
|
|
code->or_(ge_sum, ge_diff);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(ge_inst, ge_sum);
|
2017-03-24 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_halving) {
|
|
|
|
code->shl(reg_a_lo, 15);
|
2016-12-28 22:28:55 +01:00
|
|
|
code->shr(reg_a_hi, 1);
|
2017-03-24 16:56:24 +01:00
|
|
|
} else {
|
|
|
|
code->shl(reg_a_lo, 16);
|
2016-12-28 22:28:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// reg_a_lo now contains the low word and reg_a_hi now contains the high word.
|
|
|
|
// Merge them.
|
|
|
|
code->shld(reg_a_hi, reg_a_lo, 16);
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, reg_a_hi);
|
2016-12-28 22:28:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedAddSubU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedSubAdd(code, ctx, inst, true, false, false);
|
2017-03-24 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedAddSubS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedSubAdd(code, ctx, inst, true, true, false);
|
2017-03-24 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSubAddU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedSubAdd(code, ctx, inst, false, false, false);
|
2017-03-24 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSubAddS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedSubAdd(code, ctx, inst, false, true, false);
|
2017-03-24 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingAddSubU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedSubAdd(code, ctx, inst, true, false, true);
|
2017-03-24 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingAddSubS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedSubAdd(code, ctx, inst, true, true, true);
|
2017-03-24 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingSubAddU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedSubAdd(code, ctx, inst, false, false, true);
|
2017-02-24 22:25:31 +01:00
|
|
|
}
|
2016-12-04 21:52:33 +01:00
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedHalvingSubAddS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedSubAdd(code, ctx, inst, false, true, true);
|
2017-02-24 22:25:31 +01:00
|
|
|
}
|
2016-12-04 21:52:33 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
static void EmitPackedOperation(BlockOfCode* code, EmitContext& ctx, IR::Inst* inst, void (Xbyak::CodeGenerator::*fn)(const Xbyak::Mmx& mmx, const Xbyak::Operand&)) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-12-04 21:52:33 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_a = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm xmm_b = ctx.reg_alloc.UseXmm(args[1]);
|
2016-12-04 21:52:33 +01:00
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
(code->*fn)(xmm_a, xmm_b);
|
2016-12-04 21:52:33 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm_a);
|
2016-12-04 21:52:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSaturatedAddU8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::paddusb);
|
2016-08-12 19:26:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSaturatedAddS8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::paddsb);
|
2016-08-12 19:26:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSaturatedSubU8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::psubusb);
|
2016-08-12 19:18:38 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSaturatedSubS8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::psubsb);
|
2016-08-12 19:18:38 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSaturatedAddU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::paddusw);
|
2016-08-12 19:42:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSaturatedAddS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::paddsw);
|
2016-08-12 19:42:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSaturatedSubU16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::psubusw);
|
2016-08-12 19:42:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSaturatedSubS16(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::psubsw);
|
2016-08-12 19:42:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedAbsDiffSumS8(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
EmitPackedOperation(code, ctx, inst, &Xbyak::CodeGenerator::psadbw);
|
2016-12-17 20:52:22 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitPackedSelect(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-11-25 17:33:48 +01:00
|
|
|
|
2017-11-25 20:34:30 +01:00
|
|
|
size_t num_args_in_xmm = args[0].IsInXmm() + args[1].IsInXmm() + args[2].IsInXmm();
|
|
|
|
|
|
|
|
if (num_args_in_xmm >= 2) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm ge = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm to = ctx.reg_alloc.UseXmm(args[1]);
|
|
|
|
Xbyak::Xmm from = ctx.reg_alloc.UseScratchXmm(args[2]);
|
2017-11-25 18:34:39 +01:00
|
|
|
|
|
|
|
code->pand(from, ge);
|
|
|
|
code->pandn(ge, to);
|
|
|
|
code->por(from, ge);
|
2017-11-25 17:33:48 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, from);
|
2017-11-25 18:34:39 +01:00
|
|
|
} else if (code->DoesCpuSupport(Xbyak::util::Cpu::tBMI1)) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 ge = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 to = ctx.reg_alloc.UseScratchGpr(args[1]).cvt32();
|
|
|
|
Xbyak::Reg32 from = ctx.reg_alloc.UseScratchGpr(args[2]).cvt32();
|
2017-11-25 17:33:48 +01:00
|
|
|
|
2017-11-25 18:34:39 +01:00
|
|
|
code->and_(from, ge);
|
|
|
|
code->andn(to, ge, to);
|
|
|
|
code->or_(from, to);
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, from);
|
2017-11-25 18:34:39 +01:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Reg32 ge = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 to = ctx.reg_alloc.UseGpr(args[1]).cvt32();
|
|
|
|
Xbyak::Reg32 from = ctx.reg_alloc.UseScratchGpr(args[2]).cvt32();
|
2017-11-25 18:34:39 +01:00
|
|
|
|
|
|
|
code->and_(from, ge);
|
|
|
|
code->not_(ge);
|
2017-11-25 20:34:30 +01:00
|
|
|
code->and_(ge, to);
|
|
|
|
code->or_(from, ge);
|
2017-11-25 18:34:39 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, from);
|
2017-11-25 18:34:39 +01:00
|
|
|
}
|
2017-11-25 17:33:48 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2016-08-24 21:07:08 +02:00
|
|
|
static void DenormalsAreZero32(BlockOfCode* code, Xbyak::Xmm xmm_value, Xbyak::Reg32 gpr_scratch) {
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
2016-08-06 18:21:29 +02:00
|
|
|
// We need to report back whether we've found a denormal on input.
|
|
|
|
// SSE doesn't do this for us when SSE's DAZ is enabled.
|
2016-08-24 21:07:08 +02:00
|
|
|
|
|
|
|
code->movd(gpr_scratch, xmm_value);
|
|
|
|
code->and_(gpr_scratch, u32(0x7FFFFFFF));
|
|
|
|
code->sub(gpr_scratch, u32(1));
|
|
|
|
code->cmp(gpr_scratch, u32(0x007FFFFE));
|
|
|
|
code->ja(end);
|
|
|
|
code->pxor(xmm_value, xmm_value);
|
2018-01-02 18:45:39 +01:00
|
|
|
code->mov(dword[r15 + offsetof(JST, FPSCR_IDC)], u32(1 << 7));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(end);
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2016-08-24 21:07:08 +02:00
|
|
|
static void DenormalsAreZero64(BlockOfCode* code, Xbyak::Xmm xmm_value, Xbyak::Reg64 gpr_scratch) {
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
2017-03-18 18:20:21 +01:00
|
|
|
auto mask = code->MConst(f64_non_sign_mask);
|
2016-08-24 21:07:08 +02:00
|
|
|
mask.setBit(64);
|
2017-03-18 18:20:21 +01:00
|
|
|
auto penult_denormal = code->MConst(f64_penultimate_positive_denormal);
|
2016-08-24 21:07:08 +02:00
|
|
|
penult_denormal.setBit(64);
|
|
|
|
|
|
|
|
code->movq(gpr_scratch, xmm_value);
|
|
|
|
code->and_(gpr_scratch, mask);
|
|
|
|
code->sub(gpr_scratch, u32(1));
|
|
|
|
code->cmp(gpr_scratch, penult_denormal);
|
|
|
|
code->ja(end);
|
|
|
|
code->pxor(xmm_value, xmm_value);
|
2018-01-02 18:45:39 +01:00
|
|
|
code->mov(dword[r15 + offsetof(JST, FPSCR_IDC)], u32(1 << 7));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(end);
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2016-08-24 21:07:08 +02:00
|
|
|
static void FlushToZero32(BlockOfCode* code, Xbyak::Xmm xmm_value, Xbyak::Reg32 gpr_scratch) {
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
|
|
|
code->movd(gpr_scratch, xmm_value);
|
|
|
|
code->and_(gpr_scratch, u32(0x7FFFFFFF));
|
|
|
|
code->sub(gpr_scratch, u32(1));
|
|
|
|
code->cmp(gpr_scratch, u32(0x007FFFFE));
|
|
|
|
code->ja(end);
|
|
|
|
code->pxor(xmm_value, xmm_value);
|
2018-01-02 18:45:39 +01:00
|
|
|
code->mov(dword[r15 + offsetof(JST, FPSCR_UFC)], u32(1 << 3));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(end);
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2016-08-24 21:07:08 +02:00
|
|
|
static void FlushToZero64(BlockOfCode* code, Xbyak::Xmm xmm_value, Xbyak::Reg64 gpr_scratch) {
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
2017-03-18 18:20:21 +01:00
|
|
|
auto mask = code->MConst(f64_non_sign_mask);
|
2016-08-24 21:07:08 +02:00
|
|
|
mask.setBit(64);
|
2017-03-18 18:20:21 +01:00
|
|
|
auto penult_denormal = code->MConst(f64_penultimate_positive_denormal);
|
2016-08-24 21:07:08 +02:00
|
|
|
penult_denormal.setBit(64);
|
|
|
|
|
|
|
|
code->movq(gpr_scratch, xmm_value);
|
|
|
|
code->and_(gpr_scratch, mask);
|
|
|
|
code->sub(gpr_scratch, u32(1));
|
|
|
|
code->cmp(gpr_scratch, penult_denormal);
|
|
|
|
code->ja(end);
|
|
|
|
code->pxor(xmm_value, xmm_value);
|
2018-01-02 18:45:39 +01:00
|
|
|
code->mov(dword[r15 + offsetof(JST, FPSCR_UFC)], u32(1 << 3));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DefaultNaN32(BlockOfCode* code, Xbyak::Xmm xmm_value) {
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
|
|
|
code->ucomiss(xmm_value, xmm_value);
|
|
|
|
code->jnp(end);
|
2017-03-18 18:20:21 +01:00
|
|
|
code->movaps(xmm_value, code->MConst(f32_nan));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DefaultNaN64(BlockOfCode* code, Xbyak::Xmm xmm_value) {
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
|
|
|
code->ucomisd(xmm_value, xmm_value);
|
|
|
|
code->jnp(end);
|
2017-03-18 18:20:21 +01:00
|
|
|
code->movaps(xmm_value, code->MConst(f64_nan));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(end);
|
|
|
|
}
|
|
|
|
|
2016-08-26 16:23:08 +02:00
|
|
|
static void ZeroIfNaN64(BlockOfCode* code, Xbyak::Xmm xmm_value, Xbyak::Xmm xmm_scratch) {
|
|
|
|
code->pxor(xmm_scratch, xmm_scratch);
|
|
|
|
code->cmpordsd(xmm_scratch, xmm_value); // true mask when ordered (i.e.: when not an NaN)
|
|
|
|
code->pand(xmm_value, xmm_scratch);
|
2016-08-24 21:07:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2018-01-02 00:40:34 +01:00
|
|
|
static void FPThreeOp32(BlockOfCode* code, EmitContext& ctx, IR::Inst* inst, void (Xbyak::CodeGenerator::*fn)(const Xbyak::Xmm&, const Xbyak::Operand&)) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-08-06 18:21:29 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm operand = ctx.reg_alloc.UseScratchXmm(args[1]);
|
|
|
|
Xbyak::Reg32 gpr_scratch = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-08-06 18:21:29 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero32<JST>(code, result, gpr_scratch);
|
|
|
|
DenormalsAreZero32<JST>(code, operand, gpr_scratch);
|
2016-08-06 18:21:29 +02:00
|
|
|
}
|
2016-08-24 21:07:08 +02:00
|
|
|
(code->*fn)(result, operand);
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
FlushToZero32<JST>(code, result, gpr_scratch);
|
2016-08-06 18:21:29 +02:00
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_DN()) {
|
2016-08-07 19:08:48 +02:00
|
|
|
DefaultNaN32(code, result);
|
2016-08-06 18:21:29 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-06 18:21:29 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2018-01-02 00:40:34 +01:00
|
|
|
static void FPThreeOp64(BlockOfCode* code, EmitContext& ctx, IR::Inst* inst, void (Xbyak::CodeGenerator::*fn)(const Xbyak::Xmm&, const Xbyak::Operand&)) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-08-06 18:21:29 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Xmm operand = ctx.reg_alloc.UseScratchXmm(args[1]);
|
|
|
|
Xbyak::Reg64 gpr_scratch = ctx.reg_alloc.ScratchGpr();
|
2016-08-06 18:21:29 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero64<JST>(code, result, gpr_scratch);
|
|
|
|
DenormalsAreZero64<JST>(code, operand, gpr_scratch);
|
2016-08-06 18:21:29 +02:00
|
|
|
}
|
2016-08-24 21:07:08 +02:00
|
|
|
(code->*fn)(result, operand);
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
FlushToZero64<JST>(code, result, gpr_scratch);
|
2016-08-06 18:21:29 +02:00
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_DN()) {
|
2016-08-07 19:08:48 +02:00
|
|
|
DefaultNaN64(code, result);
|
2016-08-06 18:21:29 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-06 18:21:29 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2018-01-02 00:40:34 +01:00
|
|
|
static void FPTwoOp32(BlockOfCode* code, EmitContext& ctx, IR::Inst* inst, void (Xbyak::CodeGenerator::*fn)(const Xbyak::Xmm&, const Xbyak::Operand&)) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-08-07 13:19:07 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Reg32 gpr_scratch = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-08-07 13:19:07 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero32<JST>(code, result, gpr_scratch);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
2016-08-25 17:34:05 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
(code->*fn)(result, result);
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
FlushToZero32<JST>(code, result, gpr_scratch);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_DN()) {
|
2016-08-07 19:08:48 +02:00
|
|
|
DefaultNaN32(code, result);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2018-01-02 00:40:34 +01:00
|
|
|
static void FPTwoOp64(BlockOfCode* code, EmitContext& ctx, IR::Inst* inst, void (Xbyak::CodeGenerator::*fn)(const Xbyak::Xmm&, const Xbyak::Operand&)) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2016-08-07 13:19:07 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Reg64 gpr_scratch = ctx.reg_alloc.ScratchGpr();
|
2016-08-07 13:19:07 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero64<JST>(code, result, gpr_scratch);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
2016-08-25 17:34:05 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
(code->*fn)(result, result);
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
FlushToZero64<JST>(code, result, gpr_scratch);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_DN()) {
|
2016-08-07 19:08:48 +02:00
|
|
|
DefaultNaN64(code, result);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitTransferFromFP32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-08-07 20:25:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitTransferFromFP64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-08-07 20:25:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitTransferToFP32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
if (args[0].IsImmediate() && args[0].GetImmediateU32() == 0) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.ScratchXmm();
|
2016-12-03 12:36:03 +01:00
|
|
|
code->xorps(result, result);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-12-03 12:36:03 +01:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-12-03 12:36:03 +01:00
|
|
|
}
|
2016-08-07 20:25:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitTransferToFP64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2017-02-24 22:25:31 +01:00
|
|
|
if (args[0].IsImmediate() && args[0].GetImmediateU64() == 0) {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
code->xorps(result, result);
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-12-03 12:36:03 +01:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
2016-12-03 12:36:03 +01:00
|
|
|
}
|
2016-08-07 20:25:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPAbs32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
2016-08-07 02:41:25 +02:00
|
|
|
|
2017-03-18 18:20:21 +01:00
|
|
|
code->pand(result, code->MConst(f32_non_sign_mask));
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-07 02:41:25 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPAbs64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
2016-08-07 02:41:25 +02:00
|
|
|
|
2017-03-18 18:20:21 +01:00
|
|
|
code->pand(result, code->MConst(f64_non_sign_mask));
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-07 11:21:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPNeg32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
2016-08-07 11:56:12 +02:00
|
|
|
|
2017-03-18 18:20:21 +01:00
|
|
|
code->pxor(result, code->MConst(f32_negative_zero));
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-07 11:56:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPNeg64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
2016-08-07 11:56:12 +02:00
|
|
|
|
2017-03-18 18:20:21 +01:00
|
|
|
code->pxor(result, code->MConst(f64_negative_zero));
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-07 11:56:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPAdd32(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPThreeOp32<JST>(code, ctx, inst, &Xbyak::CodeGenerator::addss);
|
2016-08-07 11:21:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPAdd64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPThreeOp64<JST>(code, ctx, inst, &Xbyak::CodeGenerator::addsd);
|
2016-08-07 11:21:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPDiv32(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPThreeOp32<JST>(code, ctx, inst, &Xbyak::CodeGenerator::divss);
|
2016-08-07 11:56:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPDiv64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPThreeOp64<JST>(code, ctx, inst, &Xbyak::CodeGenerator::divsd);
|
2016-08-07 11:56:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPMul32(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPThreeOp32<JST>(code, ctx, inst, &Xbyak::CodeGenerator::mulss);
|
2016-08-07 11:21:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPMul64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPThreeOp64<JST>(code, ctx, inst, &Xbyak::CodeGenerator::mulsd);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPSqrt32(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPTwoOp32<JST>(code, ctx, inst, &Xbyak::CodeGenerator::sqrtss);
|
2016-08-07 13:19:07 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPSqrt64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPTwoOp64<JST>(code, ctx, inst, &Xbyak::CodeGenerator::sqrtsd);
|
2016-08-07 11:21:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPSub32(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPThreeOp32<JST>(code, ctx, inst, &Xbyak::CodeGenerator::subss);
|
2016-08-07 11:21:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPSub64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
FPThreeOp64<JST>(code, ctx, inst, &Xbyak::CodeGenerator::subsd);
|
2016-08-07 02:41:25 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
2018-01-02 00:40:34 +01:00
|
|
|
static void SetFpscrNzcvFromFlags(BlockOfCode* code, EmitContext& ctx) {
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RCX}); // shifting requires use of cl
|
|
|
|
Xbyak::Reg32 nzcv = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-12-04 12:43:31 +01:00
|
|
|
|
2017-11-22 18:46:46 +01:00
|
|
|
code->mov(nzcv, 0x28630000);
|
|
|
|
code->sete(cl);
|
|
|
|
code->rcl(cl, 3);
|
|
|
|
code->shl(nzcv, cl);
|
|
|
|
code->and_(nzcv, 0xF0000000);
|
2018-01-02 18:45:39 +01:00
|
|
|
code->mov(dword[r15 + offsetof(JST, FPSCR_nzcv)], nzcv);
|
2016-11-26 12:17:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPCompare32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm reg_a = ctx.reg_alloc.UseXmm(args[0]);
|
|
|
|
Xbyak::Xmm reg_b = ctx.reg_alloc.UseXmm(args[1]);
|
2017-11-22 18:45:37 +01:00
|
|
|
bool exc_on_qnan = args[2].GetImmediateU1();
|
2016-11-26 12:17:16 +01:00
|
|
|
|
2017-11-22 18:45:37 +01:00
|
|
|
if (exc_on_qnan) {
|
2016-11-26 12:17:16 +01:00
|
|
|
code->comiss(reg_a, reg_b);
|
2017-11-22 18:45:37 +01:00
|
|
|
} else {
|
|
|
|
code->ucomiss(reg_a, reg_b);
|
2016-11-26 12:17:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
SetFpscrNzcvFromFlags<JST>(code, ctx);
|
2016-11-26 12:17:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPCompare64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm reg_a = ctx.reg_alloc.UseXmm(args[0]);
|
|
|
|
Xbyak::Xmm reg_b = ctx.reg_alloc.UseXmm(args[1]);
|
2017-11-22 18:45:37 +01:00
|
|
|
bool exc_on_qnan = args[2].GetImmediateU1();
|
2016-11-26 12:17:16 +01:00
|
|
|
|
2017-11-22 18:45:37 +01:00
|
|
|
if (exc_on_qnan) {
|
2016-11-26 12:17:16 +01:00
|
|
|
code->comisd(reg_a, reg_b);
|
2017-11-22 18:45:37 +01:00
|
|
|
} else {
|
|
|
|
code->ucomisd(reg_a, reg_b);
|
2016-11-26 12:17:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
SetFpscrNzcvFromFlags<JST>(code, ctx);
|
2016-11-26 12:17:16 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPSingleToDouble(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Reg64 gpr_scratch = ctx.reg_alloc.ScratchGpr();
|
2016-08-23 23:04:46 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero32<JST>(code, result, gpr_scratch.cvt32());
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvtss2sd(result, result);
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
FlushToZero64<JST>(code, result, gpr_scratch);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_DN()) {
|
2016-08-23 23:04:46 +02:00
|
|
|
DefaultNaN64(code, result);
|
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPDoubleToSingle(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Reg64 gpr_scratch = ctx.reg_alloc.ScratchGpr();
|
2016-08-23 23:04:46 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero64<JST>(code, result, gpr_scratch);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvtsd2ss(result, result);
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
FlushToZero32<JST>(code, result, gpr_scratch.cvt32());
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_DN()) {
|
2016-08-23 23:04:46 +02:00
|
|
|
DefaultNaN32(code, result);
|
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPSingleToS32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm from = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Reg32 to = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
Xbyak::Xmm xmm_scratch = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
bool round_towards_zero = args[1].GetImmediateU1();
|
2016-08-23 23:04:46 +02:00
|
|
|
|
|
|
|
// ARM saturates on conversion; this differs from x64 which returns a sentinel value.
|
|
|
|
// Conversion to double is lossless, and allows for clamping.
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero32<JST>(code, from, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvtss2sd(from, from);
|
2016-08-23 23:04:46 +02:00
|
|
|
// First time is to set flags
|
|
|
|
if (round_towards_zero) {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvttsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
} else {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvtsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
// Clamp to output range
|
2016-08-26 16:23:08 +02:00
|
|
|
ZeroIfNaN64(code, from, xmm_scratch);
|
2017-03-18 18:20:21 +01:00
|
|
|
code->minsd(from, code->MConst(f64_max_s32));
|
|
|
|
code->maxsd(from, code->MConst(f64_min_s32));
|
2016-08-23 23:04:46 +02:00
|
|
|
// Second time is for real
|
|
|
|
if (round_towards_zero) {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvttsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
} else {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvtsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPSingleToU32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm from = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Reg32 to = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
Xbyak::Xmm xmm_scratch = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
bool round_towards_zero = args[1].GetImmediateU1();
|
2016-08-23 23:04:46 +02:00
|
|
|
|
|
|
|
// ARM saturates on conversion; this differs from x64 which returns a sentinel value.
|
|
|
|
// Conversion to double is lossless, and allows for accurate clamping.
|
|
|
|
//
|
|
|
|
// Since SSE2 doesn't provide an unsigned conversion, we shift the range as appropriate.
|
|
|
|
//
|
|
|
|
// FIXME: Inexact exception not correctly signalled with the below code
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (!ctx.FPSCR_RoundTowardsZero() && !round_towards_zero) {
|
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero32<JST>(code, from, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvtss2sd(from, from);
|
2016-08-26 16:23:08 +02:00
|
|
|
ZeroIfNaN64(code, from, xmm_scratch);
|
2016-08-23 23:04:46 +02:00
|
|
|
// Bring into SSE range
|
2017-03-18 18:20:21 +01:00
|
|
|
code->addsd(from, code->MConst(f64_min_s32));
|
2016-08-23 23:04:46 +02:00
|
|
|
// First time is to set flags
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvtsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
// Clamp to output range
|
2017-03-18 18:20:21 +01:00
|
|
|
code->minsd(from, code->MConst(f64_max_s32));
|
|
|
|
code->maxsd(from, code->MConst(f64_min_s32));
|
2016-08-23 23:04:46 +02:00
|
|
|
// Actually convert
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvtsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
// Bring back into original range
|
2017-02-24 22:25:31 +01:00
|
|
|
code->add(to, u32(2147483648u));
|
2016-08-23 23:04:46 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_mask = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Reg32 gpr_mask = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-08-23 23:04:46 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero32<JST>(code, from, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvtss2sd(from, from);
|
2016-08-26 16:23:08 +02:00
|
|
|
ZeroIfNaN64(code, from, xmm_scratch);
|
2016-08-23 23:04:46 +02:00
|
|
|
// Generate masks if out-of-signed-range
|
2017-03-18 18:20:21 +01:00
|
|
|
code->movaps(xmm_mask, code->MConst(f64_max_s32));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cmpltsd(xmm_mask, from);
|
|
|
|
code->movd(gpr_mask, xmm_mask);
|
2017-03-18 18:20:21 +01:00
|
|
|
code->pand(xmm_mask, code->MConst(f64_min_s32));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->and_(gpr_mask, u32(2147483648u));
|
2016-08-23 23:04:46 +02:00
|
|
|
// Bring into range if necessary
|
2016-08-24 21:07:08 +02:00
|
|
|
code->addsd(from, xmm_mask);
|
2016-08-23 23:04:46 +02:00
|
|
|
// First time is to set flags
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvttsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
// Clamp to output range
|
2017-03-18 18:20:21 +01:00
|
|
|
code->minsd(from, code->MConst(f64_max_s32));
|
|
|
|
code->maxsd(from, code->MConst(f64_min_u32));
|
2016-08-23 23:04:46 +02:00
|
|
|
// Actually convert
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvttsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
// Bring back into original range if necessary
|
2017-02-24 22:25:31 +01:00
|
|
|
code->add(to, gpr_mask);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPDoubleToS32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm from = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Reg32 to = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
Xbyak::Xmm xmm_scratch = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Reg32 gpr_scratch = ctx.reg_alloc.ScratchGpr().cvt32();
|
2017-02-24 22:25:31 +01:00
|
|
|
bool round_towards_zero = args[1].GetImmediateU1();
|
2016-08-23 23:04:46 +02:00
|
|
|
|
|
|
|
// ARM saturates on conversion; this differs from x64 which returns a sentinel value.
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero64<JST>(code, from, gpr_scratch.cvt64());
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
// First time is to set flags
|
|
|
|
if (round_towards_zero) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvttsd2si(gpr_scratch, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
} else {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvtsd2si(gpr_scratch, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
// Clamp to output range
|
2016-08-26 16:23:08 +02:00
|
|
|
ZeroIfNaN64(code, from, xmm_scratch);
|
2017-03-18 18:20:21 +01:00
|
|
|
code->minsd(from, code->MConst(f64_max_s32));
|
|
|
|
code->maxsd(from, code->MConst(f64_min_s32));
|
2016-08-23 23:04:46 +02:00
|
|
|
// Second time is for real
|
|
|
|
if (round_towards_zero) {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvttsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
} else {
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvtsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPDoubleToU32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Xmm from = ctx.reg_alloc.UseScratchXmm(args[0]);
|
|
|
|
Xbyak::Reg32 to = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
Xbyak::Xmm xmm_scratch = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Reg32 gpr_scratch = ctx.reg_alloc.ScratchGpr().cvt32();
|
2017-02-24 22:25:31 +01:00
|
|
|
bool round_towards_zero = args[1].GetImmediateU1();
|
2016-08-23 23:04:46 +02:00
|
|
|
|
|
|
|
// ARM saturates on conversion; this differs from x64 which returns a sentinel value.
|
|
|
|
// TODO: Use VCVTPD2UDQ when AVX512VL is available.
|
|
|
|
// FIXME: Inexact exception not correctly signalled with the below code
|
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (!ctx.FPSCR_RoundTowardsZero() && !round_towards_zero) {
|
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero64<JST>(code, from, gpr_scratch.cvt64());
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2016-08-26 16:23:08 +02:00
|
|
|
ZeroIfNaN64(code, from, xmm_scratch);
|
2016-08-23 23:04:46 +02:00
|
|
|
// Bring into SSE range
|
2017-03-18 18:20:21 +01:00
|
|
|
code->addsd(from, code->MConst(f64_min_s32));
|
2016-08-23 23:04:46 +02:00
|
|
|
// First time is to set flags
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvtsd2si(gpr_scratch, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
// Clamp to output range
|
2017-03-18 18:20:21 +01:00
|
|
|
code->minsd(from, code->MConst(f64_max_s32));
|
|
|
|
code->maxsd(from, code->MConst(f64_min_s32));
|
2016-08-23 23:04:46 +02:00
|
|
|
// Actually convert
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvtsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
// Bring back into original range
|
2017-02-24 22:25:31 +01:00
|
|
|
code->add(to, u32(2147483648u));
|
2016-08-23 23:04:46 +02:00
|
|
|
} else {
|
2018-01-02 00:40:34 +01:00
|
|
|
Xbyak::Xmm xmm_mask = ctx.reg_alloc.ScratchXmm();
|
|
|
|
Xbyak::Reg32 gpr_mask = ctx.reg_alloc.ScratchGpr().cvt32();
|
2016-08-23 23:04:46 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
if (ctx.FPSCR_FTZ()) {
|
2018-01-02 18:45:39 +01:00
|
|
|
DenormalsAreZero64<JST>(code, from, gpr_scratch.cvt64());
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2016-08-26 16:23:08 +02:00
|
|
|
ZeroIfNaN64(code, from, xmm_scratch);
|
2016-08-23 23:04:46 +02:00
|
|
|
// Generate masks if out-of-signed-range
|
2017-03-18 18:20:21 +01:00
|
|
|
code->movaps(xmm_mask, code->MConst(f64_max_s32));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cmpltsd(xmm_mask, from);
|
|
|
|
code->movd(gpr_mask, xmm_mask);
|
2017-03-18 18:20:21 +01:00
|
|
|
code->pand(xmm_mask, code->MConst(f64_min_s32));
|
2016-08-24 21:07:08 +02:00
|
|
|
code->and_(gpr_mask, u32(2147483648u));
|
2016-08-23 23:04:46 +02:00
|
|
|
// Bring into range if necessary
|
2016-08-24 21:07:08 +02:00
|
|
|
code->addsd(from, xmm_mask);
|
2016-08-23 23:04:46 +02:00
|
|
|
// First time is to set flags
|
2016-08-24 21:07:08 +02:00
|
|
|
code->cvttsd2si(gpr_scratch, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
// Clamp to output range
|
2017-03-18 18:20:21 +01:00
|
|
|
code->minsd(from, code->MConst(f64_max_s32));
|
|
|
|
code->maxsd(from, code->MConst(f64_min_u32));
|
2016-08-23 23:04:46 +02:00
|
|
|
// Actually convert
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvttsd2si(to, from); // 32 bit gpr
|
2016-08-23 23:04:46 +02:00
|
|
|
// Bring back into original range if necessary
|
2017-02-24 22:25:31 +01:00
|
|
|
code->add(to, gpr_mask);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
2017-02-24 22:25:31 +01:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPS32ToSingle(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 from = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Xmm to = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
bool round_to_nearest = args[1].GetImmediateU1();
|
2016-08-23 23:04:46 +02:00
|
|
|
ASSERT_MSG(!round_to_nearest, "round_to_nearest unimplemented");
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvtsi2ss(to, from);
|
2016-08-23 23:04:46 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPU32ToSingle(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 from = ctx.reg_alloc.UseGpr(args[0]);
|
|
|
|
Xbyak::Xmm to = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
bool round_to_nearest = args[1].GetImmediateU1();
|
2016-08-23 23:04:46 +02:00
|
|
|
ASSERT_MSG(!round_to_nearest, "round_to_nearest unimplemented");
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
// We are using a 64-bit GPR register to ensure we don't end up treating the input as signed
|
|
|
|
code->mov(from.cvt32(), from.cvt32()); // TODO: Verify if this is necessary
|
|
|
|
code->cvtsi2ss(to, from);
|
2016-08-23 23:04:46 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPS32ToDouble(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 from = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Xmm to = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
bool round_to_nearest = args[1].GetImmediateU1();
|
2016-08-23 23:04:46 +02:00
|
|
|
ASSERT_MSG(!round_to_nearest, "round_to_nearest unimplemented");
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
code->cvtsi2sd(to, from);
|
2016-08-23 23:04:46 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitFPU32ToDouble(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-02 00:40:34 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 from = ctx.reg_alloc.UseGpr(args[0]);
|
|
|
|
Xbyak::Xmm to = ctx.reg_alloc.ScratchXmm();
|
2017-02-24 22:25:31 +01:00
|
|
|
bool round_to_nearest = args[1].GetImmediateU1();
|
2016-08-23 23:04:46 +02:00
|
|
|
ASSERT_MSG(!round_to_nearest, "round_to_nearest unimplemented");
|
|
|
|
|
2017-02-24 22:25:31 +01:00
|
|
|
// We are using a 64-bit GPR register to ensure we don't end up treating the input as signed
|
|
|
|
code->mov(from.cvt32(), from.cvt32()); // TODO: Verify if this is necessary
|
|
|
|
code->cvtsi2sd(to, from);
|
2016-08-23 23:04:46 +02:00
|
|
|
|
2018-01-02 00:40:34 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, to);
|
2016-08-23 23:04:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitAddCycles(size_t cycles) {
|
2016-07-04 15:37:50 +02:00
|
|
|
ASSERT(cycles < std::numeric_limits<u32>::max());
|
2018-01-02 18:45:39 +01:00
|
|
|
code->sub(qword[r15 + offsetof(JST, cycles_remaining)], static_cast<u32>(cycles));
|
2016-07-07 11:53:09 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
Xbyak::Label EmitX64<JST>::EmitCond(IR::Cond cond) {
|
2016-08-24 21:07:08 +02:00
|
|
|
Xbyak::Label label;
|
2016-07-14 13:52:53 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
const Xbyak::Reg32 cpsr = eax;
|
2018-01-02 18:45:39 +01:00
|
|
|
code->mov(cpsr, dword[r15 + offsetof(JST, CPSR_nzcv)]);
|
2016-07-14 13:52:53 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
constexpr size_t n_shift = 31;
|
|
|
|
constexpr size_t z_shift = 30;
|
|
|
|
constexpr size_t c_shift = 29;
|
|
|
|
constexpr size_t v_shift = 28;
|
|
|
|
constexpr u32 n_mask = 1u << n_shift;
|
|
|
|
constexpr u32 z_mask = 1u << z_shift;
|
|
|
|
constexpr u32 c_mask = 1u << c_shift;
|
|
|
|
constexpr u32 v_mask = 1u << v_shift;
|
2016-07-14 13:52:53 +02:00
|
|
|
|
|
|
|
switch (cond) {
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::EQ: //z
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(cpsr, z_mask);
|
|
|
|
code->jnz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::NE: //!z
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(cpsr, z_mask);
|
|
|
|
code->jz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::CS: //c
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(cpsr, c_mask);
|
|
|
|
code->jnz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::CC: //!c
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(cpsr, c_mask);
|
|
|
|
code->jz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::MI: //n
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(cpsr, n_mask);
|
|
|
|
code->jnz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::PL: //!n
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(cpsr, n_mask);
|
|
|
|
code->jz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::VS: //v
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(cpsr, v_mask);
|
|
|
|
code->jnz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::VC: //!v
|
2016-08-24 21:07:08 +02:00
|
|
|
code->test(cpsr, v_mask);
|
|
|
|
code->jz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::HI: { //c & !z
|
2016-08-24 21:07:08 +02:00
|
|
|
code->and_(cpsr, z_mask | c_mask);
|
|
|
|
code->cmp(cpsr, c_mask);
|
|
|
|
code->je(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::LS: { //!c | z
|
2016-08-24 21:07:08 +02:00
|
|
|
code->and_(cpsr, z_mask | c_mask);
|
|
|
|
code->cmp(cpsr, c_mask);
|
|
|
|
code->jne(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::GE: { // n == v
|
2016-08-24 21:07:08 +02:00
|
|
|
code->and_(cpsr, n_mask | v_mask);
|
|
|
|
code->jz(label);
|
|
|
|
code->cmp(cpsr, n_mask | v_mask);
|
|
|
|
code->je(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::LT: { // n != v
|
2016-08-24 21:07:08 +02:00
|
|
|
Xbyak::Label fail;
|
|
|
|
code->and_(cpsr, n_mask | v_mask);
|
|
|
|
code->jz(fail);
|
|
|
|
code->cmp(cpsr, n_mask | v_mask);
|
|
|
|
code->jne(label);
|
|
|
|
code->L(fail);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::GT: { // !z & (n == v)
|
2016-08-24 21:07:08 +02:00
|
|
|
const Xbyak::Reg32 tmp1 = ebx;
|
|
|
|
const Xbyak::Reg32 tmp2 = esi;
|
|
|
|
code->mov(tmp1, cpsr);
|
|
|
|
code->mov(tmp2, cpsr);
|
|
|
|
code->shr(tmp1, n_shift);
|
|
|
|
code->shr(tmp2, v_shift);
|
|
|
|
code->shr(cpsr, z_shift);
|
|
|
|
code->xor_(tmp1, tmp2);
|
|
|
|
code->or_(tmp1, cpsr);
|
|
|
|
code->test(tmp1, 1);
|
|
|
|
code->jz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-01-01 16:23:56 +01:00
|
|
|
case IR::Cond::LE: { // z | (n != v)
|
2016-08-24 21:07:08 +02:00
|
|
|
const Xbyak::Reg32 tmp1 = ebx;
|
|
|
|
const Xbyak::Reg32 tmp2 = esi;
|
|
|
|
code->mov(tmp1, cpsr);
|
|
|
|
code->mov(tmp2, cpsr);
|
|
|
|
code->shr(tmp1, n_shift);
|
|
|
|
code->shr(tmp2, v_shift);
|
|
|
|
code->shr(cpsr, z_shift);
|
|
|
|
code->xor_(tmp1, tmp2);
|
|
|
|
code->or_(tmp1, cpsr);
|
|
|
|
code->test(tmp1, 1);
|
|
|
|
code->jnz(label);
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2016-08-26 17:43:51 +02:00
|
|
|
ASSERT_MSG(false, "Unknown cond %zu", static_cast<size_t>(cond));
|
2016-08-23 00:40:30 +02:00
|
|
|
break;
|
2016-07-14 13:52:53 +02:00
|
|
|
}
|
2016-07-18 22:04:39 +02:00
|
|
|
|
2016-08-24 21:07:08 +02:00
|
|
|
return label;
|
2016-07-18 22:04:39 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitCondPrelude(const IR::Block& block) {
|
2018-01-01 16:23:56 +01:00
|
|
|
if (block.GetCondition() == IR::Cond::AL) {
|
2016-08-25 16:35:50 +02:00
|
|
|
ASSERT(!block.HasConditionFailedLocation());
|
2016-07-18 22:04:39 +02:00
|
|
|
return;
|
2016-07-14 13:52:53 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 16:35:50 +02:00
|
|
|
ASSERT(block.HasConditionFailedLocation());
|
2016-07-18 22:04:39 +02:00
|
|
|
|
2018-01-01 23:34:05 +01:00
|
|
|
Xbyak::Label pass = EmitCond(block.GetCondition());
|
2016-08-25 16:35:50 +02:00
|
|
|
EmitAddCycles(block.ConditionFailedCycleCount());
|
2017-02-16 20:40:51 +01:00
|
|
|
EmitTerminal(IR::Term::LinkBlock{block.ConditionFailedLocation()}, block.Location());
|
2016-08-24 21:07:08 +02:00
|
|
|
code->L(pass);
|
2016-07-14 13:52:53 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::EmitTerminal(IR::Terminal terminal, IR::LocationDescriptor initial_location) {
|
2017-02-16 20:40:51 +01:00
|
|
|
Common::VisitVariant<void>(terminal, [this, &initial_location](auto x) {
|
2018-01-01 23:34:05 +01:00
|
|
|
using T = std::decay_t<decltype(x)>;
|
|
|
|
if constexpr (!std::is_same_v<T, IR::Term::Invalid>) {
|
|
|
|
this->EmitTerminalImpl(x, initial_location);
|
|
|
|
} else {
|
|
|
|
ASSERT_MSG(false, "Invalid terminal");
|
|
|
|
}
|
2017-02-16 20:40:51 +01:00
|
|
|
});
|
2016-07-04 15:37:50 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::Patch(const IR::LocationDescriptor& desc, CodePtr bb) {
|
2016-08-24 21:07:08 +02:00
|
|
|
const CodePtr save_code_ptr = code->getCurr();
|
2018-01-01 23:34:05 +01:00
|
|
|
const PatchInformation& patch_info = patch_information[desc];
|
2016-08-07 23:11:39 +02:00
|
|
|
|
2016-12-19 16:01:49 +01:00
|
|
|
for (CodePtr location : patch_info.jg) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->SetCodePtr(location);
|
2017-04-08 11:04:53 +02:00
|
|
|
EmitPatchJg(desc, bb);
|
2016-08-07 23:11:39 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 16:01:49 +01:00
|
|
|
for (CodePtr location : patch_info.jmp) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->SetCodePtr(location);
|
2016-12-19 16:01:49 +01:00
|
|
|
EmitPatchJmp(desc, bb);
|
2016-08-15 15:33:17 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 16:01:49 +01:00
|
|
|
for (CodePtr location : patch_info.mov_rcx) {
|
2016-08-24 21:07:08 +02:00
|
|
|
code->SetCodePtr(location);
|
2016-12-19 16:01:49 +01:00
|
|
|
EmitPatchMovRcx(bb);
|
2016-08-13 01:10:23 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 23:11:39 +02:00
|
|
|
code->SetCodePtr(save_code_ptr);
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::Unpatch(const IR::LocationDescriptor& desc) {
|
2016-12-19 16:01:49 +01:00
|
|
|
Patch(desc, nullptr);
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::ClearCache() {
|
2016-12-19 16:01:49 +01:00
|
|
|
block_descriptors.clear();
|
|
|
|
patch_information.clear();
|
2016-07-07 13:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 18:45:39 +01:00
|
|
|
template <typename JST>
|
|
|
|
void EmitX64<JST>::InvalidateCacheRanges(const boost::icl::interval_set<ProgramCounterType>& ranges) {
|
2017-02-16 19:18:29 +01:00
|
|
|
// Remove cached block descriptors and patch information overlapping with the given range.
|
2017-12-05 22:34:40 +01:00
|
|
|
for (auto invalidate_interval : ranges) {
|
|
|
|
auto pair = block_ranges.equal_range(invalidate_interval);
|
|
|
|
for (auto it = pair.first; it != pair.second; ++it) {
|
|
|
|
for (const auto& descriptor : it->second) {
|
2018-01-01 23:34:05 +01:00
|
|
|
if (patch_information.count(descriptor)) {
|
2017-12-05 22:34:40 +01:00
|
|
|
Unpatch(descriptor);
|
|
|
|
}
|
2018-01-01 23:34:05 +01:00
|
|
|
block_descriptors.erase(descriptor);
|
2017-02-16 19:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-05 22:34:40 +01:00
|
|
|
block_ranges.erase(pair.first, pair.second);
|
2017-02-16 19:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:01:06 +02:00
|
|
|
} // namespace BackendX64
|
|
|
|
} // namespace Dynarmic
|
2018-01-02 18:45:39 +01:00
|
|
|
|
|
|
|
#include "backend_x64/a32_jitstate.h"
|
|
|
|
|
|
|
|
template class Dynarmic::BackendX64::EmitX64<Dynarmic::BackendX64::A32JitState>;
|