riscv_nnsupportfunctions.h 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2010-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
  3. * Copyright (c) 2022 Nuclei Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the License); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /* ----------------------------------------------------------------------
  20. * Project: NMSIS NN Library
  21. * Title: riscv_nnsupportfunctions.h
  22. * Description: Public header file of support functions for NMSIS NN Library
  23. *
  24. * $Date: 30 April 2024
  25. * $Revision: V.22.0.0
  26. *
  27. * Target Processor: RISC-V Cores
  28. * -------------------------------------------------------------------- */
  29. #ifndef RISCV_NNSUPPORTFUNCTIONS_H
  30. #define RISCV_NNSUPPORTFUNCTIONS_H
  31. #include "riscv_nn_math_types.h"
  32. #include "riscv_nn_types.h"
  33. #include <stdbool.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #define USE_FAST_DW_CONV_S16_FUNCTION(dw_conv_params, filter_dims, input_dims) \
  38. (dw_conv_params->ch_mult == 1 && dw_conv_params->dilation.w == 1 && dw_conv_params->dilation.h == 1 && \
  39. filter_dims->w * filter_dims->h < 512)
  40. #define LEFT_SHIFT(_shift) (_shift > 0 ? _shift : 0)
  41. #define RIGHT_SHIFT(_shift) (_shift > 0 ? 0 : -_shift)
  42. #define MASK_IF_ZERO(x) (x) == 0 ? ~0 : 0
  43. #define MASK_IF_NON_ZERO(x) (x) != 0 ? ~0 : 0
  44. #define SELECT_USING_MASK(mask, a, b) ((mask) & (a)) ^ (~(mask) & (b))
  45. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  46. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  47. #define CLAMP(x, h, l) MAX(MIN((x), (h)), (l))
  48. #define REDUCE_MULTIPLIER(_mult) ((_mult < 0x7FFF0000) ? ((_mult + (1 << 15)) >> 16) : 0x7FFF)
  49. /*
  50. * Use RVV may help if data length > RVV_OPT_THRESHOLD, otherwise use pure C version
  51. * RVV_OPT_THRESHOLD could be {0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F ...}
  52. */
  53. #define RVV_OPT_THRESHOLD 0xF
  54. // For input of int16 when number of columns are above this limit int64 accumulation is needed
  55. // to not loose precision.
  56. #define MAX_COL_COUNT (512)
  57. /**
  58. * @brief definition to pack four 8 bit values.
  59. */
  60. #define PACK_S8x4_32x1(v0, v1, v2, v3) \
  61. ((((int32_t)(v0) << 0) & (int32_t)0x000000FF) | (((int32_t)(v1) << 8) & (int32_t)0x0000FF00) | \
  62. (((int32_t)(v2) << 16) & (int32_t)0x00FF0000) | (((int32_t)(v3) << 24) & (int32_t)0xFF000000))
  63. /**
  64. * @brief definition to pack two 16 bit values.
  65. */
  66. #define PACK_Q15x2_32x1(v0, v1) (((int32_t)v0 & (int32_t)0xFFFF) | ((int32_t)v1 << 16))
  67. /**
  68. * @defgroup groupSupport Private
  69. *
  70. * Internal Support functions. Not intended to be called direclty by a NMSIS-NN user.
  71. *
  72. */
  73. /**
  74. * @defgroup genPrivTypes Structure Types
  75. * @ingroup groupSupport
  76. * @brief Data structure types used by private functions.
  77. * @{
  78. */
  79. /**
  80. * @brief Union for SIMD access of q31/s16/s8 types
  81. */
  82. union riscv_nnword
  83. {
  84. int32_t word;
  85. /**< q31 type */
  86. int16_t half_words[2];
  87. /**< s16 type */
  88. int8_t bytes[4];
  89. /**< s8 type */
  90. };
  91. /**
  92. * @brief Union for data type long long
  93. */
  94. struct riscv_nn_double
  95. {
  96. uint32_t low;
  97. int32_t high;
  98. };
  99. union riscv_nn_long_long
  100. {
  101. int64_t long_long;
  102. struct riscv_nn_double word;
  103. };
  104. #ifndef RISCV_MATH_DSP
  105. /**
  106. * @brief definition to pack two 16 bit values.
  107. */
  108. #define __NN_PKHBT(ARG1, ARG2, ARG3) ( (((int32_t)(ARG1) << 0) & (int32_t)0x0000FFFF) | \
  109. (((int32_t)(ARG2) << ARG3) & (int32_t)0xFFFF0000) )
  110. #define __NN_PKHTB(ARG1, ARG2, ARG3) ( (((int32_t)(ARG1) << 0) & (int32_t)0xFFFF0000) | \
  111. (((int32_t)(ARG2) >> ARG3) & (int32_t)0x0000FFFF) )
  112. /**
  113. * @brief Clips Q63 to Q31 values.
  114. */
  115. __STATIC_FORCEINLINE q31_t nn_clip_q63_to_q31(
  116. q63_t x)
  117. {
  118. return ((q31_t) (x >> 32) != ((q31_t) x >> 31)) ?
  119. ((0x7FFFFFFF ^ ((q31_t) (x >> 63)))) : (q31_t) x;
  120. }
  121. /*
  122. * @brief C custom defined QADD
  123. */
  124. __STATIC_FORCEINLINE int32_t __NN_QADD(
  125. int32_t x,
  126. int32_t y)
  127. {
  128. return ((int32_t)(nn_clip_q63_to_q31((q63_t)x + (q31_t)y)));
  129. }
  130. /*
  131. * @brief C custom defined QADD16
  132. */
  133. __STATIC_FORCEINLINE uint32_t __NN_QADD16(
  134. uint32_t x,
  135. uint32_t y)
  136. {
  137. /* q31_t r, s; without initialisation 'riscv_offset_q15 test' fails but 'intrinsic' tests pass! */
  138. q31_t r = 0, s = 0;
  139. r = __SSAT(((((q31_t)x << 16) >> 16) + (((q31_t)y << 16) >> 16)), 16) & (int32_t)0x0000FFFF;
  140. s = __SSAT(((((q31_t)x ) >> 16) + (((q31_t)y ) >> 16)), 16) & (int32_t)0x0000FFFF;
  141. return ((uint32_t)((s << 16) | (r )));
  142. }
  143. /*
  144. * @brief C custom defined SXTB16
  145. */
  146. __STATIC_FORCEINLINE uint32_t __NN_SXTB16(
  147. uint32_t x)
  148. {
  149. return ((uint32_t)(((((q31_t)x << 24) >> 24) & (q31_t)0x0000FFFF) |
  150. ((((q31_t)x << 8) >> 8) & (q31_t)0xFFFF0000) ));
  151. }
  152. #else
  153. #define __NN_PKHBT __PKHBT
  154. #define __NN_PKHTB __PKHTB
  155. #define __NN_QADD __QADD
  156. #define __NN_QADD16 __QADD16
  157. #define __NN_SXTB16 __SXTB16
  158. #endif
  159. /**
  160. * @brief definition to pack four 8 bit values.
  161. */
  162. #define __NN_PACKq7(v0,v1,v2,v3) ( (((int32_t)(v0) << 0) & (int32_t)0x000000FF) | \
  163. (((int32_t)(v1) << 8) & (int32_t)0x0000FF00) | \
  164. (((int32_t)(v2) << 16) & (int32_t)0x00FF0000) | \
  165. (((int32_t)(v3) << 24) & (int32_t)0xFF000000) )
  166. /**
  167. * @} // end group groupPrivTypes
  168. */
  169. /**
  170. * @defgroup supportConversion Data Conversion
  171. *
  172. * Perform data type conversion in-between neural network operations
  173. *
  174. */
  175. /**
  176. * @brief Converts the elements of the q7 vector to q15 vector without left-shift
  177. * @param[in] *pSrc points to the q7 input vector
  178. * @param[out] *pDst points to the q15 output vector
  179. * @param[in] blockSize length of the input vector
  180. *
  181. */
  182. void riscv_q7_to_q15_no_shift(const q7_t *pSrc, q15_t *pDst, uint32_t blockSize);
  183. void riscv_q7_to_q7_no_shift(const q7_t * pSrc, q7_t * pDst, uint32_t blockSize);
  184. /**
  185. * @brief Non-saturating addition of elements of a q7 vector
  186. * @param[in] *input Pointer to the q7 input vector
  187. * @param[out] *output Pointer to the q31 output variable.
  188. * @param[in] block_size length of the input vector
  189. * \par Description:
  190. *
  191. * 2^24 samples can be added without saturating the result.
  192. *
  193. * The equation used for the conversion process is:
  194. *
  195. * <pre>
  196. * sum = input[0] + input[1] + .. + input[block_size -1]
  197. * </pre>
  198. *
  199. * */
  200. void riscv_nn_add_q7(const q7_t *input, q31_t *output, uint32_t block_size);
  201. /**
  202. * @brief Converts the elements of the s8 vector to reordered q15 vector without left-shift
  203. * @param[in] *pSrc points to the s8 input vector
  204. * @param[out] *pDst points to the s16 output vector
  205. * @param[in] blockSize length of the input vector
  206. * @return none.
  207. *
  208. */
  209. void riscv_q7_to_q15_reordered_no_shift(const q7_t *pSrc, q15_t *pDst, uint32_t blockSize);
  210. void riscv_q7_to_q7_reordered_no_shift(const q7_t * pSrc, q7_t * pDst, uint32_t blockSize);
  211. /**
  212. * @brief Converts the elements from a s8 vector to a s16 vector with an added offset
  213. * @param[in] src pointer to the s8 input vector
  214. * @param[out] dst pointer to the s16 output vector
  215. * @param[in] block_size length of the input vector
  216. * @param[in] offset s16 offset to be added to each input vector element.
  217. *
  218. * \par Description:
  219. *
  220. * Output elements are ordered.
  221. * The equation used for the conversion process is:
  222. *
  223. * <pre>
  224. * dst[n] = (int16_t) src[n] + offset; 0 <= n < block_size.
  225. * </pre>
  226. *
  227. */
  228. void riscv_q7_to_q15_with_offset(const int8_t *src, int16_t *dst, int32_t block_size, int16_t offset);
  229. #if defined(RISCV_MATH_DSP)
  230. /**
  231. * @brief Converts the elements from a s8 vector to a s16 vector with an added offset
  232. * @param[in] src pointer to the s8 input vector
  233. * @param[out] dst pointer to the s16 output vector
  234. * @param[in] block_size length of the input vector
  235. * @param[in] offset s16 offset to be added to each input vector element.
  236. *
  237. * \par Description:
  238. *
  239. * No additonal ordering is done with the result that output elements are not in order.
  240. * Instead of ABCD order will be ACBD.
  241. * Note this is for processors with DSP extension only.
  242. * The equation used for the conversion process is:
  243. *
  244. * <pre>
  245. * dst[n - 0] = (int16_t) src[n - 0] + offset; 0 <= n < block_size.
  246. * dst[n - 1] = (int16_t) src[n - 2] + offset; 0 <= n < block_size.
  247. * dst[n - 2] = (int16_t) src[n - 1] + offset; 0 <= n < block_size.
  248. * dst[n - 3] = (int16_t) src[n - 3] + offset; 0 <= n < block_size.
  249. * </pre>
  250. *
  251. */
  252. void riscv_s8_to_s16_unordered_with_offset(const int8_t *src, int16_t *dst, int32_t block_size, int16_t offset);
  253. #endif
  254. /**
  255. * @brief Get the required buffer size for optimized s8 depthwise convolution
  256. * function with constraint that in_channel equals out_channel.
  257. * This is for processors with DSP extension.
  258. * Refer to riscv_depthwise_conv_s8_opt_get_buffer_size() for function argument details.
  259. *
  260. * @note Intended for compilation on Host. If compiling for an Riscv target, use
  261. * riscv_depthwise_conv_s8_opt_get_buffer_size(). Note also this is a support function,
  262. * so not recommended to call directly even on Host.
  263. *
  264. */
  265. int32_t riscv_depthwise_conv_s8_opt_get_buffer_size_dsp(const nmsis_nn_dims *input_dims,
  266. const nmsis_nn_dims *filter_dims);
  267. /**
  268. * @brief Converts the elements from a s8 vector to a s16 vector with an added offset
  269. * @param[in] src pointer to the s8 input vector
  270. * @param[out] dst pointer to the s16 output vector
  271. * @param[in] block_size length of the input vector
  272. * @param[in] offset offset to be added to each input vector element.
  273. * @return none.
  274. *
  275. * @details This function does the q7 to q15 expansion with re-ordering of bytes. Re-ordering is a consequence of
  276. * the sign extension intrinsic(DSP extension). The tail (i.e., last (N % 4) elements) retains its
  277. * original order.
  278. *
  279. */
  280. void riscv_q7_to_q15_reordered_with_offset(const q7_t *src, q15_t *dst, uint32_t block_size, q15_t offset);
  281. /**
  282. * @brief Converts the elements from a q7 vector and accumulate to a q15 vector
  283. * @param[in] *src points to the q7 input vector
  284. * @param[out] *dst points to the q15 output vector
  285. * @param[in] block_size length of the input vector
  286. *
  287. * \par Description:
  288. *
  289. * The equation used for the conversion process is:
  290. *
  291. * <pre>
  292. * dst[n] += (q15_t) src[n] ; 0 <= n < block_size.
  293. * </pre>
  294. *
  295. */
  296. void riscv_nn_accumulate_q7_to_q15(q15_t *dst, const q7_t *src, uint32_t block_size);
  297. /**
  298. * @brief Depthwise conv on an im2col buffer where the input channel equals output channel.
  299. * @param[in] row pointer to row
  300. * @param[in] col pointer to im2col buffer, always consists of 2 columns.
  301. * @param[in] num_ch number of channels
  302. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  303. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  304. * @param[in] out_offset output tensor offset.
  305. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  306. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  307. * @param[in] kernel_size number of elements in one column.
  308. * @param[in] output_bias per output channel bias. Range : int32
  309. * @param[out] out pointer to output
  310. * @return The function returns one of the two
  311. * 1. The incremented output pointer for a successful operation or
  312. * 2. NULL if implementation is not available.
  313. *
  314. * @details Supported framework: TensorFlow Lite micro.
  315. */
  316. int8_t *riscv_nn_depthwise_conv_s8_core(const int8_t *row,
  317. const int16_t *col,
  318. const uint16_t num_ch,
  319. const int32_t *out_shift,
  320. const int32_t *out_mult,
  321. const int32_t out_offset,
  322. const int32_t activation_min,
  323. const int32_t activation_max,
  324. const uint16_t kernel_size,
  325. const int32_t *const output_bias,
  326. int8_t *out);
  327. /**
  328. * @brief General Matrix-multiplication function with per-channel requantization.
  329. * @param[in] input_row pointer to row operand
  330. * @param[in] input_col pointer to col operand
  331. * @param[in] output_ch number of rows of input_row
  332. * @param[in] col_batches number of column batches. Range: 1 to 4
  333. * @param[in] output_shift pointer to per output channel requantization shift parameter.
  334. * @param[in] output_mult pointer to per output channel requantization multiplier parameter.
  335. * @param[in] out_offset output tensor offset.
  336. * @param[in] col_offset input tensor(col) offset.
  337. * @param[in] row_offset kernel offset(row). Not used.
  338. * @param[in] out_activation_min minimum value to clamp the output to. Range : int8
  339. * @param[in] out_activation_max maximum value to clamp the output to. Range : int8
  340. * @param[in] row_len number of elements in each row
  341. * @param[in] bias per output channel bias. Range : int32
  342. * @param[in,out] out pointer to output
  343. * @return The function returns one of the two
  344. * 1. The incremented output pointer for a successful operation or
  345. * 2. NULL if implementation is not available.
  346. *
  347. * @details Supported framework: TensorFlow Lite
  348. */
  349. int8_t *riscv_nn_mat_mult_s8(const int8_t *input_row,
  350. const int8_t *input_col,
  351. const uint16_t output_ch,
  352. const uint16_t col_batches,
  353. const int32_t *output_shift,
  354. const int32_t *output_mult,
  355. const int32_t out_offset,
  356. const int32_t col_offset,
  357. const int32_t row_offset,
  358. const int16_t out_activation_min,
  359. const int16_t out_activation_max,
  360. const uint16_t row_len,
  361. const int32_t *const bias,
  362. int8_t *out);
  363. /**
  364. * @brief Matrix-multiplication function for convolution with per-channel requantization for 16 bits convolution.
  365. * @param[in] input_a pointer to operand A
  366. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  367. * @param[in] output_ch number of rows of A
  368. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  369. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  370. * @param[in] activation_min minimum value to clamp the output to. Range : int16
  371. * @param[in] activation_max maximum value to clamp the output to. Range : int16
  372. * @param[in] num_col_a number of columns of A
  373. * @param[in] bias_data pointer to struct with bias vector. The length of this vector is equal to the number
  374. * of output columns (or RHS input rows). The vector can be int32 or int64 indicated by a
  375. * flag in the struct.
  376. * @param[in,out] out_0 pointer to output
  377. * @return The function returns one of the two
  378. * 1. The incremented output pointer for a successful operation or
  379. * 2. NULL if implementation is not available.
  380. *
  381. * @details This function does the matrix multiplication of weight matrix for all output channels
  382. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  383. * clamped in the range provided by activation min and max.
  384. * Supported framework: TensorFlow Lite micro.
  385. */
  386. int16_t *riscv_nn_mat_mult_kernel_s16(const int8_t *input_a,
  387. const int16_t *input_b,
  388. const int32_t output_ch,
  389. const int32_t *out_shift,
  390. const int32_t *out_mult,
  391. const int32_t activation_min,
  392. const int32_t activation_max,
  393. const int32_t num_col_a,
  394. const nmsis_nn_bias_data *const bias_data,
  395. int16_t *out_0);
  396. /**
  397. * @brief General Vector by Matrix multiplication with requantization and storage of result.
  398. * @param[in] row_elements number of row elements
  399. * @param[in] skipped_row_elements number of row elements skipped due to padding.
  400. * row_elements + skipped_row_elements = (kernel_x * kernel_y) * input_ch
  401. * @param[in] row_base_ref pointer to row operand
  402. * @param[in] col_base_ref pointer to col operand
  403. * @param[out] out_ch Number of output channels
  404. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  405. * @param[in] quant_params Pointer to per-channel quantization parameters
  406. * @param[in] bias Pointer to optional per-channel bias
  407. * @param[out] output Pointer to output where int8 results are stored.
  408. * @return The function performs matrix(row_base_ref) multiplication with vector(col_base_ref) and
  409. * scaled result is stored in memory.
  410. *
  411. * @details Pseudo-code
  412. * *output = 0
  413. * sum_col = 0
  414. * for (j = 0; j < out_ch; j++)
  415. * for (i = 0; i < row_elements; i++)
  416. * *output += row_base_ref[i] * col_base_ref[i]
  417. * sum_col += col_base_ref[i]
  418. * scale sum_col using quant_params and bias
  419. * store result in 'output'
  420. *
  421. *
  422. */
  423. riscv_nmsis_nn_status riscv_nn_mat_mul_core_1x_s8(int32_t row_elements,
  424. const int32_t skipped_row_elements,
  425. const int8_t *row_base_ref,
  426. const int8_t *col_base_ref,
  427. const int32_t out_ch,
  428. const nmsis_nn_conv_params *conv_params,
  429. const nmsis_nn_per_channel_quant_params *quant_params,
  430. const int32_t *bias,
  431. int8_t *output);
  432. /**
  433. * @brief General Vector by Matrix multiplication with requantization, storage of result and int4 weights packed into an
  434. * int8 buffer.
  435. * @param[in] row_elements number of row elements
  436. * @param[in] skipped_row_elements number of row elements skipped due to padding.
  437. * row_elements + skipped_row_elements = (kernel_x * kernel_y) * input_ch
  438. * @param[in] row_base_ref pointer to row operand
  439. * @param[in] col_base_ref pointer to col operand as packed int4
  440. * @param[out] out_ch Number of output channels
  441. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  442. * @param[in] quant_params Pointer to per-channel quantization parameters
  443. * @param[in] bias Pointer to optional per-channel bias
  444. * @param[out] output Pointer to output where int8 results are stored.
  445. * @return The function performs matrix(row_base_ref) multiplication with vector(col_base_ref) and
  446. * scaled result is stored in memory.
  447. *
  448. * @details Pseudo-code as int8 example. Int4 filter data will be unpacked.
  449. * *output = 0
  450. * sum_col = 0
  451. * for (j = 0; j < out_ch; j++)
  452. * for (i = 0; i < row_elements; i++)
  453. * *output += row_base_ref[i] * col_base_ref[i]
  454. * sum_col += col_base_ref[i]
  455. * scale sum_col using quant_params and bias
  456. * store result in 'output'
  457. *
  458. *
  459. */
  460. riscv_nmsis_nn_status riscv_nn_mat_mul_core_1x_s4(int32_t row_elements,
  461. const int32_t skipped_row_elements,
  462. const int8_t *row_base_ref,
  463. const int8_t *col_base_ref,
  464. const int32_t out_ch,
  465. const nmsis_nn_conv_params *conv_params,
  466. const nmsis_nn_per_channel_quant_params *quant_params,
  467. const int32_t *bias,
  468. int8_t *output);
  469. /**
  470. * @brief Matrix-multiplication with requantization & activation function for four rows and one column
  471. * @param[in] row_elements number of row elements
  472. * @param[in] offset offset between rows. Can be the same as row_elements.
  473. * For e.g, in a 1x1 conv scenario with stride as 1.
  474. * @param[in] row_base pointer to row operand
  475. * @param[in] col_base pointer to col operand
  476. * @param[in] out_ch Number of output channels
  477. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  478. * @param[in] quant_params Pointer to per-channel quantization parameters
  479. * @param[in] bias Pointer to per-channel bias
  480. * @param[out] output Pointer to output where int8 results are stored.
  481. *
  482. * @return The function returns the updated output pointer or NULL if implementation is not available.
  483. *
  484. * @details Compliant to TFLM int8 specification. MVE implementation only
  485. */
  486. int8_t *riscv_nn_mat_mul_core_4x_s8(const int32_t row_elements,
  487. const int32_t offset,
  488. const int8_t *row_base,
  489. const int8_t *col_base,
  490. const int32_t out_ch,
  491. const nmsis_nn_conv_params *conv_params,
  492. const nmsis_nn_per_channel_quant_params *quant_params,
  493. const int32_t *bias,
  494. int8_t *output);
  495. /**
  496. * @brief General Matrix-multiplication function with per-channel requantization.
  497. * This function assumes:
  498. * - LHS input matrix NOT transposed (nt)
  499. * - RHS input matrix transposed (t)
  500. * - RHS is int8 packed with 2x int4
  501. * - LHS is int8
  502. *
  503. * @note This operation also performs the broadcast bias addition before the requantization
  504. *
  505. * @param[in] lhs Pointer to the LHS input matrix
  506. * @param[in] rhs Pointer to the RHS input matrix
  507. * @param[in] bias Pointer to the bias vector. The length of this vector is equal to the number of
  508. * output columns (or RHS input rows)
  509. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  510. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  511. * The length of this vector is equal to the number of output columns (or RHS input
  512. * rows)
  513. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  514. * of this vector is equal to the number of output columns (or RHS input rows)
  515. * @param[in] lhs_rows Number of LHS input rows
  516. * @param[in] rhs_rows Number of RHS input rows
  517. * @param[in] rhs_cols Number of LHS/RHS input columns
  518. * @param[in] lhs_offset Offset to be applied to the LHS input value
  519. * @param[in] dst_offset Offset to be applied the output result
  520. * @param[in] activation_min Minimum value to clamp down the output. Range : int8
  521. * @param[in] activation_max Maximum value to clamp up the output. Range : int8
  522. * @param[in] lhs_cols_offset Column offset between subsequent lhs_rows
  523. *
  524. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  525. *
  526. */
  527. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_t_s4(const int8_t *lhs,
  528. const int8_t *rhs,
  529. const int32_t *bias,
  530. int8_t *dst,
  531. const int32_t *dst_multipliers,
  532. const int32_t *dst_shifts,
  533. const int32_t lhs_rows,
  534. const int32_t rhs_rows,
  535. const int32_t rhs_cols,
  536. const int32_t lhs_offset,
  537. const int32_t dst_offset,
  538. const int32_t activation_min,
  539. const int32_t activation_max,
  540. const int32_t lhs_cols_offset);
  541. /**
  542. * @brief General Matrix-multiplication function with per-channel requantization.
  543. * This function assumes:
  544. * - LHS input matrix NOT transposed (nt)
  545. * - RHS input matrix transposed (t)
  546. *
  547. * @note This operation also performs the broadcast bias addition before the requantization
  548. *
  549. * @param[in] lhs Pointer to the LHS input matrix
  550. * @param[in] rhs Pointer to the RHS input matrix
  551. * @param[in] bias Pointer to the bias vector. The length of this vector is equal to the number of
  552. * output columns (or RHS input rows)
  553. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  554. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  555. * The length of this vector is equal to the number of output columns (or RHS input
  556. * rows)
  557. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  558. * of this vector is equal to the number of output columns (or RHS input rows)
  559. * @param[in] lhs_rows Number of LHS input rows
  560. * @param[in] rhs_rows Number of RHS input rows
  561. * @param[in] rhs_cols Number of LHS/RHS input columns
  562. * @param[in] lhs_offset Offset to be applied to the LHS input value
  563. * @param[in] dst_offset Offset to be applied the output result
  564. * @param[in] activation_min Minimum value to clamp down the output. Range : int8
  565. * @param[in] activation_max Maximum value to clamp up the output. Range : int8
  566. * @param[in] row_address_offset Address offset between rows in output.
  567. * @param[in] lhs_cols_offset Column offset between subsequent lhs_rows
  568. *
  569. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  570. *
  571. */
  572. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_t_s8(const int8_t *lhs,
  573. const int8_t *rhs,
  574. const int32_t *bias,
  575. int8_t *dst,
  576. const int32_t *dst_multipliers,
  577. const int32_t *dst_shifts,
  578. const int32_t lhs_rows,
  579. const int32_t rhs_rows,
  580. const int32_t rhs_cols,
  581. const int32_t lhs_offset,
  582. const int32_t dst_offset,
  583. const int32_t activation_min,
  584. const int32_t activation_max,
  585. const int32_t row_address_offset,
  586. const int32_t lhs_cols_offset);
  587. /**
  588. * @brief General Matrix-multiplication function with per-channel requantization and int16 input (LHS) and output.
  589. * This function assumes:
  590. * - LHS input matrix NOT transposed (nt)
  591. * - RHS input matrix transposed (t)
  592. *
  593. * @note This operation also performs the broadcast bias addition before the requantization
  594. *
  595. * @param[in] lhs Pointer to the LHS input matrix
  596. * @param[in] rhs Pointer to the RHS input matrix
  597. * @param[in] bias_data Pointer to struct with bias vector. The length of this vector is equal to the number
  598. * of output columns (or RHS input rows). The vector can be int32 or int64 indicated by a
  599. * flag in the struct.
  600. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  601. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  602. * The length of this vector is equal to the number of output columns (or RHS input
  603. * rows)
  604. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  605. * of this vector is equal to the number of output columns (or RHS input rows)
  606. * @param[in] lhs_rows Number of LHS input rows
  607. * @param[in] rhs_rows Number of RHS input rows
  608. * @param[in] rhs_cols Number of LHS/RHS input columns
  609. * @param[in] activation_min Minimum value to clamp down the output. Range : int16
  610. * @param[in] activation_max Maximum value to clamp up the output. Range : int16
  611. *
  612. * @details MVE implementation only.
  613. *
  614. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code> or
  615. * <code>RISCV_NMSIS_NN_NO_IMPL_ERROR</code> if not for MVE
  616. *
  617. */
  618. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_t_s16(const int16_t *lhs,
  619. const int8_t *rhs,
  620. const nmsis_nn_bias_data *bias_data,
  621. int16_t *dst,
  622. const int32_t *dst_multipliers,
  623. const int32_t *dst_shifts,
  624. const int32_t lhs_rows,
  625. const int32_t rhs_rows,
  626. const int32_t rhs_cols,
  627. const int32_t activation_min,
  628. const int32_t activation_max);
  629. /**
  630. * @brief General Matrix-multiplication function with int8 input and int32 output.
  631. * This function assumes:
  632. * - LHS input matrix NOT transposed (nt)
  633. * - RHS input matrix transposed (t)
  634. *
  635. * @note Dst/output buffer must be zeroed out before calling this function.
  636. *
  637. * @param[in] lhs Pointer to the LHS input matrix
  638. * @param[in] rhs Pointer to the RHS input matrix
  639. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  640. * @param[in] lhs_rows Number of LHS input rows
  641. * @param[in] rhs_rows Number of LHS input columns/RHS input rows
  642. * @param[in] rhs_cols Number of RHS input columns
  643. * @param[in] lhs_offset Offset to be applied to the LHS input value
  644. * @param[in] dst_idx_offset Offset between subsequent output results
  645. *
  646. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  647. *
  648. */
  649. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_t_s8_s32(const int8_t *lhs,
  650. const int8_t *rhs,
  651. int32_t *dst,
  652. const int32_t lhs_rows,
  653. const int32_t rhs_rows,
  654. const int32_t rhs_cols,
  655. const int32_t lhs_offset,
  656. const int32_t dst_idx_offset);
  657. /**
  658. * @brief s4 Vector by Matrix (transposed) multiplication
  659. *
  660. * @param[in] lhs Input left-hand side vector
  661. * @param[in] packed_rhs Input right-hand side matrix (transposed)
  662. * @param[in] bias Input bias
  663. * @param[out] dst Output vector
  664. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side vector.
  665. * Range: -127 to 128
  666. * @param[in] dst_offset Offset to be added to the output values. Range: -127 to 128
  667. * @param[in] dst_multiplier Output multiplier
  668. * @param[in] dst_shift Output shift
  669. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  670. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  671. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  672. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  673. *
  674. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  675. *
  676. */
  677. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_s4(const int8_t *lhs,
  678. const int8_t *packed_rhs,
  679. const int32_t *bias,
  680. int8_t *dst,
  681. const int32_t lhs_offset,
  682. const int32_t dst_offset,
  683. const int32_t dst_multiplier,
  684. const int32_t dst_shift,
  685. const int32_t rhs_cols,
  686. const int32_t rhs_rows,
  687. const int32_t activation_min,
  688. const int32_t activation_max);
  689. /**
  690. * @brief s8 Vector by Matrix (transposed) multiplication
  691. *
  692. * @param[in] lhs Input left-hand side vector
  693. * @param[in] rhs Input right-hand side matrix (transposed)
  694. * @param[in] kernel_sum Kernel sums of the kernels (rhs). See riscv_vector_sum_s8 for more info.
  695. * @param[in] bias Input bias
  696. * @param[out] dst Output vector
  697. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side vector.
  698. * Range: -127 to 128
  699. * @param[in] dst_offset Offset to be added to the output values. Range: -127 to 128
  700. * @param[in] dst_multiplier Output multiplier
  701. * @param[in] dst_shift Output shift
  702. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  703. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  704. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  705. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  706. * @param[in] address_offset Memory position offset for dst. First output is stored at 'dst', the
  707. * second at 'dst + address_offset' and so on. Default value is typically 1.
  708. * @param[in] rhs_offset Offset to be added to the input values of the right-hand side vector.
  709. * Range: -127 to 128
  710. *
  711. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  712. *
  713. */
  714. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_s8(const int8_t *lhs,
  715. const int8_t *rhs,
  716. const int32_t *kernel_sum,
  717. const int32_t *bias,
  718. int8_t *dst,
  719. const int32_t lhs_offset,
  720. const int32_t dst_offset,
  721. const int32_t dst_multiplier,
  722. const int32_t dst_shift,
  723. const int32_t rhs_cols,
  724. const int32_t rhs_rows,
  725. const int32_t activation_min,
  726. const int32_t activation_max,
  727. const int32_t address_offset,
  728. const int32_t rhs_offset);
  729. /**
  730. * @brief s16 Vector by Matrix (transposed) multiplication
  731. *
  732. * @param[in] lhs Input left-hand side vector
  733. * @param[in] rhs Input right-hand side matrix (transposed)
  734. * @param[in] bias Input bias
  735. * @param[out] dst Output vector
  736. * @param[in] dst_multiplier Output multiplier
  737. * @param[in] dst_shift Output shift
  738. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  739. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  740. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  741. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  742. *
  743. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  744. *
  745. */
  746. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_s16(const int16_t *lhs,
  747. const int8_t *rhs,
  748. const int64_t *bias,
  749. int16_t *dst,
  750. const int32_t dst_multiplier,
  751. const int32_t dst_shift,
  752. const int32_t rhs_cols,
  753. const int32_t rhs_rows,
  754. const int32_t activation_min,
  755. const int32_t activation_max);
  756. /**
  757. * @brief s8 Vector by Matrix (transposed) multiplication with s16 output
  758. *
  759. * @param[in] lhs Input left-hand side vector
  760. * @param[in] rhs Input right-hand side matrix (transposed)
  761. * @param[out] dst Output vector
  762. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side
  763. * vector. Range: -127 to 128
  764. * @param[in] scatter_offset Address offset for dst. First output is stored at 'dst', the
  765. * second at 'dst + scatter_offset' and so on.
  766. * @param[in] dst_multiplier Output multiplier
  767. * @param[in] dst_shift Output shift
  768. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  769. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  770. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  771. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  772. *
  773. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  774. *
  775. */
  776. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_svdf_s8(const int8_t *lhs,
  777. const int8_t *rhs,
  778. int16_t *dst,
  779. const int32_t lhs_offset,
  780. const int32_t scatter_offset,
  781. const int32_t dst_multiplier,
  782. const int32_t dst_shift,
  783. const int32_t rhs_cols,
  784. const int32_t rhs_rows,
  785. const int32_t activation_min,
  786. const int32_t activation_max);
  787. /**
  788. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in padded cases where
  789. * the padding is -lhs_offset(Range: int8). Dimensions are the same for lhs and rhs.
  790. *
  791. * @param[in] lhs Input left-hand side matrix
  792. * @param[in] rhs Input right-hand side matrix (transposed)
  793. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  794. * @param[in] active_ch Subset of total_ch processed
  795. * @param[in] total_ch Number of channels in LHS/RHS
  796. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels
  797. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels
  798. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  799. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  800. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  801. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  802. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels
  803. * @param[in] out Output pointer
  804. *
  805. * @return The function returns one of the two
  806. * - Updated output pointer if an implementation is available
  807. * - NULL if no implementation is available.
  808. *
  809. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  810. * out for the following.
  811. * - Output shift
  812. * - Output multiplier
  813. * - Output bias
  814. * - rhs
  815. */
  816. riscv_nmsis_nn_status riscv_nn_depthwise_conv_nt_t_padded_s8(const int8_t *lhs,
  817. const int8_t *rhs,
  818. const int32_t lhs_offset,
  819. const int32_t active_ch,
  820. const int32_t total_ch,
  821. const int32_t *out_shift,
  822. const int32_t *out_mult,
  823. const int32_t out_offset,
  824. const int32_t activation_min,
  825. const int32_t activation_max,
  826. const uint16_t row_x_col,
  827. const int32_t *const output_bias,
  828. int8_t *out);
  829. /**
  830. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases.
  831. * Dimensions are the same for lhs and rhs.
  832. *
  833. * @param[in] lhs Input left-hand side matrix
  834. * @param[in] rhs Input right-hand side matrix (transposed)
  835. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  836. * @param[in] active_ch Subset of total_ch processed
  837. * @param[in] total_ch Number of channels in LHS/RHS
  838. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  839. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  840. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  841. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  842. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  843. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  844. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  845. * @param[in] out Output pointer
  846. *
  847. * @return The function returns one of the two
  848. * - Updated output pointer if an implementation is available
  849. * - NULL if no implementation is available.
  850. *
  851. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  852. * out for the following.
  853. * - Output shift
  854. * - Output multiplier
  855. * - Output bias
  856. * - rhs
  857. */
  858. riscv_nmsis_nn_status riscv_nn_depthwise_conv_nt_t_s8(const int8_t *lhs,
  859. const int8_t *rhs,
  860. const int32_t lhs_offset,
  861. const int32_t active_ch,
  862. const int32_t total_ch,
  863. const int32_t *out_shift,
  864. const int32_t *out_mult,
  865. const int32_t out_offset,
  866. const int32_t activation_min,
  867. const int32_t activation_max,
  868. const uint16_t row_x_col,
  869. const int32_t *const output_bias,
  870. int8_t *out);
  871. /**
  872. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases. rhs
  873. * consists of packed int4 data. Dimensions are the same for lhs and rhs.
  874. *
  875. * @param[in] lhs Input left-hand side matrix
  876. * @param[in] rhs Input right-hand side matrix (transposed). Consists of int4 data packed in an int8
  877. * buffer.
  878. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  879. * @param[in] active_ch Subset of total_ch processed
  880. * @param[in] total_ch Number of channels in LHS/RHS
  881. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  882. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  883. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  884. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  885. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  886. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  887. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  888. * @param[in] out Output pointer
  889. *
  890. * @return The function returns one of the two
  891. * - Updated output pointer if an implementation is available
  892. * - NULL if no implementation is available.
  893. *
  894. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  895. * out for the following.
  896. * - Output shift
  897. * - Output multiplier
  898. * - Output bias
  899. * - rhs
  900. */
  901. riscv_nmsis_nn_status riscv_nn_depthwise_conv_nt_t_s4(const int8_t *lhs,
  902. const int8_t *rhs,
  903. const int32_t lhs_offset,
  904. const int32_t active_ch,
  905. const int32_t total_ch,
  906. const int32_t *out_shift,
  907. const int32_t *out_mult,
  908. const int32_t out_offset,
  909. const int32_t activation_min,
  910. const int32_t activation_max,
  911. const uint16_t row_x_col,
  912. const int32_t *const output_bias,
  913. int8_t *out);
  914. /**
  915. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases.
  916. * Dimensions are the same for lhs and rhs.
  917. *
  918. * @param[in] lhs Input left-hand side matrix
  919. * @param[in] rhs Input right-hand side matrix (transposed)
  920. * @param[in] num_ch Number of channels in LHS/RHS
  921. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  922. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  923. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  924. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  925. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  926. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  927. * @param[in] out Output pointer
  928. *
  929. * @return The function returns one of the two
  930. * - Updated output pointer if an implementation is available
  931. * - NULL if no implementation is available.
  932. *
  933. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  934. * out for the following.
  935. * - Output shift
  936. * - Output multiplier
  937. * - Output bias
  938. * - rhs
  939. */
  940. int16_t *riscv_nn_depthwise_conv_nt_t_s16(const int16_t *lhs,
  941. const int8_t *rhs,
  942. const uint16_t num_ch,
  943. const int32_t *out_shift,
  944. const int32_t *out_mult,
  945. const int32_t activation_min,
  946. const int32_t activation_max,
  947. const uint16_t row_x_col,
  948. const int64_t *const output_bias,
  949. int16_t *out);
  950. /**
  951. *@brief Matrix-multiplication function for convolution with reordered columns
  952. *@param[in] pA pointer to operand A
  953. *@param[in] pInBuffer pointer to operand B, always conssists of 2 vectors
  954. *@param[in] ch_im_out numRow of A
  955. *@param[in] numCol_A numCol of A
  956. *@param[in] bias_shift amount of left-shift for bias
  957. *@param[in] out_shift amount of right-shift for output
  958. *@param[in] bias the bias
  959. *@param[in,out] pOut pointer to output
  960. *@return The function returns the incremented output pointer
  961. *
  962. *@details This function assumes that data in pInBuffer are reordered
  963. */
  964. q7_t *riscv_nn_mat_mult_kernel_q7_q15_reordered(const q7_t *pA,
  965. const q15_t *pInBuffer,
  966. const uint16_t ch_im_out,
  967. const uint16_t numCol_A,
  968. const uint16_t bias_shift,
  969. const uint16_t out_shift,
  970. const q7_t *bias,
  971. q7_t *pOut);
  972. q7_t *riscv_nn_mat_mult_kernel_q7_reordered(const q7_t * pA,
  973. const q7_t * pInBuffer,
  974. const uint16_t ch_im_out,
  975. const uint16_t numCol_A,
  976. const uint16_t bias_shift,
  977. const uint16_t out_shift,
  978. const q7_t * bias,
  979. q7_t * pOut);
  980. #define __SIMD32_TYPE int32_t
  981. #define __SIMD32(addr) (*(__SIMD32_TYPE **) & (addr))
  982. #define __SIMD32_CONST(addr) ( (__SIMD32_TYPE * ) (addr))
  983. #define _SIMD32_OFFSET(addr) (*(__SIMD32_TYPE * ) (addr))
  984. #define __SIMD64(addr) (*( int64_t **) & (addr))
  985. /**
  986. @brief Read 2 s16 elements and post increment pointer.
  987. @param[in] in_q15 Pointer to pointer that holds address of input.
  988. @return q31 value
  989. */
  990. __STATIC_FORCEINLINE int32_t riscv_nn_read_q15x2_ia(const int16_t **in_q15)
  991. {
  992. int32_t val;
  993. #ifdef __RISCV_FEATURE_UNALIGNED
  994. memcpy(&val, *in_q15, 4);
  995. #else
  996. __ASM volatile ("lw %0, 0(%1)" : "=r" (val) : "r" (*in_q15));
  997. #endif
  998. *in_q15 += 2;
  999. return (val);
  1000. }
  1001. /**
  1002. @brief Read 4 s8 from s8 pointer and post increment pointer.
  1003. @param[in] in_s8 Pointer to pointer that holds address of input.
  1004. @return q31 value
  1005. */
  1006. __STATIC_FORCEINLINE int32_t riscv_nn_read_s8x4_ia(const int8_t **in_s8)
  1007. {
  1008. int32_t val;
  1009. #ifdef __RISCV_FEATURE_UNALIGNED
  1010. memcpy (&val, *in_s8, 4);
  1011. #else
  1012. val = __LW((int8_t *)(* in_s8));
  1013. #endif
  1014. *in_s8 += 4;
  1015. return (val);
  1016. }
  1017. /**
  1018. @brief Read 2 s8 from s8 pointer and post increment pointer.
  1019. @param[in] in_s8 Pointer to pointer that holds address of input.
  1020. @return q31 value
  1021. */
  1022. __STATIC_FORCEINLINE int32_t riscv_nn_read_s8x2_ia(const int8_t **in_s8)
  1023. {
  1024. int32_t val;
  1025. memcpy(&val, *in_s8, 2);
  1026. *in_s8 += 2;
  1027. return (val);
  1028. }
  1029. __STATIC_FORCEINLINE int64_t riscv_nn_read_s8x8_ia(const int8_t **in_s8)
  1030. {
  1031. int64_t val;
  1032. #ifndef __RISCV_FEATURE_UNALIGNED
  1033. #if __RISCV_XLEN == 64
  1034. val = __LD((int8_t *)(*in_s8));
  1035. #else
  1036. val = *((int64_t *)(*in_s8));
  1037. #endif /* __RISCV_XLEN == 64 */
  1038. #else
  1039. memcpy(&val, *in_s8, 8);
  1040. #endif
  1041. *in_s8 += 8;
  1042. return (val);
  1043. }
  1044. /**
  1045. @brief Read 2 int16 values from int16 pointer.
  1046. @param[in] in pointer to address of input.
  1047. @return s32 value
  1048. */
  1049. __STATIC_FORCEINLINE int32_t riscv_nn_read_s16x2(const int16_t *in)
  1050. {
  1051. int32_t val;
  1052. #ifdef __RISCV_FEATURE_UNALIGNED
  1053. memcpy (&val, in, 4);
  1054. #else
  1055. val = __LW((int16_t *)in);
  1056. #endif
  1057. return (val);
  1058. }
  1059. /**
  1060. @brief Read 2 int16 values from int16 pointer and increment pointer afterwards.
  1061. @param[in] in points to input value
  1062. @return int64 value
  1063. */
  1064. __STATIC_FORCEINLINE int32_t riscv_nn_read_s16x2_ia(const int16_t ** in)
  1065. {
  1066. int64_t val;
  1067. val = riscv_nn_read_s16x2(*in);
  1068. *in += 2;
  1069. return (val);
  1070. }
  1071. /**
  1072. @brief Write 2 int16 values to int16 pointer.
  1073. @param[in] in points to input value
  1074. @param[in] value int32 value
  1075. @return none
  1076. */
  1077. __STATIC_FORCEINLINE void riscv_nn_write_s16x2(int16_t * in, int32_t value)
  1078. {
  1079. #ifdef __RISCV_FEATURE_UNALIGNED
  1080. memcpy (in, &value, 4);
  1081. #else
  1082. __SW(in, value);
  1083. #endif
  1084. }
  1085. /**
  1086. @brief Write 2 int16 values to int16 pointer and increment pointer afterwards.
  1087. @param[in] in points to input value
  1088. @param[in] value int32 value
  1089. @return none
  1090. */
  1091. __STATIC_FORCEINLINE void riscv_nn_write_s16x2_ia(int16_t ** in, int32_t value)
  1092. {
  1093. riscv_nn_write_s16x2(*in, value);
  1094. *in += 2;
  1095. }
  1096. /**
  1097. @brief Read 4 int16 values from int16 pointer.
  1098. @param[in] in pointer to address of input.
  1099. @return s32 value
  1100. */
  1101. __STATIC_FORCEINLINE int64_t riscv_nn_read_s16x4(const int16_t *in)
  1102. {
  1103. int64_t val;
  1104. #ifndef __RISCV_FEATURE_UNALIGNED
  1105. #if __RISCV_XLEN == 64
  1106. val = __LD((int16_t *)in);
  1107. #else
  1108. val = *((int64_t *)in);
  1109. #endif /* __RISCV_XLEN == 64 */
  1110. #else
  1111. memcpy((void *)(&val), (void *)(in), 8);
  1112. #endif
  1113. return (val);
  1114. }
  1115. /**
  1116. @brief Read 4 int16 values from int16 pointer and increment pointer afterwards.
  1117. @param[in] in points to input value
  1118. @return S64 value
  1119. */
  1120. __STATIC_FORCEINLINE int64_t riscv_nn_read_s16x4_ia(const int16_t ** in)
  1121. {
  1122. int64_t val;
  1123. val = riscv_nn_read_s16x4(*in);
  1124. *in += 4;
  1125. return (val);
  1126. }
  1127. /**
  1128. @brief Write 4 int16 values to int16 pointer.
  1129. @param[in] in points to input value
  1130. @param[in] value S64 value
  1131. @return none
  1132. */
  1133. __STATIC_FORCEINLINE void riscv_nn_write_s16x4(int16_t * in, int64_t value)
  1134. {
  1135. #ifndef __RISCV_FEATURE_UNALIGNED
  1136. #if __RISCV_XLEN == 64
  1137. __SD(in, value);
  1138. #else
  1139. *((int64_t *)in) = value;
  1140. #endif
  1141. #else
  1142. memcpy((void *)(in), (void *)(&value), 8);
  1143. #endif
  1144. }
  1145. /**
  1146. @brief Write 4 int16 values to int16 pointer and increment pointer afterwards.
  1147. @param[in] in points to input value
  1148. @param[in] value int64_t value
  1149. @return none
  1150. */
  1151. __STATIC_FORCEINLINE void riscv_nn_write_s16x4_ia(int16_t ** in, int64_t value)
  1152. {
  1153. riscv_nn_write_s16x4(*in, value);
  1154. *in += 4;
  1155. }
  1156. /**
  1157. @brief Read 4 s8 values.
  1158. @param[in] in_s8 pointer to address of input.
  1159. @return s32 value
  1160. */
  1161. __STATIC_FORCEINLINE int32_t riscv_nn_read_s8x4(const int8_t *in_s8)
  1162. {
  1163. int32_t val;
  1164. #ifdef __RISCV_FEATURE_UNALIGNED
  1165. memcpy(&val, in_s8, 4);
  1166. #else
  1167. val = __LW((int8_t *)(in_s8));
  1168. #endif
  1169. return (val);
  1170. }
  1171. /**
  1172. @brief Read 2 s8 values.
  1173. @param[in] in_s8 pointer to address of input.
  1174. @return s32 value
  1175. */
  1176. __STATIC_FORCEINLINE int32_t riscv_nn_read_s8x2(const int8_t *in_s8)
  1177. {
  1178. int32_t val;
  1179. memcpy(&val, in_s8, 2);
  1180. return (val);
  1181. }
  1182. /**
  1183. @brief Write four s8 to s8 pointer and increment pointer afterwards.
  1184. @param[in] in Double pointer to input value
  1185. @param[in] value Four bytes to copy
  1186. */
  1187. __STATIC_FORCEINLINE void riscv_nn_write_s8x4_ia(int8_t **in, int32_t value)
  1188. {
  1189. #ifdef __RISCV_FEATURE_UNALIGNED
  1190. memcpy(*in, &value, 4);
  1191. #else
  1192. __SW(*in, value);
  1193. #endif
  1194. *in += 4;
  1195. }
  1196. /**
  1197. @brief Read 4 Q15 from Q15 pointer.
  1198. @param[in] pQ15 points to input value
  1199. @return Q63 value
  1200. */
  1201. __STATIC_FORCEINLINE q63_t riscv_nn_read_q15x4 (
  1202. q15_t const * pQ15)
  1203. {
  1204. q63_t val;
  1205. #ifndef __RISCV_FEATURE_UNALIGNED
  1206. #if __RISCV_XLEN == 64
  1207. val = __LD((q15_t *)pQ15);
  1208. #else
  1209. val = *((q63_t *)pQ15);
  1210. #endif /* __RISCV_XLEN == 64 */
  1211. #else
  1212. memcpy((void *)(&val), (void *)(pQ15), 8);
  1213. #endif
  1214. return (val);
  1215. }
  1216. /**
  1217. @brief Read 4 Q15 from Q15 pointer and increment pointer afterwards.
  1218. @param[in] pQ15 points to input value
  1219. @return Q63 value
  1220. */
  1221. __STATIC_FORCEINLINE q63_t riscv_nn_read_q15x4_ia (
  1222. q15_t ** pQ15)
  1223. {
  1224. q63_t val;
  1225. val = riscv_nn_read_q15x4(*pQ15);
  1226. *pQ15 += 4;
  1227. return (val);
  1228. }
  1229. /**
  1230. @brief Write 4 Q15 to Q15 pointer.
  1231. @param[in] pQ15 points to input value
  1232. @param[in] value Q31 value
  1233. @return none
  1234. */
  1235. __STATIC_FORCEINLINE void riscv_nn_write_q15x4 (
  1236. q15_t * pQ15,
  1237. q63_t value)
  1238. {
  1239. #ifndef __RISCV_FEATURE_UNALIGNED
  1240. #if __RISCV_XLEN == 64
  1241. __SD(pQ15, value);
  1242. #else
  1243. *((q63_t *)pQ15) = value;
  1244. #endif
  1245. #else
  1246. memcpy((void *)(pQ15), (void *)(&value), 8);
  1247. #endif
  1248. }
  1249. /**
  1250. @brief Write 4 Q15 to Q15 pointer and increment pointer afterwards.
  1251. @param[in] pQ15 points to input value
  1252. @param[in] value Q31 value
  1253. @return none
  1254. */
  1255. __STATIC_FORCEINLINE void riscv_nn_write_q15x4_ia (
  1256. q15_t ** pQ15,
  1257. q63_t value)
  1258. {
  1259. riscv_nn_write_q15x4(*pQ15, value);
  1260. *pQ15 += 4;
  1261. }
  1262. /**
  1263. @brief Read 4 q7 from q7 pointer and post increment pointer.
  1264. @param[in] in_q7 Pointer to pointer that holds address of input.
  1265. @return q31 value
  1266. */
  1267. __STATIC_FORCEINLINE q31_t riscv_nn_read_q7x4_ia(const q7_t **in_q7)
  1268. {
  1269. q31_t val;
  1270. #ifdef __RISCV_FEATURE_UNALIGNED
  1271. memcpy (&val, *in_q7, 4);
  1272. #else
  1273. __ASM volatile ("lw %0, 0(%1)" : "=r" (val) : "r" (*in_q7));
  1274. #endif
  1275. *in_q7 += 4;
  1276. return (val);
  1277. }
  1278. /**
  1279. @brief Read 2 q15 from q15 pointer.
  1280. @param[in] in_q15 pointer to address of input.
  1281. @return q31 value
  1282. */
  1283. __STATIC_FORCEINLINE q31_t riscv_nn_read_q15x2(const q15_t *in_q15)
  1284. {
  1285. q31_t val;
  1286. #ifdef __RISCV_FEATURE_UNALIGNED
  1287. memcpy (&val, in_q15, 4);
  1288. #else
  1289. __ASM volatile ("lw %0, 0(%1)" : "=r" (val) : "r" (in_q15));
  1290. #endif
  1291. return (val);
  1292. }
  1293. /**
  1294. @brief Read 4 q7 values.
  1295. @param[in] in_q7 pointer to address of input.
  1296. @return q31 value
  1297. */
  1298. __STATIC_FORCEINLINE q31_t riscv_nn_read_q7x4(const q7_t *in_q7)
  1299. {
  1300. q31_t val;
  1301. #ifdef __RISCV_FEATURE_UNALIGNED
  1302. memcpy (&val, in_q7, 4);
  1303. #else
  1304. __ASM volatile ("lw %0, 0(%1)" : "=r" (val) : "r" (in_q7));
  1305. #endif
  1306. return (val);
  1307. }
  1308. /**
  1309. @brief Read 8 Q7 from Q7 pointer.
  1310. @param[in] pQ7 points to input value
  1311. @return Q63 value
  1312. */
  1313. __STATIC_FORCEINLINE q63_t riscv_nn_read_q7x8 (
  1314. q7_t const * pQ7)
  1315. {
  1316. q63_t val;
  1317. #ifndef __RISCV_FEATURE_UNALIGNED
  1318. #if __RISCV_XLEN == 64
  1319. val = __LD((q7_t *)pQ7);
  1320. #else
  1321. val = *((q63_t *)pQ7);
  1322. #endif
  1323. #else
  1324. memcpy((void *)(&val), (void *)pQ7, 8);
  1325. #endif
  1326. return val;
  1327. }
  1328. /**
  1329. @brief Read 8 Q7 from Q7 pointer and increment pointer afterwards.
  1330. @param[in] pQ7 points to input value
  1331. @return Q63 value
  1332. */
  1333. __STATIC_FORCEINLINE q63_t riscv_nn_read_q7x8_ia (
  1334. q7_t ** pQ7)
  1335. {
  1336. q63_t val;
  1337. val = riscv_nn_read_q7x8(*pQ7);
  1338. *pQ7 += 8;
  1339. return val;
  1340. }
  1341. /**
  1342. @brief Write four q7 to q7 pointer and increment pointer afterwards.
  1343. @param[in] in Double pointer to input value
  1344. @param[in] value Four bytes to copy
  1345. */
  1346. __STATIC_FORCEINLINE void riscv_nn_write_q7x4_ia(q7_t **in, q31_t value)
  1347. {
  1348. memcpy(*in, &value, 4);
  1349. *in += 4;
  1350. }
  1351. /**
  1352. @brief Write 8 Q7 to Q7 pointer and increment pointer afterwards.
  1353. @param[in] pQ7 points to input value
  1354. @param[in] value Q63 value
  1355. @return none
  1356. */
  1357. __STATIC_FORCEINLINE void riscv_nn_write_q7x8_ia (
  1358. q7_t ** pQ7,
  1359. q63_t value)
  1360. {
  1361. #ifndef __RISCV_FEATURE_UNALIGNED
  1362. #if __RISCV_XLEN == 64
  1363. __SD(*pQ7,value);
  1364. #else
  1365. *((q63_t *)*pQ7) = value;
  1366. #endif
  1367. #else
  1368. memcpy((void *)(*pQ7), (void *)(&value), 8);
  1369. #endif
  1370. *pQ7 += 8;
  1371. }
  1372. /**
  1373. * @brief memset
  1374. * @param[in, out] dst Destination pointer
  1375. * @param[in] val Value to set
  1376. * @param[in] block_size Number of bytes to copy.
  1377. *
  1378. */
  1379. __STATIC_FORCEINLINE void riscv_memset_s8(int8_t *dst, const int8_t val, uint32_t block_size)
  1380. {
  1381. memset(dst, val, block_size);
  1382. }
  1383. #if defined(RISCV_MATH_DSP)
  1384. /**
  1385. * @brief read and expand one s4 word into two s8 words.
  1386. */
  1387. __STATIC_FORCEINLINE void read_and_pad_s4(const int8_t *source, int32_t *out1, int32_t *out2)
  1388. {
  1389. int16_t in = riscv_nn_read_s8x2(source);
  1390. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  1391. *out1 = __SXTB16_RORn(__SXTB16(inA << 4), 4);
  1392. *out2 = __SXTB16_RORn(__SXTB16(inA), 4);
  1393. }
  1394. /**
  1395. * @brief read and expand one s4 word into two s8 words.
  1396. * @details The s4 elements are not evenly aligned on the byte boundary, so 3 bytes need to be read instead of 2.
  1397. * In other words first nibble to read start at the middle of a byte.
  1398. * byte index, s4 element
  1399. * 0, s4_x
  1400. * 0, s4_0
  1401. * 1, s4_1
  1402. * 1, s4_2
  1403. * 2, s4_3
  1404. * 2, s4_x
  1405. */
  1406. __STATIC_FORCEINLINE void read_and_pad_s4_uneven(const int8_t *source, int32_t *out1, int32_t *out2)
  1407. {
  1408. int32_t inA1 = (source[0] & 0xFF) | ((source[1] & 0xFF) << 16);
  1409. int32_t inA2 = (source[1] & 0xFF) | ((source[2] & 0xFF) << 16);
  1410. *out1 = __SXTB16_RORn(__SXTB16(inA2 << 4), 4);
  1411. *out2 = __SXTB16_RORn(__SXTB16(inA1), 4);
  1412. }
  1413. /**
  1414. * @brief read and expand one s4 word into two s16 words with ordering.
  1415. */
  1416. __STATIC_FORCEINLINE void read_and_pad_s4_ordered(const int8_t *source, int32_t *out1, int32_t *out2)
  1417. {
  1418. int16_t in = riscv_nn_read_s8x2(source);
  1419. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  1420. int32_t inAbuf1 = __SXTB16_RORn(__SXTB16(inA), 4);
  1421. int32_t inAbuf2 = __SXTB16_RORn(__SXTB16(inA << 4), 4);
  1422. *out2 = (int32_t)(__PKHTB(inAbuf1, inAbuf2, 16));
  1423. *out1 = (int32_t)(__PKHBT(inAbuf2, inAbuf1, 16));
  1424. }
  1425. /**
  1426. * @brief read and expand two s8 word into four s16 words with ordering.
  1427. */
  1428. #if __RISCV_XLEN == 64
  1429. __STATIC_FORCEINLINE const int8_t *read_and_pad64(const int8_t *source, int64_t *out1, int64_t *out2)
  1430. {
  1431. int64_t inA = riscv_nn_read_s8x8_ia(&source);
  1432. int64_t tmp1 = __SXTB16(__ROR64((uint64_t)inA, 8)); // __RV_SUNPKD820
  1433. int64_t tmp2 = __SXTB16(inA);
  1434. int64_t inAbuf1 = (int64_t)(__PKHBT64(tmp2, tmp1, 16));
  1435. int64_t inAbuf2 = (int64_t)(__PKHTB64(tmp1, tmp2, 16));
  1436. *out2 = __RV_PKTT32(inAbuf2, inAbuf1);
  1437. *out1 = __RV_PKBB32(inAbuf2, inAbuf1);
  1438. return source;
  1439. }
  1440. #else
  1441. #if defined (NUCLEI_DSP_N2)
  1442. __STATIC_FORCEINLINE const int8_t *read_and_pad64(const int8_t *source, int64_t *out1, int64_t *out2)
  1443. {
  1444. int64_t inA = riscv_nn_read_s8x8_ia(&source);
  1445. int64_t tmp1 = __RV_DSUNPKD820(__ROR64((uint64_t)inA, 8));
  1446. int64_t tmp2 = __RV_DSUNPKD820(inA);
  1447. int64_t inAbuf1 = (int64_t)(__PKHBT64(tmp2, tmp1, 16));
  1448. int64_t inAbuf2 = (int64_t)(__PKHTB64(tmp1, tmp2, 16));
  1449. *out1 = __RV_DPKBB32(inAbuf2, inAbuf1);
  1450. *out2 = __RV_DPKTT32(inAbuf2, inAbuf1);
  1451. return source;
  1452. }
  1453. #endif /* defined (NUCLEI_DSP_N2) */
  1454. #endif /* __RISCV_XLEN == 64 */
  1455. /**
  1456. * @brief read and expand one s8 word into two s16 words with ordering.
  1457. */
  1458. __STATIC_FORCEINLINE const int8_t *read_and_pad(const int8_t *source, int32_t *out1, int32_t *out2)
  1459. {
  1460. int32_t inA = riscv_nn_read_s8x4_ia(&source);
  1461. int32_t inAbuf1 = __SXTB16_RORn((uint32_t)inA, 8);
  1462. int32_t inAbuf2 = __SXTB16(inA);
  1463. *out2 = (int32_t)(__NN_PKHTB(inAbuf1, inAbuf2, 16));
  1464. *out1 = (int32_t)(__NN_PKHBT(inAbuf2, inAbuf1, 16));
  1465. return source;
  1466. }
  1467. /**
  1468. * @brief read and expand one s8 word into two s16 words with ordering and addition.
  1469. */
  1470. __STATIC_FORCEINLINE void read_pad_and_add_s8(const int8_t *source, int32_t *out1, int32_t *out2, const uint32_t add)
  1471. {
  1472. int32_t inA = riscv_nn_read_s8x4(source);
  1473. int32_t inAbuf1 = __SXTAB16_RORn(add, (uint32_t)inA, 8);
  1474. int32_t inAbuf2 = __SXTAB16(add, inA);
  1475. #ifndef RISCV_MATH_BIG_ENDIAN
  1476. *out2 = (int32_t)(__PKHTB(inAbuf1, inAbuf2, 16));
  1477. *out1 = (int32_t)(__PKHBT(inAbuf2, inAbuf1, 16));
  1478. #else
  1479. *out1 = (int32_t)(__PKHTB(inAbuf1, inAbuf2, 16));
  1480. *out2 = (int32_t)(__PKHBT(inAbuf2, inAbuf1, 16));
  1481. #endif
  1482. }
  1483. /**
  1484. * @brief read and expand two bytes into one word with ordering.
  1485. */
  1486. __STATIC_FORCEINLINE void read_and_pad_s8x2(const int8_t *source, int32_t *out)
  1487. {
  1488. int16_t in = riscv_nn_read_s8x2(source);
  1489. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  1490. *out = __SXTB16(inA);
  1491. }
  1492. /**
  1493. * @brief read and expand two bytes into one word with ordering and addition.
  1494. */
  1495. __STATIC_FORCEINLINE void read_pad_and_add_s8x2(const int8_t *source, int32_t *out, const uint32_t add)
  1496. {
  1497. int16_t in = riscv_nn_read_s8x2(source);
  1498. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  1499. *out = __SXTAB16(add, inA);
  1500. }
  1501. /**
  1502. * @brief read and expand two s8 word into four s16 words with no additional ordering.
  1503. */
  1504. #if __RISCV_XLEN == 64
  1505. __STATIC_FORCEINLINE const int8_t *read_and_pad_reordered64(const int8_t *source, int64_t *out1, int64_t *out2)
  1506. {
  1507. int64_t inA = riscv_nn_read_s8x8_ia(&source);
  1508. int64_t tmp2 = __RV_SUNPKD820(__ROR64((uint64_t)inA, 8));
  1509. int64_t tmp1 = __RV_SUNPKD820(inA);
  1510. *out1 = __RV_PKBB32(tmp2, tmp1);
  1511. *out2 = __RV_PKTT32(tmp2, tmp1);
  1512. return source;
  1513. }
  1514. #else
  1515. #if defined (NUCLEI_DSP_N2)
  1516. __STATIC_FORCEINLINE const int8_t *read_and_pad_reordered64(const int8_t *source, int64_t *out1, int64_t *out2)
  1517. {
  1518. int64_t inA = riscv_nn_read_s8x8_ia(&source);
  1519. int64_t tmp2 = __RV_DSUNPKD820(__ROR64((uint64_t)inA, 8));
  1520. int64_t tmp1 = __RV_DSUNPKD820(inA);
  1521. *out1 = __RV_DPKBB32(tmp2, tmp1);
  1522. *out2 = __RV_DPKTT32(tmp2, tmp1);
  1523. return source;
  1524. }
  1525. #endif /* defined (NUCLEI_DSP_N2) */
  1526. #endif /* __RISCV_XLEN == 64 */
  1527. /**
  1528. * @brief read and expand one s8 word into two s16 words with no additional ordering.
  1529. */
  1530. __STATIC_FORCEINLINE const int8_t *read_and_pad_reordered(const int8_t *source, int32_t *out1, int32_t *out2)
  1531. {
  1532. int32_t inA = riscv_nn_read_s8x4_ia(&source);
  1533. *out2 = __SXTB16(__ROR((uint32_t)inA, 8));
  1534. *out1 = __SXTB16(inA);
  1535. return source;
  1536. }
  1537. /**
  1538. * @brief read and expand one q7 word into two q15 words with reordering and add an offset
  1539. */
  1540. __STATIC_FORCEINLINE const q7_t *
  1541. read_and_pad_reordered_with_offset(const q7_t *source, q31_t *out1, q31_t *out2, q31_t offset)
  1542. {
  1543. q31_t inA = riscv_nn_read_q7x4_ia(&source);
  1544. *out2 = __SXTB16(__ROR((uint32_t)inA, 8));
  1545. *out1 = __SXTB16(inA);
  1546. *out1 = __NN_QADD16(*out1, offset);
  1547. *out2 = __NN_QADD16(*out2, offset);
  1548. return source;
  1549. }
  1550. #endif
  1551. /**
  1552. * @brief Matrix-multiplication function for convolution with per-channel requantization and 4 bit weights.
  1553. * @param[in] input_a pointer to operand A, int8 packed with 2x int4.
  1554. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  1555. * @param[in] output_ch number of rows of A
  1556. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  1557. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  1558. * @param[in] out_offset output tensor offset.
  1559. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  1560. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  1561. * @param[in] num_col_a number of columns of A
  1562. * @param[in] output_bias per output channel bias. Range : int32
  1563. * @param[in,out] out_0 pointer to output
  1564. * @return The function returns one of the two
  1565. * 1. The incremented output pointer for a successful operation or
  1566. * 2. NULL if implementation is not available.
  1567. *
  1568. * @details This function does the matrix multiplication of weight matrix for all output channels
  1569. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  1570. * clamped in the range provided by activation min and max.
  1571. * Supported framework: TensorFlow Lite micro.
  1572. */
  1573. int8_t *riscv_nn_mat_mult_kernel_s4_s16(const int8_t *input_a,
  1574. const int16_t *input_b,
  1575. const uint16_t output_ch,
  1576. const int32_t *out_shift,
  1577. const int32_t *out_mult,
  1578. const int32_t out_offset,
  1579. const int32_t activation_min,
  1580. const int32_t activation_max,
  1581. const int32_t num_col_a,
  1582. const int32_t *const output_bias,
  1583. int8_t *out_0);
  1584. /**
  1585. * @defgroup NNBasicMath Basic Math Functions for Neural Network Computation
  1586. *
  1587. * Basic Math Functions for Neural Network Computation
  1588. *
  1589. */
  1590. /**
  1591. * @brief q7 vector multiplication with variable output shifts
  1592. * @param[in] *pSrcA pointer to the first input vector
  1593. * @param[in] *pSrcB pointer to the second input vector
  1594. * @param[out] *pDst pointer to the output vector
  1595. * @param[in] out_shift amount of right-shift for output
  1596. * @param[in] blockSize number of samples in each vector
  1597. * @return none.
  1598. *
  1599. * <b>Scaling and Overflow Behavior:</b>
  1600. * \par
  1601. * The function uses saturating arithmetic.
  1602. * Results outside of the allowable q15 range [0x8000 0x7FFF] will be saturated.
  1603. */
  1604. void riscv_nn_mult_q15(q15_t *pSrcA, q15_t *pSrcB, q15_t *pDst, const uint16_t out_shift, uint32_t blockSize);
  1605. /**
  1606. * @brief q7 vector multiplication with variable output shifts
  1607. * @param[in] *pSrcA pointer to the first input vector
  1608. * @param[in] *pSrcB pointer to the second input vector
  1609. * @param[out] *pDst pointer to the output vector
  1610. * @param[in] out_shift amount of right-shift for output
  1611. * @param[in] blockSize number of samples in each vector
  1612. * @return none.
  1613. *
  1614. * <b>Scaling and Overflow Behavior:</b>
  1615. * \par
  1616. * The function uses saturating arithmetic.
  1617. * Results outside of the allowable q7 range [0x80 0x7F] will be saturated.
  1618. */
  1619. void riscv_nn_mult_q7(q7_t *pSrcA, q7_t *pSrcB, q7_t *pDst, const uint16_t out_shift, uint32_t blockSize);
  1620. /**
  1621. * @brief Matrix-multiplication function for convolution with per-channel requantization.
  1622. * @param[in] input_a pointer to operand A
  1623. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  1624. * @param[in] output_ch number of rows of A
  1625. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  1626. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  1627. * @param[in] out_offset output tensor offset.
  1628. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  1629. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  1630. * @param[in] num_col_a number of columns of A
  1631. * @param[in] aligned_num_col_a number of columns of A aligned by 4
  1632. * @param[in] output_bias per output channel bias. Range : int32
  1633. * @param[in,out] out_0 pointer to output
  1634. * @return The function returns one of the two
  1635. * 1. The incremented output pointer for a successful operation or
  1636. * 2. NULL if implementation is not available.
  1637. *
  1638. * @details This function does the matrix multiplication of weight matrix for all output channels
  1639. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  1640. * clamped in the range provided by activation min and max.
  1641. * Supported framework: TensorFlow Lite micro.
  1642. */
  1643. int8_t *riscv_nn_mat_mult_kernel_s8_s16(const int8_t *input_a,
  1644. const int16_t *input_b,
  1645. const uint16_t output_ch,
  1646. const int32_t *out_shift,
  1647. const int32_t *out_mult,
  1648. const int32_t out_offset,
  1649. const int16_t activation_min,
  1650. const int16_t activation_max,
  1651. const int32_t num_col_a,
  1652. const int32_t aligned_num_col_a,
  1653. const int32_t *const output_bias,
  1654. int8_t *out_0);
  1655. /**
  1656. * @brief Matrix-multiplication function for convolution with per-channel requantization, supporting an address offset
  1657. * between rows.
  1658. * @param[in] input_a pointer to operand A
  1659. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  1660. * @param[in] output_ch number of rows of A
  1661. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  1662. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  1663. * @param[in] out_offset output tensor offset.
  1664. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  1665. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  1666. * @param[in] num_col_a number of columns of A
  1667. * @param[in] aligned_num_col_a number of columns of A aligned by 4
  1668. * @param[in] output_bias per output channel bias. Range : int32
  1669. * @param[in] row_address_offset address offset between rows in the output
  1670. * @param[in,out] out_0 pointer to output
  1671. * @return The function returns one of the two
  1672. * 1. The incremented output pointer for a successful operation or
  1673. * 2. NULL if implementation is not available.
  1674. *
  1675. * @details This function does the matrix multiplication of weight matrix for all output channels
  1676. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  1677. * clamped in the range provided by activation min and max.
  1678. *
  1679. * This function is slighly less performant than riscv_nn_mat_mult_kernel_s8_s16, but allows support for
  1680. * grouped convolution. Supported framework: TensorFlow Lite micro.
  1681. */
  1682. int8_t *riscv_nn_mat_mult_kernel_row_offset_s8_s16(const int8_t *input_a,
  1683. const int16_t *input_b,
  1684. const uint16_t output_ch,
  1685. const int32_t *out_shift,
  1686. const int32_t *out_mult,
  1687. const int32_t out_offset,
  1688. const int16_t activation_min,
  1689. const int16_t activation_max,
  1690. const int32_t num_col_a,
  1691. const int32_t aligned_num_col_a,
  1692. const int32_t *const output_bias,
  1693. const int32_t row_address_offset,
  1694. int8_t *out_0);
  1695. /**
  1696. * @brief Common softmax function for s8 input and s8 or s16 output
  1697. * @param[in] input Pointer to the input tensor
  1698. * @param[in] num_rows Number of rows in the input tensor
  1699. * @param[in] row_size Number of elements in each input row
  1700. * @param[in] mult Input quantization multiplier
  1701. * @param[in] shift Input quantization shift within the range [0, 31]
  1702. * @param[in] diff_min Minimum difference with max in row. Used to check if
  1703. * the quantized exponential operation can be performed
  1704. * @param[in] int16_output Indicating s8 output if 0 else s16 output
  1705. * @param[out] output Pointer to the output tensor
  1706. *
  1707. * @note Supported framework: TensorFlow Lite micro (bit-accurate)
  1708. *
  1709. */
  1710. void riscv_nn_softmax_common_s8(const int8_t *input,
  1711. const int32_t num_rows,
  1712. const int32_t row_size,
  1713. const int32_t mult,
  1714. const int32_t shift,
  1715. const int32_t diff_min,
  1716. const bool int16_output,
  1717. void *output);
  1718. /**
  1719. * @brief macro for adding rounding offset
  1720. */
  1721. #ifndef RISCV_NN_TRUNCATE
  1722. #define NN_ROUND(out_shift) ((0x1 << out_shift) >> 1)
  1723. #else
  1724. #define NN_ROUND(out_shift) 0
  1725. #endif
  1726. // Macros for shortening quantization functions' names and avoid long lines
  1727. #define MUL_SAT(a, b) riscv_nn_doubling_high_mult((a), (b))
  1728. #define MUL_POW2(a, b) riscv_nn_mult_by_power_of_two((a), (b))
  1729. #define DIV_POW2(a, b) riscv_nn_divide_by_power_of_two((a), (b))
  1730. #define EXP_ON_NEG(x) riscv_nn_exp_on_negative_values((x))
  1731. #define ONE_OVER1(x) riscv_nn_one_over_one_plus_x_for_x_in_0_1((x))
  1732. /**
  1733. * @brief Saturating doubling high multiply. Result matches
  1734. * NEON instruction VQRDMULH.
  1735. * @param[in] m1 Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1736. * @param[in] m2 Multiplier. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1737. * @return Result of multiplication.
  1738. *
  1739. */
  1740. __STATIC_FORCEINLINE int32_t riscv_nn_doubling_high_mult(const int32_t m1, const int32_t m2)
  1741. {
  1742. int32_t result = 0;
  1743. // Rounding offset to add for a right shift of 31
  1744. int64_t mult = 1 << 30;
  1745. if ((m1 < 0) ^ (m2 < 0))
  1746. {
  1747. mult = 1 - mult;
  1748. }
  1749. // Gets resolved as a SMLAL instruction
  1750. mult = mult + (int64_t)m1 * m2;
  1751. // Utilize all of the upper 32 bits. This is the doubling step
  1752. // as well.
  1753. result = (int32_t)(mult / (1ll << 31));
  1754. if ((m1 == m2) && (m1 == (int32_t)NN_Q31_MIN))
  1755. {
  1756. result = NN_Q31_MAX;
  1757. }
  1758. return result;
  1759. }
  1760. /**
  1761. * @brief Doubling high multiply without saturation. This is intended
  1762. * for requantization where the scale is a positive integer
  1763. *
  1764. * @param[in] m1 Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1765. * @param[in] m2 Multiplier Range: {NN_Q31_MIN, NN_Q31_MAX}
  1766. * @return Result of multiplication.
  1767. * @note The result of this matches that of neon instruction
  1768. * VQRDMULH for m1 in range {NN_Q31_MIN, NN_Q31_MAX} and m2 in
  1769. * range {NN_Q31_MIN + 1, NN_Q31_MAX}. Saturation occurs when
  1770. * m1 equals m2 equals NN_Q31_MIN and that is not handled by
  1771. * this function.
  1772. *
  1773. */
  1774. __STATIC_FORCEINLINE int32_t riscv_nn_doubling_high_mult_no_sat(const int32_t m1, const int32_t m2)
  1775. {
  1776. int32_t result = 0;
  1777. union riscv_nn_long_long mult;
  1778. // Rounding offset to add for a right shift of 31
  1779. mult.word.low = 1 << 30;
  1780. mult.word.high = 0;
  1781. // Gets resolved as a SMLAL instruction
  1782. mult.long_long = mult.long_long + (int64_t)m1 * m2;
  1783. // Utilize all of the upper 32 bits. This is the doubling step
  1784. // as well.
  1785. result = (int32_t)(mult.long_long >> 31);
  1786. return result;
  1787. }
  1788. /**
  1789. * @brief Rounding divide by power of two.
  1790. * @param[in] dividend - Dividend
  1791. * @param[in] exponent - Divisor = power(2, exponent)
  1792. * Range: [0, 31]
  1793. * @return Rounded result of division. Midpoint is rounded away from zero.
  1794. *
  1795. */
  1796. __STATIC_FORCEINLINE int32_t riscv_nn_divide_by_power_of_two(const int32_t dividend, const int32_t exponent)
  1797. {
  1798. int32_t result = 0;
  1799. const int32_t remainder_mask = (1 << exponent) - 1;
  1800. int32_t remainder = remainder_mask & dividend;
  1801. // Basic division
  1802. result = dividend >> exponent;
  1803. // Adjust 'result' for rounding (mid point away from zero)
  1804. int32_t threshold = remainder_mask >> 1;
  1805. if (result < 0)
  1806. {
  1807. threshold++;
  1808. }
  1809. if (remainder > threshold)
  1810. {
  1811. result++;
  1812. }
  1813. return result;
  1814. }
  1815. /**
  1816. * @brief Requantize a given value.
  1817. * @details Essentially returns (val * multiplier)/(2 ^ shift) with different rounding depending if
  1818. * NMSIS_NN_USE_SINGLE_ROUNDING is defined or not.
  1819. * @param[in] val Value to be requantized
  1820. * @param[in] multiplier Multiplier. Range {NN_Q31_MIN + 1, Q32_MAX}
  1821. * @param[in] shift Shift. Range: {-31, 30}
  1822. * Default branch:
  1823. * If shift is positive left shift 'val * multiplier' with shift
  1824. * If shift is negative right shift 'val * multiplier' with abs(shift)
  1825. * Single round branch:
  1826. * Input for total_shift in divide by '2 ^ total_shift'
  1827. *
  1828. * @return Default branch:
  1829. * Returns (val * multiplier) with rounding divided by (2 ^ shift) with rounding
  1830. * Single round branch:
  1831. * Returns (val * multiplier)/(2 ^ (31 - shift)) with rounding
  1832. *
  1833. */
  1834. __STATIC_FORCEINLINE int32_t riscv_nn_requantize(const int32_t val, const int32_t multiplier, const int32_t shift)
  1835. {
  1836. return riscv_nn_divide_by_power_of_two(riscv_nn_doubling_high_mult_no_sat(val * (1 << LEFT_SHIFT(shift)), multiplier),
  1837. RIGHT_SHIFT(shift));
  1838. }
  1839. #if defined(RISCV_MATH_VECTOR)
  1840. __STATIC_FORCEINLINE vint32m4_t riscv_nn_requantize_m4_rvv(vint32m4_t valm4, size_t l, const q31_t multiplier, const q31_t shift)
  1841. {
  1842. if (shift >= 0) {
  1843. valm4 = __riscv_vsmul_vx_i32m4(__riscv_vsll_vx_i32m4(valm4, shift, l), multiplier, __RISCV_VXRM_RNU, l);
  1844. } else {
  1845. q31_t exponent = -shift;
  1846. q31_t remainder_mask = (1 << exponent) - 1;
  1847. q31_t threshold = remainder_mask >> 1;
  1848. vint32m4_t b32m4, c32m4;
  1849. valm4 = __riscv_vsmul_vx_i32m4(valm4, multiplier, __RISCV_VXRM_RNU, l);
  1850. b32m4 = __riscv_vsra_vx_i32m4(valm4, exponent, l);
  1851. valm4 = __riscv_vand_vx_i32m4(valm4, remainder_mask, l);
  1852. c32m4 = __riscv_vmv_v_x_i32m4(threshold, l);
  1853. vbool8_t mask = __riscv_vmslt_vx_i32m4_b8(b32m4, 0, l);
  1854. c32m4 = __riscv_vadd_vx_i32m4_tumu(mask, c32m4, c32m4, 1, l);
  1855. mask = __riscv_vmsgt_vv_i32m4_b8(valm4, c32m4, l);
  1856. valm4 = __riscv_vadd_vx_i32m4_tumu(mask, b32m4, b32m4, 1, l);
  1857. }
  1858. return valm4;
  1859. }
  1860. __STATIC_FORCEINLINE vint32m8_t riscv_nn_requantize_m8_rvv(vint32m8_t valm8, size_t l, const q31_t multiplier, const q31_t shift)
  1861. {
  1862. if (shift >= 0) {
  1863. valm8 = __riscv_vsmul_vx_i32m8(__riscv_vsll_vx_i32m8(valm8, shift, l), multiplier, __RISCV_VXRM_RNU, l);
  1864. } else {
  1865. q31_t exponent = -shift;
  1866. q31_t remainder_mask = (1 << exponent) - 1;
  1867. q31_t threshold = remainder_mask >> 1;
  1868. vint32m8_t b32m8, c32m8;
  1869. valm8 = __riscv_vsmul_vx_i32m8(valm8, multiplier, __RISCV_VXRM_RNU, l);
  1870. b32m8 = __riscv_vsra_vx_i32m8(valm8, exponent, l);
  1871. valm8 = __riscv_vand_vx_i32m8(valm8, remainder_mask, l);
  1872. c32m8 = __riscv_vmv_v_x_i32m8(threshold, l);
  1873. vbool4_t mask = __riscv_vmslt_vx_i32m8_b4(b32m8, 0, l);
  1874. c32m8 = __riscv_vadd_vx_i32m8_tumu(mask, c32m8, c32m8, 1, l);
  1875. mask = __riscv_vmsgt_vv_i32m8_b4(valm8, c32m8, l);
  1876. valm8 = __riscv_vadd_vx_i32m8_tumu(mask, b32m8, b32m8, 1, l);
  1877. }
  1878. return valm8;
  1879. }
  1880. #endif
  1881. /**
  1882. * @brief Requantize a given 64 bit value.
  1883. * @param[in] val Value to be requantized in the range {-(1<<47)} to {(1<<47) - 1}
  1884. * @param[in] reduced_multiplier Reduced multiplier in the range {NN_Q31_MIN + 1, Q32_MAX} to {Q16_MIN + 1,
  1885. * Q16_MAX}
  1886. * @param[in] shift Left or right shift for 'val * multiplier' in the range {-31} to {7}
  1887. *
  1888. * @return Returns (val * multiplier)/(2 ^ shift)
  1889. *
  1890. */
  1891. __STATIC_FORCEINLINE int32_t riscv_nn_requantize_s64(const int64_t val,
  1892. const int32_t reduced_multiplier,
  1893. const int32_t shift)
  1894. {
  1895. const int64_t new_val = val * reduced_multiplier;
  1896. int32_t result = new_val >> (14 - shift); // 64->32 bit reduction
  1897. result = (result + 1) >> 1; // Last shift position and insert round
  1898. return result;
  1899. }
  1900. /**
  1901. * @brief memcpy
  1902. * @param[in, out] dst Destination pointer
  1903. * @param[in] src Source pointer.
  1904. * @param[in] block_size Number of bytes to copy.
  1905. *
  1906. */
  1907. __STATIC_FORCEINLINE void riscv_memcpy_s8(int8_t *__RESTRICT dst, const int8_t *__RESTRICT src, uint32_t block_size)
  1908. {
  1909. memcpy(dst, src, block_size);
  1910. }
  1911. /**
  1912. * @brief memcpy
  1913. * @param[in, out] dst Destination pointer
  1914. * @param[in] src Source pointer.
  1915. * @param[in] block_size Number of bytes to copy.
  1916. *
  1917. */
  1918. __STATIC_FORCEINLINE void riscv_memcpy_q7(q7_t *__RESTRICT dst, const q7_t *__RESTRICT src, uint32_t block_size)
  1919. {
  1920. memcpy(dst, src, block_size);
  1921. }
  1922. /**
  1923. * @brief memcpy wrapper for int16
  1924. * @param[in, out] dst Destination pointer
  1925. * @param[in] src Source pointer.
  1926. * @param[in] block_size Number of bytes to copy.
  1927. *
  1928. */
  1929. __STATIC_FORCEINLINE void riscv_memcpy_q15(int16_t *__RESTRICT dst, const int16_t *__RESTRICT src, uint32_t block_size)
  1930. {
  1931. memcpy(dst, src, block_size);
  1932. }
  1933. // @note The following functions are used only for softmax layer, scaled bits = 5 assumed
  1934. __STATIC_FORCEINLINE int32_t riscv_nn_exp_on_negative_values(int32_t val)
  1935. {
  1936. int32_t mask = 0;
  1937. int32_t shift = 24;
  1938. const int32_t val_mod_minus_quarter = (val & ((1 << shift) - 1)) - (1 << shift);
  1939. const int32_t remainder = val_mod_minus_quarter - val;
  1940. const int32_t x = (val_mod_minus_quarter << 5) + (1 << 28);
  1941. const int32_t x2 = MUL_SAT(x, x);
  1942. int32_t result = 1895147668 +
  1943. MUL_SAT(1895147668, x + DIV_POW2(MUL_SAT(DIV_POW2(MUL_SAT(x2, x2), 2) + MUL_SAT(x2, x), 715827883) + x2, 1));
  1944. #define SELECT_IF_NON_ZERO(x) \
  1945. { \
  1946. mask = MASK_IF_NON_ZERO(remainder & (1 << shift++)); \
  1947. result = SELECT_USING_MASK(mask, MUL_SAT(result, x), result); \
  1948. }
  1949. SELECT_IF_NON_ZERO(1672461947)
  1950. SELECT_IF_NON_ZERO(1302514674)
  1951. SELECT_IF_NON_ZERO(790015084)
  1952. SELECT_IF_NON_ZERO(290630308)
  1953. SELECT_IF_NON_ZERO(39332535)
  1954. SELECT_IF_NON_ZERO(720401)
  1955. SELECT_IF_NON_ZERO(242)
  1956. #undef SELECT_IF_NON_ZERO
  1957. mask = MASK_IF_ZERO(val);
  1958. return SELECT_USING_MASK(mask, NN_Q31_MAX, result);
  1959. }
  1960. __STATIC_FORCEINLINE int32_t riscv_nn_mult_by_power_of_two(const int32_t val, const int32_t exp)
  1961. {
  1962. const int32_t thresh = ((1 << (31 - exp)) - 1);
  1963. int32_t result = val << exp;
  1964. result = SELECT_USING_MASK(MASK_IF_NON_ZERO(val > thresh), NN_Q31_MAX, result);
  1965. result = SELECT_USING_MASK(MASK_IF_NON_ZERO(val < -thresh), NN_Q31_MIN, result);
  1966. return result;
  1967. }
  1968. __STATIC_FORCEINLINE int32_t riscv_nn_one_over_one_plus_x_for_x_in_0_1(int32_t val)
  1969. {
  1970. const int64_t sum = (int64_t)val + (int64_t)NN_Q31_MAX;
  1971. const int32_t half_denominator = (int32_t)((sum + (sum >= 0 ? 1 : -1)) / 2L);
  1972. int32_t x = 1515870810 + MUL_SAT(half_denominator, -1010580540);
  1973. const int32_t shift = (1 << 29);
  1974. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1975. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1976. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1977. return MUL_POW2(x, 1);
  1978. }
  1979. /**
  1980. @brief Write 2 s16 elements and post increment pointer.
  1981. @param[in] dest_q15 Pointer to pointer that holds address of destination.
  1982. @param[in] src_q31 Input value to be written.
  1983. */
  1984. __STATIC_FORCEINLINE void riscv_nn_write_q15x2_ia(int16_t **dest_q15, int32_t src_q31)
  1985. {
  1986. int32_t val = src_q31;
  1987. memcpy(*dest_q15, &val, 4);
  1988. *dest_q15 += 2;
  1989. }
  1990. /**
  1991. @brief Write 2 s8 elements and post increment pointer.
  1992. @param[in] dst Pointer to pointer that holds address of destination.
  1993. @param[in] src Input value to be written.
  1994. */
  1995. __STATIC_FORCEINLINE void riscv_nn_write_s8x2_ia(int8_t **dst, int16_t src)
  1996. {
  1997. memcpy(*dst, &src, 2);
  1998. *dst += 2;
  1999. }
  2000. /**
  2001. * @brief Copies the elements of a Q7 vector.
  2002. * @param[in] pSrc input pointer
  2003. * @param[out] pDst output pointer
  2004. * @param[in] blockSize number of samples to process
  2005. */
  2006. void riscv_nn_copy_q7(
  2007. const q7_t * pSrc,
  2008. q7_t * pDst,
  2009. uint32_t blockSize);
  2010. /**
  2011. * @brief Copies the elements of a Q15 vector.
  2012. * @param[in] pSrc input pointer
  2013. * @param[out] pDst output pointer
  2014. * @param[in] blockSize number of samples to process
  2015. */
  2016. void riscv_nn_copy_q15(
  2017. const q15_t * pSrc,
  2018. q15_t * pDst,
  2019. uint32_t blockSize);
  2020. /**
  2021. * @brief Fills a constant value into a Q7 vector.
  2022. * @param[in] value input value to be filled
  2023. * @param[out] pDst output pointer
  2024. * @param[in] blockSize number of samples to process
  2025. */
  2026. void riscv_nn_fill_q7(
  2027. q7_t value,
  2028. q7_t * pDst,
  2029. uint32_t blockSize);
  2030. /**
  2031. * @brief Fills a constant value into a Q15 vector.
  2032. * @param[in] value input value to be filled
  2033. * @param[out] pDst output pointer
  2034. * @param[in] blockSize number of samples to process
  2035. */
  2036. void riscv_nn_fill_q15(
  2037. q15_t value,
  2038. q15_t * pDst,
  2039. uint32_t blockSize);
  2040. // Support functions for LSTM
  2041. /**
  2042. * @brief Update LSTM function for an iteration step using s8 input and output, and s16 internally.
  2043. *
  2044. * @param[in] data_in Data input pointer
  2045. * @param[in] hidden_in Hidden state/ recurrent input pointer
  2046. * @param[out] hidden_out Hidden state/ recurrent output pointer
  2047. * @param[in] params Struct containg all information about the lstm operator, see
  2048. * riscv_nn_types.
  2049. * @param[in] buffers Struct containg pointers to all temporary scratch buffers needed for the
  2050. * lstm operator, see riscv_nn_types.
  2051. * @param[in] batch_offset Number of timesteps between consecutive batches.
  2052. * E.g for params->timing_major = true, all batches for t=0 are stored sequentially, so batch offset = 1.
  2053. * For params->time major = false, all time steps are stored continously before the next batch, so
  2054. * batch offset = params->time_steps.
  2055. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2056. */
  2057. riscv_nmsis_nn_status riscv_nn_lstm_step_s8(const int8_t *data_in,
  2058. const int8_t *hidden_in,
  2059. int8_t *hidden_out,
  2060. const nmsis_nn_lstm_params *params,
  2061. nmsis_nn_lstm_context *buffers,
  2062. const int32_t batch_offset);
  2063. /**
  2064. * @brief Update LSTM function for an iteration step using s16 input and output, and s16 internally.
  2065. *
  2066. * @param[in] data_in Data input pointer
  2067. * @param[in] hidden_in Hidden state/ recurrent input pointer
  2068. * @param[out] hidden_out Hidden state/ recurrent output pointer
  2069. * @param[in] params Struct containg all information about the lstm operator, see
  2070. * riscv_nn_types.
  2071. * @param[in] buffers Struct containg pointers to all temporary scratch buffers needed for the
  2072. * lstm operator, see riscv_nn_types.
  2073. * @param[in] batch_offset Number of timesteps between consecutive batches.
  2074. * E.g for params->timing_major = true, all batches for t=0 are stored sequentially, so batch offset = 1.
  2075. * For params->time major = false, all time steps are stored continously before the next batch, so
  2076. * batch offset = params->time_steps.
  2077. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2078. */
  2079. riscv_nmsis_nn_status riscv_nn_lstm_step_s16(const int16_t *data_in,
  2080. const int16_t *hidden_in,
  2081. int16_t *hidden_out,
  2082. const nmsis_nn_lstm_params *params,
  2083. nmsis_nn_lstm_context *buffers,
  2084. const int32_t batch_offset);
  2085. /**
  2086. * @brief Updates a LSTM gate for an iteration step of LSTM function, int8x8_16 version.
  2087. *
  2088. * @param[in] data_in Data input pointer
  2089. * @param[in] hidden_in Hidden state/ recurrent input pointer
  2090. * @param[in] gate_data Struct containing all information about the gate caluclation, see
  2091. * riscv_nn_types.
  2092. * @param[in] params Struct containing all information about the lstm_operation, see
  2093. * riscv_nn_types
  2094. * @param[out] output Hidden state/ recurrent output pointer
  2095. * @param[in] batch_offset Number of timesteps between consecutive batches, see
  2096. * riscv_nn_lstm_step_s8.
  2097. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2098. */
  2099. riscv_nmsis_nn_status riscv_nn_lstm_calculate_gate_s8_s16(const int8_t *data_in,
  2100. const int8_t *hidden_in,
  2101. const nmsis_nn_lstm_gate *gate_data,
  2102. const nmsis_nn_lstm_params *params,
  2103. int16_t *output,
  2104. const int32_t batch_offset);
  2105. /**
  2106. * @brief Updates a LSTM gate for an iteration step of LSTM function, int16x8_16 version.
  2107. *
  2108. * @param[in] data_in Data input pointer
  2109. * @param[in] hidden_in Hidden state/ recurrent input pointer
  2110. * @param[in] gate_data Struct containing all information about the gate caluclation, see
  2111. * riscv_nn_types.
  2112. * @param[in] params Struct containing all information about the lstm_operation, see
  2113. * riscv_nn_types
  2114. * @param[out] output Hidden state/ recurrent output pointer
  2115. * @param[in] batch_offset Number of timesteps between consecutive batches, see
  2116. * riscv_nn_lstm_step_s16.
  2117. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2118. */
  2119. riscv_nmsis_nn_status riscv_nn_lstm_calculate_gate_s16(const int16_t *data_in,
  2120. const int16_t *hidden_in,
  2121. const nmsis_nn_lstm_gate *gate_data,
  2122. const nmsis_nn_lstm_params *params,
  2123. int16_t *output,
  2124. const int32_t batch_offset);
  2125. /**
  2126. * @brief The result of the multiplication is accumulated to the passed result buffer.
  2127. * Multiplies a matrix by a "batched" vector (i.e. a matrix with a batch dimension composed by input vectors independent
  2128. * from each other).
  2129. *
  2130. * @param[in] lhs Batched vector
  2131. * @param[in] rhs Weights - input matrix (H(Rows)xW(Columns))
  2132. * @param[in] effective_bias Bias + lhs_offset * kernel_sum term precalculated into a constant vector.
  2133. * @param[out] dst Output
  2134. * @param[in] dst_multiplier Multiplier for quantization
  2135. * @param[in] dst_shift Shift for quantization
  2136. * @param[in] rhs_cols Vector/matarix column length
  2137. * @param[in] rhs_rows Row count of matrix
  2138. * @param[in] batches Batch size
  2139. * @param[in] batch_offset Number of timesteps between consecutive batches in input, see riscv_nn_lstm_step_s8. Note
  2140. that the output is always stored with sequential batches.
  2141. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  2142. */
  2143. riscv_nmsis_nn_status riscv_nn_vec_mat_mul_result_acc_s8_s16(const int8_t *lhs,
  2144. const int8_t *rhs,
  2145. const int32_t *effective_bias,
  2146. int16_t *dst,
  2147. const int32_t dst_multiplier,
  2148. const int32_t dst_shift,
  2149. const int32_t rhs_cols,
  2150. const int32_t rhs_rows,
  2151. const int32_t batches,
  2152. const int32_t batch_offset);
  2153. /**
  2154. * @brief The result of the multiplication is accumulated to the passed result buffer.
  2155. * Multiplies a matrix by a "batched" vector (i.e. a matrix with a batch dimension composed by input vectors independent
  2156. * from each other).
  2157. *
  2158. * @param[in] lhs Batched vector
  2159. * @param[in] rhs Weights - input matrix (H(Rows)xW(Columns))
  2160. * @param[in] effective_bias Bias + lhs_offset * kernel_sum term precalculated into a constant vector.
  2161. * @param[out] dst Output
  2162. * @param[in] dst_multiplier Multiplier for quantization
  2163. * @param[in] dst_shift Shift for quantization
  2164. * @param[in] rhs_cols Vector/matarix column length
  2165. * @param[in] rhs_rows Row count of matrix
  2166. * @param[in] batches Batch size
  2167. * @param[in] batch_offset Number of timesteps between consecutive batches in input, see riscv_nn_lstm_step_s16.
  2168. Note that the output is always stored with sequential batches.
  2169. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  2170. */
  2171. riscv_nmsis_nn_status riscv_nn_vec_mat_mul_result_acc_s16(const int16_t *lhs,
  2172. const int8_t *rhs,
  2173. const int64_t *effective_bias,
  2174. int16_t *dst,
  2175. const int32_t dst_multiplier,
  2176. const int32_t dst_shift,
  2177. const int32_t rhs_cols,
  2178. const int32_t rhs_rows,
  2179. const int32_t batches,
  2180. const int32_t batch_offset);
  2181. /**
  2182. * @brief s16 elementwise multiplication with s8 output
  2183. * @param[in] input_1_vect pointer to input vector 1
  2184. * @param[in] input_2_vect pointer to input vector 2
  2185. * @param[in,out] output pointer to output vector
  2186. * @param[in] out_offset output offset
  2187. * @param[in] out_mult output multiplier
  2188. * @param[in] out_shift output shift
  2189. * @param[in] block_size number of samples per batch
  2190. * @param[in] batch_size number of samples per batch
  2191. * @param[in] batch_offset Number of timesteps between consecutive batches in output, see
  2192. * riscv_nn_lstm_step_s8. Note that it is assumed that the input is stored with sequential batches.
  2193. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2194. *
  2195. * @details Supported framework: TensorFlow Lite micro
  2196. */
  2197. riscv_nmsis_nn_status riscv_elementwise_mul_s16_s8(const int16_t *input_1_vect,
  2198. const int16_t *input_2_vect,
  2199. int8_t *output,
  2200. const int32_t out_offset,
  2201. const int32_t out_mult,
  2202. const int32_t out_shift,
  2203. const int32_t block_size,
  2204. const int32_t batch_size,
  2205. const int32_t batch_offset);
  2206. /**
  2207. * @brief s16 elementwise multiplication with s16 output
  2208. * @param[in] input_1_vect pointer to input vector 1
  2209. * @param[in] input_2_vect pointer to input vector 2
  2210. * @param[in,out] output pointer to output vector
  2211. * @param[in] out_offset output offset
  2212. * @param[in] out_mult output multiplier
  2213. * @param[in] out_shift output shift
  2214. * @param[in] block_size number of samples per batch
  2215. * @param[in] batch_size number of samples per batch
  2216. * @param[in] batch_offset Number of timesteps between consecutive batches in output, see
  2217. * riscv_nn_lstm_step_s16. Note that it is assumed that the input is stored with sequential batches.
  2218. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2219. *
  2220. * @details Supported framework: TensorFlow Lite micro
  2221. */
  2222. riscv_nmsis_nn_status riscv_elementwise_mul_s16_batch_offset(const int16_t *input_1_vect,
  2223. const int16_t *input_2_vect,
  2224. int16_t *output,
  2225. const int32_t out_offset,
  2226. const int32_t out_mult,
  2227. const int32_t out_shift,
  2228. const int32_t block_size,
  2229. const int32_t batch_size,
  2230. const int32_t batch_offset);
  2231. /**
  2232. * @brief s16 elementwise multiplication. The result of the multiplication is accumulated to the passed result buffer.
  2233. * @param[in] input_1_vect pointer to input vector 1
  2234. * @param[in] input_2_vect pointer to input vector 2
  2235. * @param[in] input_1_offset offset for input 1. Not used.
  2236. * @param[in] input_2_offset offset for input 2. Not used.
  2237. * @param[in,out] output pointer to output vector
  2238. * @param[in] out_offset output offset. Not used.
  2239. * @param[in] out_mult output multiplier
  2240. * @param[in] out_shift output shift
  2241. * @param[in] out_activation_min minimum value to clamp output to. Min: -32768
  2242. * @param[in] out_activation_max maximum value to clamp output to. Max: 32767
  2243. * @param[in] block_size number of samples
  2244. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2245. *
  2246. * @details Supported framework: TensorFlow Lite micro
  2247. */
  2248. riscv_nmsis_nn_status riscv_elementwise_mul_acc_s16(const int16_t *input_1_vect,
  2249. const int16_t *input_2_vect,
  2250. const int32_t input_1_offset,
  2251. const int32_t input_2_offset,
  2252. int16_t *output,
  2253. const int32_t out_offset,
  2254. const int32_t out_mult,
  2255. const int32_t out_shift,
  2256. const int32_t out_activation_min,
  2257. const int32_t out_activation_max,
  2258. const int32_t block_size);
  2259. #ifdef __cplusplus
  2260. }
  2261. #endif
  2262. #endif /* RISCV_NNSUPPORTFUNCTIONS_H */