From 1f245b6b36e0d5357d4ee5b1bd3dcd8733a320c8 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 13:15:31 +0100 Subject: [PATCH] addition addition_float sum of two positiv integer --- src/addition.c | 37 +++++++++++++++++++++++++++++++++++-- src/addition.h | 2 ++ 2 files changed, 37 insertions(+), 2 deletions(-) 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);