summaryrefslogtreecommitdiff
path: root/fw/midi-dials/Drivers/CMSIS/DSP/DSP_Lib_TestSuite/Common/src/controller_tests/sin_cos_tests.c
blob: e3887acc6e0feb644fb37640627f5a0bfd8fa7e2 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "jtest.h"
#include "arr_desc.h"
#include "arm_math.h"
#include "ref.h"
#include "type_abbrev.h"
#include "test_templates.h"

/*--------------------------------------------------------------------------------*/
/* Input Data */
/*--------------------------------------------------------------------------------*/

ARR_DESC_DEFINE(float32_t,
                arm_sin_cos_degrees_f32,
                9,
                CURLY(
                    0,
                    17,
                    45,
                    90,
                    180,
                    360,
                    362,
                    -73,
                    -191.111
                      ));

/* The Q31 version of the function maps numbers in the range [-1, 0.9999999]
 * to degrees in the range [-180, 179]*/
ARR_DESC_DEFINE(q31_t,
                arm_sin_cos_degrees_q31,
                6,
                CURLY(
                    0,
                    0x80000000, /* -1 */
                    0x7fffffff, /* 0.99999 */
                    /* Randoms */
                    0xf7badafa,
                    0x285954a1,
                    0xb9d09511
                      ));

/*--------------------------------------------------------------------------------*/
/* Output Variables */
/*--------------------------------------------------------------------------------*/
float32_t sin_val_fut = 0;
float32_t cos_val_fut = 0;
float32_t sin_val_ref = 0;
float32_t cos_val_ref = 0;

/*--------------------------------------------------------------------------------*/
/* Test Definitions */
/*--------------------------------------------------------------------------------*/

#define MAX_DELTA_f32 50.0e-8f
#define ABS(x) ((x) > 0 ? (x) : -(x))

/*
  Function to test correctness of sin_cos output by comparing it with reference library
*/
#define COMPARISON_INTERFACE(type, threshold)                           \
    if ( (ABS((type) sin_val_ref - (type) sin_val_fut) >                 \
         (type) threshold ) ||                                          \
        (ABS((type) cos_val_ref - (type) cos_val_fut) >                 \
         (type) threshold))                                             \
    {                                                                   \
        JTEST_DUMP_STRF("Error: %f %f\n",                               \
                        ABS((type) sin_val_ref - (type) sin_val_fut),   \
                        ABS((type) cos_val_ref - (type) cos_val_fut));  \
        return JTEST_TEST_FAILED;                                       \
    }

/*
  Sine and cosine test function for float32_t input
*/
JTEST_DEFINE_TEST(arm_sin_cos_f32_test, arm_sin_cos_f32)
{
    /* Test function for all input degree values */
    TEMPLATE_DO_ARR_DESC(
        degree_idx, TYPE_FROM_ABBREV(f32),
        degree, arm_sin_cos_degrees_f32
        ,
        /* Display cycle count and run test */
        JTEST_COUNT_CYCLES(
            arm_sin_cos_f32(
                degree,
                (TYPE_FROM_ABBREV(f32) *) &sin_val_fut,
                (TYPE_FROM_ABBREV(f32) *) &cos_val_fut)
        );
        ref_sin_cos_f32(
            degree,
            (TYPE_FROM_ABBREV(f32) *) &sin_val_ref,
            (TYPE_FROM_ABBREV(f32) *) &cos_val_ref);

        /* Test correctness */
        COMPARISON_INTERFACE(
            TYPE_FROM_ABBREV(f32),
            MAX_DELTA_f32));

    return JTEST_TEST_PASSED;
}


/*
  Sine and cosine test function for q31_t input
*/
JTEST_DEFINE_TEST(arm_sin_cos_q31_test,
                  arm_sin_cos_q31)
{
    /* Test function for all input degree values */
    TEMPLATE_DO_ARR_DESC(
        degree_idx, TYPE_FROM_ABBREV(q31),
        degree, arm_sin_cos_degrees_q31
        ,
        /* Display cycle count and run test */
        JTEST_COUNT_CYCLES(
            arm_sin_cos_q31(
                degree,
                (TYPE_FROM_ABBREV(q31) *) &sin_val_fut,
                (TYPE_FROM_ABBREV(q31) *) &cos_val_fut)
        );
        ref_sin_cos_q31(
            degree,
            (TYPE_FROM_ABBREV(q31) *) &sin_val_ref,
            (TYPE_FROM_ABBREV(q31) *) &cos_val_ref);

        /* Convert q31 numbers to float for comparison purposes. */
        ref_q31_t_to_float((TYPE_FROM_ABBREV(q31) *) &sin_val_fut, &sin_val_fut, 1);
        ref_q31_t_to_float((TYPE_FROM_ABBREV(q31) *) &cos_val_fut, &cos_val_fut, 1);
        ref_q31_t_to_float((TYPE_FROM_ABBREV(q31) *) &sin_val_ref, &sin_val_ref, 1);
        ref_q31_t_to_float((TYPE_FROM_ABBREV(q31) *) &cos_val_ref, &cos_val_ref, 1);

        /* Test correctness */
        COMPARISON_INTERFACE(
            TYPE_FROM_ABBREV(f32),
            MAX_DELTA_f32));

    return JTEST_TEST_PASSED;
}

/*--------------------------------------------------------------------------------*/
/* Collect all tests in a group */
/*--------------------------------------------------------------------------------*/

JTEST_DEFINE_GROUP(sin_cos_tests)
{
    /*
      To skip a test, comment it out.
    */
    JTEST_TEST_CALL(arm_sin_cos_f32_test);
    JTEST_TEST_CALL(arm_sin_cos_q31_test);
}