Browse Source

Merge branch 'addition' into 'master'

Addition

See merge request fdai7848/Linkes-Twix!5
master
fdai7859 11 months ago
parent
commit
dd43948ba1
  1. 153
      src/addition.c
  2. 30
      src/addition.h
  3. 297
      test/test_addition.c

153
src/addition.c

@ -1,13 +1,40 @@
#include "addition.h" #include "addition.h"
#include <stdlib.h> #include <stdlib.h>
// Struct for number in floating and integer
struct data
{
union {
float floating;
unsigned int integer;
} number;
unsigned int sign;
unsigned int exponent;
unsigned int precision;
};
struct datad {
union {
double dnum;
unsigned long long lnum;
} number;
unsigned long long sign;
unsigned long long exponent;
unsigned long 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) { void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry) {
sum[0] = (number1 % 2) ^ (number2 % 2) ^ (carry % 2); sum[0] = (number1 % 2) ^ (number2 % 2) ^ (carry % 2);
nextcarry[0] = ((number1 % 2) & (number2 % 2)) | ((number1 % 2) & (carry % 2)) | ((number2 % 2) & (carry % 2)); nextcarry[0] = ((number1 % 2) & (number2 % 2)) | ((number1 % 2) & (carry % 2)) | ((number2 % 2) & (carry % 2));
} }
unsigned int addition(num number1, num number2) {
// addition is an implementation of an 32 bit addition
unsigned int addition_uint(num number1, num number2) {
unsigned int result = 0; unsigned int result = 0;
num sum[1] = {0}; num sum[1] = {0};
num nextcarry[1] = {0}; num nextcarry[1] = {0};
@ -24,6 +51,126 @@ unsigned int addition(num number1, num number2) {
return result; return result;
} }
float addition_float(float a, float b) {
return 0.0;
// reading sign out of an integer (floating number)
unsigned int sign_float(unsigned int number) {
return number >> 31;
}
// reading precision out of an integer (floating number)
unsigned int precision_float(unsigned int number) {
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 = 0x7f800000;
return (number & exponent) >> 23;
}
// adding two precision together
unsigned int addition_precision_float(unsigned int p1, unsigned int p2) {
return addition_uint(p1, p2);
}
// 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;
}
// addition of two floating numbers
float addition_float(float number1, float number2) {
if (number2 == (float) 0.0) return number1;
else if (number1 == (float) 0.0) return number2;
else if (number2 > number1) return addition_float(number2, number1);
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);
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;
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);
}
else {
num3.exponent = num1.exponent;
}
num3.precision ^= e;
num3.number.integer = output_float(num1.sign, num3.exponent, num3.precision);
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;
}
// 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;
}
// 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);
}
// 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;
} }

30
src/addition.h

@ -3,10 +3,36 @@
typedef unsigned int num; typedef unsigned int num;
struct data;
struct datad;
void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry);
unsigned int addition(num a, num b);
unsigned int addition_uint(num number1, num number2);
unsigned int sign_float(unsigned int number);
unsigned int precision_float(unsigned int number);
unsigned int exponent_float(unsigned int number);
unsigned int addition_precision_float(unsigned int p1, unsigned int p2);
unsigned int output_float(unsigned int sign, unsigned int exponent, unsigned int precision);
float addition_float(float number1, float number2);
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);
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);
float addition_float(float a, float b);
double addition_double(double number1, double number2);
#endif // ADDITION_H #endif // ADDITION_H

297
test/test_addition.c

@ -6,6 +6,20 @@
num carry[1]; num carry[1];
unsigned int e = 0x00800000;
unsigned long long ed = 0x0010000000000000;
union {
float floating;
unsigned int integer;
} number[6];
union {
double dnum;
unsigned long long lnum;
} numberd[6];
void setUp(void) void setUp(void)
{ {
} }
@ -75,38 +89,38 @@ void test_addition_full_adder_completesumwithcarry(void)
TEST_ASSERT_EQUAL_UINT(expectedcarry[4], carryresult[4]); 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 result;
unsigned int expected = 0; unsigned int expected = 0;
result = addition(0, 0);
result = addition_uint(0, 0);
TEST_ASSERT_EQUAL_UINT(expected, result); 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 result[2];
unsigned int expected[2] = { 1, 1 }; 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[0], result[0]);
TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); 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 result[5];
unsigned int expected[5] = { 3, 3, 2, 3, 3 }; 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[0], result[0]);
TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
@ -115,16 +129,111 @@ void test_addition_addition_basecasetotenscolumns(void)
TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); 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 result[5];
unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 }; 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]);
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_sign_floatingnumber(void)
{
unsigned int result[2];
unsigned int expected[2] = { 0, 1 };
number[0].floating = 1, number[1].floating = -1;
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]);
}
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;
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_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]);
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_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;
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_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]);
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_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;
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_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_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]);
}
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;
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_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[0], result[0]);
TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
@ -133,4 +242,158 @@ void test_addition_addition_allunsignedinteger(void)
TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
} }
void test_addition_addition_float_0plus0gleich0(void)
{
float result;
float expected = (float) 0.0;
result = addition_float( (float) 0.0, (float) 0.0);
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]);
}
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]);
}
void test_addition_sign_double_numbers(void)
{
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);
result[1] = sign_double(numberd[1].lnum);
TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]);
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]);
}
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]);
}
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]);
}
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]);
}
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 #endif // TEST
Loading…
Cancel
Save