summaryrefslogtreecommitdiff
path: root/midi-dials/Drivers/CMSIS/DSP/DSP_Lib_TestSuite/Common/inc/matrix_tests/matrix_templates.h
blob: 98f3552bcd5e798572b7c363023e200e50740c60 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifndef _MATRIX_TEMPLATES_H_
#define _MATRIX_TEMPLATES_H_

/*--------------------------------------------------------------------------------*/
/* Includes */
/*--------------------------------------------------------------------------------*/
#include "test_templates.h"

/*--------------------------------------------------------------------------------*/
/* Group Specific Templates */
/*--------------------------------------------------------------------------------*/

/**
 *  Compare the outputs from the function under test and the reference
 *  function.
 */
#define MATRIX_COMPARE_INTERFACE(output_type, output_content_type)  \
    TEST_ASSERT_BUFFERS_EQUAL(                                      \
        ((output_type *) &matrix_output_ref)->pData,                \
        ((output_type *) &matrix_output_fut)->pData,                \
        ((output_type *) &matrix_output_fut)->numRows *             \
        ((output_type *) &matrix_output_ref)->numCols *             \
        sizeof(output_content_type))

/**
 * Comparison SNR thresholds for the data types used in matrix_tests.
 */
#define MATRIX_SNR_THRESHOLD 120

/**
 *  Compare the outputs from the function under test and the reference
 *  function using SNR.
 */
#define MATRIX_SNR_COMPARE_INTERFACE(output_type, output_content_type)  \
    do                                                                  \
    {                                                                   \
        TEST_CONVERT_AND_ASSERT_SNR(                                    \
            (float32_t *)matrix_output_f32_ref,                         \
            ((output_type *) &matrix_output_ref)->pData,                \
            (float32_t *)matrix_output_f32_fut,                         \
            ((output_type *) &matrix_output_ref)->pData,                \
            ((output_type *) &matrix_output_fut)->numRows *             \
            ((output_type *) &matrix_output_ref)->numCols,              \
            output_content_type,                                        \
            MATRIX_SNR_THRESHOLD                                        \
            );                                                          \
    } while (0)

/**
 *  Compare the outputs from the function under test and the reference
 *  function using SNR. This is special for float64_t
 */
#define MATRIX_DBL_SNR_COMPARE_INTERFACE(output_type)                   \
    do                                                                  \
    {                                                                   \
        TEST_ASSERT_DBL_SNR(                                            \
            (float64_t *)matrix_output_f32_ref,                         \
            (float64_t *)matrix_output_f32_fut,                         \
            ((output_type *) &matrix_output_fut)->numRows *             \
            ((output_type *) &matrix_output_ref)->numCols,              \
            MATRIX_SNR_THRESHOLD                                        \
            );                                                          \
    } while (0)

/*--------------------------------------------------------------------------------*/
/* Input Interfaces */
/*--------------------------------------------------------------------------------*/
/*
 *  General:
 *  Input interfaces provide inputs to functions inside test templates.  They
 *  ONLY provide the inputs.  The output variables should be hard coded.
 *
 *  The input interfaces must have the following format:
 *
 *  ARM_xxx_INPUT_INTERFACE() or
 *  REF_xxx_INPUT_INTERFACE()
 *
 *  The xxx must be lowercase, and is intended to be the indentifying substring
 *  in the function's name.  Acceptable values are 'sub' or 'add' from the
 *  functions arm_add_q31.
 */

#define ARM_mat_add_INPUT_INTERFACE(input_a_ptr, input_b_ptr)    \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_fut)

#define REF_mat_add_INPUT_INTERFACE(input_a_ptr, input_b_ptr)    \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_ref)

#define ARM_mat_cmplx_mult_INPUT_INTERFACE(input_a_ptr, input_b_ptr)    \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_fut)

#define REF_mat_cmplx_mult_INPUT_INTERFACE(input_a_ptr, input_b_ptr)    \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_ref)

#define ARM_mat_inverse_INPUT_INTERFACE(input_ptr)  \
    PAREN(input_ptr, (void *) &matrix_output_fut)

#define REF_mat_inverse_INPUT_INTERFACE(input_ptr)  \
    PAREN(input_ptr, (void *) &matrix_output_ref)

#define ARM_mat_mult_INPUT_INTERFACE(input_a_ptr, input_b_ptr)      \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_fut)

#define REF_mat_mult_INPUT_INTERFACE(input_a_ptr, input_b_ptr)      \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_ref)

#define ARM_mat_mult_fast_INPUT_INTERFACE(input_a_ptr, input_b_ptr) \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_fut)

#define REF_mat_mult_fast_INPUT_INTERFACE(input_a_ptr, input_b_ptr) \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_ref)

#define ARM_mat_sub_INPUT_INTERFACE(input_a_ptr, input_b_ptr)    \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_fut)

#define REF_mat_sub_INPUT_INTERFACE(input_a_ptr, input_b_ptr)    \
    PAREN(input_a_ptr, input_b_ptr, (void *) &matrix_output_ref)

#define ARM_mat_trans_INPUT_INTERFACE(input_ptr)    \
    PAREN(input_ptr, (void *) &matrix_output_fut)

#define REF_mat_trans_INPUT_INTERFACE(input_ptr)    \
    PAREN(input_ptr, (void *) &matrix_output_ref)

/*--------------------------------------------------------------------------------*/
/* Dimension Validation Interfaces */
/*--------------------------------------------------------------------------------*/

#define MATRIX_TEST_VALID_ADDITIVE_DIMENSIONS(input_type,   \
                                              matrix_a_ptr, \
                                              matrix_b_ptr) \
    ((((input_type) (matrix_a_ptr))->numRows ==             \
      ((input_type) (matrix_b_ptr))->numRows) &&            \
     (((input_type) (matrix_a_ptr))->numCols ==             \
      ((input_type) (matrix_b_ptr))->numCols))

#define MATRIX_TEST_VALID_MULTIPLICATIVE_DIMENSIONS(input_type,     \
                                                    matrix_a_ptr,   \
                                                    matrix_b_ptr)   \
    (((input_type) (matrix_a_ptr))->numCols ==                      \
     ((input_type) (matrix_b_ptr))->numRows)

#define MATRIX_TEST_VALID_SQUARE_DIMENSIONS(input_type, \
                                            matrix_ptr) \
    (((input_type)(matrix_ptr))->numRows ==             \
     ((input_type)(matrix_ptr))->numCols)

#define MATRIX_TEST_VALID_DIMENSIONS_ALWAYS(input_type, \
                                            matrix_ptr) \
    (1 == 1)                                            \

/*--------------------------------------------------------------------------------*/
/* Output Configuration Interfaces */
/*--------------------------------------------------------------------------------*/
/* The matrix tests assume the output matrix is always the correct size.  These
 * interfaces size the properly size the output matrices according to the input
 * matrices and the operation at hand.*/

#define MATRIX_TEST_CONFIG_ADDITIVE_OUTPUT(input_type,      \
                                           matrix_a_ptr,    \
                                           matrix_b_ptr)    \
    do                                                      \
    {                                                       \
        ((input_type) &matrix_output_fut)->numRows =        \
            ((input_type)(matrix_a_ptr))->numRows;          \
        ((input_type) &matrix_output_fut)->numCols =        \
            ((input_type)(matrix_a_ptr))->numCols;          \
        ((input_type) &matrix_output_ref)->numRows =        \
            ((input_type)(matrix_a_ptr))->numRows;          \
        ((input_type) &matrix_output_ref)->numCols =        \
            ((input_type)(matrix_a_ptr))->numCols;          \
    } while (0)

#define MATRIX_TEST_CONFIG_MULTIPLICATIVE_OUTPUT(input_type,    \
                                                 matrix_a_ptr,  \
                                                 matrix_b_ptr)  \
    do                                                          \
    {                                                           \
        ((input_type) &matrix_output_fut)->numRows =            \
            ((input_type)(matrix_a_ptr))->numRows;              \
        ((input_type) &matrix_output_fut)->numCols =            \
            ((input_type)(matrix_b_ptr))->numCols;              \
        ((input_type) &matrix_output_ref)->numRows =            \
            ((input_type)(matrix_a_ptr))->numRows;              \
        ((input_type) &matrix_output_ref)->numCols =            \
            ((input_type)(matrix_b_ptr))->numCols;              \
    } while (0)

#define MATRIX_TEST_CONFIG_SAMESIZE_OUTPUT(input_type,  \
                                           matrix_ptr)  \
    do                                                  \
    {                                                   \
        ((input_type) &matrix_output_fut)->numRows =    \
            ((input_type)(matrix_ptr))->numRows;        \
        ((input_type) &matrix_output_fut)->numCols =    \
            ((input_type)(matrix_ptr))->numCols;        \
        ((input_type) &matrix_output_ref)->numRows =    \
            ((input_type)(matrix_ptr))->numRows;        \
        ((input_type) &matrix_output_ref)->numCols =    \
            ((input_type)(matrix_ptr))->numCols;        \
    } while (0)

#define MATRIX_TEST_CONFIG_TRANSPOSE_OUTPUT(input_type,     \
                                            matrix_ptr)     \
        do                                                  \
        {                                                   \
            ((input_type) &matrix_output_fut)->numRows =    \
                ((input_type)(matrix_ptr))->numCols;        \
            ((input_type) &matrix_output_fut)->numCols =    \
                ((input_type)(matrix_ptr))->numRows;        \
            ((input_type) &matrix_output_ref)->numRows =    \
                ((input_type)(matrix_ptr))->numCols;        \
            ((input_type) &matrix_output_ref)->numCols =    \
                ((input_type)(matrix_ptr))->numRows;        \
        } while (0)

/*--------------------------------------------------------------------------------*/
/* TEST Templates */
/*--------------------------------------------------------------------------------*/

#define MATRIX_TEST_TEMPLATE_ELT1(arr_desc_inputs,                      \
                                  input_type,                           \
                                  output_type, output_content_type,     \
                                  fut, fut_arg_interface,               \
                                  ref, ref_arg_interface,               \
                                  output_config_interface,              \
                                  dim_validation_interface,             \
                                  compare_interface)                    \
    do                                                                  \
    {                                                                   \
        TEMPLATE_DO_ARR_DESC(                                           \
            input_idx, input_type, input, arr_desc_inputs               \
            ,                                                           \
            JTEST_DUMP_STRF("Matrix Dimensions: %dx%d\n",               \
                         (int)input->numRows,                           \
                         (int)input->numCols);                          \
                                                                        \
            if (dim_validation_interface(input_type,                     \
                                        input)) {                       \
                output_config_interface(input_type,                     \
                                        input);                         \
                TEST_CALL_FUT_AND_REF(                                  \
                    fut, fut_arg_interface(input),                      \
                    ref, ref_arg_interface(input));                     \
                compare_interface(output_type,                          \
                                  output_content_type);                 \
            } else {                                                    \
                arm_status matrix_test_retval;                          \
                TEST_CALL_FUT(                                          \
                    matrix_test_retval = fut,                           \
                    fut_arg_interface(input));                          \
                                                                        \
                /* If dimensions are known bad, the fut should */       \
                /* detect it. */                                        \
                if ( matrix_test_retval != ARM_MATH_SIZE_MISMATCH) {     \
                    return JTEST_TEST_FAILED;                           \
                }                                                       \
            });                                                         \
        return JTEST_TEST_PASSED;                                       \
    } while (0)


#define MATRIX_TEST_TEMPLATE_ELT2(arr_desc_inputs_a,                    \
                                  arr_desc_inputs_b,                    \
                                  input_type,                           \
                                  output_type, output_content_type,     \
                                  fut, fut_arg_interface,               \
                                  ref, ref_arg_interface,               \
                                  output_config_interface,              \
                                  dim_validation_interface,             \
                                  compare_interface)                    \
    do                                                                  \
    {                                                                   \
        TEMPLATE_DO_ARR_DESC(                                           \
            input_a_idx, input_type, input_a, arr_desc_inputs_a         \
            ,                                                           \
            input_type input_b = ARR_DESC_ELT(                          \
                input_type, input_a_idx,                                \
                &(arr_desc_inputs_b));                                  \
                                                                        \
            JTEST_DUMP_STRF("Matrix Dimensions: A %dx%d  B %dx%d\n",    \
                     (int)input_a->numRows,                             \
                     (int)input_a->numCols,                             \
                     (int)input_b->numRows,                             \
                     (int)input_b->numCols);                            \
                                                                        \
            if (dim_validation_interface(input_type,                     \
                                        input_a,                        \
                                        input_b)) {                     \
                                                                        \
                output_config_interface(input_type,                     \
                                        input_a,                        \
                                        input_b);                       \
                                                                        \
                TEST_CALL_FUT_AND_REF(                                  \
                    fut, fut_arg_interface(input_a, input_b),           \
                    ref, ref_arg_interface(input_a, input_b));          \
                                                                        \
                compare_interface(output_type, output_content_type);    \
                                                                        \
            } else {                                                    \
                arm_status matrix_test_retval;                          \
                TEST_CALL_FUT(                                          \
                    matrix_test_retval = fut, fut_arg_interface(input_a, input_b)); \
                                                                        \
                /* If dimensions are known bad, the fut should */       \
                /* detect it. */                                        \
                if ( matrix_test_retval != ARM_MATH_SIZE_MISMATCH) {     \
                    return JTEST_TEST_FAILED;                           \
                }                                                       \
            });                                                         \
        return JTEST_TEST_PASSED;                                       \
    } while (0)

/**
 *  Specialization of #MATRIX_TEST_TEMPLATE_ELT2() for matrix tests.
 *
 *  @note This macro relies on the existance of ARM_xxx_INPUT_INTERFACE and
 *  REF_xxx_INPUT_INTERFACEs.
 */
#define MATRIX_DEFINE_TEST_TEMPLATE_ELT2(fn_name, suffix,           \
                                         output_config_interface,   \
                                         dim_validation_interface,  \
                                         comparison_interface)      \
        JTEST_DEFINE_TEST(arm_##fn_name##_##suffix##_test,          \
                          arm_##fn_name##_##suffix)                 \
        {                                                           \
            MATRIX_TEST_TEMPLATE_ELT2(                              \
                matrix_##suffix##_a_inputs,                         \
                matrix_##suffix##_b_inputs,                         \
                arm_matrix_instance_##suffix * ,                    \
                arm_matrix_instance_##suffix,                       \
                TYPE_FROM_ABBREV(suffix),                           \
                arm_##fn_name##_##suffix,                           \
                ARM_##fn_name##_INPUT_INTERFACE,                    \
                ref_##fn_name##_##suffix,                           \
                REF_##fn_name##_INPUT_INTERFACE,                    \
                output_config_interface,                            \
                dim_validation_interface,                           \
                comparison_interface);                              \
        }                                                           \

/**
 *  Specialization of #MATRIX_TEST_TEMPLATE_ELT1() for matrix tests.
 *
 *  @note This macro relies on the existance of ARM_xxx_INPUT_INTERFACE and
 *  REF_xxx_INPUT_INTERFACEs.
 */
#define MATRIX_DEFINE_TEST_TEMPLATE_ELT1(fn_name, suffix,           \
                                         output_config_interface,   \
                                         dim_validation_interface)  \
        JTEST_DEFINE_TEST(arm_##fn_name##_##suffix##_test,          \
                          arm_##fn_name##_##suffix)                 \
        {                                                           \
            MATRIX_TEST_TEMPLATE_ELT1(                              \
                matrix_##suffix##_a_inputs,                         \
                arm_matrix_instance_##suffix * ,                    \
                arm_matrix_instance_##suffix,                       \
                TYPE_FROM_ABBREV(suffix),                           \
                arm_##fn_name##_##suffix,                           \
                ARM_##fn_name##_INPUT_INTERFACE,                    \
                ref_##fn_name##_##suffix,                           \
                REF_##fn_name##_INPUT_INTERFACE,                    \
                output_config_interface,                            \
                dim_validation_interface,                           \
                MATRIX_COMPARE_INTERFACE);                          \
        }                                                           \


#endif /* _MATRIX_TEMPLATES_H_ */