From c544e82c43ecb4703929fd678a98318e9438ea7a Mon Sep 17 00:00:00 2001 From: Stanislav Shwartsman Date: Sun, 2 Mar 2014 19:16:13 +0000 Subject: [PATCH] fixed code duplication in BEXTR implementations --- bochs/cpu/bmi32.cc | 17 +++-------------- bochs/cpu/bmi64.cc | 15 ++------------- bochs/cpu/scalar_arith.h | 34 ++++++++++++++++++++++++++++++++++ bochs/cpu/tbm32.cc | 15 ++------------- bochs/cpu/tbm64.cc | 15 ++------------- 5 files changed, 43 insertions(+), 53 deletions(-) diff --git a/bochs/cpu/bmi32.cc b/bochs/cpu/bmi32.cc index e70e1e395..17e518396 100644 --- a/bochs/cpu/bmi32.cc +++ b/bochs/cpu/bmi32.cc @@ -2,7 +2,7 @@ // $Id$ ///////////////////////////////////////////////////////////////////////// // -// Copyright (c) 2011-2013 Stanislav Shwartsman +// Copyright (c) 2011-2014 Stanislav Shwartsman // Written by Stanislav Shwartsman [sshwarts at sourceforge net] // // This library is free software; you can redistribute it and/or @@ -158,20 +158,9 @@ BX_INSF_TYPE BX_CPP_AttrRegparmN(1) BX_CPU_C::BEXTR_GdEdBdR(bxInstruction_c *i) Bit16u control = BX_READ_16BIT_REG(i->src2()); unsigned start = control & 0xff; unsigned len = control >> 8; - Bit32u op1_32 = 0; - - if (start < 32 && len > 0) { - op1_32 = BX_READ_32BIT_REG(i->src1()); - op1_32 >>= start; - - if (len < 32) { - Bit32u extract_mask = (1 << len) - 1; - op1_32 &= extract_mask; - } - } - + + Bit32u op1_32 = bextrd(BX_READ_32BIT_REG(i->src1()), start, len); SET_FLAGS_OSZAPC_LOGIC_32(op1_32); - BX_WRITE_32BIT_REGZ(i->dst(), op1_32); BX_NEXT_INSTR(i); diff --git a/bochs/cpu/bmi64.cc b/bochs/cpu/bmi64.cc index 575832145..ccfe9de1d 100644 --- a/bochs/cpu/bmi64.cc +++ b/bochs/cpu/bmi64.cc @@ -2,7 +2,7 @@ // $Id$ ///////////////////////////////////////////////////////////////////////// // -// Copyright (c) 2011-2013 Stanislav Shwartsman +// Copyright (c) 2011-2014 Stanislav Shwartsman // Written by Stanislav Shwartsman [sshwarts at sourceforge net] // // This library is free software; you can redistribute it and/or @@ -169,20 +169,9 @@ BX_INSF_TYPE BX_CPP_AttrRegparmN(1) BX_CPU_C::BEXTR_GqEqBqR(bxInstruction_c *i) Bit16u control = BX_READ_16BIT_REG(i->src2()); unsigned start = control & 0xff; unsigned len = control >> 8; - Bit64u op1_64 = 0; - - if (start < 64 && len > 0) { - op1_64 = BX_READ_64BIT_REG(i->src1()); - op1_64 >>= start; - - if (len < 64) { - Bit64u extract_mask = (BX_CONST64(1) << len) - 1; - op1_64 &= extract_mask; - } - } + Bit64u op1_64 = bextrq(BX_READ_64BIT_REG(i->src1()), start, len); SET_FLAGS_OSZAPC_LOGIC_64(op1_64); - BX_WRITE_64BIT_REG(i->dst(), op1_64); BX_NEXT_INSTR(i); diff --git a/bochs/cpu/scalar_arith.h b/bochs/cpu/scalar_arith.h index 2c5d6f3c4..05e865c69 100644 --- a/bochs/cpu/scalar_arith.h +++ b/bochs/cpu/scalar_arith.h @@ -141,4 +141,38 @@ BX_CPP_INLINE unsigned popcntq(Bit64u val_64) return count; } +// bit extract + +BX_CPP_INLINE Bit32u bextrd(Bit32u val_32, unsigned start, unsigned len) +{ + Bit32u result = 0; + + if (start < 32 && len > 0) { + result = val_32 >> start; + + if (len < 32) { + Bit32u extract_mask = (1 << len) - 1; + result &= extract_mask; + } + } + + return result; +} + +BX_CPP_INLINE Bit64u bextrq(Bit64u val_64, unsigned start, unsigned len) +{ + Bit64u result = 0; + + if (start < 64 && len > 0) { + result = val_64 >> start; + + if (len < 64) { + Bit64u extract_mask = (BX_CONST64(1) << len) - 1; + result &= extract_mask; + } + } + + return result; +} + #endif diff --git a/bochs/cpu/tbm32.cc b/bochs/cpu/tbm32.cc index 4cec908de..160433076 100644 --- a/bochs/cpu/tbm32.cc +++ b/bochs/cpu/tbm32.cc @@ -2,7 +2,7 @@ // $Id$ ///////////////////////////////////////////////////////////////////////// // -// Copyright (c) 2011-2012 Stanislav Shwartsman +// Copyright (c) 2011-2014 Stanislav Shwartsman // Written by Stanislav Shwartsman [sshwarts at sourceforge net] // // This library is free software; you can redistribute it and/or @@ -32,20 +32,9 @@ BX_INSF_TYPE BX_CPP_AttrRegparmN(1) BX_CPU_C::BEXTR_GdEdIdR(bxInstruction_c *i) Bit16u control = (Bit16u) i->Id(); unsigned start = control & 0xff; unsigned len = control >> 8; - Bit32u op1_32 = 0; - - if (start < 32 && len > 0) { - op1_32 = BX_READ_32BIT_REG(i->src()); - op1_32 >>= start; - - if (len < 32) { - Bit32u extract_mask = (1 << len) - 1; - op1_32 &= extract_mask; - } - } + Bit32u op1_32 = bextrd(BX_READ_32BIT_REG(i->src()), start, len); SET_FLAGS_OSZAPC_LOGIC_32(op1_32); - BX_WRITE_32BIT_REGZ(i->dst(), op1_32); BX_NEXT_INSTR(i); diff --git a/bochs/cpu/tbm64.cc b/bochs/cpu/tbm64.cc index 80a24077e..e108f4de4 100644 --- a/bochs/cpu/tbm64.cc +++ b/bochs/cpu/tbm64.cc @@ -2,7 +2,7 @@ // $Id$ ///////////////////////////////////////////////////////////////////////// // -// Copyright (c) 2011-2012 Stanislav Shwartsman +// Copyright (c) 2011-2014 Stanislav Shwartsman // Written by Stanislav Shwartsman [sshwarts at sourceforge net] // // This library is free software; you can redistribute it and/or @@ -32,20 +32,9 @@ BX_INSF_TYPE BX_CPP_AttrRegparmN(1) BX_CPU_C::BEXTR_GqEqIdR(bxInstruction_c *i) Bit16u control = (Bit16u) i->Id(); unsigned start = control & 0xff; unsigned len = control >> 8; - Bit64u op1_64 = 0; - - if (start < 64 && len > 0) { - op1_64 = BX_READ_64BIT_REG(i->src()); - op1_64 >>= start; - - if (len < 64) { - Bit64u extract_mask = (BX_CONST64(1) << len) - 1; - op1_64 &= extract_mask; - } - } + Bit64u op1_64 = bextrq(BX_READ_64BIT_REG(i->src()), start, len); SET_FLAGS_OSZAPC_LOGIC_64(op1_64); - BX_WRITE_64BIT_REG(i->dst(), op1_64); BX_NEXT_INSTR(i);