ALU { public String floatAddition (String operand1, String operand2, int eLength, int sLength, int gLength) { if (operand1.substring(1).indexOf("1")==-1){ return "0"+operand2; } if (operand2.substring(1).indexOf("1")==-1){ return "0"+operand1; } boolean operand1IsInfinity = floatIsInfinity(operand1, eLength, sLength); boolean operand2IsInfinity = floatIsInfinity(operand2, eLength, sLength); if ((operand1IsInfinity && operand2IsInfinity) | (operand1.matches(String.format("(1|0)(1{%d})[01]{%d}",eLength,sLength)) || operand2.matches(String.format("(1|0)(1{%d})[01]{%d}",eLength,sLength)))){ return "0"+repeatedString("1", eLength)+repeatedString("0", sLength-1)+"1"; } if (operand1IsInfinity){ return operand1; } if (operand2IsInfinity){ return operand2; } String value1 = "1."+operand1.substring(eLength+1); String value2= "1."+operand2.substring(eLength+1); boolean operand1Denormalized = operand1.substring(1,eLength+1).equals(repeatedString("0", eLength)); boolean operand2Denormalized = operand2.substring(1,eLength+1).equals(repeatedString("0", eLength)); int order1 = Integer.parseInt(integerTrueValue("0"+operand1.substring(1, eLength+1))); int order2 = Integer.parseInt(integerTrueValue("0"+operand2.substring(1, eLength+1))); int biggerOrder = order1; if (operand1Denormalized){ value1 = "0."+value1.split("\\.")[1]; } if (operand2Denormalized){ value2 = "0."+value2.split("\\.")[1]; } int originalValue1Length = value1.length(); int originalValue2Length = value2.length(); if (order1-order2>0){ value2 = movePoint(value2, order2-order1); if (value2.length()>originalValue2Length + gLength){ value2 = value2.substring(0, originalValue2Length + gLength); } }else{ biggerOrder = order2; value1 = movePoint(value1, order1-order2); if (value1.length()>originalValue1Length + gLength){ value2 = value2.substring(0, originalValue1Length + gLength); } } value1 = operand1.charAt(0)+ value1; value2 = operand2.charAt(0)+ value2; if (value1.length()<value2.length()){ value1= value1 + repeatedString("0", value2.length()-value1.length()); }else{ value2 = value2+repeatedString("0", value1.length()-value2.length()); } String totalResult = signedAddition(value1.replace(".", ""), value2.replace(".",""), value1.length()-1); if (!totalResult.substring(2).contains("1")){ return repeatedString("0", sLength+eLength+2); } totalResult = (new StringBuilder(totalResult)).insert(4, ".").toString(); char sign = totalResult.charAt(1); if (totalResult.charAt(totalResult.indexOf(".")-2) == '1'){ biggerOrder++; totalResult = movePoint(totalResult, -1); } while(totalResult.charAt(totalResult.indexOf(".")-1)!='1'){ totalResult = movePoint(totalResult, 1); biggerOrder--; if (biggerOrder==0) break; } if (biggerOrder > Math.pow(2, eLength)-1){ return "1"+sign+repeatedString("1", eLength)+repeatedString("0", sLength); } totalResult = totalResult.split("\\.")[1]; totalResult = totalResult + repeatedString("0", sLength-totalResult.length()); totalResult = totalResult.substring(0, sLength); StringBuilder result = new StringBuilder(); result.append("0"); result.append(sign).append(biggerOrder <= 0 ? repeatedString("0", eLength) : integerRepresentation(String.valueOf(biggerOrder), eLength)); result.append(totalResult); return result.toString(); }  static void main(String[] args); static final boolean floatIsInfinity(String input, int eLength, int sLength); static final String complementLength(String binary, int length); static final String repeatedString(String str, int times); static final String bitOp(String oneBitOperand1, String oneBitOperand2, BinaryOperator<Boolean> op); static final boolean isSignTheSame(String binary1, String binary2); static String movePoint(String operand, int offset); static final char charOp(BinaryOperator<Boolean> op, Character... bits); static final String stringOp(String operand1, String operand2, BinaryOperator<Boolean> op); static final boolean xor(boolean operand1, boolean operand2); static int minBinaryLong(int number); static String integerBinRepresentationInMinLength(String decimalNumber); static String get2sComplement(String binaryNumber, boolean isNegative); String integerRepresentation(String number, int length); String floatRepresentation(String number, int eLength, int sLength); String ieee754(String number, int length); String integerTrueValue(String operand); String floatTrueValue(String operand, int eLength, int sLength); String negation(String operand); String leftShift(String operand, int n); String logRightShift(String operand, int n); String ariRightShift(String operand, int n); String fullAdder(char x, char y, char c); String claAdder(String operand1, String operand2, char c); String oneAdder(String operand); String adder(String operand1, String operand2, char c, int length); String integerAddition(String operand1, String operand2, int length); String integerSubtraction(String operand1, String operand2, int length); String integerMultiplication(String operand1, String operand2, int length); String integerDivision(String operand1, String operand2, int length); String signedAddition(String operand1, String operand2, int length); String floatAddition(String operand1, String operand2, int eLength, int sLength, int gLength); String floatSubtraction(String operand1, String operand2, int eLength, int sLength, int gLength); String floatMultiplication(String operand1, String operand2, int eLength, int sLength); String floatDivision(String operand1, String operand2, int eLength, int sLength); String trueFormDivision(String operand1, String operand2, int length); static final BinaryOperator<Boolean> booleanAnd; static final BinaryOperator<Boolean> booleanOr; static final BinaryOperator<Boolean> booleanXOR; }
@Test public void testFloatAddition() { ALU alu = new ALU(); assertEquals("000111111101110000", alu.floatAddition("00111111010100000", "00111111001000000", 8, 8, 4)); }
@Test public void testFloatAddition1(){ assertEquals("0001011011",ALU.floatAddition("000100110", "001011001", 4, 4, 4)); }
@Test public void testFloatAddition2(){ assertEquals("0001010101",ALU.floatAddition("100101110", "001011001", 4, 4, 4)); }
@Test public void testFloatAddition3(){ assertEquals("0001011100",ALU.floatAddition("000101110", "001011001", 4, 4, 4)); }
@Test public void testFloatAddition4(){ assertEquals("0010001000",ALU.floatAddition("010101010", "110100100", 4, 4, 4)); }
@Test public void testFloatAddition5(){ assertEquals("0111010100",ALU.floatAddition("111010110", "010011101", 4, 4, 4)); }