Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,28 @@ Fixed point (16.16 & 8.8) library for Arduino to replace resource hungry float/d
Implementation based on libfixmath (https://code.google.com/p/libfixmath).
See also http://en.wikipedia.org/wiki/Libfixmath.

Works on AVR (Uno, etc) and ARM (Due, etc) based Arduinos.

PS: The examples may not compile, only Fix16_benchmark was tested and fixed from the original repository.

Benchmarks (Fix16_benchmark.ino example)
```
Arduino Due fix16_t vs double Benchmark (time in micro seconds):
Op double fixpt speed improvement fixpt over double
Mult 372545 97631 381.58%
Div 1335860 203392 656.79%
Add 457899 14830 3087.65%
Sub 456773 37275 1225.41%
Sqrt 14453 2842 508.55%
```

```
Arduino Due fix16_t vs float Benchmark (time in micro seconds):
Op float fixpt speed improvement fixpt over float
Mult 304898 103902 293.45%
Div 640075 206859 309.43%
Add 356009 22301 1596.38%
Sub 361209 39842 906.60%
Sqrt 14750 2662 554.09%
```
Released under MIT License.
42 changes: 7 additions & 35 deletions examples/Fix16_benchmark/Fix16_benchmark.ino
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
#define NO_DOUBLE
#define NO_FIX16

#if !defined(NO_DOUBLE) && !defined(NO_FIX16)
#include <stdio.h>
#endif

#ifndef NO_FIX16
#include <fix16.h>
#endif
#ifndef NO_DOUBLE
#include <math.h>
#endif

#define NUM_RUNS (5)
#define NUM_RUNS 5

#define COMMENT(x) Serial.println(F("\n----" x "----"));

Expand Down Expand Up @@ -81,7 +71,7 @@ template<typename T> void test_divTestcases( void )
T a = testcases[i];
T b = testcases[j];
// We don't require a solution for /0 :)
if (b == T(0)) continue;
if (b == T((int16_t)0)) continue;
f = a / b;
}
}
Expand Down Expand Up @@ -115,20 +105,17 @@ template<typename T> void test_subTestcases( void )
}
}

#ifndef NO_FIX16
void test_sqrtTestcasesFix16( void )
{
unsigned int i;
for (i = 0; i < TESTCASES_COUNT; i++)
{
Fix16 a = testcases[i];
if (a < Fix16(0)) continue;
if (a < (int32_t)0) continue;
f = a.sqrt();
}
}
#endif

#ifndef NO_DOUBLE
void test_sqrtTestcasesDouble( void )
{
unsigned int i;
Expand All @@ -139,7 +126,6 @@ void test_sqrtTestcasesDouble( void )
f = sqrt(a);
}
}
#endif

#define TIMED_EXEC(func,delta,runs) \
{ \
Expand All @@ -151,8 +137,11 @@ void test_sqrtTestcasesDouble( void )

void setup()
{
#if !defined(NO_DOUBLE) && !defined(NO_FIX16)
Serial.begin(115200);
while(!Serial.available()){
Serial.println("Enter any key to begin");
delay(1000);
}

COMMENT("Running testcases for multiplication");
unsigned long time_Fix16Mult, time_doubleMult;
Expand Down Expand Up @@ -193,23 +182,6 @@ void setup()
Serial.print("Sqrt "); Serial.print(time_doubleSqrt); Serial.print("\t"); Serial.print(time_Fix16Sqrt); Serial.print("\t"); Serial.print(incr_Sqrt); Serial.println("%");

COMMENT("Test finished");
#endif

#if defined(NO_DOUBLE) && !defined(NO_FIX16)
test_multTestcases<Fix16>();
test_divTestcases<Fix16>();
test_addTestcases<Fix16>();
test_subTestcases<Fix16>();
test_sqrtTestcasesFix16();
#endif

#if !defined(NO_DOUBLE) && defined(NO_FIX16)
test_multTestcases<double>();
test_divTestcases<double>();
test_addTestcases<double>();
test_subTestcases<double>();
test_sqrtTestcasesDouble();
#endif

while (1) {};
}
Expand Down
18 changes: 14 additions & 4 deletions libfixmath_conf.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
#ifndef _LIBFIXMATH_CONF_H
#define _LIBFIXMATH_CONF_H

#ifdef ARDUINO_ARCH_AVR
// ARDUINO_ARCH_AVR might not be the optimum define to check on,
// but nothing else is available...
#if defined(__arm__)

//#define FIXMATH_NO_64BIT
//#define FIXMATH_OPTIMIZE_8BIT
//#define FIXMATH_NO_CACHE
#define FIXMATH_NO_OVERFLOW
//#define FIXMATH_NO_ROUNDING

#elif defined(__AVR__)

// AVR Architecture is 8bit & limited in memory
#define FIXMATH_NO_64BIT
#define FIXMATH_OPTIMIZE_8BIT
#define FIXMATH_NO_CACHE
//#define FIXMATH_NO_OVERFLOW
#define FIXMATH_NO_OVERFLOW
//#define FIXMATH_NO_ROUNDING

#else
#error Architecture or board not supported.
#endif

#endif