From aedf039805edab7fc856bc3f7d2f227178794f25 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 10:06:06 +0100 Subject: [PATCH 01/34] testing: addition floating numbers base case 0+0=0 --- test/test_addition.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 40af2ff..89ddcd2 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -133,4 +133,13 @@ void test_addition_addition_allunsignedinteger(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } +void test_addition_addition_float_0plus0gleich0(void) { + float result; + float expected = 0.0; + + result = addition_float(0.0, 0.0); + + TEST_ASSERT_EQUAL_FLOAT(expected, result); +} + #endif // TEST From 60494752b222d8269c23282296dc305d0318dd54 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 10:45:53 +0100 Subject: [PATCH 02/34] addition read sign out of number --- src/addition.c | 4 ++++ src/addition.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/addition.c b/src/addition.c index 0298801..ca1d9ed 100644 --- a/src/addition.c +++ b/src/addition.c @@ -24,6 +24,10 @@ unsigned int addition(num number1, num number2) { return result; } +unsigned int s(unsigned int a) { + return a >> 31; +} + float addition_float(float a, float b) { return 0.0; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 0898765..80ede14 100644 --- a/src/addition.h +++ b/src/addition.h @@ -7,6 +7,8 @@ void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); unsigned int addition(num a, num b); +unsigned int s(unsigned int a); + float addition_float(float a, float b); #endif // ADDITION_H From f3ee1a219dc5b22da708b105974ab459b0381877 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 10:47:16 +0100 Subject: [PATCH 03/34] testing: addition read sign out of number --- test/test_addition.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 89ddcd2..0d97725 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -133,6 +133,24 @@ void test_addition_addition_allunsignedinteger(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } +void test_addition_signnumber(void) { + unsigned int result[2]; + unsigned int expected[2] = { 0, 1 }; + union { + float f; + unsigned int i; + }a; + + a.f = 1; + result[0] = s(a.i); + + a.f = -1; + result[1] = s(a.i); + + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); +} + void test_addition_addition_float_0plus0gleich0(void) { float result; float expected = 0.0; From 29d0070ef3174db2691175e57a5163d647e66ff2 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 11:46:43 +0100 Subject: [PATCH 04/34] addition read precision of a floating number --- src/addition.c | 5 +++++ src/addition.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/addition.c b/src/addition.c index ca1d9ed..afa0ea4 100644 --- a/src/addition.c +++ b/src/addition.c @@ -28,6 +28,11 @@ unsigned int s(unsigned int a) { return a >> 31; } +unsigned int m(unsigned int a) { + unsigned int num = 8388607; + return (a & num); +} + float addition_float(float a, float b) { return 0.0; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 80ede14..66497e0 100644 --- a/src/addition.h +++ b/src/addition.h @@ -9,6 +9,8 @@ unsigned int addition(num a, num b); unsigned int s(unsigned int a); +unsigned int m(unsigned int a); + float addition_float(float a, float b); #endif // ADDITION_H From 1f296acd8707018872227d7c8ffa22f74291ac1d Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 11:47:57 +0100 Subject: [PATCH 05/34] testing: addition read precision of a floating number --- test/test_addition.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 0d97725..85143ac 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -6,6 +6,11 @@ num carry[1]; +union { + float f; + unsigned int i; +} a[5]; + void setUp(void) { } @@ -151,6 +156,24 @@ void test_addition_signnumber(void) { TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); } +void test_addition_precisionfloatingnumber(void) { + unsigned int result[5]; + a[0].f = 0, a[1].f = -34753168, a[2].f = 75613594, a[3].f = -0.00000044568721, a[4].f = 0.0000004796123; + unsigned int expected[5] = { ( a[0].i << 9) >> 9 , ( a[1].i << 9) >> 9, ( a[2].i << 9) >> 9, ( a[3].i << 9) >> 9, ( a[4].i << 9) >> 9}; + + result[0] = m( a[0].i ); + result[1] = m( a[1].i ); + result[2] = m( a[2].i ); + result[3] = m( a[3].i ); + result[4] = m( a[4].i ); + + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); +} + void test_addition_addition_float_0plus0gleich0(void) { float result; float expected = 0.0; From 03758ed16392f84d049dbcf014995894d88a131a Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 12:09:06 +0100 Subject: [PATCH 06/34] refactoring: rename union in additions test --- test/test_addition.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/test/test_addition.c b/test/test_addition.c index 85143ac..0b3058a 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -7,9 +7,9 @@ num carry[1]; union { - float f; - unsigned int i; -} a[5]; + float floating; + unsigned int integer; +} number[5]; void setUp(void) { @@ -138,19 +138,13 @@ void test_addition_addition_allunsignedinteger(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } -void test_addition_signnumber(void) { +void test_addition_signfloatingnumber(void) { unsigned int result[2]; unsigned int expected[2] = { 0, 1 }; - union { - float f; - unsigned int i; - }a; - - a.f = 1; - result[0] = s(a.i); - - a.f = -1; - result[1] = s(a.i); + number[0].floating = 1, number[1].floating = -1; + + result[0] = s(number[0].integer); + result[1] = s(number[1].integer); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -158,14 +152,14 @@ void test_addition_signnumber(void) { void test_addition_precisionfloatingnumber(void) { unsigned int result[5]; - a[0].f = 0, a[1].f = -34753168, a[2].f = 75613594, a[3].f = -0.00000044568721, a[4].f = 0.0000004796123; - unsigned int expected[5] = { ( a[0].i << 9) >> 9 , ( a[1].i << 9) >> 9, ( a[2].i << 9) >> 9, ( a[3].i << 9) >> 9, ( a[4].i << 9) >> 9}; + number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123; + unsigned int expected[5] = { ( number[0].integer << 9) >> 9 , ( number[1].integer << 9) >> 9, ( number[2].integer << 9) >> 9, ( number[3].integer << 9) >> 9, ( number[4].integer << 9) >> 9}; - result[0] = m( a[0].i ); - result[1] = m( a[1].i ); - result[2] = m( a[2].i ); - result[3] = m( a[3].i ); - result[4] = m( a[4].i ); + result[0] = m( number[0].integer ); + result[1] = m( number[1].integer ); + result[2] = m( number[2].integer ); + result[3] = m( number[3].integer ); + result[4] = m( number[4].integer ); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -176,9 +170,9 @@ void test_addition_precisionfloatingnumber(void) { void test_addition_addition_float_0plus0gleich0(void) { float result; - float expected = 0.0; + float expected = (float) 0.0; - result = addition_float(0.0, 0.0); + result = addition_float( (float) 0.0, (float) 0.0); TEST_ASSERT_EQUAL_FLOAT(expected, result); } From 537ef341b5bebf41159b494f2a7d3edb66542c9d Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 12:27:23 +0100 Subject: [PATCH 07/34] refactoring: rename variables and function due raise readability --- src/addition.c | 22 ++++++++++++++++------ src/addition.h | 8 ++++---- test/test_addition.c | 14 +++++++------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/addition.c b/src/addition.c index afa0ea4..724dd99 100644 --- a/src/addition.c +++ b/src/addition.c @@ -1,12 +1,16 @@ #include "addition.h" #include +// full_adder is an implementation of two bit addition with carry + void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry) { sum[0] = (number1 % 2) ^ (number2 % 2) ^ (carry % 2); nextcarry[0] = ((number1 % 2) & (number2 % 2)) | ((number1 % 2) & (carry % 2)) | ((number2 % 2) & (carry % 2)); } +// addition is an implementation of an 32 bit addition + unsigned int addition(num number1, num number2) { unsigned int result = 0; num sum[1] = {0}; @@ -24,15 +28,21 @@ unsigned int addition(num number1, num number2) { return result; } -unsigned int s(unsigned int a) { - return a >> 31; +// reading sign out of an integer (floating number) + +unsigned int sign(unsigned int number) { + return number >> 31; } -unsigned int m(unsigned int a) { - unsigned int num = 8388607; - return (a & num); +// reading precision out of an integer (floating number) + +unsigned int precision(unsigned int number) { + unsigned int precision = 8388607; + return (number & precision); } -float addition_float(float a, float b) { +// addition of two floating numbers + +float addition_float(float number1, float number2) { return 0.0; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 66497e0..cf09c60 100644 --- a/src/addition.h +++ b/src/addition.h @@ -5,12 +5,12 @@ typedef unsigned int num; void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); -unsigned int addition(num a, num b); +unsigned int addition(num number1, num number2); -unsigned int s(unsigned int a); +unsigned int sign(unsigned int number); -unsigned int m(unsigned int a); +unsigned int precision(unsigned int number); -float addition_float(float a, float b); +float addition_float(float number1, float number2); #endif // ADDITION_H diff --git a/test/test_addition.c b/test/test_addition.c index 0b3058a..2f68711 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -143,8 +143,8 @@ void test_addition_signfloatingnumber(void) { unsigned int expected[2] = { 0, 1 }; number[0].floating = 1, number[1].floating = -1; - result[0] = s(number[0].integer); - result[1] = s(number[1].integer); + result[0] = sign(number[0].integer); + result[1] = sign(number[1].integer); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -155,11 +155,11 @@ void test_addition_precisionfloatingnumber(void) { number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123; unsigned int expected[5] = { ( number[0].integer << 9) >> 9 , ( number[1].integer << 9) >> 9, ( number[2].integer << 9) >> 9, ( number[3].integer << 9) >> 9, ( number[4].integer << 9) >> 9}; - result[0] = m( number[0].integer ); - result[1] = m( number[1].integer ); - result[2] = m( number[2].integer ); - result[3] = m( number[3].integer ); - result[4] = m( number[4].integer ); + result[0] = precision( number[0].integer ); + result[1] = precision( number[1].integer ); + result[2] = precision( number[2].integer ); + result[3] = precision( number[3].integer ); + result[4] = precision( number[4].integer ); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); From 978a20ee7c4dfbaec3d3dadff832c91a85821d8e Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 13:32:24 +0100 Subject: [PATCH 08/34] addition read exponent of a floating number --- src/addition.c | 7 +++++++ src/addition.h | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/addition.c b/src/addition.c index 724dd99..6afc239 100644 --- a/src/addition.c +++ b/src/addition.c @@ -41,6 +41,13 @@ unsigned int precision(unsigned int number) { return (number & precision); } +// reading exponent out of an integer (floating number) + +unsigned int e(unsigned int a) { + unsigned int e = 2139095040; + return (a & e) >> 23; +} + // addition of two floating numbers float addition_float(float number1, float number2) { diff --git a/src/addition.h b/src/addition.h index cf09c60..86dd0a4 100644 --- a/src/addition.h +++ b/src/addition.h @@ -11,6 +11,8 @@ unsigned int sign(unsigned int number); unsigned int precision(unsigned int number); +unsigned int e(unsigned int a); + float addition_float(float number1, float number2); -#endif // ADDITION_H +#endif // ADDITION_H \ No newline at end of file From 78307a0c6b92a34b4656b0172aacb3ccb813e067 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 13:33:14 +0100 Subject: [PATCH 09/34] testing: addition read exponent of a floating number --- test/test_addition.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 2f68711..a6e7e7d 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -168,6 +168,24 @@ void test_addition_precisionfloatingnumber(void) { TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } +void test_addition_exponentfloatingnumber(void) { + unsigned int result[5]; + number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943; + unsigned int expected[5] = { ( number[0].integer << 1) >> 24 , ( number[1].integer << 1) >> 24, ( number[2].integer << 1) >> 24, ( number[3].integer << 1) >> 24, ( number[4].integer << 1) >> 24}; + + result[0] = e( number[0].integer ); + result[1] = e( number[1].integer ); + result[2] = e( number[2].integer ); + result[3] = e( number[3].integer ); + result[4] = e( number[4].integer ); + + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); +} + void test_addition_addition_float_0plus0gleich0(void) { float result; float expected = (float) 0.0; From 677a3b6e9e712a31f04ddb10556ee9f83cfd8b1d Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 14:00:39 +0100 Subject: [PATCH 10/34] addition addition_float base case 0+number=number --- src/addition.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/addition.c b/src/addition.c index 6afc239..fcf652b 100644 --- a/src/addition.c +++ b/src/addition.c @@ -51,5 +51,7 @@ unsigned int e(unsigned int a) { // addition of two floating numbers float addition_float(float number1, float number2) { - return 0.0; + if (number2 == (float) 0.0) return number1; + else if (number1 == (float) 0.0) return number2; + return (float) 0.0; } \ No newline at end of file From feeafc2a743e816034d6b082a8972ed9e9ec1362 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 14:01:33 +0100 Subject: [PATCH 11/34] testing: addition addition_float base case 0+number=number --- test/test_addition.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index a6e7e7d..c130001 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -195,4 +195,22 @@ void test_addition_addition_float_0plus0gleich0(void) { TEST_ASSERT_EQUAL_FLOAT(expected, result); } +void test_addition_addition_float_0plusnumbergleichnumber(void) { + float result[5]; + number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698; + float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating }; + + result[0] = addition_float(number[0].floating, (float) 0.0); + result[1] = addition_float((float) 0.0, number[1].floating); + result[2] = addition_float(number[2].floating, (float) 0.0); + result[3] = addition_float((float) 0.0, number[3].floating); + result[4] = addition_float((float) 0.0, number[4].floating); + + TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]); + TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]); + TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]); + TEST_ASSERT_EQUAL_FLOAT(expected[3], result[3]); + TEST_ASSERT_EQUAL_FLOAT(expected[4], result[4]); +} + #endif // TEST From 9454b905692f6ae656abefa5d5cbab5cf8aa704f Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 14:55:58 +0100 Subject: [PATCH 12/34] addition number output with sign, exponent and precision --- src/addition.c | 6 ++++++ test/test_addition.c | 15 ++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/addition.c b/src/addition.c index fcf652b..c7963bd 100644 --- a/src/addition.c +++ b/src/addition.c @@ -48,6 +48,12 @@ unsigned int e(unsigned int a) { return (a & e) >> 23; } +// writing number out of sign, exponent and precision + +unsigned int o(unsigned int s, unsigned int e, unsigned int p) { + return (s << 31) | (e << 23) | p; +} + // addition of two floating numbers float addition_float(float number1, float number2) { diff --git a/test/test_addition.c b/test/test_addition.c index c130001..5bcf631 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -138,7 +138,8 @@ void test_addition_addition_allunsignedinteger(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } -void test_addition_signfloatingnumber(void) { +void test_addition_signfloatingnumber(void) +{ unsigned int result[2]; unsigned int expected[2] = { 0, 1 }; number[0].floating = 1, number[1].floating = -1; @@ -150,7 +151,8 @@ void test_addition_signfloatingnumber(void) { TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); } -void test_addition_precisionfloatingnumber(void) { +void test_addition_precisionfloatingnumber(void) +{ unsigned int result[5]; number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123; unsigned int expected[5] = { ( number[0].integer << 9) >> 9 , ( number[1].integer << 9) >> 9, ( number[2].integer << 9) >> 9, ( number[3].integer << 9) >> 9, ( number[4].integer << 9) >> 9}; @@ -168,7 +170,8 @@ void test_addition_precisionfloatingnumber(void) { TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } -void test_addition_exponentfloatingnumber(void) { +void test_addition_exponentfloatingnumber(void) +{ unsigned int result[5]; number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943; unsigned int expected[5] = { ( number[0].integer << 1) >> 24 , ( number[1].integer << 1) >> 24, ( number[2].integer << 1) >> 24, ( number[3].integer << 1) >> 24, ( number[4].integer << 1) >> 24}; @@ -186,7 +189,8 @@ void test_addition_exponentfloatingnumber(void) { TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } -void test_addition_addition_float_0plus0gleich0(void) { +void test_addition_addition_float_0plus0gleich0(void) +{ float result; float expected = (float) 0.0; @@ -195,7 +199,8 @@ void test_addition_addition_float_0plus0gleich0(void) { TEST_ASSERT_EQUAL_FLOAT(expected, result); } -void test_addition_addition_float_0plusnumbergleichnumber(void) { +void test_addition_addition_float_0plusnumbergleichnumber(void) +{ float result[5]; number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698; float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating }; From d616d52a76df953849f198ba3c1d3c6b0b893732 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 14:58:08 +0100 Subject: [PATCH 13/34] testing: addition number output with sign, exponent and precision --- src/addition.h | 2 ++ test/test_addition.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/addition.h b/src/addition.h index 86dd0a4..9382434 100644 --- a/src/addition.h +++ b/src/addition.h @@ -13,6 +13,8 @@ unsigned int precision(unsigned int number); unsigned int e(unsigned int a); +unsigned int o(unsigned int s, unsigned int e, unsigned int p); + float addition_float(float number1, float number2); #endif // ADDITION_H \ No newline at end of file diff --git a/test/test_addition.c b/test/test_addition.c index 5bcf631..bc70b6d 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -189,6 +189,30 @@ void test_addition_exponentfloatingnumber(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } +void test_addition_output_numberwithsignexponentprecision(void) +{ + unsigned int result[5]; + number[0].floating = 1468921, number[1].floating = -32468127698, number[2].floating = 0.000000795311468, number[3].floating = -0.00000843214521, number[4].floating = 14; + unsigned int expected[5] = { number[0].integer, number[1].integer, number[2].integer, number[3].integer, number[4].integer }; + + unsigned int s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4; + + s0 = sign(number[0].integer), s1 = sign(number[1].integer), s2 = sign(number[2].integer), s3 = sign(number[3].integer), s4 = sign(number[4].integer); + e0 = e(number[0].integer), e1 = e(number[1].integer), e2 = e(number[2].integer), e3 = e(number[3].integer), e4 = e(number[4].integer); + p0 = precision(number[0].integer), p1 = precision(number[1].integer), p2 = precision(number[2].integer), p3 = precision(number[3].integer), p4 = precision(number[4].integer); + result[0] = o(s0, e0, p0); + result[1] = o(s1, e1, p1); + result[2] = o(s2, e2, p2); + result[3] = o(s3, e3, p3); + result[4] = o(s4, e4, p4); + + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); +} + void test_addition_addition_float_0plus0gleich0(void) { float result; From 78305c0852ed92b3de28c482ac8a0a302c702745 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 08:52:49 +0100 Subject: [PATCH 14/34] refactoring: rename function exponent and output and variables --- src/addition.c | 10 +++++----- src/addition.h | 4 ++-- test/test_addition.c | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/addition.c b/src/addition.c index c7963bd..fd74c3e 100644 --- a/src/addition.c +++ b/src/addition.c @@ -43,15 +43,15 @@ unsigned int precision(unsigned int number) { // reading exponent out of an integer (floating number) -unsigned int e(unsigned int a) { - unsigned int e = 2139095040; - return (a & e) >> 23; +unsigned int exponent(unsigned int number) { + unsigned int exponent = 2139095040; + return (number & exponent) >> 23; } // writing number out of sign, exponent and precision -unsigned int o(unsigned int s, unsigned int e, unsigned int p) { - return (s << 31) | (e << 23) | p; +unsigned int output(unsigned int sign, unsigned int exponent, unsigned int precision) { + return (sign << 31) | (exponent << 23) | precision; } // addition of two floating numbers diff --git a/src/addition.h b/src/addition.h index 9382434..2d2878f 100644 --- a/src/addition.h +++ b/src/addition.h @@ -11,9 +11,9 @@ unsigned int sign(unsigned int number); unsigned int precision(unsigned int number); -unsigned int e(unsigned int a); +unsigned int exponent(unsigned int number); -unsigned int o(unsigned int s, unsigned int e, unsigned int p); +unsigned int output(unsigned int sign, unsigned int exponent, unsigned int precision); float addition_float(float number1, float number2); diff --git a/test/test_addition.c b/test/test_addition.c index bc70b6d..e7ff443 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -176,11 +176,11 @@ void test_addition_exponentfloatingnumber(void) number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943; unsigned int expected[5] = { ( number[0].integer << 1) >> 24 , ( number[1].integer << 1) >> 24, ( number[2].integer << 1) >> 24, ( number[3].integer << 1) >> 24, ( number[4].integer << 1) >> 24}; - result[0] = e( number[0].integer ); - result[1] = e( number[1].integer ); - result[2] = e( number[2].integer ); - result[3] = e( number[3].integer ); - result[4] = e( number[4].integer ); + result[0] = exponent( number[0].integer ); + result[1] = exponent( number[1].integer ); + result[2] = exponent( number[2].integer ); + result[3] = exponent( number[3].integer ); + result[4] = exponent( number[4].integer ); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -198,13 +198,13 @@ void test_addition_output_numberwithsignexponentprecision(void) unsigned int s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4; s0 = sign(number[0].integer), s1 = sign(number[1].integer), s2 = sign(number[2].integer), s3 = sign(number[3].integer), s4 = sign(number[4].integer); - e0 = e(number[0].integer), e1 = e(number[1].integer), e2 = e(number[2].integer), e3 = e(number[3].integer), e4 = e(number[4].integer); + e0 = exponent(number[0].integer), e1 = exponent(number[1].integer), e2 = exponent(number[2].integer), e3 = exponent(number[3].integer), e4 = exponent(number[4].integer); p0 = precision(number[0].integer), p1 = precision(number[1].integer), p2 = precision(number[2].integer), p3 = precision(number[3].integer), p4 = precision(number[4].integer); - result[0] = o(s0, e0, p0); - result[1] = o(s1, e1, p1); - result[2] = o(s2, e2, p2); - result[3] = o(s3, e3, p3); - result[4] = o(s4, e4, p4); + result[0] = output(s0, e0, p0); + result[1] = output(s1, e1, p1); + result[2] = output(s2, e2, p2); + result[3] = output(s3, e3, p3); + result[4] = output(s4, e4, p4); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); From 29a4d6cae70502218c5c07c660cb00c397175832 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 10:28:55 +0100 Subject: [PATCH 15/34] addition addition_precision sum of two precisions --- src/addition.c | 6 ++++++ src/addition.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/addition.c b/src/addition.c index fd74c3e..e311e57 100644 --- a/src/addition.c +++ b/src/addition.c @@ -48,6 +48,12 @@ unsigned int exponent(unsigned int number) { return (number & exponent) >> 23; } +// adding two precision together with sum < 2^24 + +unsigned int addition_precision(unsigned int p1, unsigned int p2) { + return addition(p1, p2); +} + // writing number out of sign, exponent and precision unsigned int output(unsigned int sign, unsigned int exponent, unsigned int precision) { diff --git a/src/addition.h b/src/addition.h index 2d2878f..fcbf305 100644 --- a/src/addition.h +++ b/src/addition.h @@ -13,6 +13,8 @@ unsigned int precision(unsigned int number); unsigned int exponent(unsigned int number); +unsigned int addition_precision(unsigned int p1, unsigned int p2); + unsigned int output(unsigned int sign, unsigned int exponent, unsigned int precision); float addition_float(float number1, float number2); From 3c6dfb4fb6d70ad66fae097158f20acb38c19864 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 10:32:44 +0100 Subject: [PATCH 16/34] testing: addition addition_precision sum of two precisions with sum lower than 2^24 --- test/test_addition.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/test_addition.c b/test/test_addition.c index e7ff443..4f069a5 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -6,10 +6,12 @@ num carry[1]; +unsigned int e = 8388608; + union { float floating; unsigned int integer; -} number[5]; +} number[6]; void setUp(void) { @@ -189,6 +191,26 @@ void test_addition_exponentfloatingnumber(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } +void test_addition_addition_precision_positivnumberlowerthan2tothepowerof24(void) +{ + unsigned int result[2]; + number[0].floating = 652468761.2348, number[1].floating = 2.675312546943, number[2].floating = 321647956212.2146, number[3].floating = 0.00000324567861, number[4].floating = number[0].floating + number[1].floating; number[5].floating = number[2].floating + number[3].floating; + unsigned int expected[2] = { precision(number[4].integer), precision(number[5].integer) }; + + unsigned int e0, e1, e2, e3, p0, p1, p2, p3; + + e0 = exponent(number[0].integer), e1 = exponent(number[1].integer), e2 = exponent(number[2].integer), e3 = exponent(number[3].integer); + p0 = precision(number[0].integer) | e, p1 = precision(number[1].integer) | e, p2 = precision(number[2].integer) | e, p3 = precision(number[3].integer) | e; + + p1 >>= e0 - e1, p3 >>= e2 - e3; + + result[0] = addition_precision( p0, p1 ) ^ e; + result[1] = addition_precision( p2, p3 ) ^ e; + + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); +} + void test_addition_output_numberwithsignexponentprecision(void) { unsigned int result[5]; From 1f245b6b36e0d5357d4ee5b1bd3dcd8733a320c8 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 13:15:31 +0100 Subject: [PATCH 17/34] 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); From 826a0ec5df3d9d9fff9be1d05ca513ef56d729ed Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 13:29:19 +0100 Subject: [PATCH 18/34] testing: addition addition_float sum of two positiv integer --- test/test_addition.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 4f069a5..2edab05 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -264,4 +264,19 @@ void test_addition_addition_float_0plusnumbergleichnumber(void) TEST_ASSERT_EQUAL_FLOAT(expected[4], result[4]); } +void test_addition_addition_float_sumofpositivnumbers(void) +{ + float result[3]; + number[0].floating = 31972543.42176, number[1].floating = 423576.006432054, number[2].floating = 1346.4546124016, number[3].floating = 75620056402.5431, number[4].floating = 0.5351401453104, number[5].floating = 0.469178612; + float expected[3] = { number[0].floating + number[1].floating, number[2].floating + number[3].floating, number[4].floating + number[5].floating }; + + result[0] = addition_float(number[0].floating, number[1].floating); + result[1] = addition_float(number[2].floating, number[3].floating); + result[2] = addition_float(number[4].floating, number[5].floating); + + TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]); + TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]); + TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]); +} + #endif // TEST From b207b058f6989bb553c76c07745d91192ae822bb Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 14:35:43 +0100 Subject: [PATCH 19/34] refactoring: rename variables in addition_float and struct data --- src/addition.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/addition.c b/src/addition.c index f1f626a..b11e2ca 100644 --- a/src/addition.c +++ b/src/addition.c @@ -9,9 +9,9 @@ struct data float floating; unsigned int integer; } number; - unsigned int s; - unsigned int e; - unsigned int p; + unsigned int sign; + unsigned int exponent; + unsigned int precision; }; // full_adder is an implementation of two bit addition with carry @@ -83,20 +83,20 @@ float addition_float(float number1, float number2) { 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); + num1.sign = sign(num1.number.integer), num2.sign = sign(num2.number.integer); + num1.exponent = exponent(num1.number.integer), num2.exponent = exponent(num2.number.integer); + num1.precision = precision(num1.number.integer) | e, num2.precision = precision(num2.number.integer) | e; + + num3.precision = addition_precision(num1.precision, num2.precision >> (num1.exponent - num2.exponent)); + if (num3.precision > 16777215) { + num3.exponent = addition(num1.exponent, 1); + num3.precision = (num3.precision >> 1); } else { - num3.e = num1.e; + num3.exponent = num1.exponent; } - num3.p ^= e; - num3.number.integer = output(num1.s, num3.e, num3.p); + num3.precision ^= e; + num3.number.integer = output(num1.sign, num3.exponent, num3.precision); return num3.number.floating; } \ No newline at end of file From cd70bc3d280c737c6eb7e9997879a1af36d9a5f5 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 16:50:44 +0100 Subject: [PATCH 20/34] refactoring: rename addition functions for float sum --- src/addition.c | 26 +++++++-------- src/addition.h | 12 +++---- test/test_addition.c | 76 ++++++++++++++++++++++---------------------- 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/addition.c b/src/addition.c index b11e2ca..a618655 100644 --- a/src/addition.c +++ b/src/addition.c @@ -24,7 +24,7 @@ void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry) // addition is an implementation of an 32 bit addition -unsigned int addition(num number1, num number2) { +unsigned int addition_uint(num number1, num number2) { unsigned int result = 0; num sum[1] = {0}; num nextcarry[1] = {0}; @@ -43,33 +43,33 @@ unsigned int addition(num number1, num number2) { // reading sign out of an integer (floating number) -unsigned int sign(unsigned int number) { +unsigned int sign_float(unsigned int number) { return number >> 31; } // reading precision out of an integer (floating number) -unsigned int precision(unsigned int number) { +unsigned int precision_float(unsigned int number) { unsigned int precision = 8388607; return (number & precision); } // reading exponent out of an integer (floating number) -unsigned int exponent(unsigned int number) { +unsigned int exponent_float(unsigned int number) { unsigned int exponent = 2139095040; return (number & exponent) >> 23; } // adding two precision together -unsigned int addition_precision(unsigned int p1, unsigned int p2) { - return addition(p1, p2); +unsigned int addition_precision_float(unsigned int p1, unsigned int p2) { + return addition_uint(p1, p2); } // writing number out of sign, exponent and precision -unsigned int output(unsigned int sign, unsigned int exponent, unsigned int precision) { +unsigned int output_float(unsigned int sign, unsigned int exponent, unsigned int precision) { return (sign << 31) | (exponent << 23) | precision; } @@ -83,20 +83,20 @@ float addition_float(float number1, float number2) { unsigned int e = 8388608; struct data num1, num2, num3; num1.number.floating = number1, num2.number.floating = number2; - num1.sign = sign(num1.number.integer), num2.sign = sign(num2.number.integer); - num1.exponent = exponent(num1.number.integer), num2.exponent = exponent(num2.number.integer); - num1.precision = precision(num1.number.integer) | e, num2.precision = precision(num2.number.integer) | e; + num1.sign = sign_float(num1.number.integer), num2.sign = sign_float(num2.number.integer); + num1.exponent = exponent_float(num1.number.integer), num2.exponent = exponent_float(num2.number.integer); + num1.precision = precision_float(num1.number.integer) | e, num2.precision = precision_float(num2.number.integer) | e; - num3.precision = addition_precision(num1.precision, num2.precision >> (num1.exponent - num2.exponent)); + num3.precision = addition_precision_float(num1.precision, num2.precision >> (num1.exponent - num2.exponent)); if (num3.precision > 16777215) { - num3.exponent = addition(num1.exponent, 1); + num3.exponent = addition_uint(num1.exponent, 1); num3.precision = (num3.precision >> 1); } else { num3.exponent = num1.exponent; } num3.precision ^= e; - num3.number.integer = output(num1.sign, num3.exponent, num3.precision); + num3.number.integer = output_float(num1.sign, num3.exponent, num3.precision); return num3.number.floating; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 9a42d6a..86b202c 100644 --- a/src/addition.h +++ b/src/addition.h @@ -7,17 +7,17 @@ struct data; void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); -unsigned int addition(num number1, num number2); +unsigned int addition_uint(num number1, num number2); -unsigned int sign(unsigned int number); +unsigned int sign_float(unsigned int number); -unsigned int precision(unsigned int number); +unsigned int precision_float(unsigned int number); -unsigned int exponent(unsigned int number); +unsigned int exponent_float(unsigned int number); -unsigned int addition_precision(unsigned int p1, unsigned int p2); +unsigned int addition_precision_float(unsigned int p1, unsigned int p2); -unsigned int output(unsigned int sign, unsigned int exponent, unsigned int precision); +unsigned int output_float(unsigned int sign, unsigned int exponent, unsigned int precision); float addition_float(float number1, float number2); diff --git a/test/test_addition.c b/test/test_addition.c index 2edab05..66b799d 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -87,7 +87,7 @@ void test_addition_addition_basecasezeropluszeroequalzero(void) unsigned int result; unsigned int expected = 0; - result = addition(0, 0); + result = addition_uint(0, 0); TEST_ASSERT_EQUAL_UINT(expected, result); } @@ -97,8 +97,8 @@ void test_addition_addition_basecaseonescolumns(void) unsigned int result[2]; unsigned int expected[2] = { 1, 1 }; - result[0] = addition(1, 0); - result[1] = addition(0, 1); + result[0] = addition_uint(1, 0); + result[1] = addition_uint(0, 1); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -109,11 +109,11 @@ void test_addition_addition_basecasetotenscolumns(void) unsigned int result[5]; unsigned int expected[5] = { 3, 3, 2, 3, 3 }; - result[0] = addition(0, 3); - result[1] = addition(1, 2); - result[2] = addition(1, 1); - result[3] = addition(2, 1); - result[4] = addition(3, 0); + result[0] = addition_uint(0, 3); + result[1] = addition_uint(1, 2); + result[2] = addition_uint(1, 1); + result[3] = addition_uint(2, 1); + result[4] = addition_uint(3, 0); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -127,11 +127,11 @@ void test_addition_addition_allunsignedinteger(void) unsigned int result[5]; unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 }; - result[0] = addition(13456275, 75423461); - result[1] = addition(4294967294, 1); - result[2] = addition(1023, 65535); - result[3] = addition(0, 4294967295); - result[4] = addition(54321567,423584312); + result[0] = addition_uint(13456275, 75423461); + result[1] = addition_uint(4294967294, 1); + result[2] = addition_uint(1023, 65535); + result[3] = addition_uint(0, 4294967295); + result[4] = addition_uint(54321567,423584312); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -146,8 +146,8 @@ void test_addition_signfloatingnumber(void) unsigned int expected[2] = { 0, 1 }; number[0].floating = 1, number[1].floating = -1; - result[0] = sign(number[0].integer); - result[1] = sign(number[1].integer); + result[0] = sign_float(number[0].integer); + result[1] = sign_float(number[1].integer); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -159,11 +159,11 @@ void test_addition_precisionfloatingnumber(void) number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123; unsigned int expected[5] = { ( number[0].integer << 9) >> 9 , ( number[1].integer << 9) >> 9, ( number[2].integer << 9) >> 9, ( number[3].integer << 9) >> 9, ( number[4].integer << 9) >> 9}; - result[0] = precision( number[0].integer ); - result[1] = precision( number[1].integer ); - result[2] = precision( number[2].integer ); - result[3] = precision( number[3].integer ); - result[4] = precision( number[4].integer ); + result[0] = precision_float( number[0].integer ); + result[1] = precision_float( number[1].integer ); + result[2] = precision_float( number[2].integer ); + result[3] = precision_float( number[3].integer ); + result[4] = precision_float( number[4].integer ); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -178,11 +178,11 @@ void test_addition_exponentfloatingnumber(void) number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943; unsigned int expected[5] = { ( number[0].integer << 1) >> 24 , ( number[1].integer << 1) >> 24, ( number[2].integer << 1) >> 24, ( number[3].integer << 1) >> 24, ( number[4].integer << 1) >> 24}; - result[0] = exponent( number[0].integer ); - result[1] = exponent( number[1].integer ); - result[2] = exponent( number[2].integer ); - result[3] = exponent( number[3].integer ); - result[4] = exponent( number[4].integer ); + result[0] = exponent_float( number[0].integer ); + result[1] = exponent_float( number[1].integer ); + result[2] = exponent_float( number[2].integer ); + result[3] = exponent_float( number[3].integer ); + result[4] = exponent_float( number[4].integer ); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -195,17 +195,17 @@ void test_addition_addition_precision_positivnumberlowerthan2tothepowerof24(void { unsigned int result[2]; number[0].floating = 652468761.2348, number[1].floating = 2.675312546943, number[2].floating = 321647956212.2146, number[3].floating = 0.00000324567861, number[4].floating = number[0].floating + number[1].floating; number[5].floating = number[2].floating + number[3].floating; - unsigned int expected[2] = { precision(number[4].integer), precision(number[5].integer) }; + unsigned int expected[2] = { precision_float(number[4].integer), precision_float(number[5].integer) }; unsigned int e0, e1, e2, e3, p0, p1, p2, p3; - e0 = exponent(number[0].integer), e1 = exponent(number[1].integer), e2 = exponent(number[2].integer), e3 = exponent(number[3].integer); - p0 = precision(number[0].integer) | e, p1 = precision(number[1].integer) | e, p2 = precision(number[2].integer) | e, p3 = precision(number[3].integer) | e; + e0 = exponent_float(number[0].integer), e1 = exponent_float(number[1].integer), e2 = exponent_float(number[2].integer), e3 = exponent_float(number[3].integer); + p0 = precision_float(number[0].integer) | e, p1 = precision_float(number[1].integer) | e, p2 = precision_float(number[2].integer) | e, p3 = precision_float(number[3].integer) | e; p1 >>= e0 - e1, p3 >>= e2 - e3; - result[0] = addition_precision( p0, p1 ) ^ e; - result[1] = addition_precision( p2, p3 ) ^ e; + result[0] = addition_precision_float( p0, p1 ) ^ e; + result[1] = addition_precision_float( p2, p3 ) ^ e; TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); @@ -219,14 +219,14 @@ void test_addition_output_numberwithsignexponentprecision(void) unsigned int s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4; - s0 = sign(number[0].integer), s1 = sign(number[1].integer), s2 = sign(number[2].integer), s3 = sign(number[3].integer), s4 = sign(number[4].integer); - e0 = exponent(number[0].integer), e1 = exponent(number[1].integer), e2 = exponent(number[2].integer), e3 = exponent(number[3].integer), e4 = exponent(number[4].integer); - p0 = precision(number[0].integer), p1 = precision(number[1].integer), p2 = precision(number[2].integer), p3 = precision(number[3].integer), p4 = precision(number[4].integer); - result[0] = output(s0, e0, p0); - result[1] = output(s1, e1, p1); - result[2] = output(s2, e2, p2); - result[3] = output(s3, e3, p3); - result[4] = output(s4, e4, p4); + s0 = sign_float(number[0].integer), s1 = sign_float(number[1].integer), s2 = sign_float(number[2].integer), s3 = sign_float(number[3].integer), s4 = sign_float(number[4].integer); + e0 = exponent_float(number[0].integer), e1 = exponent_float(number[1].integer), e2 = exponent_float(number[2].integer), e3 = exponent_float(number[3].integer), e4 = exponent_float(number[4].integer); + p0 = precision_float(number[0].integer), p1 = precision_float(number[1].integer), p2 = precision_float(number[2].integer), p3 = precision_float(number[3].integer), p4 = precision_float(number[4].integer); + result[0] = output_float(s0, e0, p0); + result[1] = output_float(s1, e1, p1); + result[2] = output_float(s2, e2, p2); + result[3] = output_float(s3, e3, p3); + result[4] = output_float(s4, e4, p4); TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); From c151871e6c8c3b5d512e771412c95839f20ad635 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 20:22:05 +0100 Subject: [PATCH 21/34] addition reading sign out of double number --- src/addition.c | 15 +++++++++++++++ src/addition.h | 4 ++++ test/test_addition.c | 18 +++++++++--------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/addition.c b/src/addition.c index a618655..0b864a9 100644 --- a/src/addition.c +++ b/src/addition.c @@ -14,6 +14,16 @@ struct data unsigned int precision; }; +struct datad { + union { + double dnum; + unsigned long lnum; + } number; + unsigned long sign; + unsigned long exponent; + unsigned long precision; +}; + // full_adder is an implementation of two bit addition with carry void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry) { @@ -99,4 +109,9 @@ float addition_float(float number1, float number2) { num3.number.integer = output_float(num1.sign, num3.exponent, num3.precision); return num3.number.floating; +} + +unsigned long sign_double(unsigned long a) { + if (a & 0x8000000000000000 == 0x8000000000000000) return 1; + else return 0; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 86b202c..89a0297 100644 --- a/src/addition.h +++ b/src/addition.h @@ -5,6 +5,8 @@ typedef unsigned int num; struct data; +struct datad; + void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); unsigned int addition_uint(num number1, num number2); @@ -21,4 +23,6 @@ unsigned int output_float(unsigned int sign, unsigned int exponent, unsigned int float addition_float(float number1, float number2); +unsigned long sign_double(unsigned long a); + #endif // ADDITION_H \ No newline at end of file diff --git a/test/test_addition.c b/test/test_addition.c index 66b799d..4190d62 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -82,7 +82,7 @@ void test_addition_full_adder_completesumwithcarry(void) TEST_ASSERT_EQUAL_UINT(expectedcarry[4], carryresult[4]); } -void test_addition_addition_basecasezeropluszeroequalzero(void) +void test_addition_addition_uint_basecasezeropluszeroequalzero(void) { unsigned int result; unsigned int expected = 0; @@ -92,7 +92,7 @@ void test_addition_addition_basecasezeropluszeroequalzero(void) TEST_ASSERT_EQUAL_UINT(expected, result); } -void test_addition_addition_basecaseonescolumns(void) +void test_addition_addition_uint_basecaseonescolumns(void) { unsigned int result[2]; unsigned int expected[2] = { 1, 1 }; @@ -104,7 +104,7 @@ void test_addition_addition_basecaseonescolumns(void) TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); } -void test_addition_addition_basecasetotenscolumns(void) +void test_addition_addition_uint_basecasetotenscolumns(void) { unsigned int result[5]; unsigned int expected[5] = { 3, 3, 2, 3, 3 }; @@ -122,7 +122,7 @@ void test_addition_addition_basecasetotenscolumns(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } -void test_addition_addition_allunsignedinteger(void) +void test_addition_addition_uint_allunsignedinteger(void) { unsigned int result[5]; unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 }; @@ -140,7 +140,7 @@ void test_addition_addition_allunsignedinteger(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } -void test_addition_signfloatingnumber(void) +void test_addition_sign_floatingnumber(void) { unsigned int result[2]; unsigned int expected[2] = { 0, 1 }; @@ -153,7 +153,7 @@ void test_addition_signfloatingnumber(void) TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); } -void test_addition_precisionfloatingnumber(void) +void test_addition_precision_floatingnumber(void) { unsigned int result[5]; number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123; @@ -172,7 +172,7 @@ void test_addition_precisionfloatingnumber(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } -void test_addition_exponentfloatingnumber(void) +void test_addition_exponent_floatingnumber(void) { unsigned int result[5]; number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943; @@ -191,7 +191,7 @@ void test_addition_exponentfloatingnumber(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } -void test_addition_addition_precision_positivnumberlowerthan2tothepowerof24(void) +void test_addition_addition_precision_float_positivnumberlowerthan2tothepowerof24(void) { unsigned int result[2]; number[0].floating = 652468761.2348, number[1].floating = 2.675312546943, number[2].floating = 321647956212.2146, number[3].floating = 0.00000324567861, number[4].floating = number[0].floating + number[1].floating; number[5].floating = number[2].floating + number[3].floating; @@ -211,7 +211,7 @@ void test_addition_addition_precision_positivnumberlowerthan2tothepowerof24(void TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); } -void test_addition_output_numberwithsignexponentprecision(void) +void test_addition_output_float_numberwithsignexponentprecision(void) { unsigned int result[5]; number[0].floating = 1468921, number[1].floating = -32468127698, number[2].floating = 0.000000795311468, number[3].floating = -0.00000843214521, number[4].floating = 14; From 5bf98dc8ec4ade869ed808cf1e7c30213c0cf124 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 6 Feb 2024 20:23:32 +0100 Subject: [PATCH 22/34] testing: addition reading sign out of double number --- test/test_addition.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 4190d62..ae4f1e7 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -13,6 +13,11 @@ union { unsigned int integer; } number[6]; +union { + double dnum; + unsigned long lnum; +} numberd[6]; + void setUp(void) { } @@ -279,4 +284,17 @@ void test_addition_addition_float_sumofpositivnumbers(void) TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]); } +void test_addition_sign_double_numbers(void) +{ + unsigned long result[2]; + unsigned long expected[2] = { 1, 0 }; + numberd[0].dnum = -3004683105640520235, numberd[1].dnum = 0.05313546453135; + + result[0] = sign_double(numberd[0].lnum); + result[1] = sign_double(numberd[1].lnum); + + TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]); +} + #endif // TEST From 8f20eaafb841cb090a1b2880118cfa771498b5ca Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 7 Feb 2024 10:13:58 +0100 Subject: [PATCH 23/34] addition reading precision out of double numbers --- src/addition.c | 5 +++++ src/addition.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/addition.c b/src/addition.c index 0b864a9..d43de60 100644 --- a/src/addition.c +++ b/src/addition.c @@ -114,4 +114,9 @@ float addition_float(float number1, float number2) { unsigned long sign_double(unsigned long a) { if (a & 0x8000000000000000 == 0x8000000000000000) return 1; else return 0; +} + +unsigned long precision_double(unsigned long a) { + a &= 0x000fffffffffffff; + return a; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 89a0297..c770bd8 100644 --- a/src/addition.h +++ b/src/addition.h @@ -25,4 +25,6 @@ float addition_float(float number1, float number2); unsigned long sign_double(unsigned long a); +unsigned long precision_double(unsigned long a); + #endif // ADDITION_H \ No newline at end of file From 89056fb86a547451506805156041465a877388d1 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 7 Feb 2024 10:31:01 +0100 Subject: [PATCH 24/34] refactoring: preparing functions for double datatypes --- src/addition.c | 6 +++--- src/addition.h | 4 ++-- test/test_addition.c | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/addition.c b/src/addition.c index d43de60..208161a 100644 --- a/src/addition.c +++ b/src/addition.c @@ -17,7 +17,7 @@ struct data struct datad { union { double dnum; - unsigned long lnum; + unsigned long long lnum; } number; unsigned long sign; unsigned long exponent; @@ -111,12 +111,12 @@ float addition_float(float number1, float number2) { return num3.number.floating; } -unsigned long sign_double(unsigned long a) { +unsigned long long sign_double(unsigned long long a) { if (a & 0x8000000000000000 == 0x8000000000000000) return 1; else return 0; } -unsigned long precision_double(unsigned long a) { +unsigned long long precision_double(unsigned long long a) { a &= 0x000fffffffffffff; return a; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index c770bd8..e02a55a 100644 --- a/src/addition.h +++ b/src/addition.h @@ -23,8 +23,8 @@ unsigned int output_float(unsigned int sign, unsigned int exponent, unsigned int float addition_float(float number1, float number2); -unsigned long sign_double(unsigned long a); +unsigned long long sign_double(unsigned long long a); -unsigned long precision_double(unsigned long a); +unsigned long long precision_double(unsigned long long a); #endif // ADDITION_H \ No newline at end of file diff --git a/test/test_addition.c b/test/test_addition.c index ae4f1e7..ad81d17 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -15,7 +15,7 @@ union { union { double dnum; - unsigned long lnum; + unsigned long long lnum; } numberd[6]; void setUp(void) @@ -286,8 +286,8 @@ void test_addition_addition_float_sumofpositivnumbers(void) void test_addition_sign_double_numbers(void) { - unsigned long result[2]; - unsigned long expected[2] = { 1, 0 }; + unsigned long long result[2]; + unsigned long long expected[2] = { 1, 0 }; numberd[0].dnum = -3004683105640520235, numberd[1].dnum = 0.05313546453135; result[0] = sign_double(numberd[0].lnum); From d0da273ffc387e31f42cf578d86566907edec0ed Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 7 Feb 2024 10:32:47 +0100 Subject: [PATCH 25/34] testing: addition reading precision out of double numbers --- test/test_addition.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index ad81d17..7456ef4 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -297,4 +297,23 @@ void test_addition_sign_double_numbers(void) TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]); } +void test_addition_precision_double_numbers(void) +{ + unsigned long long result[5]; + numberd[0].dnum = 3216752.2348648931012, numberd[1].dnum = -79865431546, numberd[2].dnum = 203, numberd[3].dnum = -0.00460023540056432002, numberd[4].dnum = 0.000000000791340265431213; + unsigned long long expected[5] = { (numberd[0].lnum << 12) >> 12 , (numberd[1].lnum << 12) >> 12, (numberd[2].lnum << 12) >> 12, (numberd[3].lnum << 12) >> 12, (numberd[4].lnum << 12) >> 12}; + + result[0] = precision_double( numberd[0].lnum ); + result[1] = precision_double( numberd[1].lnum ); + result[2] = precision_double( numberd[2].lnum ); + result[3] = precision_double( numberd[3].lnum ); + result[4] = precision_double( numberd[4].lnum ); + + TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT64(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT64(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]); +} + #endif // TEST From 2a6e61d44f94b7aca627f0aa5880ec7cfe3c7a77 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 7 Feb 2024 10:51:47 +0100 Subject: [PATCH 26/34] refactoring: write numbers for bit operartions in hex for better readability --- src/addition.c | 13 ++++++------- test/test_addition.c | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/addition.c b/src/addition.c index 208161a..2db8d35 100644 --- a/src/addition.c +++ b/src/addition.c @@ -60,14 +60,14 @@ unsigned int sign_float(unsigned int number) { // reading precision out of an integer (floating number) unsigned int precision_float(unsigned int number) { - unsigned int precision = 8388607; + unsigned int precision = 0x007fffff; return (number & precision); } // reading exponent out of an integer (floating number) unsigned int exponent_float(unsigned int number) { - unsigned int exponent = 2139095040; + unsigned int exponent = 0x7f800000; return (number & exponent) >> 23; } @@ -90,7 +90,7 @@ float addition_float(float number1, float number2) { else if (number1 == (float) 0.0) return number2; else if (number2 > number1) return addition_float(number2, number1); - unsigned int e = 8388608; + unsigned int e = 0x00800000; struct data num1, num2, num3; num1.number.floating = number1, num2.number.floating = number2; num1.sign = sign_float(num1.number.integer), num2.sign = sign_float(num2.number.integer); @@ -98,7 +98,7 @@ float addition_float(float number1, float number2) { num1.precision = precision_float(num1.number.integer) | e, num2.precision = precision_float(num2.number.integer) | e; num3.precision = addition_precision_float(num1.precision, num2.precision >> (num1.exponent - num2.exponent)); - if (num3.precision > 16777215) { + if (num3.precision > 0x00ffffff) { num3.exponent = addition_uint(num1.exponent, 1); num3.precision = (num3.precision >> 1); } @@ -112,11 +112,10 @@ float addition_float(float number1, float number2) { } unsigned long long sign_double(unsigned long long a) { - if (a & 0x8000000000000000 == 0x8000000000000000) return 1; + if (a & 0x8000000000000000) return 1; else return 0; } unsigned long long precision_double(unsigned long long a) { - a &= 0x000fffffffffffff; - return a; + return a & 0x000fffffffffffff; } \ No newline at end of file diff --git a/test/test_addition.c b/test/test_addition.c index 7456ef4..08b668b 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -6,7 +6,7 @@ num carry[1]; -unsigned int e = 8388608; +unsigned int e = 0x00800000; union { float floating; From 8043dce84fba36ab7695c80203bb8320544dc0ef Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 7 Feb 2024 11:10:30 +0100 Subject: [PATCH 27/34] addition reading exponent out of double numbers --- src/addition.c | 11 +++++++++++ src/addition.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/src/addition.c b/src/addition.c index 2db8d35..6b17097 100644 --- a/src/addition.c +++ b/src/addition.c @@ -111,11 +111,22 @@ float addition_float(float number1, float number2) { return num3.number.floating; } +// reading sign out of an integer (double number) + unsigned long long sign_double(unsigned long long a) { if (a & 0x8000000000000000) return 1; else return 0; } +// reading precision out of an integer (double number) + unsigned long long precision_double(unsigned long long a) { return a & 0x000fffffffffffff; +} + +// reading exponent out of an integer (double number) + +unsigned long long exponent_double(unsigned long long number) { + unsigned long long exponent = 0x7ff0000000000000; + return (number & exponent) >> 52; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index e02a55a..20a8068 100644 --- a/src/addition.h +++ b/src/addition.h @@ -27,4 +27,6 @@ unsigned long long sign_double(unsigned long long a); unsigned long long precision_double(unsigned long long a); +unsigned long long exponent_double(unsigned long long number); + #endif // ADDITION_H \ No newline at end of file From 370f445b219121007e6c4edc1c06f50d46a47509 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 7 Feb 2024 11:11:18 +0100 Subject: [PATCH 28/34] testing: addition reading exponent out of double numbers --- test/test_addition.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 08b668b..876c32f 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -316,4 +316,23 @@ void test_addition_precision_double_numbers(void) TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]); } +void test_addition_exponent_double_numbers(void) +{ + unsigned long long result[5]; + numberd[0].dnum = 830046503435461000, numberd[1].dnum = -906056810308432, numberd[2].dnum = 0.5315020335035034357, numberd[3].dnum = -34, numberd[4].dnum = 94315454312021310; + unsigned long long expected[5] = { (numberd[0].lnum << 1) >> 53 , (numberd[1].lnum << 1) >> 53, (numberd[2].lnum << 1) >> 53, (numberd[3].lnum << 1) >> 53, (numberd[4].lnum << 1) >> 53}; + + result[0] = exponent_double( numberd[0].lnum ); + result[1] = exponent_double( numberd[1].lnum ); + result[2] = exponent_double( numberd[2].lnum ); + result[3] = exponent_double( numberd[3].lnum ); + result[4] = exponent_double( numberd[4].lnum ); + + TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT64(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT64(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]); +} + #endif // TEST From 2ee9c6a7df1c0c764ad3a607b5507113c1c07a3c Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 7 Feb 2024 11:34:51 +0100 Subject: [PATCH 29/34] addition double number output with sign, exponent and precision --- src/addition.c | 8 +++++++- src/addition.h | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/addition.c b/src/addition.c index 6b17097..5dcd167 100644 --- a/src/addition.c +++ b/src/addition.c @@ -77,7 +77,7 @@ unsigned int addition_precision_float(unsigned int p1, unsigned int p2) { return addition_uint(p1, p2); } -// writing number out of sign, exponent and precision +// writing floating number out of sign, exponent and precision unsigned int output_float(unsigned int sign, unsigned int exponent, unsigned int precision) { return (sign << 31) | (exponent << 23) | precision; @@ -129,4 +129,10 @@ unsigned long long precision_double(unsigned long long a) { unsigned long long exponent_double(unsigned long long number) { unsigned long long exponent = 0x7ff0000000000000; return (number & exponent) >> 52; +} + +// writing double number out of sign, exponent and precision + +unsigned long long output_double(unsigned long long sign, unsigned long long exponent, unsigned long long precision) { + return (sign << 63) | (exponent << 52) | precision; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 20a8068..0ed5aec 100644 --- a/src/addition.h +++ b/src/addition.h @@ -29,4 +29,6 @@ unsigned long long precision_double(unsigned long long a); unsigned long long exponent_double(unsigned long long number); +unsigned long long output_double(unsigned long long sign, unsigned long long exponent, unsigned long long precision); + #endif // ADDITION_H \ No newline at end of file From 41d04742b7c05ed4ddd7b808ecd97618ad4bd400 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 7 Feb 2024 11:37:03 +0100 Subject: [PATCH 30/34] testing: addition double number output with sign, exponent and precision --- test/test_addition.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 876c32f..225882a 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -335,4 +335,28 @@ void test_addition_exponent_double_numbers(void) TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]); } +void test_addition_output_double_numberwithsignexponentprecision(void) +{ + unsigned long long result[5]; + numberd[0].dnum = 7, numberd[1].dnum = -0.0000000006432154846123454, numberd[2].dnum = 731202035312050934, numberd[3].dnum = 0.0000009341213541974341, numberd[4].dnum = -9443345434194197313103.1136; + unsigned long long expected[5] = { numberd[0].lnum, numberd[1].lnum, numberd[2].lnum, numberd[3].lnum, numberd[4].lnum }; + + unsigned long long s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4; + + s0 = sign_double(numberd[0].lnum), s1 = sign_double(numberd[1].lnum), s2 = sign_double(numberd[2].lnum), s3 = sign_double(numberd[3].lnum), s4 = sign_double(numberd[4].lnum); + e0 = exponent_double(numberd[0].lnum), e1 = exponent_double(numberd[1].lnum), e2 = exponent_double(numberd[2].lnum), e3 = exponent_double(numberd[3].lnum), e4 = exponent_double(numberd[4].lnum); + p0 = precision_double(numberd[0].lnum), p1 = precision_double(numberd[1].lnum), p2 = precision_double(numberd[2].lnum), p3 = precision_double(numberd[3].lnum), p4 = precision_double(numberd[4].lnum); + result[0] = output_double(s0, e0, p0); + result[1] = output_double(s1, e1, p1); + result[2] = output_double(s2, e2, p2); + result[3] = output_double(s3, e3, p3); + result[4] = output_double(s4, e4, p4); + + TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT64(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT64(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]); +} + #endif // TEST From 80281b1b9004bd700b46f6b5b47c2b6510467566 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 8 Feb 2024 09:01:07 +0100 Subject: [PATCH 31/34] addition sum of two double numbers precisions --- src/addition.c | 17 +++++++++++++---- src/addition.h | 2 ++ test/test_addition.c | 2 ++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/addition.c b/src/addition.c index 5dcd167..e898b9f 100644 --- a/src/addition.c +++ b/src/addition.c @@ -19,9 +19,9 @@ struct datad { double dnum; unsigned long long lnum; } number; - unsigned long sign; - unsigned long exponent; - unsigned long precision; + unsigned long long sign; + unsigned long long exponent; + unsigned long long precision; }; // full_adder is an implementation of two bit addition with carry @@ -97,7 +97,8 @@ float addition_float(float number1, float number2) { num1.exponent = exponent_float(num1.number.integer), num2.exponent = exponent_float(num2.number.integer); num1.precision = precision_float(num1.number.integer) | e, num2.precision = precision_float(num2.number.integer) | e; - num3.precision = addition_precision_float(num1.precision, num2.precision >> (num1.exponent - num2.exponent)); + unsigned int k = (num1.exponent - num2.exponent > 23 ? 24 : num1.exponent - num2.exponent); + num3.precision = addition_precision_float(num1.precision, num2.precision >> k ); if (num3.precision > 0x00ffffff) { num3.exponent = addition_uint(num1.exponent, 1); num3.precision = (num3.precision >> 1); @@ -135,4 +136,12 @@ unsigned long long exponent_double(unsigned long long number) { unsigned long long output_double(unsigned long long sign, unsigned long long exponent, unsigned long long precision) { return (sign << 63) | (exponent << 52) | precision; +} + +// adding two precision together + +unsigned long long addition_precision_double(unsigned long long p1, unsigned long long p2) { + unsigned long long hinten = 0x00000000ffffffff; + unsigned int vornep1 = p1 >> 32, hintenp1 = p1 & hinten, vornep2 = p2 >> 32, hintenp2 = p2 & hinten; + return ((unsigned long long) addition_uint(vornep1, vornep2) << 32) + (unsigned long long) addition_uint(hintenp1, hintenp2); } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 0ed5aec..e04231e 100644 --- a/src/addition.h +++ b/src/addition.h @@ -31,4 +31,6 @@ unsigned long long exponent_double(unsigned long long number); unsigned long long output_double(unsigned long long sign, unsigned long long exponent, unsigned long long precision); +unsigned long long addition_precision_double(unsigned long long p1, unsigned long long p2); + #endif // ADDITION_H \ No newline at end of file diff --git a/test/test_addition.c b/test/test_addition.c index 225882a..642842c 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -8,6 +8,8 @@ num carry[1]; unsigned int e = 0x00800000; +unsigned long long ed = 0x0010000000000000; + union { float floating; unsigned int integer; From 93a7774edb0fa937e9bf301eb508d8f2cf3eb9ec Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 8 Feb 2024 09:02:14 +0100 Subject: [PATCH 32/34] testing: addition sum of two double numbers precisions --- test/test_addition.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 642842c..bbe2788 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -361,4 +361,24 @@ void test_addition_output_double_numberwithsignexponentprecision(void) TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]); } +void test_addition_addition_precision_double_positivnumberlowerthan2tothepowerof53(void) +{ + unsigned long long result[2]; + numberd[0].dnum = 0.0000434534473513646, numberd[1].dnum = 0.0000000036543515354, numberd[2].dnum = 9735105130351.113543, numberd[3].dnum = 78531.13543441354, numberd[4].dnum = numberd[0].dnum + numberd[1].dnum; numberd[5].dnum = numberd[2].dnum + numberd[3].dnum; + unsigned long long expected[2] = { precision_double(numberd[4].lnum), precision_double(numberd[5].lnum) }; + + unsigned long long e0, e1, e2, e3, p0, p1, p2, p3; + + e0 = exponent_double(numberd[0].lnum), e1 = exponent_double(numberd[1].lnum), e2 = exponent_double(numberd[2].lnum), e3 = exponent_double(numberd[3].lnum); + p0 = precision_double(numberd[0].lnum) | ed, p1 = precision_double(numberd[1].lnum) | ed, p2 = precision_double(numberd[2].lnum) | ed, p3 = precision_double(numberd[3].lnum) | ed; + + p1 >>= e0 - e1, p3 >>= e2 - e3; + + result[0] = addition_precision_double( p0, p1 ) ^ ed; + result[1] = addition_precision_double( p2, p3 ) ^ ed; + + TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]); +} + #endif // TEST From ae978dd2442c44d5e55ec105b3af58d68e70a9dd Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 8 Feb 2024 09:03:50 +0100 Subject: [PATCH 33/34] addition sum of two positiv double numbers --- src/addition.c | 29 +++++++++++++++++++++++++++++ src/addition.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/addition.c b/src/addition.c index e898b9f..d8d8c66 100644 --- a/src/addition.c +++ b/src/addition.c @@ -144,4 +144,33 @@ unsigned long long addition_precision_double(unsigned long long p1, unsigned lon unsigned long long hinten = 0x00000000ffffffff; unsigned int vornep1 = p1 >> 32, hintenp1 = p1 & hinten, vornep2 = p2 >> 32, hintenp2 = p2 & hinten; return ((unsigned long long) addition_uint(vornep1, vornep2) << 32) + (unsigned long long) addition_uint(hintenp1, hintenp2); +} + +// addition of two double numbers + +double addition_double(double number1, double number2) { + if (number2 == 0.0) return number1; + else if (number1 == 0.0) return number2; + else if (number2 > number1) return addition_double(number2, number1); + + unsigned long long e = 0x0010000000000000; + struct datad num1, num2, num3; + num1.number.dnum = number1, num2.number.dnum = number2; + num1.sign = sign_double(num1.number.lnum), num2.sign = sign_double(num2.number.lnum); + num1.exponent = exponent_double(num1.number.lnum), num2.exponent = exponent_double(num2.number.lnum); + num1.precision = precision_double(num1.number.lnum) | e, num2.precision = precision_double(num2.number.lnum) | e; + + unsigned int k = (num1.exponent - num2.exponent > 52 ? 53 : num1.exponent - num2.exponent); + num3.precision = addition_precision_double(num1.precision, num2.precision >> k); + if (num3.precision > 0x001fffffffffffff) { + num3.exponent = addition_uint((unsigned int) num1.exponent, 1); + num3.precision = (num3.precision >> 1); + } + else { + num3.exponent = num1.exponent; + } + num3.precision ^= e; + num3.number.lnum = output_double(num1.sign, num3.exponent, num3.precision); + + return num3.number.dnum; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index e04231e..1dca384 100644 --- a/src/addition.h +++ b/src/addition.h @@ -33,4 +33,6 @@ unsigned long long output_double(unsigned long long sign, unsigned long long exp unsigned long long addition_precision_double(unsigned long long p1, unsigned long long p2); +double addition_double(double number1, double number2); + #endif // ADDITION_H \ No newline at end of file From 619fbe293486256493bc029bc101ebf94a537240 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 8 Feb 2024 09:05:33 +0100 Subject: [PATCH 34/34] testing: addition sum of two positiv double numbers --- test/test_addition.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/test_addition.c b/test/test_addition.c index bbe2788..f3d9f4e 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -381,4 +381,19 @@ void test_addition_addition_precision_double_positivnumberlowerthan2tothepowerof TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]); } -#endif // TEST +void test_addition_addition_double_sumofpositivnumbers(void) +{ + double result[3]; + numberd[0].dnum = 93415405440107.45515, numberd[1].dnum = 78634100530331.45341, numberd[2].dnum = 0.00043419445734139434, numberd[3].dnum = 940313051379341035.3739, numberd[4].dnum = 0.000007936136439442513434, numberd[5].dnum = 0.0000000093953351384343102; + double expected[3] = { numberd[0].dnum + numberd[1].dnum, numberd[2].dnum + numberd[3].dnum, numberd[4].dnum + numberd[5].dnum }; + + result[0] = addition_double(numberd[0].dnum, numberd[1].dnum); + result[1] = addition_double(numberd[2].dnum, numberd[3].dnum); + result[2] = addition_double(numberd[4].dnum, numberd[5].dnum); + + TEST_ASSERT_EQUAL_DOUBLE(expected[0], result[0]); + TEST_ASSERT_EQUAL_DOUBLE(expected[1], result[1]); + TEST_ASSERT_EQUAL_DOUBLE(expected[2], result[2]); +} + +#endif // TEST \ No newline at end of file