Initial commit

This commit is contained in:
Ahmet Inan 2018-03-02 13:42:20 +01:00
commit 826ee2b085
7 changed files with 98 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.*.swp

5
LICENSE Normal file
View file

@ -0,0 +1,5 @@
Copyright (C) 2018 by Ahmet Inan <inan@aicodix.de>
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.

11
README.md Normal file
View file

@ -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)

48
kahan.hh Normal file
View file

@ -0,0 +1,48 @@
/*
Kahan summation algorithm
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#ifndef KAHAN_HH
#define KAHAN_HH
namespace DSP {
template <typename T>
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 <class I, class T>
T kahan_sum(I begin, I end, T init)
{
Kahan<T> kahan(init);
while (begin != end)
kahan(*begin++);
return kahan();
}
}
#endif

1
tests/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
kahan

13
tests/Makefile Normal file
View file

@ -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

19
tests/kahan.cc Normal file
View file

@ -0,0 +1,19 @@
/*
Test for the Kahan summation algorithm
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#include "kahan.hh"
int main()
{
const double pi = 3.14159265358979323846;
DSP::Kahan<double> 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;
}