summaryrefslogtreecommitdiff
path: root/Drivers/CMSIS/DSP/DSP_Lib_TestSuite/RefLibs/src/Intrinsics/intrinsics.c
blob: 7412adb1cf38b257410bf471a18291416e14abc8 (plain) (blame)
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
#include "ref.h"

q31_t ref__QADD8(q31_t x, q31_t y)
{
   q31_t sum;
   q7_t r, s, t, u;

   r = (q7_t) x;
   s = (q7_t) y;

   r = ref_sat_n((q31_t) (r + s), 8);
   s = ref_sat_n(((q31_t) (((x << 16) >> 24) + ((y << 16) >> 24))), 8);
   t = ref_sat_n(((q31_t) (((x << 8) >> 24) + ((y << 8) >> 24))), 8);
   u = ref_sat_n(((q31_t) ((x >> 24) + (y >> 24))), 8);

   sum =
      (((q31_t) u << 24) & 0xFF000000) | (((q31_t) t << 16) & 0x00FF0000) |
      (((q31_t) s << 8) & 0x0000FF00) | (r & 0x000000FF);

   return sum;

}

q31_t ref__QSUB8(q31_t x, q31_t y)
{
   q31_t sum;
   q31_t r, s, t, u;

   r = (q7_t) x;
   s = (q7_t) y;

   r = ref_sat_n((r - s), 8);
   s = ref_sat_n(((q31_t) (((x << 16) >> 24) - ((y << 16) >> 24))), 8) << 8;
   t = ref_sat_n(((q31_t) (((x << 8) >> 24) - ((y << 8) >> 24))), 8) << 16;
   u = ref_sat_n(((q31_t) ((x >> 24) - (y >> 24))), 8) << 24;

   sum = (u & 0xFF000000) | (t & 0x00FF0000) | (s & 0x0000FF00) | (r & 0x000000FF);

   return sum;
}

q31_t ref__QADD16(q31_t x, q31_t y)
{
   q31_t sum;
   q31_t r, s;

   r = (q15_t) x;
   s = (q15_t) y;

   r = ref_sat_q15(r + s);
   s = (q31_t)ref_sat_q15(((q31_t) ((x >> 16) + (y >> 16)))) << 16;

   sum = (s & 0xFFFF0000) | (r & 0x0000FFFF);

   return sum;

}

q31_t ref__SHADD16(q31_t x, q31_t y)
{
   q31_t sum;
   q31_t r, s;

   r = (q15_t) x;
   s = (q15_t) y;

   r = (r + s) >> 1;
   s = ((q31_t) (((x >> 16) + (y >> 16)) >> 1) << 16);

   sum = (s & 0xFFFF0000) | (r & 0x0000FFFF);

   return sum;

}

q31_t ref__QSUB16(q31_t x, q31_t y)
{
   q31_t sum;
   q31_t r, s;

   r = (q15_t) x;
   s = (q15_t) y;

   r = ref_sat_q15(r - s);
   s = (q31_t)ref_sat_q15(((q31_t) ((x >> 16) - (y >> 16)))) << 16;

   sum = (s & 0xFFFF0000) | (r & 0x0000FFFF);

   return sum;
}

q31_t ref__SHSUB16(q31_t x, q31_t y)
{
   q31_t diff;
   q31_t r, s;

   r = (q15_t) x;
   s = (q15_t) y;

   r = ((r >> 1) - (s >> 1));
   s = (((x >> 17) - (y >> 17)) << 16);

   diff = (s & 0xFFFF0000) | (r & 0x0000FFFF);

   return diff;
}

q31_t ref__QASX(q31_t x, q31_t y)
{
   q31_t sum = 0;
	q31_t xL, xH, yL, yH;
	
   // extract bottom halfword and sign extend
	xL = (q15_t)(x & 0xffff);
   // extract bottom halfword and sign extend
	yL = (q15_t)(y & 0xffff);
   // extract top halfword and sign extend
   xH = (q15_t)(x >> 16);
   // extract top halfword and sign extend
   yH = (q15_t)(y >> 16);
   
   sum = (((q31_t)ref_sat_q15(xH + yL )) << 16) |
         (((q31_t)ref_sat_q15(xL - yH )) & 0xffff);

   return sum;
}

q31_t ref__SHASX(q31_t x, q31_t y)
{
   q31_t sum;
   q31_t r, s;

   r = (q15_t) x;
   s = (q15_t) y;

    r = (r - (y >> 16)) / 2;
    s = (((x >> 16) + s) << 15);

   sum = (s & 0xFFFF0000) | (r & 0x0000FFFF);

   return sum;
}

q31_t ref__QSAX(q31_t x, q31_t y)
{
   q31_t sum = 0;
	q31_t xL, xH, yL, yH;
	
   // extract bottom halfword and sign extend
	xL = (q15_t)(x & 0xffff);
   // extract bottom halfword and sign extend
	yL = (q15_t)(y & 0xffff);
   // extract top halfword and sign extend
   xH = (q15_t)(x >> 16);
   // extract top halfword and sign extend
   yH = (q15_t)(y >> 16);
   
   sum = (((q31_t)ref_sat_q15(xH - yL )) << 16) |
         (((q31_t)ref_sat_q15(xL + yH )) & 0xffff);

   return sum;
}

q31_t ref__SHSAX(q31_t x, q31_t y)
{
   q31_t sum;
   q31_t r, s;

   r = (q15_t) x;
   s = (q15_t) y;

    r = (r + (y >> 16)) / 2;
    s = (((x >> 16) - s) << 15);

   sum = (s & 0xFFFF0000) | (r & 0x0000FFFF);

   return sum;
}

q31_t ref__SMUSDX(q31_t x, q31_t y)
{
   return ((q31_t) (((q15_t) x * (q15_t) (y >> 16)) - ((q15_t) (x >> 16) * (q15_t) y)));
}

q31_t ref__SMUADX(q31_t x, q31_t y)
{
   return ((q31_t) (((q15_t) x * (q15_t) (y >> 16)) + ((q15_t) (x >> 16) * (q15_t) y)));
}

q31_t ref__QADD(q31_t x, q31_t y)
{
   return ref_sat_q31((q63_t) x + y);
}

q31_t ref__QSUB(q31_t x, q31_t y)
{
   return ref_sat_q31((q63_t) x - y);
}

q31_t ref__SMLAD(q31_t x, q31_t y, q31_t sum)
{
   return (sum + ((q15_t) (x >> 16) * (q15_t) (y >> 16)) + ((q15_t) x * (q15_t) y));
}

q31_t ref__SMLADX(q31_t x, q31_t y, q31_t sum)
{
   return (sum + ((q15_t) (x >> 16) * (q15_t) (y)) + ((q15_t) x * (q15_t) (y >> 16)));
}

q31_t ref__SMLSDX(q31_t x, q31_t y, q31_t sum)
{
   return (sum - ((q15_t) (x >> 16) * (q15_t) (y)) + ((q15_t) x * (q15_t) (y >> 16)));
}

q63_t ref__SMLALD(q31_t x, q31_t y, q63_t sum)
{
   return (sum + ((q15_t) (x >> 16) * (q15_t) (y >> 16)) + ((q15_t) x * (q15_t) y));
}

q63_t ref__SMLALDX(q31_t x, q31_t y, q63_t sum)
{
   return (sum + ((q15_t) (x >> 16) * (q15_t) y)) + ((q15_t) x * (q15_t) (y >> 16));
}

q31_t ref__SMUAD(q31_t x, q31_t y)
{
   return (((x >> 16) * (y >> 16)) + (((x << 16) >> 16) * ((y << 16) >> 16)));
}

q31_t ref__SMUSD(q31_t x, q31_t y)
{
   return (-((x >> 16) * (y >> 16)) + (((x << 16) >> 16) * ((y << 16) >> 16)));
}

q31_t ref__SXTB16(q31_t x)
{
   return ((((x << 24) >> 24) & 0x0000FFFF) | (((x << 8) >> 8) & 0xFFFF0000));
}