Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.vm.instructions.arith.Compare.java

Here you can find all the information about the file org.dgpf.gp.vm.instructions.arith.Compare.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-06-26 08:21:11
 * Original Filename: org.dgpf.gp.vm.instructions.arith.Compare.java
 * Version          : 1.0.1
 * Last modification: 2006-07-15
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */

 
package org.dgpf.gp.vm.instructions.arith;

import org.dgpf.gp.vm.base.IInstructionHandler;
import org.dgpf.gp.vm.base.VM;
import org.dgpf.gp.vm.instructions.BasicCosts;
import org.dgpf.gp.vm.instructions.base.MemInstr2;

/**
 * The comparison operation.
 *
 * @author Thomas Weise
 */

public class Compare extends MemInstr2
  {
/**
 * The serial version uid.
 */

  private static final long serialVersionUID = 1;
  
/**
 * Compare for non-equality.
 */

  public  static  final byte  CMP_NON_EQUAL   = 0;
/**
 * Compare for being smaller.
 */

  public  static  final byte  CMP_LT          = CMP_NON_EQUAL + 1;
/**
 * Compare for being smaller or equal.
 */

  public  static  final byte  CMP_LTE         = CMP_LT        + 1;
/**
 * Compare for equality.
 */

  public  static  final byte  CMP_EQUAL       = CMP_LTE       + 1;
/**
 * Compare for being bigger.
 */

  public  static  final byte  CMP_GT          = CMP_EQUAL     + 1;
/**
 * Compare for being bigger or equal.
 */

  public  static  final byte  CMP_GTE         = CMP_GT        + 1;
/**
 * The internally used comparision modulo.
 */

          static  final int   CMP_MOD         = CMP_GTE       + 1;
  
/**
 * The operator index.
 */

                byte  m_op  ;
    
/**
 * Create a new comparison instruction.
 * @param p_addr_1      The first address.
 * @param p_relative_1  <code>true</code> for relative memory access,
 *                      <code>false</code> for direct access for the first
 *                      address.
 * @param p_addr_2      The second address.
 * @param p_relative_2  <code>true</code> for relative memory access,
 *                      <code>false</code> for direct access for the
 *                      second address.
 * @param p_operator    The operator.
 */

  public  Compare     (final int      p_addr_1,
                       final boolean  p_relative_1,
                       final int      p_addr_2,
                       final boolean  p_relative_2,
                       final byte     p_operator)
    {
    super(p_addr_1, p_relative_1, p_addr_2, p_relative_2);
    this.m_op = p_operator;
    }
  
  
/**
 * Execute this instruction on a virtual machine.
 * @param p_machine The virtual machine to execute on.
 * @return  The cost of the execution of the last instruction.
 */

  @Override
  protected   final double  execute (final VM p_machine)
    {
    int     l_a, l_b;
    boolean l_d;
    
    l_a = this.read_1(p_machine);
    l_b = this.read_2(p_machine);
    
    switch(this.m_op)
      {
      case CMP_NON_EQUAL:  {
                           l_d = (l_a != l_b);
                           break;
                           }
      case CMP_LT:         {
                           l_d = (l_a < l_b);
                           break;
                           }
      case CMP_LTE:        {
                           l_d = (l_a <= l_b);
                           break;
                           }
      case CMP_EQUAL:      {
                           l_d = (l_a == l_b);
                           break;
                           }
      case CMP_GT:         {
                           l_d = (l_a > l_b);
                           break;
                           }
      default:             {
                           l_d = (l_a >= l_b);
                           break;
                           }
      }
    
    p_machine.set_zero_flag(l_d);
    return BasicCosts.CMP_COSTS;
    }
  

/**
 * Store this objects content into a string builder.
 * @param p_sb  The string builder to store stuff into.
 */

  @Override
  public  final void  to_string (final StringBuilder p_sb)
    {    
    p_sb.append("zf = (");
    
    this.mem_1_str(p_sb);
    
    switch(this.m_op)
      {
      case CMP_NON_EQUAL:  {
                           p_sb.append(" != ");
                           break;
                           }
      case CMP_LT:         {
                           p_sb.append(" < ");
                           break;
                           }
      case CMP_LTE:        {
                           p_sb.append(" <= ");
                           break;
                           }
      case CMP_EQUAL:      {
                           p_sb.append(" == ");
                           break;
                           }
      case CMP_GT:         {
                           p_sb.append(" > ");
                           break;
                           }
      default:             {
                           p_sb.append(" >= ");
                           break;
                           }
      }
    
    this.mem_2_str(p_sb);
    p_sb.append(')');
    }
  
    
/**
 * Obtain the comparison operator used by this instruction.
 * @return The comparison operator used by this instruction.
 */

  public  final byte  get_operator()
    {
    return this.m_op;
    }
  

/**
 * Obtain the instruction handler to be used for this sort of instructions.
 * @return The instruction handler to be used for this sort of instructions.
 */

  @Override
  public  final IInstructionHandler get_handler()
    {
    return CompareHandler.INSTANCE;
    }
  
/**
 * Check whether this instruction equals to a specified object, called
 * internally.
 * @param p_o   The object to compare with, which is of the same class as
 *              this instruction.
 * @return  <code>true</code> if and only if this instruction equals
 *          the specified object.
 */

  @Override
  protected boolean does_equal  (final Object p_o)
    {
    return (super.does_equal(p_o) && (((Compare)p_o).m_op == this.m_op));
    }
  

/**
 * Check whether the address parameter of the index <code>p_index</code>
 * is read from or written to.
 * @param p_index The index of the address parameter wanted.
 * @return Or-ed combination of <code>1</code> if this addres is read from,
 *         <code>2</code> if it is written to.
 */

  @Override
  protected final int get_access (final int p_index)
    {
    return (((p_index > 0) && (p_index < 3)) ? 1 : 0);
    }
  

/**
 * Get a string that can be copy-pasted into a java file an that creates
 * this instruction.
 * @param p_b The string builder to add with. 
 */

  @Override
  public final void    to_creation_string (final StringBuilder p_b)
    {
    p_b.append("new Compare(");
    super.to_creation_string(p_b);
    p_b.append(", (byte)");
    p_b.append(this.m_op);
    p_b.append(')');
    }
  }

File Information:

file name:Compare.java
package:org.dgpf.gp.vm.instructions.arith
qualified name:org.dgpf.gp.vm.instructions.arith.Compare.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/vm/instructions/arith/Compare.java
size:7.568 KB (7750 B)
uploaded: 2015-07-22 04:10:56 GMT+0000
last update: 2006-07-16 06:36:38 GMT+0000
last access: 2017-11-21 15:31:18 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-21 15:31:18 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo