summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNicolas 'Pixel' Noble <pixel@nobis-crew.org>2013-01-21 17:35:46 -0800
committerNicolas 'Pixel' Noble <pixel@nobis-crew.org>2013-01-21 17:35:46 -0800
commitfe2e7f51c8af87e12fa2f4ca86fa6fb6943f0588 (patch)
treea2757ff978161718640415d055ea927fbbd58b2b /tests
parentd2fdc279df461d164860cf08645affca39232355 (diff)
Adding the cosine generator in the test-Handles as a data generator.
Diffstat (limited to 'tests')
-rw-r--r--tests/test-Handles.cc59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/test-Handles.cc b/tests/test-Handles.cc
index 24e92aa..5f09fca 100644
--- a/tests/test-Handles.cc
+++ b/tests/test-Handles.cc
@@ -15,6 +15,65 @@ void ctime_r(const time_t * t, char * str) {
}
#endif
+// this may look weird for a dumb test, but I always
+// struggle to remember this formula, so I'm going
+// to put it here as a "data generator" and then
+// I'll know where to find it if I ever need it.
+
+class DiscreteCos {
+ public:
+ DiscreteCos() { generate(); }
+ static const unsigned int DC_2PI = 2048;
+ static const unsigned int DC_PI = 1024;
+ static const unsigned int DC_PI2 = 512;
+
+ int32_t cos(unsigned int t) {
+ t %= DC_2PI;
+ int32_t r;
+
+ if (t < DC_PI2) {
+ r = m_cosTable[t];
+ } else if (t < DC_PI) {
+ r = -m_cosTable[DC_PI - 1 - t];
+ } else if (t < (DC_PI + DC_PI2)) {
+ r = -m_cosTable[t - DC_PI];
+ } else {
+ r = m_cosTable[DC_2PI - 1 - t];
+ }
+
+ return r;
+ }
+
+ // sin(x) = cos(x - pi / 2)
+ int32_t sin(unsigned int t) {
+ t %= DC_2PI;
+
+ if (t < DC_PI2)
+ return cos(t + DC_2PI - DC_PI2);
+
+ return cos(t - DC_PI2);
+ }
+
+ private:
+ int32_t m_cosTable[512] = {
+ 16777216, // 2^24 * cos(0 * 2pi / 2048)
+ 16777137, // 2^24 * cos(1 * 2pi / 2048) = C = f(1)
+ };
+
+ // f(n) = cos(n * 2pi / 2048)
+ // f(n) = 2 * f(1) * f(n - 1) - f(n - 2)
+ void generate() {
+ int64_t C = m_cosTable[1];
+
+ for (int i = 2; i < 512; i++)
+ m_cosTable[i] = ((C * m_cosTable[i - 1]) >> 23) - m_cosTable[i - 2];
+
+ m_cosTable[511] = 0;
+ }
+};
+
+DiscreteCos dc;
+
using namespace Balau;
void MainTask::Do() {