summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/math/qa_fxpt.cc
blob: d368e7de82b753bfdab82ec54ae5417c857e2b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* -*- c++ -*- */
/*
 * Copyright 2004,2013 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <qa_fxpt.h>
#include <gnuradio/fxpt.h>
#include <cppunit/TestAssert.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <math.h>

static const float SIN_COS_TOLERANCE = 1e-5;

void
qa_fxpt::t0()
{
  CPPUNIT_ASSERT_DOUBLES_EQUAL(M_PI/2, gr::fxpt::fixed_to_float(0x40000000), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0,    gr::fxpt::fixed_to_float(0x00000000), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL(-M_PI,  gr::fxpt::fixed_to_float(0x80000000), SIN_COS_TOLERANCE);

  if(0) {
    /*
     * These are disabled because of some precision issues.
     *
     * Different compilers seem to have different opinions on whether
     * the calulations are done single or double (or extended)
     * precision.  Any of the answers are fine for our real purpose, but
     * sometimes the answer is off by a few bits at the bottom.
     * Hence, the disabled check.
     */
    CPPUNIT_ASSERT_EQUAL((gr_int32)0x40000000, gr::fxpt::float_to_fixed(M_PI/2));
    CPPUNIT_ASSERT_EQUAL((gr_int32)0,          gr::fxpt::float_to_fixed(0));
    CPPUNIT_ASSERT_EQUAL((gr_int32)0x80000000, gr::fxpt::float_to_fixed(-M_PI));
  }
}

void
qa_fxpt::t1()
{
  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0,           gr::fxpt::sin(0x00000000), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.707106781, gr::fxpt::sin(0x20000000), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL( 1,           gr::fxpt::sin(0x40000000), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.707106781, gr::fxpt::sin(0x60000000), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0,           gr::fxpt::sin(0x7fffffff), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0,           gr::fxpt::sin(0x80000000), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL( 0,           gr::fxpt::sin(0x80000001), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL(-1,           gr::fxpt::sin(-0x40000000), SIN_COS_TOLERANCE);
  CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.707106781, gr::fxpt::sin(-0x20000000), SIN_COS_TOLERANCE);

  for(float p = -M_PI; p < M_PI; p += 2 * M_PI / 3600) {
    float expected = sin(p);
    float actual = gr::fxpt::sin(gr::fxpt::float_to_fixed (p));
    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, SIN_COS_TOLERANCE);
  }
}

void
qa_fxpt::t2()
{
  for(float p = -M_PI; p < M_PI; p += 2 * M_PI / 3600) {
    float expected = cos(p);
    float actual = gr::fxpt::cos(gr::fxpt::float_to_fixed(p));
    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, SIN_COS_TOLERANCE);
  }
}

void
qa_fxpt::t3()
{
  for(float p = -M_PI; p < M_PI; p += 2 * M_PI / 3600) {
    float expected_sin = sin(p);
    float expected_cos = cos(p);
    float actual_sin;
    float actual_cos;
    gr::fxpt::sincos(gr::fxpt::float_to_fixed (p), &actual_sin, &actual_cos);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected_sin, actual_sin, SIN_COS_TOLERANCE);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(expected_cos, actual_cos, SIN_COS_TOLERANCE);
  }
}