commit 826ee2b085dabc68b8f9e3f3b9c61dcfbc03c8a7 Author: Ahmet Inan Date: Fri Mar 2 13:42:20 2018 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a01ee28 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.swp diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5e19f16 --- /dev/null +++ b/LICENSE @@ -0,0 +1,5 @@ +Copyright (C) 2018 by Ahmet Inan + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2bab8c --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +This is a work in progress and a long overdue attempt to bring all our DSP code together and make it reusable for our future projects. + +Before using any of this you should enter the tests directory and execute "make". +This will check if your compiler is able to create binaries that are able to produce correct results when executed. + +What we have included so far: + +### [kahan.hh](kahan.hh) + +The [Kahan summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm) + diff --git a/kahan.hh b/kahan.hh new file mode 100644 index 0000000..a169d38 --- /dev/null +++ b/kahan.hh @@ -0,0 +1,48 @@ +/* +Kahan summation algorithm + +Copyright 2018 Ahmet Inan +*/ + +#ifndef KAHAN_HH +#define KAHAN_HH + +namespace DSP { + +template +class Kahan +{ + T high, low; +public: + Kahan() : high(0), low(0) {} + Kahan(T init) : high(init), low(0) {} +#if __clang__ + [[clang::optnone]] +#elif __GNUC__ + [[gnu::optimize("no-associative-math")]] +#else +#error unsupported compiler +#endif + T operator ()(T input) + { + T tmp = input - low; + T sum = high + tmp; + low = (sum - high) - tmp; + return high = sum; + } + T operator ()() { return high; } +}; + +template +T kahan_sum(I begin, I end, T init) +{ + Kahan kahan(init); + while (begin != end) + kahan(*begin++); + return kahan(); +} + +} + +#endif + diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..c83433c --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +kahan diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..47a7852 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,13 @@ + +CXXFLAGS = -I.. -std=c++11 -W -Wall -O3 -march=native -ffast-math +CXX = clang++ -stdlib=libc++ +#CXX = g++ + +.PHONY: clean test + +test: kahan + ./kahan + +clean: + rm -f kahan + diff --git a/tests/kahan.cc b/tests/kahan.cc new file mode 100644 index 0000000..be4b8c0 --- /dev/null +++ b/tests/kahan.cc @@ -0,0 +1,19 @@ +/* +Test for the Kahan summation algorithm + +Copyright 2018 Ahmet Inan +*/ + +#include "kahan.hh" + +int main() +{ + const double pi = 3.14159265358979323846; + DSP::Kahan kahan(3.0); + for (double i = 2.0; i < 181423.0; i += 4.0) { + kahan(4.0 / (i * (i + 1.0) * (i + 2.0))); + kahan(-4.0 / ((i + 2.0) * (i + 3.0) * (i + 4.0))); + } + return kahan() != pi; +} +