📄 constantmath.java
字号:
} originalResultOperand = (computedResultOperand != null) ? Binary.getResult(inst) : null; } // end simple one constants } // end binary operations // ABS, INV, NOT, SQRT if(Unary.conforms(inst)) { Operand valOperand = Unary.getVal(inst); if (valOperand.isConstant() || valOperand.isBooleanConstant()) { String valString = getIntegralValString(type, valOperand); BigInteger valBigInteger = new BigInteger(valString, 2); // Do the computation now instead of at runtime switch (inst.getOpcode()) { case Operators.ABS_opcode: resultBigInteger = valBigInteger.abs(); //System.out.println("absolute value " + " BigInteger: " + resultBigInteger.toString(2)); break; case Operators.INV_opcode: resultBigInteger = BigInteger.ONE.divide(valBigInteger); break; case Operators.NOT_opcode: resultBigInteger = valBigInteger.not(); break; case Operators.SQRT_opcode: BigSquareRoot bsr = new BigSquareRoot(); resultBigInteger = bsr.get(valBigInteger).toBigInteger(); break; default: break; } originalResultOperand = Unary.getResult(inst); } } // We need to generate result operands if ((originalResultOperand != null) && (computedResultOperand == null)) { if ((type == Type.Sbyte) || (type == Type.Ubyte)) { computedResultOperand = new IntConstantOperand(resultBigInteger.byteValue()); } if ((type == Type.Short) || (type == Type.Ushort)) { computedResultOperand = new IntConstantOperand(resultBigInteger.shortValue()); } if ((type == Type.Int) || (type == Type.Uint)) { computedResultOperand = new IntConstantOperand(resultBigInteger.intValue()); } if ((type == Type.Long) || (type == Type.Ulong)) { computedResultOperand = new LongConstantOperand(resultBigInteger.longValue()); } if (type == Type.Bool) { boolean testBit = resultBigInteger.testBit(0); if (testBit == true) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } } } if (computedResultOperand != null) { System.out.println("Found a math integer instruction with constant operand(s):\n " + inst); subsMap.put(originalResultOperand, computedResultOperand); System.out.println(" op "+originalResultOperand+" -> "+computedResultOperand); instRemovalList.add(inst); return true; } return false; } private boolean computeFloatResult(Instruction inst, Type type, HashMap subsMap, ArrayList instRemovalList) { Operand computedResultOperand = null; Operand originalResultOperand = null; float result = 0; //Examine math instructions to look for constant math if (Binary.conforms(inst)) { Operand val1Operand = Binary.getVal1(inst); Operand val2Operand = Binary.getVal2(inst); if (val1Operand.isConstant() && val2Operand.isConstant()) { float val1 = ((FloatConstantOperand)val1Operand).getValue(); float val2 = ((FloatConstantOperand)val2Operand).getValue(); // Do the computation now instead of at runtime switch (inst.getOpcode()) { case Operators.ADD_opcode: result = val1 + val2; break; case Operators.SUB_opcode: result = val1 - val2; break; case Operators.MUL_opcode: result = val1 * val2; break; case Operators.DIV_opcode: result = val1 / val2; break; case Operators.SETEQ_opcode: if (val1 == val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETNE_opcode: if (val1 != val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETLT_opcode: if (val1 < val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETGT_opcode: if (val1 > val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETLE_opcode: if (val1 <= val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETGE_opcode: if (val1 >= val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; default: break; } originalResultOperand = Binary.getResult(inst); } } // ABS, INV, NOT, SQRT if(Unary.conforms(inst)) { Operand valOperand = Unary.getVal(inst); if (valOperand.isConstant()) { float val = ((FloatConstantOperand)valOperand).getValue(); // Do the computation now instead of at runtime switch (inst.getOpcode()) { case Operators.ABS_opcode: result = Math.abs(val); break; case Operators.INV_opcode: result = 1 /val; break; case Operators.SQRT_opcode: // I don't know if this is correct, will casting make a difference in the result result = (float)(Math.sqrt((double)val)); break; default: break; } originalResultOperand = Unary.getResult(inst); } } // We need to generate result operands if ((originalResultOperand != null) && (computedResultOperand == null)) { computedResultOperand = new FloatConstantOperand(result); } if (computedResultOperand != null) { System.out.println("Found a math floatinstruction with constant operands: " + inst.toString()); subsMap.put(originalResultOperand, computedResultOperand); instRemovalList.add(inst); return true; } return false; } private boolean computeDoubleResult(Instruction inst, Type type, HashMap subsMap, ArrayList instRemovalList) { Operand computedResultOperand = null; Operand originalResultOperand = null; double result = 0; //Examine math instructions to look for constant math if (Binary.conforms(inst)) { Operand val1Operand = Binary.getVal1(inst); Operand val2Operand = Binary.getVal2(inst); if (val1Operand.isConstant() && val2Operand.isConstant()) { double val1 = ((DoubleConstantOperand)val1Operand).getValue(); double val2 = ((DoubleConstantOperand)val2Operand).getValue(); // Do the computation now instead of at runtime switch (inst.getOpcode()) { case Operators.ADD_opcode: result = val1 + val2; break; case Operators.SUB_opcode: result = val1 - val2; break; case Operators.MUL_opcode: result = val1 * val2; break; case Operators.DIV_opcode: result = val1 / val2; break; case Operators.SETEQ_opcode: if (val1 == val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETNE_opcode: if (val1 != val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETLT_opcode: if (val1 < val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETGT_opcode: if (val1 > val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETLE_opcode: if (val1 <= val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; case Operators.SETGE_opcode: if (val1 >= val2) { computedResultOperand = BooleanOperand.TRUE; } else { computedResultOperand = BooleanOperand.FALSE; } break; default: break; } originalResultOperand = Binary.getResult(inst); } } // ABS, INV, NOT, SQRT if(Unary.conforms(inst)) { Operand valOperand = Unary.getVal(inst); if (valOperand.isConstant()) { double val = ((DoubleConstantOperand)valOperand).getValue(); // Do the computation now instead of at runtime switch (inst.getOpcode()) { case Operators.ABS_opcode: result = Math.abs(val); break; case Operators.INV_opcode: result = 1 /val; break; case Operators.SQRT_opcode: result = (Math.sqrt(val)); break; default: break; } originalResultOperand = Unary.getResult(inst); } } // We need to generate result operands if ((originalResultOperand != null) && (computedResultOperand == null)) { computedResultOperand = new DoubleConstantOperand(result); } if (computedResultOperand != null) { System.out.println("Found a math double instruction with constant operands: " + inst.toString()); subsMap.put(originalResultOperand, computedResultOperand); instRemovalList.add(inst); return true; } return false; } // for Integral types only private String getIntegralValString(Type type, Operand valOperand) { String valString = null; if ((type == Type.Long) || (type == Type.Ulong)) { long val = ((LongConstantOperand)valOperand).getValue(); // make sure the string is correctly signed if (type.isSigned() && (val < 0)) { valString = "-" + Long.toBinaryString(-val); } else { valString = Long.toBinaryString(val); } } else if (type.isInteger()) { int val = ((IntConstantOperand)valOperand).getValue(); // make sure the string is correctly signed if (type.isSigned() && (val < 0)) { valString = "-" + Integer.toBinaryString(-val); } else { valString = Integer.toBinaryString(val); } } else if (type == Type.Bool) { if (valOperand == BooleanOperand.TRUE) { valString = "1"; } else { valString = "0"; } } return valString; } public String name() { return "ConstantMath"; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -