Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mom1X-0003xK-TJ for qemu-devel@nongnu.org; Fri, 18 Sep 2009 18:33:51 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mom1S-0003ps-CM for qemu-devel@nongnu.org; Fri, 18 Sep 2009 18:33:51 -0400 Received: from [199.232.76.173] (port=38477 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mom1S-0003pf-5G for qemu-devel@nongnu.org; Fri, 18 Sep 2009 18:33:46 -0400 Received: from va3ehsobe002.messaging.microsoft.com ([216.32.180.12]:36491 helo=VA3EHSOBE002.bigfish.com) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_ARCFOUR_MD5:16) (Exim 4.60) (envelope-from ) id 1Mom1R-0003BI-Rj for qemu-devel@nongnu.org; Fri, 18 Sep 2009 18:33:45 -0400 Received: from mail95-va3 (localhost.localdomain [127.0.0.1]) by mail95-va3-R.bigfish.com (Postfix) with ESMTP id ABC727A0145 for ; Fri, 18 Sep 2009 22:33:44 +0000 (UTC) Received: from ausb3extmailp02.amd.com (ausb3extmailp02.amd.com [163.181.251.22]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail95-va3.bigfish.com (Postfix) with ESMTP id E164B7E0055 for ; Fri, 18 Sep 2009 22:33:42 +0000 (UTC) Received: from ausb3twp02.amd.com ([163.181.250.38]) by ausb3extmailp02.amd.com (Switch-3.2.7/Switch-3.2.7) with ESMTP id n8IMXdQI031024 for ; Fri, 18 Sep 2009 17:33:42 -0500 Received: from sausexbh2.amd.com (SAUSEXBH2.amd.com [163.181.22.102]) by ausb3twp02.amd.com (Tumbleweed MailGate 3.7.0) with ESMTP id 2FF68C853A for ; Fri, 18 Sep 2009 17:33:38 -0500 (CDT) From: Andre Przywara Date: Sat, 19 Sep 2009 00:30:46 +0200 Message-ID: <1253313049-25874-2-git-send-email-andre.przywara@amd.com> In-Reply-To: <1253313049-25874-1-git-send-email-andre.przywara@amd.com> References: <1253313049-25874-1-git-send-email-andre.przywara@amd.com> MIME-Version: 1.0 Content-Type: text/plain Subject: [Qemu-devel] [PATCH 1/4] TCG x86: implement lzcnt emulation List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Andre Przywara lzcnt is an AMD Phenom/Barcelona added instruction returning the number of leading zero bits in a word. As this is similar to the "bsr" instruction, reuse the existing code. There need to be some more changes, though, as lzcnt always returns a valid value (in opposite to bsr, which has a special case when the operand is 0). lzcnt is guarded by the ABM CPUID bit (Fn8000_0001:ECX[5]). Signed-off-by: Andre Przywara --- target-i386/helper.h | 2 +- target-i386/op_helper.c | 11 ++++++++--- target-i386/translate.c | 37 +++++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/target-i386/helper.h b/target-i386/helper.h index 68d57b1..38d0708 100644 --- a/target-i386/helper.h +++ b/target-i386/helper.h @@ -191,7 +191,7 @@ DEF_HELPER_2(frstor, void, tl, int) DEF_HELPER_2(fxsave, void, tl, int) DEF_HELPER_2(fxrstor, void, tl, int) DEF_HELPER_1(bsf, tl, tl) -DEF_HELPER_1(bsr, tl, tl) +DEF_HELPER_2(bsr, tl, tl, int) /* MMX/SSE */ diff --git a/target-i386/op_helper.c b/target-i386/op_helper.c index c3f5af6..9ffda36 100644 --- a/target-i386/op_helper.c +++ b/target-i386/op_helper.c @@ -5457,11 +5457,14 @@ target_ulong helper_bsf(target_ulong t0) return count; } -target_ulong helper_bsr(target_ulong t0) +target_ulong helper_bsr(target_ulong t0, int lzcnt) { int count; target_ulong res, mask; - + + if (lzcnt > 0 && t0 == 0) { + return lzcnt; + } res = t0; count = TARGET_LONG_BITS - 1; mask = (target_ulong)1 << (TARGET_LONG_BITS - 1); @@ -5469,10 +5472,12 @@ target_ulong helper_bsr(target_ulong t0) count--; res <<= 1; } + if (lzcnt > 0) { + return lzcnt - 1 - count; + } return count; } - static int compute_all_eflags(void) { return CC_SRC; diff --git a/target-i386/translate.c b/target-i386/translate.c index 335fc08..aaa4492 100644 --- a/target-i386/translate.c +++ b/target-i386/translate.c @@ -6538,22 +6538,35 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start) reg = ((modrm >> 3) & 7) | rex_r; gen_ldst_modrm(s, modrm, ot, OR_TMP0, 0); gen_extu(ot, cpu_T[0]); - label1 = gen_new_label(); - tcg_gen_movi_tl(cpu_cc_dst, 0); t0 = tcg_temp_local_new(); tcg_gen_mov_tl(t0, cpu_T[0]); - tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, label1); - if (b & 1) { - gen_helper_bsr(cpu_T[0], t0); + if ((b & 1) && (prefixes & PREFIX_REPZ) && + (s->cpuid_ext3_features & CPUID_EXT3_ABM)) { + switch(ot) { + case OT_WORD: gen_helper_bsr(cpu_T[0], t0, + tcg_const_i32(16)); break; + case OT_LONG: gen_helper_bsr(cpu_T[0], t0, + tcg_const_i32(32)); break; + case OT_QUAD: gen_helper_bsr(cpu_T[0], t0, + tcg_const_i32(64)); break; + } + gen_op_mov_reg_T0(ot, reg); } else { - gen_helper_bsf(cpu_T[0], t0); + label1 = gen_new_label(); + tcg_gen_movi_tl(cpu_cc_dst, 0); + tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, label1); + if (b & 1) { + gen_helper_bsr(cpu_T[0], t0, tcg_const_i32(0)); + } else { + gen_helper_bsf(cpu_T[0], t0); + } + gen_op_mov_reg_T0(ot, reg); + tcg_gen_movi_tl(cpu_cc_dst, 1); + gen_set_label(label1); + tcg_gen_discard_tl(cpu_cc_src); + s->cc_op = CC_OP_LOGICB + ot; + tcg_temp_free(t0); } - gen_op_mov_reg_T0(ot, reg); - tcg_gen_movi_tl(cpu_cc_dst, 1); - gen_set_label(label1); - tcg_gen_discard_tl(cpu_cc_src); - s->cc_op = CC_OP_LOGICB + ot; - tcg_temp_free(t0); } break; /************************/ -- 1.6.1.3