xref: /aosp_15_r20/external/libopus/dnn/test_vec.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 #include <stdio.h>
2 #include <math.h>
3 #include "opus_types.h"
4 #include "arch.h"
5 #include "common.h"
6 #include "tansig_table.h"
7 
8 #define LPCNET_TEST
9 
10 // we need to call two versions of each functions that have the same
11 // name, so use #defines to temp rename them
12 
13 #define lpcnet_exp2 lpcnet_exp2_fast
14 #define tansig_approx tansig_approx_fast
15 #define sigmoid_approx sigmoid_approx_fast
16 #define softmax softmax_fast
17 #define vec_tanh vec_tanh_fast
18 #define vec_sigmoid vec_sigmoid_fast
19 #define sgemv_accum16 sgemv_accum16_fast
20 #define sparse_sgemv_accum16 sparse_sgemv_accum16_fast
21 
22 #ifdef __AVX__
23 #include "vec_avx.h"
24 #ifdef __AVX2__
25 const char simd[]="AVX2";
26 #else
27 const char simd[]="AVX";
28 #endif
29 #elif __ARM_NEON__
30 #include "vec_neon.h"
31 const char simd[]="NEON";
32 #else
33 const char simd[]="none";
34 
35 #endif
36 
37 #undef lpcnet_exp2
38 #undef tansig_approx
39 #undef sigmoid_approx
40 #undef softmax
41 #undef vec_tanh
42 #undef vec_sigmoid
43 #undef sgemv_accum16
44 #undef sparse_sgemv_accum16
45 #include "vec.h"
46 
47 #define ROW_STEP 16
48 #define ROWS     ROW_STEP*10
49 #define COLS     2
50 #define ENTRIES  2
51 
test_sgemv_accum16()52 int test_sgemv_accum16() {
53     float weights[ROWS*COLS];
54     float x[COLS];
55     float out[ROWS], out_fast[ROWS];
56     int i;
57 
58     printf("sgemv_accum16.....................: ");
59     for(i=0; i<ROWS*COLS; i++) {
60 	weights[i] = i;
61     }
62     for(i=0; i<ROWS; i++) {
63 	out[i] = 0;
64 	out_fast[i] = 0;
65     }
66 
67     for(i=0; i<COLS; i++) {
68 	x[i] = i+1;
69     }
70 
71     sgemv_accum16(out, weights, ROWS, COLS, 1, x);
72     sgemv_accum16_fast(out_fast, weights, ROWS, COLS, 1, x);
73 
74     for(i=0; i<ROWS; i++) {
75 	if (out[i] != out_fast[i]) {
76 	    printf("fail\n");
77 	    for(i=0; i<ROWS; i++) {
78 		printf("%d %f %f\n", i, out[i], out_fast[i]);
79 		if (out[i] != out_fast[i])
80 		    return 1;
81 	    }
82 	}
83     }
84 
85     printf("pass\n");
86     return 0;
87 }
88 
89 
test_sparse_sgemv_accum16()90 int test_sparse_sgemv_accum16() {
91     int rows = ROW_STEP*ENTRIES;
92     int indx[] = {1,0,2,0,1};
93     float w[ROW_STEP*(1+2)];
94     float x[ENTRIES] = {1,2};
95     float out[ROW_STEP*(1+2)], out_fast[ROW_STEP*(1+2)];
96     int i;
97 
98     printf("sparse_sgemv_accum16..............: ");
99     for(i=0; i<ROW_STEP*(1+2); i++) {
100 	w[i] = i;
101 	out[i] = 0;
102 	out_fast[i] = 0;
103     }
104 
105     sparse_sgemv_accum16(out, w, rows, indx, x);
106     sparse_sgemv_accum16_fast(out_fast, w, rows, indx, x);
107 
108     for(i=0; i<ROW_STEP*ENTRIES; i++) {
109 	if (out[i] != out_fast[i]) {
110 	    printf("fail\n");
111 	    for(i=0; i<ROW_STEP*ENTRIES; i++) {
112 		printf("%d %f %f\n", i, out[i], out_fast[i]);
113 		if (out[i] != out_fast[i])
114 		    return 1;
115 	    }
116 	}
117     }
118 
119     printf("pass\n");
120     return 0;
121 }
122 
main()123 int main() {
124     printf("testing vector routines on SIMD: %s\n", simd);
125     int test1 = test_sgemv_accum16();
126     int test2 = test_sparse_sgemv_accum16();
127     return test1 || test2;
128 }
129