arm_nnsupportfunctions.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * Copyright (C) 2010-2022 Arm Limited or its affiliates.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /* ----------------------------------------------------------------------
  19. * Project: CMSIS NN Library
  20. * Title: arm_nnsupportfunctions.h
  21. * Description: Public header file of support functions for CMSIS NN Library
  22. *
  23. * $Date: 19. April 2022
  24. * $Revision: V.7.0.1
  25. *
  26. * Target Processor: Cortex-M CPUs
  27. * -------------------------------------------------------------------- */
  28. #ifndef _ARM_NNSUPPORTFUNCTIONS_H_
  29. #define _ARM_NNSUPPORTFUNCTIONS_H_
  30. #include "arm_nn_math_types.h"
  31. #include "arm_nn_types.h"
  32. #include <stdbool.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #define LEFT_SHIFT(_shift) (_shift > 0 ? _shift : 0)
  37. #define RIGHT_SHIFT(_shift) (_shift > 0 ? 0 : -_shift)
  38. #define MASK_IF_ZERO(x) (x) == 0 ? ~0 : 0
  39. #define MASK_IF_NON_ZERO(x) (x) != 0 ? ~0 : 0
  40. #define SELECT_USING_MASK(mask, a, b) ((mask) & (a)) ^ (~(mask) & (b))
  41. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  42. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  43. #define CLAMP(x, h, l) MAX(MIN((x), (h)), (l))
  44. #define REDUCE_MULTIPLIER(_mult) ((_mult < 0x7FFF0000) ? ((_mult + (1 << 15)) >> 16) : 0x7FFF)
  45. /**
  46. * @brief definition to pack four 8 bit values.
  47. */
  48. #define PACK_Q7x4_32x1(v0, v1, v2, v3) \
  49. ((((int32_t)(v0) << 0) & (int32_t)0x000000FF) | (((int32_t)(v1) << 8) & (int32_t)0x0000FF00) | \
  50. (((int32_t)(v2) << 16) & (int32_t)0x00FF0000) | (((int32_t)(v3) << 24) & (int32_t)0xFF000000))
  51. /**
  52. * @brief Union for SIMD access of q31/q15/q7 types
  53. */
  54. union arm_nnword
  55. {
  56. q31_t word;
  57. /**< q31 type */
  58. q15_t half_words[2];
  59. /**< q15 type */
  60. q7_t bytes[4];
  61. /**< q7 type */
  62. };
  63. /**
  64. * @brief Union for data type long long
  65. */
  66. struct arm_nn_double
  67. {
  68. uint32_t low;
  69. int32_t high;
  70. };
  71. union arm_nn_long_long
  72. {
  73. int64_t long_long;
  74. struct arm_nn_double word;
  75. };
  76. /**
  77. * @defgroup nndata_convert Neural Network Data Conversion Functions
  78. *
  79. * Perform data type conversion in-between neural network operations
  80. *
  81. */
  82. /**
  83. * @brief Converts the elements of the q7 vector to q15 vector without left-shift
  84. * @param[in] *pSrc points to the q7 input vector
  85. * @param[out] *pDst points to the q15 output vector
  86. * @param[in] blockSize length of the input vector
  87. *
  88. */
  89. void arm_q7_to_q15_no_shift(const q7_t *pSrc, q15_t *pDst, uint32_t blockSize);
  90. /**
  91. * @brief Non-saturating addition of elements of a q7 vector
  92. * @param[in] *input Pointer to the q7 input vector
  93. * @param[out] *output Pointer to the q31 output variable.
  94. * @param[in] block_size length of the input vector
  95. * \par Description:
  96. *
  97. * 2^24 samples can be added without saturating the result.
  98. *
  99. * The equation used for the conversion process is:
  100. *
  101. * <pre>
  102. * sum = input[0] + input[1] + .. + input[block_size -1]
  103. * </pre>
  104. *
  105. * */
  106. void arm_nn_add_q7(const q7_t *input, q31_t *output, uint32_t block_size);
  107. /**
  108. * @brief Converts the elements of the q7 vector to reordered q15 vector without left-shift
  109. * @param[in] *pSrc points to the q7 input vector
  110. * @param[out] *pDst points to the q15 output vector
  111. * @param[in] blockSize length of the input vector
  112. * @return none.
  113. *
  114. */
  115. void arm_q7_to_q15_reordered_no_shift(const q7_t *pSrc, q15_t *pDst, uint32_t blockSize);
  116. /**
  117. * @brief Converts the elements from a q7 vector to a q15 vector with an added offset
  118. * @param[in] src pointer to the q7 input vector
  119. * @param[out] dst pointer to the q15 output vector
  120. * @param[in] block_size length of the input vector
  121. * @param[in] offset q7 offset to be added to each input vector element.
  122. *
  123. * \par Description:
  124. *
  125. * The equation used for the conversion process is:
  126. *
  127. * <pre>
  128. * dst[n] = (q15_t) src[n] + offset; 0 <= n < block_size.
  129. * </pre>
  130. *
  131. */
  132. void arm_q7_to_q15_with_offset(const q7_t *src, q15_t *dst, uint32_t block_size, q15_t offset);
  133. /**
  134. * @brief Converts the elements of the q7 vector to reordered q15 vector with an added offset
  135. * @param[in] src pointer to the q7 input vector
  136. * @param[out] dst pointer to the q15 output vector
  137. * @param[in] block_size length of the input vector
  138. * @param[in] offset offset to be added to each input vector element.
  139. * @return none.
  140. *
  141. * @details This function does the q7 to q15 expansion with re-ordering of bytes. Re-ordering is a consequence of
  142. * the sign extension intrinsic(DSP extension). The tail (i.e., last (N % 4) elements) retains its
  143. * original order.
  144. *
  145. */
  146. void arm_q7_to_q15_reordered_with_offset(const q7_t *src, q15_t *dst, uint32_t block_size, q15_t offset);
  147. /**
  148. * @brief Converts the elements from a q7 vector and accumulate to a q15 vector
  149. * @param[in] *src points to the q7 input vector
  150. * @param[out] *dst points to the q15 output vector
  151. * @param[in] block_size length of the input vector
  152. *
  153. * \par Description:
  154. *
  155. * The equation used for the conversion process is:
  156. *
  157. * <pre>
  158. * dst[n] += (q15_t) src[n] ; 0 <= n < block_size.
  159. * </pre>
  160. *
  161. */
  162. void arm_nn_accumulate_q7_to_q15(q15_t *dst, const q7_t *src, uint32_t block_size);
  163. /**
  164. * @brief Depthwise conv on an im2col buffer where the input channel equals output channel.
  165. * @param[in] row pointer to row
  166. * @param[in] col pointer to im2col buffer, always consists of 2 columns.
  167. * @param[in] num_ch number of channels
  168. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  169. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  170. * @param[in] out_offset output tensor offset.
  171. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  172. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  173. * @param[in] kernel_size number of elements in one column.
  174. * @param[in] output_bias per output channel bias. Range : int32
  175. * @param[out] out pointer to output
  176. * @return The function returns one of the two
  177. * 1. The incremented output pointer for a successful operation or
  178. * 2. NULL if implementation is not available.
  179. *
  180. * @details Supported framework: TensorFlow Lite micro.
  181. */
  182. q7_t *arm_nn_depthwise_conv_s8_core(const q7_t *row,
  183. const q15_t *col,
  184. const uint16_t num_ch,
  185. const int32_t *out_shift,
  186. const int32_t *out_mult,
  187. const int32_t out_offset,
  188. const int32_t activation_min,
  189. const int32_t activation_max,
  190. const uint16_t kernel_size,
  191. const int32_t *const output_bias,
  192. q7_t *out);
  193. /**
  194. * @brief General Matrix-multiplication function with per-channel requantization.
  195. * @param[in] input_row pointer to row operand
  196. * @param[in] input_col pointer to col operand
  197. * @param[in] output_ch number of rows of input_row
  198. * @param[in] col_batches number of column batches. Range: 1 to 4
  199. * @param[in] output_shift pointer to per output channel requantization shift parameter.
  200. * @param[in] output_mult pointer to per output channel requantization multiplier parameter.
  201. * @param[in] out_offset output tensor offset.
  202. * @param[in] col_offset input tensor(col) offset.
  203. * @param[in] row_offset kernel offset(row). Not used.
  204. * @param[in] out_activation_min minimum value to clamp the output to. Range : int8
  205. * @param[in] out_activation_max maximum value to clamp the output to. Range : int8
  206. * @param[in] row_len number of elements in each row
  207. * @param[in] bias per output channel bias. Range : int32
  208. * @param[in,out] out pointer to output
  209. * @return The function returns one of the two
  210. * 1. The incremented output pointer for a successful operation or
  211. * 2. NULL if implementation is not available.
  212. *
  213. * @details Supported framework: TensorFlow Lite
  214. */
  215. q7_t *arm_nn_mat_mult_s8(const q7_t *input_row,
  216. const q7_t *input_col,
  217. const uint16_t output_ch,
  218. const uint16_t col_batches,
  219. const int32_t *output_shift,
  220. const int32_t *output_mult,
  221. const int32_t out_offset,
  222. const int32_t col_offset,
  223. const int32_t row_offset,
  224. const int16_t out_activation_min,
  225. const int16_t out_activation_max,
  226. const uint16_t row_len,
  227. const int32_t *const bias,
  228. q7_t *out);
  229. /**
  230. * @brief Matrix-multiplication function for convolution with per-channel requantization for 16 bits convolution.
  231. * @param[in] input_a pointer to operand A
  232. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  233. * @param[in] output_ch number of rows of A
  234. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  235. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  236. * @param[in] activation_min minimum value to clamp the output to. Range : int16
  237. * @param[in] activation_max maximum value to clamp the output to. Range : int16
  238. * @param[in] num_col_a number of columns of A
  239. * @param[in] output_bias per output channel bias. Range : int64
  240. * @param[in,out] out_0 pointer to output
  241. * @return The function returns one of the two
  242. * 1. The incremented output pointer for a successful operation or
  243. * 2. NULL if implementation is not available.
  244. *
  245. * @details This function does the matrix multiplication of weight matrix for all output channels
  246. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  247. * clamped in the range provided by activation min and max.
  248. * Supported framework: TensorFlow Lite micro.
  249. */
  250. q15_t *arm_nn_mat_mult_kernel_s16(const q7_t *input_a,
  251. const q15_t *input_b,
  252. const int32_t output_ch,
  253. const int32_t *out_shift,
  254. const int32_t *out_mult,
  255. const int16_t activation_min,
  256. const int16_t activation_max,
  257. const int32_t num_col_a,
  258. const int64_t *const output_bias,
  259. q15_t *out_0);
  260. /**
  261. * @brief General Matrix-multiplication without requantization for one row & one column
  262. * @param[in] row_elements number of row elements
  263. * @param[in] row_base pointer to row operand
  264. * @param[in] col_base pointer to col operand
  265. * @param[out] sum_col pointer to store sum of column elements
  266. * @param[out] output pointer to store result of multiply-accumulate
  267. * @return The function returns the multiply-accumulated result of the row by column.
  268. *
  269. * @details Pseudo-code
  270. * *output = 0
  271. * sum_col = 0
  272. * for (i = 0; i < row_elements; i++)
  273. * *output += row_base[i] * col_base[i]
  274. * sum_col += col_base[i]
  275. *
  276. */
  277. arm_status arm_nn_mat_mul_core_1x_s8(int32_t row_elements,
  278. const int8_t *row_base,
  279. const int8_t *col_base,
  280. int32_t *const sum_col,
  281. int32_t *const output);
  282. /**
  283. * @brief Matrix-multiplication with requantization & activation function for four rows and one column
  284. * @param[in] row_elements number of row elements
  285. * @param[in] offset offset between rows. Can be the same as row_elements.
  286. * For e.g, in a 1x1 conv scenario with stride as 1.
  287. * @param[in] row_base pointer to row operand
  288. * @param[in] col_base pointer to col operand
  289. * @param[in] out_ch Number of output channels
  290. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  291. * @param[in] quant_params Pointer to per-channel quantization parameters
  292. * @param[in] bias Pointer to per-channel bias
  293. * @param[out] output Pointer to output where int8 results are stored.
  294. *
  295. * @return The function returns the updated output pointer or NULL if implementation is not available.
  296. *
  297. * @details Compliant to TFLM int8 specification. MVE implementation only
  298. */
  299. int8_t *arm_nn_mat_mul_core_4x_s8(const int32_t row_elements,
  300. const int32_t offset,
  301. const int8_t *row_base,
  302. const int8_t *col_base,
  303. const int32_t out_ch,
  304. const cmsis_nn_conv_params *conv_params,
  305. const cmsis_nn_per_channel_quant_params *quant_params,
  306. const int32_t *bias,
  307. int8_t *output);
  308. /**
  309. * @brief General Matrix-multiplication function with per-channel requantization.
  310. * This function assumes:
  311. * - LHS input matrix NOT transposed (nt)
  312. * - RHS input matrix transposed (t)
  313. *
  314. * @note This operation also performs the broadcast bias addition before the requantization
  315. *
  316. * @param[in] lhs Pointer to the LHS input matrix
  317. * @param[in] rhs Pointer to the RHS input matrix
  318. * @param[in] bias Pointer to the bias vector. The length of this vector is equal to the number of
  319. * output columns (or RHS input rows)
  320. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  321. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  322. * The length of this vector is equal to the number of output columns (or RHS input
  323. * rows)
  324. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  325. * of this vector is equal to the number of output columns (or RHS input rows)
  326. * @param[in] lhs_rows Number of LHS input rows
  327. * @param[in] rhs_rows Number of RHS input rows
  328. * @param[in] rhs_cols Number of LHS/RHS input columns
  329. * @param[in] lhs_offset Offset to be applied to the LHS input value
  330. * @param[in] dst_offset Offset to be applied the output result
  331. * @param[in] activation_min Minimum value to clamp down the output. Range : int8
  332. * @param[in] activation_max Maximum value to clamp up the output. Range : int8
  333. *
  334. * @return The function returns <code>ARM_MATH_SUCCESS</code>
  335. *
  336. */
  337. arm_status arm_nn_mat_mult_nt_t_s8(const q7_t *lhs,
  338. const q7_t *rhs,
  339. const q31_t *bias,
  340. q7_t *dst,
  341. const int32_t *dst_multipliers,
  342. const int32_t *dst_shifts,
  343. const int32_t lhs_rows,
  344. const int32_t rhs_rows,
  345. const int32_t rhs_cols,
  346. const int32_t lhs_offset,
  347. const int32_t dst_offset,
  348. const int32_t activation_min,
  349. const int32_t activation_max);
  350. /**
  351. * @brief s8 Vector by Matrix (transposed) multiplication
  352. *
  353. * @param[in] lhs Input left-hand side vector
  354. * @param[in] rhs Input right-hand side matrix (transposed)
  355. * @param[in] bias Input bias
  356. * @param[out] dst Output vector
  357. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side vector.
  358. * Range: -127 to 128
  359. * @param[in] rhs_offset Not used
  360. * @param[in] dst_offset Offset to be added to the output values. Range: -127 to 128
  361. * @param[in] dst_multiplier Output multiplier
  362. * @param[in] dst_shift Output shift
  363. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  364. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  365. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  366. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  367. * @param[in] address_offset Memory position offset for dst. First output is stored at 'dst', the
  368. * second at 'dst + address_offset' and so on. Default value is typically 1.
  369. *
  370. * @return The function returns <code>ARM_MATH_SUCCESS</code>
  371. *
  372. */
  373. arm_status arm_nn_vec_mat_mult_t_s8(const q7_t *lhs,
  374. const q7_t *rhs,
  375. const q31_t *bias,
  376. q7_t *dst,
  377. const int32_t lhs_offset,
  378. const int32_t rhs_offset,
  379. const int32_t dst_offset,
  380. const int32_t dst_multiplier,
  381. const int32_t dst_shift,
  382. const int32_t rhs_cols,
  383. const int32_t rhs_rows,
  384. const int32_t activation_min,
  385. const int32_t activation_max,
  386. const int32_t address_offset);
  387. /**
  388. * @brief s16 Vector by Matrix (transposed) multiplication
  389. *
  390. * @param[in] lhs Input left-hand side vector
  391. * @param[in] rhs Input right-hand side matrix (transposed)
  392. * @param[in] bias Input bias
  393. * @param[out] dst Output vector
  394. * @param[in] dst_multiplier Output multiplier
  395. * @param[in] dst_shift Output shift
  396. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  397. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  398. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  399. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  400. *
  401. * @return The function returns <code>ARM_MATH_SUCCESS</code>
  402. *
  403. */
  404. arm_status arm_nn_vec_mat_mult_t_s16(const q15_t *lhs,
  405. const q7_t *rhs,
  406. const q63_t *bias,
  407. q15_t *dst,
  408. const int32_t dst_multiplier,
  409. const int32_t dst_shift,
  410. const int32_t rhs_cols,
  411. const int32_t rhs_rows,
  412. const int32_t activation_min,
  413. const int32_t activation_max);
  414. /**
  415. * @brief s8 Vector by Matrix (transposed) multiplication with s16 output
  416. *
  417. * @param[in] lhs Input left-hand side vector
  418. * @param[in] rhs Input right-hand side matrix (transposed)
  419. * @param[out] dst Output vector
  420. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side
  421. * vector. Range: -127 to 128
  422. * @param[in] rhs_offset Not used
  423. * @param[in] scatter_offset Address offset for dst. First output is stored at 'dst', the
  424. * second at 'dst + scatter_offset' and so on.
  425. * @param[in] dst_multiplier Output multiplier
  426. * @param[in] dst_shift Output shift
  427. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  428. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  429. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  430. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  431. *
  432. * @return The function returns <code>ARM_MATH_SUCCESS</code>
  433. *
  434. */
  435. arm_status arm_nn_vec_mat_mult_t_svdf_s8(const q7_t *lhs,
  436. const q7_t *rhs,
  437. q15_t *dst,
  438. const int32_t lhs_offset,
  439. const int32_t rhs_offset,
  440. const int32_t scatter_offset,
  441. const int32_t dst_multiplier,
  442. const int32_t dst_shift,
  443. const int32_t rhs_cols,
  444. const int32_t rhs_rows,
  445. const int32_t activation_min,
  446. const int32_t activation_max);
  447. /**
  448. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in padded cases where
  449. * the padding is -lhs_offset(Range: int8). Dimensions are the same for lhs and rhs.
  450. *
  451. * @param[in] lhs Input left-hand side matrix
  452. * @param[in] rhs Input right-hand side matrix (transposed)
  453. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  454. * @param[in] num_ch Number of channels in LHS/RHS
  455. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels
  456. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels
  457. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  458. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  459. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  460. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  461. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels
  462. * @param[in] out Output pointer
  463. *
  464. * @return The function returns one of the two
  465. * - Updated output pointer if an implementation is available
  466. * - NULL if no implementation is available.
  467. *
  468. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  469. * out for the following.
  470. * - Output shift
  471. * - Output multiplier
  472. * - Output bias
  473. * - rhs
  474. */
  475. q7_t *arm_nn_depthwise_conv_nt_t_padded_s8(const q7_t *lhs,
  476. const q7_t *rhs,
  477. const int32_t lhs_offset,
  478. const uint16_t num_ch,
  479. const int32_t *out_shift,
  480. const int32_t *out_mult,
  481. const int32_t out_offset,
  482. const int32_t activation_min,
  483. const int32_t activation_max,
  484. const uint16_t row_x_col,
  485. const int32_t *const output_bias,
  486. q7_t *out);
  487. /**
  488. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases.
  489. * Dimensions are the same for lhs and rhs.
  490. *
  491. * @param[in] lhs Input left-hand side matrix
  492. * @param[in] rhs Input right-hand side matrix (transposed)
  493. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  494. * @param[in] num_ch Number of channels in LHS/RHS
  495. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  496. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  497. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  498. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  499. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  500. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  501. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  502. * @param[in] out Output pointer
  503. *
  504. * @return The function returns one of the two
  505. * - Updated output pointer if an implementation is available
  506. * - NULL if no implementation is available.
  507. *
  508. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  509. * out for the following.
  510. * - Output shift
  511. * - Output multiplier
  512. * - Output bias
  513. * - rhs
  514. */
  515. q7_t *arm_nn_depthwise_conv_nt_t_s8(const q7_t *lhs,
  516. const q7_t *rhs,
  517. const int32_t lhs_offset,
  518. const uint16_t num_ch,
  519. const int32_t *out_shift,
  520. const int32_t *out_mult,
  521. const int32_t out_offset,
  522. const int32_t activation_min,
  523. const int32_t activation_max,
  524. const uint16_t row_x_col,
  525. const int32_t *const output_bias,
  526. q7_t *out);
  527. /**
  528. *@brief Matrix-multiplication function for convolution with reordered columns
  529. *@param[in] pA pointer to operand A
  530. *@param[in] pInBuffer pointer to operand B, always conssists of 2 vectors
  531. *@param[in] ch_im_out numRow of A
  532. *@param[in] numCol_A numCol of A
  533. *@param[in] bias_shift amount of left-shift for bias
  534. *@param[in] out_shift amount of right-shift for output
  535. *@param[in] bias the bias
  536. *@param[in,out] pOut pointer to output
  537. *@return The function returns the incremented output pointer
  538. *
  539. *@details This function assumes that data in pInBuffer are reordered
  540. */
  541. q7_t *arm_nn_mat_mult_kernel_q7_q15_reordered(const q7_t *pA,
  542. const q15_t *pInBuffer,
  543. const uint16_t ch_im_out,
  544. const uint16_t numCol_A,
  545. const uint16_t bias_shift,
  546. const uint16_t out_shift,
  547. const q7_t *bias,
  548. q7_t *pOut);
  549. /**
  550. @brief Read 2 q15 elements and post increment pointer.
  551. @param[in] in_q15 Pointer to pointer that holds address of input.
  552. @return q31 value
  553. */
  554. __STATIC_FORCEINLINE q31_t arm_nn_read_q15x2_ia(const q15_t **in_q15)
  555. {
  556. q31_t val;
  557. memcpy(&val, *in_q15, 4);
  558. *in_q15 += 2;
  559. return (val);
  560. }
  561. /**
  562. @brief Read 4 q7 from q7 pointer and post increment pointer.
  563. @param[in] in_q7 Pointer to pointer that holds address of input.
  564. @return q31 value
  565. */
  566. __STATIC_FORCEINLINE q31_t arm_nn_read_q7x4_ia(const q7_t **in_q7)
  567. {
  568. q31_t val;
  569. memcpy(&val, *in_q7, 4);
  570. *in_q7 += 4;
  571. return (val);
  572. }
  573. /**
  574. @brief Read 2 q15 from q15 pointer.
  575. @param[in] in_q15 pointer to address of input.
  576. @return q31 value
  577. */
  578. __STATIC_FORCEINLINE q31_t arm_nn_read_q15x2(const q15_t *in_q15)
  579. {
  580. q31_t val;
  581. memcpy(&val, in_q15, 4);
  582. return (val);
  583. }
  584. /**
  585. @brief Read 4 q7 values.
  586. @param[in] in_q7 pointer to address of input.
  587. @return q31 value
  588. */
  589. __STATIC_FORCEINLINE q31_t arm_nn_read_q7x4(const q7_t *in_q7)
  590. {
  591. q31_t val;
  592. memcpy(&val, in_q7, 4);
  593. return (val);
  594. }
  595. /**
  596. @brief Write four q7 to q7 pointer and increment pointer afterwards.
  597. @param[in] in Double pointer to input value
  598. @param[in] value Four bytes to copy
  599. */
  600. __STATIC_FORCEINLINE void arm_nn_write_q7x4_ia(q7_t **in, q31_t value)
  601. {
  602. memcpy(*in, &value, 4);
  603. *in += 4;
  604. }
  605. /**
  606. * @brief memset optimized for MVE
  607. * @param[in, out] dst Destination pointer
  608. * @param[in] val Value to set
  609. * @param[in] block_size Number of bytes to copy.
  610. *
  611. */
  612. __STATIC_FORCEINLINE void arm_memset_q7(q7_t *dst, const q7_t val, uint32_t block_size)
  613. {
  614. #if defined(ARM_MATH_MVEI)
  615. __asm volatile(" vdup.8 q0, %[set_val] \n"
  616. " wlstp.8 lr, %[cnt], 1f \n"
  617. "2: \n"
  618. " vstrb.8 q0, [%[in]], #16 \n"
  619. " letp lr, 2b \n"
  620. "1: \n"
  621. : [ in ] "+r"(dst)
  622. : [ cnt ] "r"(block_size), [ set_val ] "r"(val)
  623. : "q0", "memory", "r14");
  624. #else
  625. memset(dst, val, block_size);
  626. #endif
  627. }
  628. #if defined(ARM_MATH_DSP)
  629. /**
  630. * @brief read and expand one q7 word into two q15 words
  631. */
  632. __STATIC_FORCEINLINE const q7_t *read_and_pad(const q7_t *source, q31_t *out1, q31_t *out2)
  633. {
  634. q31_t inA = arm_nn_read_q7x4_ia(&source);
  635. q31_t inAbuf1 = __SXTB16_RORn((uint32_t)inA, 8);
  636. q31_t inAbuf2 = __SXTB16(inA);
  637. #ifndef ARM_MATH_BIG_ENDIAN
  638. *out2 = (int32_t)(__PKHTB(inAbuf1, inAbuf2, 16));
  639. *out1 = (int32_t)(__PKHBT(inAbuf2, inAbuf1, 16));
  640. #else
  641. *out1 = (int32_t)(__PKHTB(inAbuf1, inAbuf2, 16));
  642. *out2 = (int32_t)(__PKHBT(inAbuf2, inAbuf1, 16));
  643. #endif
  644. return source;
  645. }
  646. /**
  647. * @brief read and expand one q7 word into two q15 words with reordering
  648. */
  649. __STATIC_FORCEINLINE const q7_t *read_and_pad_reordered(const q7_t *source, q31_t *out1, q31_t *out2)
  650. {
  651. q31_t inA = arm_nn_read_q7x4_ia(&source);
  652. #ifndef ARM_MATH_BIG_ENDIAN
  653. *out2 = __SXTB16(__ROR((uint32_t)inA, 8));
  654. *out1 = __SXTB16(inA);
  655. #else
  656. *out1 = __SXTB16(__ROR((uint32_t)inA, 8));
  657. *out2 = __SXTB16(inA);
  658. #endif
  659. return source;
  660. }
  661. /**
  662. * @brief read and expand one q7 word into two q15 words with reordering and add an offset
  663. */
  664. __STATIC_FORCEINLINE const q7_t *
  665. read_and_pad_reordered_with_offset(const q7_t *source, q31_t *out1, q31_t *out2, q31_t offset)
  666. {
  667. q31_t inA = arm_nn_read_q7x4_ia(&source);
  668. #ifndef ARM_MATH_BIG_ENDIAN
  669. *out2 = __SXTB16(__ROR((uint32_t)inA, 8));
  670. *out1 = __SXTB16(inA);
  671. #else
  672. *out1 = __SXTB16(__ROR((uint32_t)inA, 8));
  673. *out2 = __SXTB16(inA);
  674. #endif
  675. *out1 = __QADD16(*out1, offset);
  676. *out2 = __QADD16(*out2, offset);
  677. return source;
  678. }
  679. #endif
  680. /**
  681. * @defgroup NNBasicMath Basic Math Functions for Neural Network Computation
  682. *
  683. * Basic Math Functions for Neural Network Computation
  684. *
  685. */
  686. /**
  687. * @brief q7 vector multiplication with variable output shifts
  688. * @param[in] *pSrcA pointer to the first input vector
  689. * @param[in] *pSrcB pointer to the second input vector
  690. * @param[out] *pDst pointer to the output vector
  691. * @param[in] out_shift amount of right-shift for output
  692. * @param[in] blockSize number of samples in each vector
  693. * @return none.
  694. *
  695. * <b>Scaling and Overflow Behavior:</b>
  696. * \par
  697. * The function uses saturating arithmetic.
  698. * Results outside of the allowable q15 range [0x8000 0x7FFF] will be saturated.
  699. */
  700. void arm_nn_mult_q15(q15_t *pSrcA, q15_t *pSrcB, q15_t *pDst, const uint16_t out_shift, uint32_t blockSize);
  701. /**
  702. * @brief q7 vector multiplication with variable output shifts
  703. * @param[in] *pSrcA pointer to the first input vector
  704. * @param[in] *pSrcB pointer to the second input vector
  705. * @param[out] *pDst pointer to the output vector
  706. * @param[in] out_shift amount of right-shift for output
  707. * @param[in] blockSize number of samples in each vector
  708. * @return none.
  709. *
  710. * <b>Scaling and Overflow Behavior:</b>
  711. * \par
  712. * The function uses saturating arithmetic.
  713. * Results outside of the allowable q7 range [0x80 0x7F] will be saturated.
  714. */
  715. void arm_nn_mult_q7(q7_t *pSrcA, q7_t *pSrcB, q7_t *pDst, const uint16_t out_shift, uint32_t blockSize);
  716. /**
  717. * @brief Matrix-multiplication function for convolution with per-channel requantization.
  718. * @param[in] input_a pointer to operand A
  719. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  720. * @param[in] output_ch number of rows of A
  721. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  722. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  723. * @param[in] out_offset output tensor offset.
  724. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  725. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  726. * @param[in] num_col_a number of columns of A
  727. * @param[in] output_bias per output channel bias. Range : int32
  728. * @param[in,out] out_0 pointer to output
  729. * @return The function returns one of the two
  730. * 1. The incremented output pointer for a successful operation or
  731. * 2. NULL if implementation is not available.
  732. *
  733. * @details This function does the matrix multiplication of weight matrix for all output channels
  734. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  735. * clamped in the range provided by activation min and max.
  736. * Supported framework: TensorFlow Lite micro.
  737. */
  738. q7_t *arm_nn_mat_mult_kernel_s8_s16(const q7_t *input_a,
  739. const q15_t *input_b,
  740. const uint16_t output_ch,
  741. const int32_t *out_shift,
  742. const int32_t *out_mult,
  743. const int32_t out_offset,
  744. const int16_t activation_min,
  745. const int16_t activation_max,
  746. const uint16_t num_col_a,
  747. const int32_t *const output_bias,
  748. q7_t *out_0);
  749. /**
  750. * @brief Common softmax function for s8 input and s8 or s16 output
  751. * @param[in] input Pointer to the input tensor
  752. * @param[in] num_rows Number of rows in the input tensor
  753. * @param[in] row_size Number of elements in each input row
  754. * @param[in] mult Input quantization multiplier
  755. * @param[in] shift Input quantization shift within the range [0, 31]
  756. * @param[in] diff_min Minimum difference with max in row. Used to check if
  757. * the quantized exponential operation can be performed
  758. * @param[in] int16_output Indicating s8 output if 0 else s16 output
  759. * @param[out] output Pointer to the output tensor
  760. *
  761. * @note Supported framework: TensorFlow Lite micro (bit-accurate)
  762. *
  763. */
  764. void arm_nn_softmax_common_s8(const int8_t *input,
  765. const int32_t num_rows,
  766. const int32_t row_size,
  767. const int32_t mult,
  768. const int32_t shift,
  769. const int32_t diff_min,
  770. const bool int16_output,
  771. void *output);
  772. /**
  773. * @brief macro for adding rounding offset
  774. */
  775. #ifndef ARM_NN_TRUNCATE
  776. #define NN_ROUND(out_shift) ((0x1 << out_shift) >> 1)
  777. #else
  778. #define NN_ROUND(out_shift) 0
  779. #endif
  780. // Macros for shortening quantization functions' names and avoid long lines
  781. #define MUL_SAT(a, b) arm_nn_doubling_high_mult((a), (b))
  782. #define MUL_SAT_MVE(a, b) arm_doubling_high_mult_mve_32x4((a), (b))
  783. #define MUL_POW2(a, b) arm_nn_mult_by_power_of_two((a), (b))
  784. #define DIV_POW2(a, b) arm_nn_divide_by_power_of_two((a), (b))
  785. #define DIV_POW2_MVE(a, b) arm_divide_by_power_of_two_mve((a), (b))
  786. #define EXP_ON_NEG(x) arm_nn_exp_on_negative_values((x))
  787. #define ONE_OVER1(x) arm_nn_one_over_one_plus_x_for_x_in_0_1((x))
  788. /**
  789. * @brief Saturating doubling high multiply. Result matches
  790. * NEON instruction VQRDMULH.
  791. * @param[in] m1 Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
  792. * @param[in] m2 Multiplier. Range: {NN_Q31_MIN, NN_Q31_MAX}
  793. * @return Result of multiplication.
  794. *
  795. */
  796. __STATIC_FORCEINLINE q31_t arm_nn_doubling_high_mult(const q31_t m1, const q31_t m2)
  797. {
  798. q31_t result = 0;
  799. // Rounding offset to add for a right shift of 31
  800. q63_t mult = 1 << 30;
  801. if ((m1 < 0) ^ (m2 < 0))
  802. {
  803. mult = 1 - mult;
  804. }
  805. // Gets resolved as a SMLAL instruction
  806. mult = mult + (q63_t)m1 * m2;
  807. // Utilize all of the upper 32 bits. This is the doubling step
  808. // as well.
  809. result = (int32_t)(mult / (1ll << 31));
  810. if ((m1 == m2) && (m1 == (int32_t)NN_Q31_MIN))
  811. {
  812. result = NN_Q31_MAX;
  813. }
  814. return result;
  815. }
  816. /**
  817. * @brief Doubling high multiply without saturation. This is intended
  818. * for requantization where the scale is a positive integer
  819. *
  820. * @param[in] m1 Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
  821. * @param[in] m2 Multiplier Range: {NN_Q31_MIN, NN_Q31_MAX}
  822. * @return Result of multiplication.
  823. * @note The result of this matches that of neon instruction
  824. * VQRDMULH for m1 in range {NN_Q31_MIN, NN_Q31_MAX} and m2 in
  825. * range {NN_Q31_MIN + 1, NN_Q31_MAX}. Saturation occurs when
  826. * m1 equals m2 equals NN_Q31_MIN and that is not handled by
  827. * this function.
  828. *
  829. */
  830. __STATIC_FORCEINLINE q31_t arm_nn_doubling_high_mult_no_sat(const q31_t m1, const q31_t m2)
  831. {
  832. q31_t result = 0;
  833. union arm_nn_long_long mult;
  834. // Rounding offset to add for a right shift of 31
  835. mult.word.low = 1 << 30;
  836. mult.word.high = 0;
  837. // Gets resolved as a SMLAL instruction
  838. mult.long_long = mult.long_long + (q63_t)m1 * m2;
  839. // Utilize all of the upper 32 bits. This is the doubling step
  840. // as well.
  841. result = (int32_t)(mult.long_long >> 31);
  842. return result;
  843. }
  844. /**
  845. * @brief Rounding divide by power of two.
  846. * @param[in] dividend - Dividend
  847. * @param[in] exponent - Divisor = power(2, exponent)
  848. * Range: [0, 31]
  849. * @return Rounded result of division. Midpoint is rounded away from zero.
  850. *
  851. */
  852. __STATIC_FORCEINLINE q31_t arm_nn_divide_by_power_of_two(const q31_t dividend, const q31_t exponent)
  853. {
  854. q31_t result = 0;
  855. const q31_t remainder_mask = (1 << exponent) - 1;
  856. int32_t remainder = remainder_mask & dividend;
  857. // Basic division
  858. result = dividend >> exponent;
  859. // Adjust 'result' for rounding (mid point away from zero)
  860. q31_t threshold = remainder_mask >> 1;
  861. if (result < 0)
  862. {
  863. threshold++;
  864. }
  865. if (remainder > threshold)
  866. {
  867. result++;
  868. }
  869. return result;
  870. }
  871. /**
  872. * @brief Requantize a given value.
  873. * @param[in] val Value to be requantized
  874. * @param[in] multiplier multiplier. Range {NN_Q31_MIN + 1, Q32_MAX}
  875. * @param[in] shift left or right shift for 'val * multiplier'
  876. *
  877. * @return Returns (val * multiplier)/(2 ^ shift)
  878. *
  879. */
  880. __STATIC_FORCEINLINE q31_t arm_nn_requantize(const q31_t val, const q31_t multiplier, const q31_t shift)
  881. {
  882. #ifdef CMSIS_NN_USE_SINGLE_ROUNDING
  883. const int64_t total_shift = 31 - shift;
  884. const int64_t new_val = val * (int64_t)multiplier;
  885. int32_t result = new_val >> (total_shift - 1);
  886. result = (result + 1) >> 1;
  887. return result;
  888. #else
  889. return arm_nn_divide_by_power_of_two(arm_nn_doubling_high_mult_no_sat(val * (1 << LEFT_SHIFT(shift)), multiplier),
  890. RIGHT_SHIFT(shift));
  891. #endif
  892. }
  893. /**
  894. * @brief Requantize a given 64 bit value.
  895. * @param[in] val Value to be requantized in the range {-(1<<47)} to {(1<<47) - 1}
  896. * @param[in] reduced_multiplier Reduced multiplier in the range {NN_Q31_MIN + 1, Q32_MAX} to {Q16_MIN + 1,
  897. * Q16_MAX}
  898. * @param[in] shift Left or right shift for 'val * multiplier' in the range {-31} to {7}
  899. *
  900. * @return Returns (val * multiplier)/(2 ^ shift)
  901. *
  902. */
  903. __STATIC_FORCEINLINE q31_t arm_nn_requantize_s64(const q63_t val, const q31_t reduced_multiplier, const q31_t shift)
  904. {
  905. const q63_t new_val = val * reduced_multiplier;
  906. q31_t result = new_val >> (14 - shift); // 64->32 bit reduction
  907. result = (result + 1) >> 1; // Last shift position and insert round
  908. return result;
  909. }
  910. /**
  911. * @brief memcpy optimized for MVE
  912. * @param[in, out] dst Destination pointer
  913. * @param[in] src Source pointer.
  914. * @param[in] block_size Number of bytes to copy.
  915. *
  916. */
  917. __STATIC_FORCEINLINE void arm_memcpy_q7(q7_t *__RESTRICT dst, const q7_t *__RESTRICT src, uint32_t block_size)
  918. {
  919. #if defined(ARM_MATH_MVEI)
  920. __asm volatile(" wlstp.8 lr, %[cnt], 1f \n"
  921. "2: \n"
  922. " vldrb.8 q0, [%[in]], #16 \n"
  923. " vstrb.8 q0, [%[out]], #16 \n"
  924. " letp lr, 2b \n"
  925. "1: \n"
  926. : [ in ] "+r"(src), [ out ] "+r"(dst)
  927. : [ cnt ] "r"(block_size)
  928. : "q0", "memory", "r14");
  929. #else
  930. memcpy(dst, src, block_size);
  931. #endif
  932. }
  933. #if defined(ARM_MATH_MVEI)
  934. /**
  935. * @brief Vector saturating doubling high multiply returning high half.
  936. * @param[in] m1 Multiplicand
  937. * @param[in] m2 Multiplier
  938. * @return Result of multiplication.
  939. *
  940. */
  941. __STATIC_FORCEINLINE int32x4_t arm_doubling_high_mult_mve(const int32x4_t m1, const q31_t m2)
  942. {
  943. return vqrdmulhq_n_s32(m1, m2);
  944. }
  945. /**
  946. * @brief Vector rounding divide by power of two.
  947. * @param[in] dividend - Dividend vector
  948. * @param[in] exponent - Divisor = power(2, exponent)
  949. * Range: [0, 31]
  950. * @return Rounded result of division. Midpoint is rounded away from zero.
  951. *
  952. */
  953. __STATIC_FORCEINLINE int32x4_t arm_divide_by_power_of_two_mve(const int32x4_t dividend, const q31_t exponent)
  954. {
  955. const int32x4_t shift = vdupq_n_s32(-exponent);
  956. const int32x4_t fixup = vshrq_n_s32(vandq_s32(dividend, shift), 31);
  957. const int32x4_t fixed_up_dividend = vqaddq_s32(dividend, fixup);
  958. return vrshlq_s32(fixed_up_dividend, shift);
  959. }
  960. /**
  961. * @brief Requantize a given vector.
  962. * @param[in] val Vector to be requantized
  963. * @param[in] multiplier multiplier
  964. * @param[in] shift shift
  965. *
  966. * @return Returns (val * multiplier)/(2 ^ shift)
  967. *
  968. */
  969. __STATIC_FORCEINLINE int32x4_t arm_requantize_mve(const int32x4_t val, const q31_t multiplier, const q31_t shift)
  970. {
  971. #ifdef CMSIS_NN_USE_SINGLE_ROUNDING
  972. const int right_shift = MIN(-1, shift);
  973. const int left_shift = shift - right_shift;
  974. const int32x4_t left_shift_dup = vdupq_n_s32(left_shift);
  975. const int32x4_t right_shift_dup = vdupq_n_s32(right_shift);
  976. int32x4_t result = vqdmulhq_n_s32(vshlq_s32(val, left_shift_dup), multiplier);
  977. result = vrshlq_s32(result, right_shift_dup);
  978. return result;
  979. #else
  980. return arm_divide_by_power_of_two_mve(
  981. arm_doubling_high_mult_mve(vshlq_s32(val, vdupq_n_s32(LEFT_SHIFT(shift))), multiplier), RIGHT_SHIFT(shift));
  982. #endif
  983. }
  984. __STATIC_FORCEINLINE int32x4_t arm_doubling_high_mult_mve_32x4(const int32x4_t m1, const int32x4_t m2)
  985. {
  986. return vqrdmulhq_s32(m1, m2);
  987. }
  988. __STATIC_FORCEINLINE int32x4_t arm_divide_by_power_of_two_mve_32x4(const int32x4_t dividend, const int32x4_t exponent)
  989. {
  990. const int32x4_t shift = -exponent;
  991. const int32x4_t fixup = vshrq_n_s32(vandq_s32(dividend, shift), 31);
  992. const int32x4_t fixed_up_dividend = vqaddq_s32(dividend, fixup);
  993. return vrshlq_s32(fixed_up_dividend, shift);
  994. }
  995. __STATIC_FORCEINLINE int32x4_t arm_requantize_mve_32x4(const int32x4_t val,
  996. const int32x4_t multiplier,
  997. const int32x4_t shift)
  998. {
  999. #ifdef CMSIS_NN_USE_SINGLE_ROUNDING
  1000. const int32x4_t right_shift = vminq_s32(vdupq_n_s32(-1), shift);
  1001. const int32x4_t left_shift = vqsubq_s32(shift, right_shift);
  1002. int32x4_t result = vqdmulhq_s32(vshlq_s32(val, left_shift), multiplier);
  1003. result = vrshlq_s32(result, right_shift);
  1004. return result;
  1005. #else
  1006. const int32x4_t zz = vdupq_n_s32(0);
  1007. const mve_pred16_t p = vcmpgtq_n_s32(shift, 0);
  1008. const int32x4_t left_shift = vpselq_s32(shift, zz, p);
  1009. const int32x4_t right_shift = -vpselq_s32(zz, shift, p);
  1010. return arm_divide_by_power_of_two_mve_32x4(arm_doubling_high_mult_mve_32x4(vshlq_s32(val, left_shift), multiplier),
  1011. right_shift);
  1012. #endif
  1013. }
  1014. #endif
  1015. // @note The following functions are used only for softmax layer, scaled bits = 5 assumed
  1016. __STATIC_FORCEINLINE int32_t arm_nn_exp_on_negative_values(int32_t val)
  1017. {
  1018. int32_t mask = 0;
  1019. int32_t shift = 24;
  1020. const int32_t val_mod_minus_quarter = (val & ((1 << shift) - 1)) - (1 << shift);
  1021. const int32_t remainder = val_mod_minus_quarter - val;
  1022. const int32_t x = (val_mod_minus_quarter << 5) + (1 << 28);
  1023. const int32_t x2 = MUL_SAT(x, x);
  1024. int32_t result = 1895147668 +
  1025. MUL_SAT(1895147668, x + DIV_POW2(MUL_SAT(DIV_POW2(MUL_SAT(x2, x2), 2) + MUL_SAT(x2, x), 715827883) + x2, 1));
  1026. #define SELECT_IF_NON_ZERO(x) \
  1027. { \
  1028. mask = MASK_IF_NON_ZERO(remainder & (1 << shift++)); \
  1029. result = SELECT_USING_MASK(mask, MUL_SAT(result, x), result); \
  1030. }
  1031. SELECT_IF_NON_ZERO(1672461947)
  1032. SELECT_IF_NON_ZERO(1302514674)
  1033. SELECT_IF_NON_ZERO(790015084)
  1034. SELECT_IF_NON_ZERO(290630308)
  1035. SELECT_IF_NON_ZERO(39332535)
  1036. SELECT_IF_NON_ZERO(720401)
  1037. SELECT_IF_NON_ZERO(242)
  1038. #undef SELECT_IF_NON_ZERO
  1039. mask = MASK_IF_ZERO(val);
  1040. return SELECT_USING_MASK(mask, NN_Q31_MAX, result);
  1041. }
  1042. __STATIC_FORCEINLINE q31_t arm_nn_mult_by_power_of_two(const int32_t val, const int32_t exp)
  1043. {
  1044. const int32_t thresh = ((1 << (31 - exp)) - 1);
  1045. int32_t result = val << exp;
  1046. result = SELECT_USING_MASK(MASK_IF_NON_ZERO(val > thresh), NN_Q31_MAX, result);
  1047. result = SELECT_USING_MASK(MASK_IF_NON_ZERO(val < -thresh), NN_Q31_MIN, result);
  1048. return result;
  1049. }
  1050. __STATIC_FORCEINLINE int32_t arm_nn_one_over_one_plus_x_for_x_in_0_1(int32_t val)
  1051. {
  1052. const int64_t sum = (int64_t)val + (int64_t)NN_Q31_MAX;
  1053. const int32_t half_denominator = (int32_t)((sum + (sum >= 0 ? 1 : -1)) / 2L);
  1054. int32_t x = 1515870810 + MUL_SAT(half_denominator, -1010580540);
  1055. const int32_t shift = (1 << 29);
  1056. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1057. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1058. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1059. return MUL_POW2(x, 1);
  1060. }
  1061. /**
  1062. @brief Write 2 q15 elements and post increment pointer.
  1063. @param[in] dest_q15 Pointer to pointer that holds address of destination.
  1064. @param[in] src_q31 Input value to be written.
  1065. */
  1066. __STATIC_FORCEINLINE void arm_nn_write_q15x2_ia(q15_t **dest_q15, q31_t src_q31)
  1067. {
  1068. q31_t val = src_q31;
  1069. memcpy(*dest_q15, &val, 4);
  1070. *dest_q15 += 2;
  1071. }
  1072. #ifdef __cplusplus
  1073. }
  1074. #endif
  1075. #endif