Skip to content

Commit

Permalink
Develop (#4)
Browse files Browse the repository at this point in the history
* update unit tests
* add example code (tests)
* some refactor
  • Loading branch information
RobTillaart committed Nov 27, 2021
1 parent 5ee4e07 commit ea2e710
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 7 deletions.
133 changes: 133 additions & 0 deletions examples/float16_test_all/float16_test_all.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//
// FILE: float16_test_all.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: test float16
// DATE: 2021-11-27
// URL: https://github.com/RobTillaart/float16
//

// test all values except the NAN
// test_1 takes ~ 2 minutes on UNO @ 115200baud

// https://github.com/RobTillaart/float16/issues/2


#include "float16.h"

float16 f16;

uint32_t start, stop;
uint32_t errors = 0;
float prev;


void setup()
{
while (!Serial);
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("FLOAT16_LIB_VERSION: ");
Serial.println(FLOAT16_LIB_VERSION);
Serial.println("\nStart ");

f16.setDecimals(6);

test_1();
test_2();
}


void loop()
{
}


void test_2()
{
start = millis();
for (uint32_t x = 0x0001; x < 0x7C01; x++)
{
if (x % 16 == 0) Serial.println();
f16.setBinary(x);
float16 f17 = f16.toDouble();
Serial.print(x, HEX);
Serial.print("\t");
Serial.print(f17.toDouble() - f16.toDouble(), 5);
Serial.println();
}
stop = millis();
Serial.println();
Serial.print(" TIME: ");
Serial.println(stop - start);
}


void test_1()
{
// POSITIVE NUMBERS
prev = 0;
errors = 0;
start = millis();
for (uint32_t x = 0x0000; x < 0x7C01; x++)
{
if (x % 16 == 0) Serial.println();
f16.setBinary(x);
Serial.print(x, HEX);
Serial.print('\t');
float current = f16.toDouble();
Serial.print(current, 8);
if (prev > current)
{
Serial.print("\t\tERROR");
errors++;
}
prev = current;
Serial.println();
}
stop = millis();
Serial.println();
Serial.print(" TIME: ");
Serial.println(stop - start);
Serial.print("ERRORS: ");
Serial.println(errors);
Serial.println();
Serial.println();


// NEGATIVE NUMBERS
prev = 0;
errors = 0;
start = millis();
for (uint32_t x = 0x8000; x < 0xFC01; x++)
{
if (x % 16 == 0) Serial.println();
f16.setBinary(x);
Serial.print(x, HEX);
Serial.print('\t');
float current = f16.toDouble();
Serial.print(current, 8);
if (prev < current)
{
Serial.print("\t\tERROR");
errors++;
}
prev = current;
Serial.println();
}
stop = millis();
Serial.println();
Serial.print(" TIME: ");
Serial.println(stop - start);
Serial.print("ERRORS: ");
Serial.println(errors);
Serial.println();
Serial.println();


Serial.println("\ndone");
}



// -- END OF FILE --
61 changes: 59 additions & 2 deletions examples/float16_test_performance/float16_test_performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

uint32_t start, stop;
volatile float f;

volatile bool b;

void setup()
{
Expand All @@ -22,23 +22,80 @@ void setup()
Serial.println(__FILE__);
Serial.print("FLOAT16_LIB_VERSION: ");
Serial.println(FLOAT16_LIB_VERSION);
Serial.println("\nStart ");
Serial.println();

f = random(1000000) * 0.001;

// CONSTRUCTORS
start = micros();
float16 f16(f);
stop = micros();
Serial.print("Constructor: \t");
Serial.println(stop - start);
delay(10);

float16 f17(f + 1);
start = micros();
f16 = f17;
stop = micros();
Serial.print("a = b: \t");
Serial.println(stop - start);
delay(10);

// CONVERSION
start = micros();
f = f16.toDouble();
stop = micros();
Serial.print("toDouble(): \t");
Serial.println(stop - start);
delay(10);
Serial.println();


// COMPARE
f17 = f16.toDouble() + 1;

start = micros();
b = f16 == f17;
stop = micros();
Serial.print("compare == : \t");
Serial.println(stop - start);
delay(10);

start = micros();
b = f16 != f17;
stop = micros();
Serial.print("compare != : \t");
Serial.println(stop - start);
delay(10);

start = micros();
b = f16 < f17;
stop = micros();
Serial.print("compare < : \t");
Serial.println(stop - start);
delay(10);

start = micros();
b = f16 <= f17;
stop = micros();
Serial.print("compare <= : \t");
Serial.println(stop - start);
delay(10);

start = micros();
b = f16 >= f17;
stop = micros();
Serial.print("compare >= : \t");
Serial.println(stop - start);
delay(10);

start = micros();
b = f16 > f17;
stop = micros();
Serial.print("compare > : \t");
Serial.println(stop - start);
delay(10);

Serial.println("\ndone");
}
Expand Down
19 changes: 19 additions & 0 deletions examples/float16_test_performance/performance_0.1.4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// test: UNO
// IDE: 1.8.13
//

FLOAT16_LIB_VERSION: 0.1.4

Constructor: 24
a = b: 4
toDouble(): 396

compare == : 4
compare != : 4
compare < : 784
compare <= : 784
compare >= : 784
compare > : 780

done
74 changes: 74 additions & 0 deletions examples/float16_test_special/float16_test_special.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// FILE: float16_test_special.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: test float16
// DATE: 2021-11-26
// URL: https://github.com/RobTillaart/float16
//

// test special values ...
// https://github.com/RobTillaart/float16/issues/2


#include "float16.h"

uint16_t value[32] =
{
0xFC00, 0xF400, 0xEC00, 0xE400, 0xDC00, 0xD400, 0xCC00, 0xC400,
0xBC00, 0xB400, 0xAC00, 0xA400, 0x9C00, 0x9400, 0x8C00, 0x8400,
0x0400, 0x0C00, 0x1400, 0x1C00, 0x2400, 0x2C00, 0x3400, 0x3C00,
0x4400, 0x4C00, 0x5400, 0x5C00, 0x6400, 0x6C00, 0x7400, 0x7C00
};

float16 f16;


void setup()
{
while (!Serial);
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("FLOAT16_LIB_VERSION: ");
Serial.println(FLOAT16_LIB_VERSION);
Serial.println("\nStart ");

f16.setDecimals(6);

for (int i = 0; i < 32; i++)
{
f16.setBinary(value[i]);
Serial.print(value[i], HEX);
Serial.print("\t");
Serial.print(f16);
Serial.print("\t");
Serial.print(f16.toDouble(), 6);
Serial.print("\t");
Serial.println();
}

Serial.println();
Serial.println();

for (uint32_t x = 1; x < 65535; x *= 4)
{
f16 = x;
Serial.print(f16.getBinary(), HEX);
Serial.print("\t");
Serial.print(f16);
Serial.print("\t");
Serial.print(f16.toDouble(), 6);
Serial.print("\t");
Serial.println();
}

Serial.println("\ndone");
}


void loop()
{
}


// -- END OF FILE --
8 changes: 5 additions & 3 deletions float16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ float float16::f16tof32(uint16_t n) const
#ifdef DEBUG
Serial.println("ZERO");
#endif
return sgn?-0:0;
return sgn ? -0 : 0;
}
// NAN & INF
if (exp == 0x001F)
Expand Down Expand Up @@ -232,7 +232,8 @@ uint16_t float16::f32tof16(float f) const
// rounding
man++;
man >>= 1;
return (sgn ? 0x8000 : 0x0000) | man;
if (sgn) return 0x8000 | man;
return man;
}

// normal
Expand All @@ -243,7 +244,8 @@ uint16_t float16::f32tof16(float f) const
// Serial.print("SGN: "); Serial.println(sgn, BIN);
// Serial.print("EXP: "); Serial.println(exp, BIN);
// Serial.print("MAN: "); Serial.println(man, BIN);
return (sgn ? 0x8000 : 0x0000) | exp | man;
if (sgn) return 0x8000 | exp | man;
return exp | man;
}


Expand Down
Loading

0 comments on commit ea2e710

Please sign in to comment.