Skip to content

Commit ea2e710

Browse files
authored
Develop (#4)
* update unit tests * add example code (tests) * some refactor
1 parent 5ee4e07 commit ea2e710

6 files changed

Lines changed: 311 additions & 7 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//
2+
// FILE: float16_test_all.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: test float16
6+
// DATE: 2021-11-27
7+
// URL: https://github.com/RobTillaart/float16
8+
//
9+
10+
// test all values except the NAN
11+
// test_1 takes ~ 2 minutes on UNO @ 115200baud
12+
13+
// https://github.com/RobTillaart/float16/issues/2
14+
15+
16+
#include "float16.h"
17+
18+
float16 f16;
19+
20+
uint32_t start, stop;
21+
uint32_t errors = 0;
22+
float prev;
23+
24+
25+
void setup()
26+
{
27+
while (!Serial);
28+
Serial.begin(115200);
29+
Serial.println(__FILE__);
30+
Serial.print("FLOAT16_LIB_VERSION: ");
31+
Serial.println(FLOAT16_LIB_VERSION);
32+
Serial.println("\nStart ");
33+
34+
f16.setDecimals(6);
35+
36+
test_1();
37+
test_2();
38+
}
39+
40+
41+
void loop()
42+
{
43+
}
44+
45+
46+
void test_2()
47+
{
48+
start = millis();
49+
for (uint32_t x = 0x0001; x < 0x7C01; x++)
50+
{
51+
if (x % 16 == 0) Serial.println();
52+
f16.setBinary(x);
53+
float16 f17 = f16.toDouble();
54+
Serial.print(x, HEX);
55+
Serial.print("\t");
56+
Serial.print(f17.toDouble() - f16.toDouble(), 5);
57+
Serial.println();
58+
}
59+
stop = millis();
60+
Serial.println();
61+
Serial.print(" TIME: ");
62+
Serial.println(stop - start);
63+
}
64+
65+
66+
void test_1()
67+
{
68+
// POSITIVE NUMBERS
69+
prev = 0;
70+
errors = 0;
71+
start = millis();
72+
for (uint32_t x = 0x0000; x < 0x7C01; x++)
73+
{
74+
if (x % 16 == 0) Serial.println();
75+
f16.setBinary(x);
76+
Serial.print(x, HEX);
77+
Serial.print('\t');
78+
float current = f16.toDouble();
79+
Serial.print(current, 8);
80+
if (prev > current)
81+
{
82+
Serial.print("\t\tERROR");
83+
errors++;
84+
}
85+
prev = current;
86+
Serial.println();
87+
}
88+
stop = millis();
89+
Serial.println();
90+
Serial.print(" TIME: ");
91+
Serial.println(stop - start);
92+
Serial.print("ERRORS: ");
93+
Serial.println(errors);
94+
Serial.println();
95+
Serial.println();
96+
97+
98+
// NEGATIVE NUMBERS
99+
prev = 0;
100+
errors = 0;
101+
start = millis();
102+
for (uint32_t x = 0x8000; x < 0xFC01; x++)
103+
{
104+
if (x % 16 == 0) Serial.println();
105+
f16.setBinary(x);
106+
Serial.print(x, HEX);
107+
Serial.print('\t');
108+
float current = f16.toDouble();
109+
Serial.print(current, 8);
110+
if (prev < current)
111+
{
112+
Serial.print("\t\tERROR");
113+
errors++;
114+
}
115+
prev = current;
116+
Serial.println();
117+
}
118+
stop = millis();
119+
Serial.println();
120+
Serial.print(" TIME: ");
121+
Serial.println(stop - start);
122+
Serial.print("ERRORS: ");
123+
Serial.println(errors);
124+
Serial.println();
125+
Serial.println();
126+
127+
128+
Serial.println("\ndone");
129+
}
130+
131+
132+
133+
// -- END OF FILE --

examples/float16_test_performance/float16_test_performance.ino

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
uint32_t start, stop;
1515
volatile float f;
16-
16+
volatile bool b;
1717

1818
void setup()
1919
{
@@ -22,23 +22,80 @@ void setup()
2222
Serial.println(__FILE__);
2323
Serial.print("FLOAT16_LIB_VERSION: ");
2424
Serial.println(FLOAT16_LIB_VERSION);
25-
Serial.println("\nStart ");
25+
Serial.println();
2626

2727
f = random(1000000) * 0.001;
2828

29+
// CONSTRUCTORS
2930
start = micros();
3031
float16 f16(f);
3132
stop = micros();
3233
Serial.print("Constructor: \t");
3334
Serial.println(stop - start);
3435
delay(10);
3536

37+
float16 f17(f + 1);
38+
start = micros();
39+
f16 = f17;
40+
stop = micros();
41+
Serial.print("a = b: \t");
42+
Serial.println(stop - start);
43+
delay(10);
44+
45+
// CONVERSION
3646
start = micros();
3747
f = f16.toDouble();
3848
stop = micros();
3949
Serial.print("toDouble(): \t");
4050
Serial.println(stop - start);
4151
delay(10);
52+
Serial.println();
53+
54+
55+
// COMPARE
56+
f17 = f16.toDouble() + 1;
57+
58+
start = micros();
59+
b = f16 == f17;
60+
stop = micros();
61+
Serial.print("compare == : \t");
62+
Serial.println(stop - start);
63+
delay(10);
64+
65+
start = micros();
66+
b = f16 != f17;
67+
stop = micros();
68+
Serial.print("compare != : \t");
69+
Serial.println(stop - start);
70+
delay(10);
71+
72+
start = micros();
73+
b = f16 < f17;
74+
stop = micros();
75+
Serial.print("compare < : \t");
76+
Serial.println(stop - start);
77+
delay(10);
78+
79+
start = micros();
80+
b = f16 <= f17;
81+
stop = micros();
82+
Serial.print("compare <= : \t");
83+
Serial.println(stop - start);
84+
delay(10);
85+
86+
start = micros();
87+
b = f16 >= f17;
88+
stop = micros();
89+
Serial.print("compare >= : \t");
90+
Serial.println(stop - start);
91+
delay(10);
92+
93+
start = micros();
94+
b = f16 > f17;
95+
stop = micros();
96+
Serial.print("compare > : \t");
97+
Serial.println(stop - start);
98+
delay(10);
4299

43100
Serial.println("\ndone");
44101
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// test: UNO
3+
// IDE: 1.8.13
4+
//
5+
6+
FLOAT16_LIB_VERSION: 0.1.4
7+
8+
Constructor: 24
9+
a = b: 4
10+
toDouble(): 396
11+
12+
compare == : 4
13+
compare != : 4
14+
compare < : 784
15+
compare <= : 784
16+
compare >= : 784
17+
compare > : 780
18+
19+
done
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// FILE: float16_test_special.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: test float16
6+
// DATE: 2021-11-26
7+
// URL: https://github.com/RobTillaart/float16
8+
//
9+
10+
// test special values ...
11+
// https://github.com/RobTillaart/float16/issues/2
12+
13+
14+
#include "float16.h"
15+
16+
uint16_t value[32] =
17+
{
18+
0xFC00, 0xF400, 0xEC00, 0xE400, 0xDC00, 0xD400, 0xCC00, 0xC400,
19+
0xBC00, 0xB400, 0xAC00, 0xA400, 0x9C00, 0x9400, 0x8C00, 0x8400,
20+
0x0400, 0x0C00, 0x1400, 0x1C00, 0x2400, 0x2C00, 0x3400, 0x3C00,
21+
0x4400, 0x4C00, 0x5400, 0x5C00, 0x6400, 0x6C00, 0x7400, 0x7C00
22+
};
23+
24+
float16 f16;
25+
26+
27+
void setup()
28+
{
29+
while (!Serial);
30+
Serial.begin(115200);
31+
Serial.println(__FILE__);
32+
Serial.print("FLOAT16_LIB_VERSION: ");
33+
Serial.println(FLOAT16_LIB_VERSION);
34+
Serial.println("\nStart ");
35+
36+
f16.setDecimals(6);
37+
38+
for (int i = 0; i < 32; i++)
39+
{
40+
f16.setBinary(value[i]);
41+
Serial.print(value[i], HEX);
42+
Serial.print("\t");
43+
Serial.print(f16);
44+
Serial.print("\t");
45+
Serial.print(f16.toDouble(), 6);
46+
Serial.print("\t");
47+
Serial.println();
48+
}
49+
50+
Serial.println();
51+
Serial.println();
52+
53+
for (uint32_t x = 1; x < 65535; x *= 4)
54+
{
55+
f16 = x;
56+
Serial.print(f16.getBinary(), HEX);
57+
Serial.print("\t");
58+
Serial.print(f16);
59+
Serial.print("\t");
60+
Serial.print(f16.toDouble(), 6);
61+
Serial.print("\t");
62+
Serial.println();
63+
}
64+
65+
Serial.println("\ndone");
66+
}
67+
68+
69+
void loop()
70+
{
71+
}
72+
73+
74+
// -- END OF FILE --

float16.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ float float16::f16tof32(uint16_t n) const
154154
#ifdef DEBUG
155155
Serial.println("ZERO");
156156
#endif
157-
return sgn?-0:0;
157+
return sgn ? -0 : 0;
158158
}
159159
// NAN & INF
160160
if (exp == 0x001F)
@@ -232,7 +232,8 @@ uint16_t float16::f32tof16(float f) const
232232
// rounding
233233
man++;
234234
man >>= 1;
235-
return (sgn ? 0x8000 : 0x0000) | man;
235+
if (sgn) return 0x8000 | man;
236+
return man;
236237
}
237238

238239
// normal
@@ -243,7 +244,8 @@ uint16_t float16::f32tof16(float f) const
243244
// Serial.print("SGN: "); Serial.println(sgn, BIN);
244245
// Serial.print("EXP: "); Serial.println(exp, BIN);
245246
// Serial.print("MAN: "); Serial.println(man, BIN);
246-
return (sgn ? 0x8000 : 0x0000) | exp | man;
247+
if (sgn) return 0x8000 | exp | man;
248+
return exp | man;
247249
}
248250

249251

0 commit comments

Comments
 (0)