diff --git a/src/addition.c b/src/addition.c index e311e57..f1f626a 100644 --- a/src/addition.c +++ b/src/addition.c @@ -1,6 +1,19 @@ #include "addition.h" #include +// Struct for number in floating and integer + +struct data +{ + union { + float floating; + unsigned int integer; + } number; + unsigned int s; + unsigned int e; + unsigned int p; +}; + // full_adder is an implementation of two bit addition with carry void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry) { @@ -48,7 +61,7 @@ unsigned int exponent(unsigned int number) { return (number & exponent) >> 23; } -// adding two precision together with sum < 2^24 +// adding two precision together unsigned int addition_precision(unsigned int p1, unsigned int p2) { return addition(p1, p2); @@ -65,5 +78,25 @@ unsigned int output(unsigned int sign, unsigned int exponent, unsigned int preci float addition_float(float number1, float number2) { if (number2 == (float) 0.0) return number1; else if (number1 == (float) 0.0) return number2; - return (float) 0.0; + else if (number2 > number1) return addition_float(number2, number1); + + unsigned int e = 8388608; + struct data num1, num2, num3; + num1.number.floating = number1, num2.number.floating = number2; + num1.s = sign(num1.number.integer), num2.s = sign(num2.number.integer); + num1.e = exponent(num1.number.integer), num2.e = exponent(num2.number.integer); + num1.p = precision(num1.number.integer) | e, num2.p = precision(num2.number.integer) | e; + + num3.p = addition_precision(num1.p, num2.p >> (num1.e - num2.e)); + if (num3.p > 16777215) { + num3.e = addition(num1.e, 1); + num3.p = (num3.p >> 1); + } + else { + num3.e = num1.e; + } + num3.p ^= e; + num3.number.integer = output(num1.s, num3.e, num3.p); + + return num3.number.floating; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index fcbf305..9a42d6a 100644 --- a/src/addition.h +++ b/src/addition.h @@ -3,6 +3,8 @@ typedef unsigned int num; +struct data; + void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); unsigned int addition(num number1, num number2);