fixed code duplication in BEXTR implementations

This commit is contained in:
Stanislav Shwartsman 2014-03-02 19:16:13 +00:00
parent bc5af269b7
commit c544e82c43
5 changed files with 43 additions and 53 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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);