csi_rv32_gcc.h 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830
  1. /*
  2. * Copyright (C) 2017-2019 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file csi_rv32_gcc.h
  6. * @brief CSI Header File for GCC.
  7. * @version V1.0
  8. * @date 01. Sep 2020
  9. ******************************************************************************/
  10. #ifndef _CSI_RV32_GCC_H_
  11. #define _CSI_RV32_GCC_H_
  12. #include <stdlib.h>
  13. #ifndef __ASM
  14. #define __ASM __asm /*!< asm keyword for GNU Compiler */
  15. #endif
  16. #ifndef __INLINE
  17. #define __INLINE inline /*!< inline keyword for GNU Compiler */
  18. #endif
  19. #ifndef __ALWAYS_STATIC_INLINE
  20. #define __ALWAYS_STATIC_INLINE __attribute__((always_inline)) static inline
  21. #endif
  22. #ifndef __STATIC_INLINE
  23. #define __STATIC_INLINE static inline
  24. #endif
  25. #ifndef __NO_RETURN
  26. #define __NO_RETURN __attribute__((__noreturn__))
  27. #endif
  28. #ifndef __USED
  29. #define __USED __attribute__((used))
  30. #endif
  31. #ifndef __WEAK
  32. #define __WEAK __attribute__((weak))
  33. #endif
  34. #ifndef __PACKED
  35. #define __PACKED __attribute__((packed, aligned(1)))
  36. #endif
  37. #ifndef __PACKED_STRUCT
  38. #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
  39. #endif
  40. #ifndef __PACKED_UNION
  41. #define __PACKED_UNION union __attribute__((packed, aligned(1)))
  42. #endif
  43. /* ########################### Core Function Access ########################### */
  44. /** \ingroup CSI_Core_FunctionInterface
  45. \defgroup CSI_Core_RegAccFunctions CSI Core Register Access Functions
  46. @{
  47. */
  48. /**
  49. \brief Enable IRQ Interrupts
  50. \details Enables IRQ interrupts by setting the IE-bit in the PSR.
  51. Can only be executed in Privileged modes.
  52. */
  53. __ALWAYS_STATIC_INLINE void __enable_irq(void)
  54. {
  55. __ASM volatile("csrs mstatus, 8");
  56. }
  57. /**
  58. \brief Disable IRQ Interrupts
  59. \details Disables IRQ interrupts by clearing the IE-bit in the PSR.
  60. Can only be executed in Privileged modes.
  61. */
  62. __ALWAYS_STATIC_INLINE void __disable_irq(void)
  63. {
  64. __ASM volatile("csrc mstatus, 8");
  65. }
  66. /**
  67. \brief Get MXSTATUS
  68. \details Returns the content of the MXSTATUS Register.
  69. \return MXSTATUS Register value
  70. */
  71. __ALWAYS_STATIC_INLINE uint32_t __get_MXSTATUS(void)
  72. {
  73. uint32_t result;
  74. __ASM volatile("csrr %0, mxstatus" : "=r"(result));
  75. return (result);
  76. }
  77. /**
  78. \brief Set MXSTATUS
  79. \details Writes the given value to the MXSTATUS Register.
  80. \param [in] mxstatus MXSTATUS Register value to set
  81. */
  82. __ALWAYS_STATIC_INLINE void __set_MXSTATUS(uint32_t mxstatus)
  83. {
  84. __ASM volatile("csrw mxstatus, %0" : : "r"(mxstatus));
  85. }
  86. /**
  87. \brief Get MSTATUS
  88. \details Returns the content of the MSTATUS Register.
  89. \return MSTATUS Register value
  90. */
  91. __ALWAYS_STATIC_INLINE uint32_t __get_MSTATUS(void)
  92. {
  93. uint32_t result;
  94. __ASM volatile("csrr %0, mstatus" : "=r"(result));
  95. return (result);
  96. }
  97. /**
  98. \brief Set MSTATUS
  99. \details Writes the given value to the MSTATUS Register.
  100. \param [in] mstatus MSTATUS Register value to set
  101. */
  102. __ALWAYS_STATIC_INLINE void __set_MSTATUS(uint32_t mstatus)
  103. {
  104. __ASM volatile("csrw mstatus, %0" : : "r"(mstatus));
  105. }
  106. /**
  107. \brief Get MHCR
  108. \details Returns the content of the MHCR Register.
  109. \return MHCR Register value
  110. */
  111. __ALWAYS_STATIC_INLINE uint32_t __get_MHCR(void)
  112. {
  113. uint32_t result;
  114. __ASM volatile("csrr %0, mhcr" : "=r"(result));
  115. return (result);
  116. }
  117. /**
  118. \brief Set MHCR
  119. \details Writes the given value to the MHCR Register.
  120. \param [in] mstatus MHCR Register value to set
  121. */
  122. __ALWAYS_STATIC_INLINE void __set_MHCR(uint32_t mhcr)
  123. {
  124. __ASM volatile("csrw mhcr, %0" : : "r"(mhcr));
  125. }
  126. /**
  127. \brief Get MISA Register
  128. \details Returns the content of the MISA Register.
  129. \return MISA Register value
  130. */
  131. __ALWAYS_STATIC_INLINE uint32_t __get_MISA(void)
  132. {
  133. uint32_t result;
  134. __ASM volatile("csrr %0, misa" : "=r"(result));
  135. return (result);
  136. }
  137. /**
  138. \brief Set MISA
  139. \details Writes the given value to the MISA Register.
  140. \param [in] misa MISA Register value to set
  141. */
  142. __ALWAYS_STATIC_INLINE void __set_MISA(uint32_t misa)
  143. {
  144. __ASM volatile("csrw misa, %0" : : "r"(misa));
  145. }
  146. /**
  147. \brief Get MIE Register
  148. \details Returns the content of the MIE Register.
  149. \return MIE Register value
  150. */
  151. __ALWAYS_STATIC_INLINE uint32_t __get_MIE(void)
  152. {
  153. uint32_t result;
  154. __ASM volatile("csrr %0, mie" : "=r"(result));
  155. return (result);
  156. }
  157. /**
  158. \brief Set MIE
  159. \details Writes the given value to the MIE Register.
  160. \param [in] mie MIE Register value to set
  161. */
  162. __ALWAYS_STATIC_INLINE void __set_MIE(uint32_t mie)
  163. {
  164. __ASM volatile("csrw mie, %0" : : "r"(mie));
  165. }
  166. /**
  167. \brief Get MTVEC Register
  168. \details Returns the content of the MTVEC Register.
  169. \return MTVEC Register value
  170. */
  171. __ALWAYS_STATIC_INLINE uint32_t __get_MTVEC(void)
  172. {
  173. uint32_t result;
  174. __ASM volatile("csrr %0, mtvec" : "=r"(result));
  175. return (result);
  176. }
  177. /**
  178. \brief Set MTVEC
  179. \details Writes the given value to the MTVEC Register.
  180. \param [in] mtvec MTVEC Register value to set
  181. */
  182. __ALWAYS_STATIC_INLINE void __set_MTVEC(uint32_t mtvec)
  183. {
  184. __ASM volatile("csrw mtvec, %0" : : "r"(mtvec));
  185. }
  186. /**
  187. \brief Set MTVT
  188. \details Writes the given value to the MTVT Register.
  189. \param [in] mtvt MTVT Register value to set
  190. */
  191. __ALWAYS_STATIC_INLINE void __set_MTVT(uint32_t mtvt)
  192. {
  193. __ASM volatile("csrw mtvt, %0" : : "r"(mtvt));
  194. }
  195. /**
  196. \brief Get MTVT Register
  197. \details Returns the content of the MTVT Register.
  198. \return MTVT Register value
  199. */
  200. __ALWAYS_STATIC_INLINE uint32_t __get_MTVT(void)
  201. {
  202. uint32_t result;
  203. __ASM volatile("csrr %0, mtvt" : "=r"(result));
  204. return (result);
  205. }
  206. /**
  207. \brief Get SP
  208. \details Returns the content of the SP Register.
  209. \return SP Register value
  210. */
  211. __ALWAYS_STATIC_INLINE uint32_t __get_SP(void)
  212. {
  213. uint32_t result;
  214. __ASM volatile("mv %0, sp" : "=r"(result));
  215. return (result);
  216. }
  217. /**
  218. \brief Set SP
  219. \details Writes the given value to the SP Register.
  220. \param [in] sp SP Register value to set
  221. */
  222. __ALWAYS_STATIC_INLINE void __set_SP(uint32_t sp)
  223. {
  224. __ASM volatile("mv sp, %0" : : "r"(sp): "sp");
  225. }
  226. /**
  227. \brief Get MSCRATCH Register
  228. \details Returns the content of the MSCRATCH Register.
  229. \return MSCRATCH Register value
  230. */
  231. __ALWAYS_STATIC_INLINE uint32_t __get_MSCRATCH(void)
  232. {
  233. uint32_t result;
  234. __ASM volatile("csrr %0, mscratch" : "=r"(result));
  235. return (result);
  236. }
  237. /**
  238. \brief Set MSCRATCH
  239. \details Writes the given value to the MSCRATCH Register.
  240. \param [in] mscratch MSCRATCH Register value to set
  241. */
  242. __ALWAYS_STATIC_INLINE void __set_MSCRATCH(uint32_t mscratch)
  243. {
  244. __ASM volatile("csrw mscratch, %0" : : "r"(mscratch));
  245. }
  246. /**
  247. \brief Get MEPC Register
  248. \details Returns the content of the MEPC Register.
  249. \return MEPC Register value
  250. */
  251. __ALWAYS_STATIC_INLINE uint32_t __get_MEPC(void)
  252. {
  253. uint32_t result;
  254. __ASM volatile("csrr %0, mepc" : "=r"(result));
  255. return (result);
  256. }
  257. /**
  258. \brief Set MEPC
  259. \details Writes the given value to the MEPC Register.
  260. \param [in] mepc MEPC Register value to set
  261. */
  262. __ALWAYS_STATIC_INLINE void __set_MEPC(uint32_t mepc)
  263. {
  264. __ASM volatile("csrw mepc, %0" : : "r"(mepc));
  265. }
  266. /**
  267. \brief Get MCAUSE Register
  268. \details Returns the content of the MCAUSE Register.
  269. \return MCAUSE Register value
  270. */
  271. __ALWAYS_STATIC_INLINE uint32_t __get_MCAUSE(void)
  272. {
  273. uint32_t result;
  274. __ASM volatile("csrr %0, mcause" : "=r"(result));
  275. return (result);
  276. }
  277. /**
  278. \brief Get MNXTI Register
  279. \details Returns the content of the MNXTI Register.
  280. \return MNXTI Register value
  281. */
  282. __ALWAYS_STATIC_INLINE uint32_t __get_MNXTI(void)
  283. {
  284. uint32_t result;
  285. __ASM volatile("csrr %0, mnxti" : "=r"(result));
  286. return (result);
  287. }
  288. /**
  289. \brief Set MNXTI
  290. \details Writes the given value to the MNXTI Register.
  291. \param [in] mnxti MNXTI Register value to set
  292. */
  293. __ALWAYS_STATIC_INLINE void __set_MNXTI(uint32_t mnxti)
  294. {
  295. __ASM volatile("csrw mnxti, %0" : : "r"(mnxti));
  296. }
  297. /**
  298. \brief Get MINTSTATUS Register
  299. \details Returns the content of the MINTSTATUS Register.
  300. \return MINTSTATUS Register value
  301. */
  302. __ALWAYS_STATIC_INLINE uint32_t __get_MINTSTATUS(void)
  303. {
  304. uint32_t result;
  305. __ASM volatile("csrr %0, mintstatus" : "=r"(result));
  306. return (result);
  307. }
  308. /**
  309. \brief Get MTVAL Register
  310. \details Returns the content of the MTVAL Register.
  311. \return MTVAL Register value
  312. */
  313. __ALWAYS_STATIC_INLINE uint32_t __get_MTVAL(void)
  314. {
  315. uint32_t result;
  316. __ASM volatile("csrr %0, mtval" : "=r"(result));
  317. return (result);
  318. }
  319. /**
  320. \brief Get MIP Register
  321. \details Returns the content of the MIP Register.
  322. \return MIP Register value
  323. */
  324. __ALWAYS_STATIC_INLINE uint32_t __get_MIP(void)
  325. {
  326. uint32_t result;
  327. __ASM volatile("csrr %0, mip" : "=r"(result));
  328. return (result);
  329. }
  330. /**
  331. \brief Set MIP
  332. \details Writes the given value to the MIP Register.
  333. \param [in] mip MIP Register value to set
  334. */
  335. __ALWAYS_STATIC_INLINE void __set_MIP(uint32_t mip)
  336. {
  337. __ASM volatile("csrw mip, %0" : : "r"(mip));
  338. }
  339. /**
  340. \brief Get MCYCLEL Register
  341. \details Returns the content of the MCYCLEL Register.
  342. \return MCYCLE Register value
  343. */
  344. __ALWAYS_STATIC_INLINE uint32_t __get_MCYCLE(void)
  345. {
  346. uint32_t result;
  347. __ASM volatile("csrr %0, mcycle" : "=r"(result));
  348. return (result);
  349. }
  350. /**
  351. \brief Get MCYCLEH Register
  352. \details Returns the content of the MCYCLEH Register.
  353. \return MCYCLEH Register value
  354. */
  355. __ALWAYS_STATIC_INLINE uint32_t __get_MCYCLEH(void)
  356. {
  357. uint32_t result;
  358. __ASM volatile("csrr %0, mcycleh" : "=r"(result));
  359. return (result);
  360. }
  361. /**
  362. \brief Get MINSTRET Register
  363. \details Returns the content of the MINSTRET Register.
  364. \return MINSTRET Register value
  365. */
  366. __ALWAYS_STATIC_INLINE uint32_t __get_MINSTRET(void)
  367. {
  368. uint32_t result;
  369. __ASM volatile("csrr %0, minstret" : "=r"(result));
  370. return (result);
  371. }
  372. /**
  373. \brief Get MINSTRETH Register
  374. \details Returns the content of the MINSTRETH Register.
  375. \return MINSTRETH Register value
  376. */
  377. __ALWAYS_STATIC_INLINE uint32_t __get_MINSTRETH(void)
  378. {
  379. uint32_t result;
  380. __ASM volatile("csrr %0, minstreth" : "=r"(result));
  381. return (result);
  382. }
  383. /**
  384. \brief Get MVENDORID Register
  385. \details Returns the content of the MVENDROID Register.
  386. \return MVENDORID Register value
  387. */
  388. __ALWAYS_STATIC_INLINE uint32_t __get_MVENDORID(void)
  389. {
  390. uint32_t result;
  391. __ASM volatile("csrr %0, mvendorid" : "=r"(result));
  392. return (result);
  393. }
  394. /**
  395. \brief Get MARCHID Register
  396. \details Returns the content of the MARCHID Register.
  397. \return MARCHID Register value
  398. */
  399. __ALWAYS_STATIC_INLINE uint32_t __get_MARCHID(void)
  400. {
  401. uint32_t result;
  402. __ASM volatile("csrr %0, marchid" : "=r"(result));
  403. return (result);
  404. }
  405. /**
  406. \brief Get MIMPID Register
  407. \details Returns the content of the MIMPID Register.
  408. \return MIMPID Register value
  409. */
  410. __ALWAYS_STATIC_INLINE uint32_t __get_MIMPID(void)
  411. {
  412. uint32_t result;
  413. __ASM volatile("csrr %0, mimpid" : "=r"(result));
  414. return (result);
  415. }
  416. /**
  417. \brief Get MHARTID Register
  418. \details Returns the content of the MHARTID Register.
  419. \return MHARTID Register value
  420. */
  421. __ALWAYS_STATIC_INLINE uint32_t __get_MHARTID(void)
  422. {
  423. uint32_t result;
  424. __ASM volatile("csrr %0, mhartid" : "=r"(result));
  425. return (result);
  426. }
  427. /**
  428. \brief Get PMPCFGx Register
  429. \details Returns the content of the PMPCFGx Register.
  430. \return PMPCFGx Register value
  431. */
  432. __ALWAYS_STATIC_INLINE uint32_t __get_PMPCFG0(void)
  433. {
  434. uint32_t result;
  435. __ASM volatile("csrr %0, pmpcfg0" : "=r"(result));
  436. return (result);
  437. }
  438. __ALWAYS_STATIC_INLINE uint32_t __get_PMPCFG1(void)
  439. {
  440. uint32_t result;
  441. __ASM volatile("csrr %0, pmpcfg1" : "=r"(result));
  442. return (result);
  443. }
  444. __ALWAYS_STATIC_INLINE uint32_t __get_PMPCFG2(void)
  445. {
  446. uint32_t result;
  447. __ASM volatile("csrr %0, pmpcfg2" : "=r"(result));
  448. return (result);
  449. }
  450. __ALWAYS_STATIC_INLINE uint32_t __get_PMPCFG3(void)
  451. {
  452. uint32_t result;
  453. __ASM volatile("csrr %0, pmpcfg3" : "=r"(result));
  454. return (result);
  455. }
  456. /**
  457. \brief Get PMPxCFG Register by index
  458. \details Returns the content of the PMPxCFG Register.
  459. \param [in] idx PMP region index
  460. \return PMPxCFG Register value
  461. */
  462. __STATIC_INLINE uint8_t __get_PMPxCFG(uint32_t idx)
  463. {
  464. uint32_t pmpcfgx = 0;
  465. if (idx < 4) {
  466. pmpcfgx = __get_PMPCFG0();
  467. } else if (idx >=4 && idx < 8) {
  468. idx -= 4;
  469. pmpcfgx = __get_PMPCFG1();
  470. } else if (idx >=8 && idx < 12) {
  471. idx -= 8;
  472. pmpcfgx = __get_PMPCFG2();
  473. } else if (idx >=12 && idx < 16) {
  474. idx -= 12;
  475. pmpcfgx = __get_PMPCFG3();
  476. } else {
  477. return 0;
  478. }
  479. return (uint8_t)((pmpcfgx & (0xFF << (idx << 3))) >> (idx << 3));
  480. }
  481. /**
  482. \brief Set PMPCFGx
  483. \details Writes the given value to the PMPCFGx Register.
  484. \param [in] pmpcfg PMPCFGx Register value to set
  485. */
  486. __ALWAYS_STATIC_INLINE void __set_PMPCFG0(uint32_t pmpcfg)
  487. {
  488. __ASM volatile("csrw pmpcfg0, %0" : : "r"(pmpcfg));
  489. }
  490. __ALWAYS_STATIC_INLINE void __set_PMPCFG1(uint32_t pmpcfg)
  491. {
  492. __ASM volatile("csrw pmpcfg1, %0" : : "r"(pmpcfg));
  493. }
  494. __ALWAYS_STATIC_INLINE void __set_PMPCFG2(uint32_t pmpcfg)
  495. {
  496. __ASM volatile("csrw pmpcfg2, %0" : : "r"(pmpcfg));
  497. }
  498. __ALWAYS_STATIC_INLINE void __set_PMPCFG3(uint32_t pmpcfg)
  499. {
  500. __ASM volatile("csrw pmpcfg3, %0" : : "r"(pmpcfg));
  501. }
  502. /**
  503. \brief Set PMPxCFG by index
  504. \details Writes the given value to the PMPxCFG Register.
  505. \param [in] idx PMPx region index
  506. \param [in] pmpxcfg PMPxCFG Register value to set
  507. */
  508. __STATIC_INLINE void __set_PMPxCFG(uint32_t idx, uint8_t pmpxcfg)
  509. {
  510. uint32_t pmpcfgx = 0;
  511. if (idx < 4) {
  512. pmpcfgx = __get_PMPCFG0();
  513. pmpcfgx = (pmpcfgx & ~(0xFF << (idx << 3))) | (pmpxcfg << (idx << 3));
  514. __set_PMPCFG0(pmpcfgx);
  515. } else if (idx >=4 && idx < 8) {
  516. idx -= 4;
  517. pmpcfgx = __get_PMPCFG1();
  518. pmpcfgx = (pmpcfgx & ~(0xFF << (idx << 3))) | (pmpxcfg << (idx << 3));
  519. __set_PMPCFG1(pmpcfgx);
  520. } else if (idx >=8 && idx < 12) {
  521. idx -= 8;
  522. pmpcfgx = __get_PMPCFG2();
  523. pmpcfgx = (pmpcfgx & ~(0xFF << (idx << 3))) | (pmpxcfg << (idx << 3));
  524. __set_PMPCFG2(pmpcfgx);
  525. } else if (idx >=12 && idx < 16) {
  526. idx -= 12;
  527. pmpcfgx = __get_PMPCFG3();
  528. pmpcfgx = (pmpcfgx & ~(0xFF << (idx << 3))) | (pmpxcfg << (idx << 3));
  529. __set_PMPCFG3(pmpcfgx);
  530. } else {
  531. return;
  532. }
  533. }
  534. /**
  535. \brief Get PMPADDRx Register
  536. \details Returns the content of the PMPADDRx Register.
  537. \return PMPADDRx Register value
  538. */
  539. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR0(void)
  540. {
  541. uint32_t result;
  542. __ASM volatile("csrr %0, pmpaddr0" : "=r"(result));
  543. return (result);
  544. }
  545. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR1(void)
  546. {
  547. uint32_t result;
  548. __ASM volatile("csrr %0, pmpaddr1" : "=r"(result));
  549. return (result);
  550. }
  551. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR2(void)
  552. {
  553. uint32_t result;
  554. __ASM volatile("csrr %0, pmpaddr2" : "=r"(result));
  555. return (result);
  556. }
  557. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR3(void)
  558. {
  559. uint32_t result;
  560. __ASM volatile("csrr %0, pmpaddr3" : "=r"(result));
  561. return (result);
  562. }
  563. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR4(void)
  564. {
  565. uint32_t result;
  566. __ASM volatile("csrr %0, pmpaddr4" : "=r"(result));
  567. return (result);
  568. }
  569. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR5(void)
  570. {
  571. uint32_t result;
  572. __ASM volatile("csrr %0, pmpaddr5" : "=r"(result));
  573. return (result);
  574. }
  575. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR6(void)
  576. {
  577. uint32_t result;
  578. __ASM volatile("csrr %0, pmpaddr6" : "=r"(result));
  579. return (result);
  580. }
  581. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR7(void)
  582. {
  583. uint32_t result;
  584. __ASM volatile("csrr %0, pmpaddr7" : "=r"(result));
  585. return (result);
  586. }
  587. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR8(void)
  588. {
  589. uint32_t result;
  590. __ASM volatile("csrr %0, pmpaddr8" : "=r"(result));
  591. return (result);
  592. }
  593. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR9(void)
  594. {
  595. uint32_t result;
  596. __ASM volatile("csrr %0, pmpaddr9" : "=r"(result));
  597. return (result);
  598. }
  599. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR10(void)
  600. {
  601. uint32_t result;
  602. __ASM volatile("csrr %0, pmpaddr10" : "=r"(result));
  603. return (result);
  604. }
  605. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR11(void)
  606. {
  607. uint32_t result;
  608. __ASM volatile("csrr %0, pmpaddr11" : "=r"(result));
  609. return (result);
  610. }
  611. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR12(void)
  612. {
  613. uint32_t result;
  614. __ASM volatile("csrr %0, pmpaddr12" : "=r"(result));
  615. return (result);
  616. }
  617. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR13(void)
  618. {
  619. uint32_t result;
  620. __ASM volatile("csrr %0, pmpaddr13" : "=r"(result));
  621. return (result);
  622. }
  623. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR14(void)
  624. {
  625. uint32_t result;
  626. __ASM volatile("csrr %0, pmpaddr14" : "=r"(result));
  627. return (result);
  628. }
  629. __ALWAYS_STATIC_INLINE uint32_t __get_PMPADDR15(void)
  630. {
  631. uint32_t result;
  632. __ASM volatile("csrr %0, pmpaddr15" : "=r"(result));
  633. return (result);
  634. }
  635. /**
  636. \brief Get PMPADDRx Register by index
  637. \details Returns the content of the PMPADDRx Register.
  638. \param [in] idx PMP region index
  639. \return PMPADDRx Register value
  640. */
  641. __STATIC_INLINE uint32_t __get_PMPADDRx(uint32_t idx)
  642. {
  643. switch (idx) {
  644. case 0: return __get_PMPADDR0();
  645. case 1: return __get_PMPADDR1();
  646. case 2: return __get_PMPADDR2();
  647. case 3: return __get_PMPADDR3();
  648. case 4: return __get_PMPADDR4();
  649. case 5: return __get_PMPADDR5();
  650. case 6: return __get_PMPADDR6();
  651. case 7: return __get_PMPADDR7();
  652. case 8: return __get_PMPADDR8();
  653. case 9: return __get_PMPADDR9();
  654. case 10: return __get_PMPADDR10();
  655. case 11: return __get_PMPADDR11();
  656. case 12: return __get_PMPADDR12();
  657. case 13: return __get_PMPADDR13();
  658. case 14: return __get_PMPADDR14();
  659. case 15: return __get_PMPADDR15();
  660. default: return 0;
  661. }
  662. }
  663. /**
  664. \brief Set PMPADDRx
  665. \details Writes the given value to the PMPADDRx Register.
  666. \param [in] pmpaddr PMPADDRx Register value to set
  667. */
  668. __ALWAYS_STATIC_INLINE void __set_PMPADDR0(uint32_t pmpaddr)
  669. {
  670. __ASM volatile("csrw pmpaddr0, %0" : : "r"(pmpaddr));
  671. }
  672. __ALWAYS_STATIC_INLINE void __set_PMPADDR1(uint32_t pmpaddr)
  673. {
  674. __ASM volatile("csrw pmpaddr1, %0" : : "r"(pmpaddr));
  675. }
  676. __ALWAYS_STATIC_INLINE void __set_PMPADDR2(uint32_t pmpaddr)
  677. {
  678. __ASM volatile("csrw pmpaddr2, %0" : : "r"(pmpaddr));
  679. }
  680. __ALWAYS_STATIC_INLINE void __set_PMPADDR3(uint32_t pmpaddr)
  681. {
  682. __ASM volatile("csrw pmpaddr3, %0" : : "r"(pmpaddr));
  683. }
  684. __ALWAYS_STATIC_INLINE void __set_PMPADDR4(uint32_t pmpaddr)
  685. {
  686. __ASM volatile("csrw pmpaddr4, %0" : : "r"(pmpaddr));
  687. }
  688. __ALWAYS_STATIC_INLINE void __set_PMPADDR5(uint32_t pmpaddr)
  689. {
  690. __ASM volatile("csrw pmpaddr5, %0" : : "r"(pmpaddr));
  691. }
  692. __ALWAYS_STATIC_INLINE void __set_PMPADDR6(uint32_t pmpaddr)
  693. {
  694. __ASM volatile("csrw pmpaddr6, %0" : : "r"(pmpaddr));
  695. }
  696. __ALWAYS_STATIC_INLINE void __set_PMPADDR7(uint32_t pmpaddr)
  697. {
  698. __ASM volatile("csrw pmpaddr7, %0" : : "r"(pmpaddr));
  699. }
  700. __ALWAYS_STATIC_INLINE void __set_PMPADDR8(uint32_t pmpaddr)
  701. {
  702. __ASM volatile("csrw pmpaddr8, %0" : : "r"(pmpaddr));
  703. }
  704. __ALWAYS_STATIC_INLINE void __set_PMPADDR9(uint32_t pmpaddr)
  705. {
  706. __ASM volatile("csrw pmpaddr9, %0" : : "r"(pmpaddr));
  707. }
  708. __ALWAYS_STATIC_INLINE void __set_PMPADDR10(uint32_t pmpaddr)
  709. {
  710. __ASM volatile("csrw pmpaddr10, %0" : : "r"(pmpaddr));
  711. }
  712. __ALWAYS_STATIC_INLINE void __set_PMPADDR11(uint32_t pmpaddr)
  713. {
  714. __ASM volatile("csrw pmpaddr11, %0" : : "r"(pmpaddr));
  715. }
  716. __ALWAYS_STATIC_INLINE void __set_PMPADDR12(uint32_t pmpaddr)
  717. {
  718. __ASM volatile("csrw pmpaddr12, %0" : : "r"(pmpaddr));
  719. }
  720. __ALWAYS_STATIC_INLINE void __set_PMPADDR13(uint32_t pmpaddr)
  721. {
  722. __ASM volatile("csrw pmpaddr13, %0" : : "r"(pmpaddr));
  723. }
  724. __ALWAYS_STATIC_INLINE void __set_PMPADDR14(uint32_t pmpaddr)
  725. {
  726. __ASM volatile("csrw pmpaddr14, %0" : : "r"(pmpaddr));
  727. }
  728. __ALWAYS_STATIC_INLINE void __set_PMPADDR15(uint32_t pmpaddr)
  729. {
  730. __ASM volatile("csrw pmpaddr15, %0" : : "r"(pmpaddr));
  731. }
  732. /**
  733. \brief Set PMPADDRx by index
  734. \details Writes the given value to the PMPADDRx Register.
  735. \param [in] idx PMP region index
  736. \param [in] pmpaddr PMPADDRx Register value to set
  737. */
  738. __STATIC_INLINE void __set_PMPADDRx(uint32_t idx, uint32_t pmpaddr)
  739. {
  740. switch (idx) {
  741. case 0: __set_PMPADDR0(pmpaddr); break;
  742. case 1: __set_PMPADDR1(pmpaddr); break;
  743. case 2: __set_PMPADDR2(pmpaddr); break;
  744. case 3: __set_PMPADDR3(pmpaddr); break;
  745. case 4: __set_PMPADDR4(pmpaddr); break;
  746. case 5: __set_PMPADDR5(pmpaddr); break;
  747. case 6: __set_PMPADDR6(pmpaddr); break;
  748. case 7: __set_PMPADDR7(pmpaddr); break;
  749. case 8: __set_PMPADDR8(pmpaddr); break;
  750. case 9: __set_PMPADDR9(pmpaddr); break;
  751. case 10: __set_PMPADDR10(pmpaddr); break;
  752. case 11: __set_PMPADDR11(pmpaddr); break;
  753. case 12: __set_PMPADDR12(pmpaddr); break;
  754. case 13: __set_PMPADDR13(pmpaddr); break;
  755. case 14: __set_PMPADDR14(pmpaddr); break;
  756. case 15: __set_PMPADDR15(pmpaddr); break;
  757. default: return;
  758. }
  759. }
  760. /**
  761. \brief Enable interrupts and exceptions
  762. \details Enables interrupts and exceptions by setting the IE-bit and EE-bit in the PSR.
  763. Can only be executed in Privileged modes.
  764. */
  765. __ALWAYS_STATIC_INLINE void __enable_excp_irq(void)
  766. {
  767. __enable_irq();
  768. }
  769. /**
  770. \brief Disable interrupts and exceptions
  771. \details Disables interrupts and exceptions by clearing the IE-bit and EE-bit in the PSR.
  772. Can only be executed in Privileged modes.
  773. */
  774. __ALWAYS_STATIC_INLINE void __disable_excp_irq(void)
  775. {
  776. __disable_irq();
  777. }
  778. #define __CSI_GCC_OUT_REG(r) "=r" (r)
  779. #define __CSI_GCC_USE_REG(r) "r" (r)
  780. /**
  781. \brief No Operation
  782. \details No Operation does nothing. This instruction can be used for code alignment purposes.
  783. */
  784. __ALWAYS_STATIC_INLINE void __NOP(void)
  785. {
  786. __ASM volatile("nop");
  787. }
  788. /**
  789. \brief Wait For Interrupt
  790. \details Wait For Interrupt is a hint instruction that suspends execution until one of a number of events occurs.
  791. */
  792. __ALWAYS_STATIC_INLINE void __WFI(void)
  793. {
  794. __ASM volatile("wfi");
  795. }
  796. /**
  797. \brief Wait For Interrupt
  798. \details Wait For Interrupt is a hint instruction that suspends execution until one interrupt occurs.
  799. */
  800. __ALWAYS_STATIC_INLINE void __WAIT(void)
  801. {
  802. __ASM volatile("wfi");
  803. }
  804. /**
  805. \brief Doze For Interrupt
  806. \details Doze For Interrupt is a hint instruction that suspends execution until one interrupt occurs.
  807. */
  808. __ALWAYS_STATIC_INLINE void __DOZE(void)
  809. {
  810. __ASM volatile("wfi");
  811. }
  812. /**
  813. \brief Stop For Interrupt
  814. \details Stop For Interrupt is a hint instruction that suspends execution until one interrupt occurs.
  815. */
  816. __ALWAYS_STATIC_INLINE void __STOP(void)
  817. {
  818. __ASM volatile("wfi");
  819. }
  820. /**
  821. \brief Instruction Synchronization Barrier
  822. \details Instruction Synchronization Barrier flushes the pipeline in the processor,
  823. so that all instructions following the ISB are fetched from cache or memory,
  824. after the instruction has been completed.
  825. */
  826. __ALWAYS_STATIC_INLINE void __ISB(void)
  827. {
  828. __ASM volatile("fence");
  829. }
  830. /**
  831. \brief Data Synchronization Barrier
  832. \details Acts as a special kind of Data Memory Barrier.
  833. It completes when all explicit memory accesses before this instruction complete.
  834. */
  835. __ALWAYS_STATIC_INLINE void __DSB(void)
  836. {
  837. __ASM volatile("fence");
  838. }
  839. /**
  840. \brief Invalid all icache
  841. \details invalid all icache.
  842. */
  843. __ALWAYS_STATIC_INLINE void __ICACHE_IALL(void)
  844. {
  845. #if defined(__riscv_xtheade) || defined(__riscv_xtheadc)
  846. __ASM volatile("icache.iall");
  847. #endif
  848. }
  849. /**
  850. \brief Invalid Icache by addr
  851. \details Invalid Icache by addr.
  852. \param [in] addr operate addr
  853. */
  854. __ALWAYS_STATIC_INLINE void __ICACHE_IPA(uint32_t addr)
  855. {
  856. #if defined(__riscv_xtheade) || defined(__riscv_xtheadc)
  857. __ASM volatile("icache.ipa %0" : : "r"(addr));
  858. #endif
  859. }
  860. /**
  861. \brief Invalid all dcache
  862. \details invalid all dcache.
  863. */
  864. __ALWAYS_STATIC_INLINE void __DCACHE_IALL(void)
  865. {
  866. #if defined(__riscv_xtheade) || defined(__riscv_xtheadc)
  867. __ASM volatile("dcache.iall");
  868. #endif
  869. }
  870. /**
  871. \brief Clear all dcache
  872. \details clear all dcache.
  873. */
  874. __ALWAYS_STATIC_INLINE void __DCACHE_CALL(void)
  875. {
  876. #if defined(__riscv_xtheade) || defined(__riscv_xtheadc)
  877. __ASM volatile("dcache.call");
  878. #endif
  879. }
  880. /**
  881. \brief Clear&invalid all dcache
  882. \details clear & invalid all dcache.
  883. */
  884. __ALWAYS_STATIC_INLINE void __DCACHE_CIALL(void)
  885. {
  886. #if defined(__riscv_xtheade) || defined(__riscv_xtheadc)
  887. __ASM volatile("dcache.ciall");
  888. #endif
  889. }
  890. /**
  891. \brief Invalid Dcache by addr
  892. \details Invalid Dcache by addr.
  893. \param [in] addr operate addr
  894. */
  895. __ALWAYS_STATIC_INLINE void __DCACHE_IPA(uint32_t addr)
  896. {
  897. #if defined(__riscv_xtheade) || defined(__riscv_xtheadc)
  898. __ASM volatile("dcache.ipa %0" : : "r"(addr));
  899. #endif
  900. }
  901. /**
  902. \brief Clear Dcache by addr
  903. \details Clear Dcache by addr.
  904. \param [in] addr operate addr
  905. */
  906. __ALWAYS_STATIC_INLINE void __DCACHE_CPA(uint32_t addr)
  907. {
  908. #if defined(__riscv_xtheade) || defined(__riscv_xtheadc)
  909. __ASM volatile("dcache.cpa %0" : : "r"(addr));
  910. #endif
  911. }
  912. /**
  913. \brief Clear & Invalid Dcache by addr
  914. \details Clear & Invalid Dcache by addr.
  915. \param [in] addr operate addr
  916. */
  917. __ALWAYS_STATIC_INLINE void __DCACHE_CIPA(uint32_t addr)
  918. {
  919. #if defined(__riscv_xtheade) || defined(__riscv_xtheadc)
  920. __ASM volatile("dcache.cipa %0" : : "r"(addr));
  921. #endif
  922. }
  923. /**
  924. \brief Data Memory Barrier
  925. \details Ensures the apparent order of the explicit memory operations before
  926. and after the instruction, without ensuring their completion.
  927. */
  928. __ALWAYS_STATIC_INLINE void __DMB(void)
  929. {
  930. __ASM volatile("fence");
  931. }
  932. /**
  933. \brief Reverse byte order (32 bit)
  934. \details Reverses the byte order in integer value.
  935. \param [in] value Value to reverse
  936. \return Reversed value
  937. */
  938. __ALWAYS_STATIC_INLINE uint32_t __REV(uint32_t value)
  939. {
  940. return __builtin_bswap32(value);
  941. }
  942. /**
  943. \brief Reverse byte order (16 bit)
  944. \details Reverses the byte order in two unsigned short values.
  945. \param [in] value Value to reverse
  946. \return Reversed value
  947. */
  948. __ALWAYS_STATIC_INLINE uint32_t __REV16(uint32_t value)
  949. {
  950. uint32_t result;
  951. result = ((value & 0xFF000000) >> 8) | ((value & 0x00FF0000) << 8) |
  952. ((value & 0x0000FF00) >> 8) | ((value & 0x000000FF) << 8);
  953. return (result);
  954. }
  955. /**
  956. \brief Reverse byte order in signed short value
  957. \details Reverses the byte order in a signed short value with sign extension to integer.
  958. \param [in] value Value to reverse
  959. \return Reversed value
  960. */
  961. __ALWAYS_STATIC_INLINE int32_t __REVSH(int32_t value)
  962. {
  963. return (short)(((value & 0xFF00) >> 8) | ((value & 0x00FF) << 8));
  964. }
  965. /**
  966. \brief Rotate Right in unsigned value (32 bit)
  967. \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  968. \param [in] op1 Value to rotate
  969. \param [in] op2 Number of Bits to rotate
  970. \return Rotated value
  971. */
  972. __ALWAYS_STATIC_INLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
  973. {
  974. return (op1 >> op2) | (op1 << (32U - op2));
  975. }
  976. /**
  977. \brief Breakpoint
  978. \details Causes the processor to enter Debug state
  979. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  980. */
  981. __ALWAYS_STATIC_INLINE void __BKPT(void)
  982. {
  983. __ASM volatile("ebreak");
  984. }
  985. /**
  986. \brief Reverse bit order of value
  987. \details Reverses the bit order of the given value.
  988. \param [in] value Value to reverse
  989. \return Reversed value
  990. */
  991. __ALWAYS_STATIC_INLINE uint32_t __RBIT(uint32_t value)
  992. {
  993. uint32_t result;
  994. int32_t s = 4 /*sizeof(v)*/ * 8 - 1; /* extra shift needed at end */
  995. result = value; /* r will be reversed bits of v; first get LSB of v */
  996. for (value >>= 1U; value; value >>= 1U) {
  997. result <<= 1U;
  998. result |= value & 1U;
  999. s--;
  1000. }
  1001. result <<= s; /* shift when v's highest bits are zero */
  1002. return (result);
  1003. }
  1004. /**
  1005. \brief Count leading zeros
  1006. \details Counts the number of leading zeros of a data value.
  1007. \param [in] value Value to count the leading zeros
  1008. \return number of leading zeros in value
  1009. */
  1010. #define __CLZ __builtin_clz
  1011. /**
  1012. \details This function saturates a signed value.
  1013. \param [in] x Value to be saturated
  1014. \param [in] y Bit position to saturate to [1..32]
  1015. \return Saturated value.
  1016. */
  1017. __ALWAYS_STATIC_INLINE int32_t __SSAT(int32_t x, uint32_t y)
  1018. {
  1019. int32_t posMax, negMin;
  1020. uint32_t i;
  1021. posMax = 1;
  1022. for (i = 0; i < (y - 1); i++) {
  1023. posMax = posMax * 2;
  1024. }
  1025. if (x > 0) {
  1026. posMax = (posMax - 1);
  1027. if (x > posMax) {
  1028. x = posMax;
  1029. }
  1030. // x &= (posMax * 2 + 1);
  1031. } else {
  1032. negMin = -posMax;
  1033. if (x < negMin) {
  1034. x = negMin;
  1035. }
  1036. // x &= (posMax * 2 - 1);
  1037. }
  1038. return (x);
  1039. }
  1040. /**
  1041. \brief Unsigned Saturate
  1042. \details Saturates an unsigned value.
  1043. \param [in] value Value to be saturated
  1044. \param [in] sat Bit position to saturate to (0..31)
  1045. \return Saturated value
  1046. */
  1047. __ALWAYS_STATIC_INLINE uint32_t __USAT(uint32_t value, uint32_t sat)
  1048. {
  1049. uint32_t result;
  1050. if ((((0xFFFFFFFF >> sat) << sat) & value) != 0) {
  1051. result = 0xFFFFFFFF >> (32 - sat);
  1052. } else {
  1053. result = value;
  1054. }
  1055. return (result);
  1056. }
  1057. /**
  1058. \brief Unsigned Saturate for internal use
  1059. \details Saturates an unsigned value, should not call directly.
  1060. \param [in] value Value to be saturated
  1061. \param [in] sat Bit position to saturate to (0..31)
  1062. \return Saturated value
  1063. */
  1064. __ALWAYS_STATIC_INLINE uint32_t __IUSAT(uint32_t value, uint32_t sat)
  1065. {
  1066. uint32_t result;
  1067. if (value & 0x80000000) { /* only overflow set bit-31 */
  1068. result = 0;
  1069. } else if ((((0xFFFFFFFF >> sat) << sat) & value) != 0) {
  1070. result = 0xFFFFFFFF >> (32 - sat);
  1071. } else {
  1072. result = value;
  1073. }
  1074. return (result);
  1075. }
  1076. /**
  1077. \brief Rotate Right with Extend
  1078. \details This function moves each bit of a bitstring right by one bit.
  1079. The carry input is shifted in at the left end of the bitstring.
  1080. \note carry input will always 0.
  1081. \param [in] op1 Value to rotate
  1082. \return Rotated value
  1083. */
  1084. __ALWAYS_STATIC_INLINE uint32_t __RRX(uint32_t op1)
  1085. {
  1086. return 0;
  1087. }
  1088. /**
  1089. \brief LDRT Unprivileged (8 bit)
  1090. \details Executes a Unprivileged LDRT instruction for 8 bit value.
  1091. \param [in] addr Pointer to location
  1092. \return value of type uint8_t at (*ptr)
  1093. */
  1094. __ALWAYS_STATIC_INLINE uint8_t __LDRBT(volatile uint8_t *addr)
  1095. {
  1096. uint32_t result;
  1097. __ASM volatile("lb %0, 0(%1)" : "=r"(result) : "r"(addr));
  1098. return ((uint8_t) result); /* Add explicit type cast here */
  1099. }
  1100. /**
  1101. \brief LDRT Unprivileged (16 bit)
  1102. \details Executes a Unprivileged LDRT instruction for 16 bit values.
  1103. \param [in] addr Pointer to location
  1104. \return value of type uint16_t at (*ptr)
  1105. */
  1106. __ALWAYS_STATIC_INLINE uint16_t __LDRHT(volatile uint16_t *addr)
  1107. {
  1108. uint32_t result;
  1109. __ASM volatile("lh %0, 0(%1)" : "=r"(result) : "r"(addr));
  1110. return ((uint16_t) result); /* Add explicit type cast here */
  1111. }
  1112. /**
  1113. \brief LDRT Unprivileged (32 bit)
  1114. \details Executes a Unprivileged LDRT instruction for 32 bit values.
  1115. \param [in] addr Pointer to location
  1116. \return value of type uint32_t at (*ptr)
  1117. */
  1118. __ALWAYS_STATIC_INLINE uint32_t __LDRT(volatile uint32_t *addr)
  1119. {
  1120. uint32_t result;
  1121. __ASM volatile("lw %0, 0(%1)" : "=r"(result) : "r"(addr));
  1122. return (result);
  1123. }
  1124. /**
  1125. \brief STRT Unprivileged (8 bit)
  1126. \details Executes a Unprivileged STRT instruction for 8 bit values.
  1127. \param [in] value Value to store
  1128. \param [in] addr Pointer to location
  1129. */
  1130. __ALWAYS_STATIC_INLINE void __STRBT(uint8_t value, volatile uint8_t *addr)
  1131. {
  1132. __ASM volatile("sb %1, 0(%0)" :: "r"(addr), "r"((uint32_t)value) : "memory");
  1133. }
  1134. /**
  1135. \brief STRT Unprivileged (16 bit)
  1136. \details Executes a Unprivileged STRT instruction for 16 bit values.
  1137. \param [in] value Value to store
  1138. \param [in] addr Pointer to location
  1139. */
  1140. __ALWAYS_STATIC_INLINE void __STRHT(uint16_t value, volatile uint16_t *addr)
  1141. {
  1142. __ASM volatile("sh %1, 0(%0)" :: "r"(addr), "r"((uint32_t)value) : "memory");
  1143. }
  1144. /**
  1145. \brief STRT Unprivileged (32 bit)
  1146. \details Executes a Unprivileged STRT instruction for 32 bit values.
  1147. \param [in] value Value to store
  1148. \param [in] addr Pointer to location
  1149. */
  1150. __ALWAYS_STATIC_INLINE void __STRT(uint32_t value, volatile uint32_t *addr)
  1151. {
  1152. __ASM volatile("sw %1, 0(%0)" :: "r"(addr), "r"(value) : "memory");
  1153. }
  1154. /*@}*/ /* end of group CSI_Core_InstructionInterface */
  1155. /* ################### Compiler specific Intrinsics ########################### */
  1156. /** \defgroup CSI_SIMD_intrinsics CSI SIMD Intrinsics
  1157. Access to dedicated SIMD instructions \n
  1158. Single Instruction Multiple Data (SIMD) extensions are provided to simplify development of application software. SIMD extensions increase the processing capability without materially increasing the power consumption. The SIMD extensions are completely transparent to the operating system (OS), allowing existing OS ports to be used.
  1159. @{
  1160. */
  1161. /**
  1162. \brief Halfword packing instruction. Combines bits[15:0] of val1 with bits[31:16]
  1163. of val2 levitated with the val3.
  1164. \details Combine a halfword from one register with a halfword from another register.
  1165. The second argument can be left-shifted before extraction of the halfword.
  1166. \param [in] val1 first 16-bit operands
  1167. \param [in] val2 second 16-bit operands
  1168. \param [in] val3 value for left-shifting val2. Value range [0..31].
  1169. \return the combination of halfwords.
  1170. \remark
  1171. res[15:0] = val1[15:0] \n
  1172. res[31:16] = val2[31:16] << val3
  1173. */
  1174. __ALWAYS_STATIC_INLINE uint32_t __PKHBT(uint32_t val1, uint32_t val2, uint32_t val3)
  1175. {
  1176. return ((((int32_t)(val1) << 0) & (int32_t)0x0000FFFF) | (((int32_t)(val2) << val3) & (int32_t)0xFFFF0000));
  1177. }
  1178. /**
  1179. \brief Halfword packing instruction. Combines bits[31:16] of val1 with bits[15:0]
  1180. of val2 right-shifted with the val3.
  1181. \details Combine a halfword from one register with a halfword from another register.
  1182. The second argument can be right-shifted before extraction of the halfword.
  1183. \param [in] val1 first 16-bit operands
  1184. \param [in] val2 second 16-bit operands
  1185. \param [in] val3 value for right-shifting val2. Value range [1..32].
  1186. \return the combination of halfwords.
  1187. \remark
  1188. res[15:0] = val2[15:0] >> val3 \n
  1189. res[31:16] = val1[31:16]
  1190. */
  1191. __ALWAYS_STATIC_INLINE uint32_t __PKHTB(uint32_t val1, uint32_t val2, uint32_t val3)
  1192. {
  1193. return ((((int32_t)(val1) << 0) & (int32_t)0xFFFF0000) | (((int32_t)(val2) >> val3) & (int32_t)0x0000FFFF));
  1194. }
  1195. /**
  1196. \brief Dual 16-bit signed saturate.
  1197. \details This function saturates a signed value.
  1198. \param [in] x two signed 16-bit values to be saturated.
  1199. \param [in] y bit position for saturation, an integral constant expression in the range 1 to 16.
  1200. \return the sum of the absolute differences of the following bytes, added to the accumulation value:\n
  1201. the signed saturation of the low halfword in val1, saturated to the bit position specified in
  1202. val2 and returned in the low halfword of the return value.\n
  1203. the signed saturation of the high halfword in val1, saturated to the bit position specified in
  1204. val2 and returned in the high halfword of the return value.
  1205. */
  1206. __ALWAYS_STATIC_INLINE uint32_t __SSAT16(int32_t x, const uint32_t y)
  1207. {
  1208. int32_t r = 0, s = 0;
  1209. r = __SSAT((((int32_t)x << 16) >> 16), y) & (int32_t)0x0000FFFF;
  1210. s = __SSAT((((int32_t)x) >> 16), y) & (int32_t)0x0000FFFF;
  1211. return ((uint32_t)((s << 16) | (r)));
  1212. }
  1213. /**
  1214. \brief Dual 16-bit unsigned saturate.
  1215. \details This function enables you to saturate two signed 16-bit values to a selected unsigned range.
  1216. \param [in] x two signed 16-bit values to be saturated.
  1217. \param [in] y bit position for saturation, an integral constant expression in the range 1 to 16.
  1218. \return the saturation of the two signed 16-bit values, as non-negative values:
  1219. the saturation of the low halfword in val1, saturated to the bit position specified in
  1220. val2 and returned in the low halfword of the return value.\n
  1221. the saturation of the high halfword in val1, saturated to the bit position specified in
  1222. val2 and returned in the high halfword of the return value.
  1223. */
  1224. __ALWAYS_STATIC_INLINE uint32_t __USAT16(uint32_t x, const uint32_t y)
  1225. {
  1226. int32_t r = 0, s = 0;
  1227. r = __IUSAT(((x << 16) >> 16), y) & 0x0000FFFF;
  1228. s = __IUSAT(((x) >> 16), y) & 0x0000FFFF;
  1229. return ((s << 16) | (r));
  1230. }
  1231. /**
  1232. \brief Quad 8-bit saturating addition.
  1233. \details This function enables you to perform four 8-bit integer additions,
  1234. saturating the results to the 8-bit signed integer range -2^7 <= x <= 2^7 - 1.
  1235. \param [in] x first four 8-bit summands.
  1236. \param [in] y second four 8-bit summands.
  1237. \return the saturated addition of the first byte of each operand in the first byte of the return value.\n
  1238. the saturated addition of the second byte of each operand in the second byte of the return value.\n
  1239. the saturated addition of the third byte of each operand in the third byte of the return value.\n
  1240. the saturated addition of the fourth byte of each operand in the fourth byte of the return value.\n
  1241. The returned results are saturated to the 8-bit signed integer range -2^7 <= x <= 2^7 - 1.
  1242. \remark
  1243. res[7:0] = val1[7:0] + val2[7:0] \n
  1244. res[15:8] = val1[15:8] + val2[15:8] \n
  1245. res[23:16] = val1[23:16] + val2[23:16] \n
  1246. res[31:24] = val1[31:24] + val2[31:24]
  1247. */
  1248. __ALWAYS_STATIC_INLINE uint32_t __QADD8(uint32_t x, uint32_t y)
  1249. {
  1250. int32_t r, s, t, u;
  1251. r = __SSAT(((((int32_t)x << 24) >> 24) + (((int32_t)y << 24) >> 24)), 8) & (int32_t)0x000000FF;
  1252. s = __SSAT(((((int32_t)x << 16) >> 24) + (((int32_t)y << 16) >> 24)), 8) & (int32_t)0x000000FF;
  1253. t = __SSAT(((((int32_t)x << 8) >> 24) + (((int32_t)y << 8) >> 24)), 8) & (int32_t)0x000000FF;
  1254. u = __SSAT(((((int32_t)x) >> 24) + (((int32_t)y) >> 24)), 8) & (int32_t)0x000000FF;
  1255. return ((uint32_t)((u << 24) | (t << 16) | (s << 8) | (r)));
  1256. }
  1257. /**
  1258. \brief Quad 8-bit unsigned saturating addition.
  1259. \details This function enables you to perform four unsigned 8-bit integer additions,
  1260. saturating the results to the 8-bit unsigned integer range 0 < x < 2^8 - 1.
  1261. \param [in] x first four 8-bit summands.
  1262. \param [in] y second four 8-bit summands.
  1263. \return the saturated addition of the first byte of each operand in the first byte of the return value.\n
  1264. the saturated addition of the second byte of each operand in the second byte of the return value.\n
  1265. the saturated addition of the third byte of each operand in the third byte of the return value.\n
  1266. the saturated addition of the fourth byte of each operand in the fourth byte of the return value.\n
  1267. The returned results are saturated to the 8-bit signed integer range 0 <= x <= 2^8 - 1.
  1268. \remark
  1269. res[7:0] = val1[7:0] + val2[7:0] \n
  1270. res[15:8] = val1[15:8] + val2[15:8] \n
  1271. res[23:16] = val1[23:16] + val2[23:16] \n
  1272. res[31:24] = val1[31:24] + val2[31:24]
  1273. */
  1274. __ALWAYS_STATIC_INLINE uint32_t __UQADD8(uint32_t x, uint32_t y)
  1275. {
  1276. int32_t r, s, t, u;
  1277. r = __IUSAT((((x << 24) >> 24) + ((y << 24) >> 24)), 8) & 0x000000FF;
  1278. s = __IUSAT((((x << 16) >> 24) + ((y << 16) >> 24)), 8) & 0x000000FF;
  1279. t = __IUSAT((((x << 8) >> 24) + ((y << 8) >> 24)), 8) & 0x000000FF;
  1280. u = __IUSAT((((x) >> 24) + ((y) >> 24)), 8) & 0x000000FF;
  1281. return ((u << 24) | (t << 16) | (s << 8) | (r));
  1282. }
  1283. /**
  1284. \brief Quad 8-bit signed addition.
  1285. \details This function performs four 8-bit signed integer additions.
  1286. \param [in] x first four 8-bit summands.
  1287. \param [in] y second four 8-bit summands.
  1288. \return the addition of the first bytes from each operand, in the first byte of the return value.\n
  1289. the addition of the second bytes of each operand, in the second byte of the return value.\n
  1290. the addition of the third bytes of each operand, in the third byte of the return value.\n
  1291. the addition of the fourth bytes of each operand, in the fourth byte of the return value.
  1292. \remark
  1293. res[7:0] = val1[7:0] + val2[7:0] \n
  1294. res[15:8] = val1[15:8] + val2[15:8] \n
  1295. res[23:16] = val1[23:16] + val2[23:16] \n
  1296. res[31:24] = val1[31:24] + val2[31:24]
  1297. */
  1298. __ALWAYS_STATIC_INLINE uint32_t __SADD8(uint32_t x, uint32_t y)
  1299. {
  1300. int32_t r, s, t, u;
  1301. r = ((((int32_t)x << 24) >> 24) + (((int32_t)y << 24) >> 24)) & (int32_t)0x000000FF;
  1302. s = ((((int32_t)x << 16) >> 24) + (((int32_t)y << 16) >> 24)) & (int32_t)0x000000FF;
  1303. t = ((((int32_t)x << 8) >> 24) + (((int32_t)y << 8) >> 24)) & (int32_t)0x000000FF;
  1304. u = ((((int32_t)x) >> 24) + (((int32_t)y) >> 24)) & (int32_t)0x000000FF;
  1305. return ((uint32_t)((u << 24) | (t << 16) | (s << 8) | (r)));
  1306. }
  1307. /**
  1308. \brief Quad 8-bit unsigned addition.
  1309. \details This function performs four unsigned 8-bit integer additions.
  1310. \param [in] x first four 8-bit summands.
  1311. \param [in] y second four 8-bit summands.
  1312. \return the addition of the first bytes from each operand, in the first byte of the return value.\n
  1313. the addition of the second bytes of each operand, in the second byte of the return value.\n
  1314. the addition of the third bytes of each operand, in the third byte of the return value.\n
  1315. the addition of the fourth bytes of each operand, in the fourth byte of the return value.
  1316. \remark
  1317. res[7:0] = val1[7:0] + val2[7:0] \n
  1318. res[15:8] = val1[15:8] + val2[15:8] \n
  1319. res[23:16] = val1[23:16] + val2[23:16] \n
  1320. res[31:24] = val1[31:24] + val2[31:24]
  1321. */
  1322. __ALWAYS_STATIC_INLINE uint32_t __UADD8(uint32_t x, uint32_t y)
  1323. {
  1324. int32_t r, s, t, u;
  1325. r = (((x << 24) >> 24) + ((y << 24) >> 24)) & 0x000000FF;
  1326. s = (((x << 16) >> 24) + ((y << 16) >> 24)) & 0x000000FF;
  1327. t = (((x << 8) >> 24) + ((y << 8) >> 24)) & 0x000000FF;
  1328. u = (((x) >> 24) + ((y) >> 24)) & 0x000000FF;
  1329. return ((u << 24) | (t << 16) | (s << 8) | (r));
  1330. }
  1331. /**
  1332. \brief Quad 8-bit saturating subtract.
  1333. \details This function enables you to perform four 8-bit integer subtractions,
  1334. saturating the results to the 8-bit signed integer range -2^7 <= x <= 2^7 - 1.
  1335. \param [in] x first four 8-bit summands.
  1336. \param [in] y second four 8-bit summands.
  1337. \return the subtraction of the first byte of each operand in the first byte of the return value.\n
  1338. the subtraction of the second byte of each operand in the second byte of the return value.\n
  1339. the subtraction of the third byte of each operand in the third byte of the return value.\n
  1340. the subtraction of the fourth byte of each operand in the fourth byte of the return value.\n
  1341. The returned results are saturated to the 8-bit signed integer range -2^7 <= x <= 2^7 - 1.
  1342. \remark
  1343. res[7:0] = val1[7:0] - val2[7:0] \n
  1344. res[15:8] = val1[15:8] - val2[15:8] \n
  1345. res[23:16] = val1[23:16] - val2[23:16] \n
  1346. res[31:24] = val1[31:24] - val2[31:24]
  1347. */
  1348. __ALWAYS_STATIC_INLINE uint32_t __QSUB8(uint32_t x, uint32_t y)
  1349. {
  1350. int32_t r, s, t, u;
  1351. r = __SSAT(((((int32_t)x << 24) >> 24) - (((int32_t)y << 24) >> 24)), 8) & (int32_t)0x000000FF;
  1352. s = __SSAT(((((int32_t)x << 16) >> 24) - (((int32_t)y << 16) >> 24)), 8) & (int32_t)0x000000FF;
  1353. t = __SSAT(((((int32_t)x << 8) >> 24) - (((int32_t)y << 8) >> 24)), 8) & (int32_t)0x000000FF;
  1354. u = __SSAT(((((int32_t)x) >> 24) - (((int32_t)y) >> 24)), 8) & (int32_t)0x000000FF;
  1355. return ((uint32_t)((u << 24) | (t << 16) | (s << 8) | (r)));
  1356. }
  1357. /**
  1358. \brief Quad 8-bit unsigned saturating subtraction.
  1359. \details This function enables you to perform four unsigned 8-bit integer subtractions,
  1360. saturating the results to the 8-bit unsigned integer range 0 < x < 2^8 - 1.
  1361. \param [in] x first four 8-bit summands.
  1362. \param [in] y second four 8-bit summands.
  1363. \return the subtraction of the first byte of each operand in the first byte of the return value.\n
  1364. the subtraction of the second byte of each operand in the second byte of the return value.\n
  1365. the subtraction of the third byte of each operand in the third byte of the return value.\n
  1366. the subtraction of the fourth byte of each operand in the fourth byte of the return value.\n
  1367. The returned results are saturated to the 8-bit unsigned integer range 0 <= x <= 2^8 - 1.
  1368. \remark
  1369. res[7:0] = val1[7:0] - val2[7:0] \n
  1370. res[15:8] = val1[15:8] - val2[15:8] \n
  1371. res[23:16] = val1[23:16] - val2[23:16] \n
  1372. res[31:24] = val1[31:24] - val2[31:24]
  1373. */
  1374. __ALWAYS_STATIC_INLINE uint32_t __UQSUB8(uint32_t x, uint32_t y)
  1375. {
  1376. int32_t r, s, t, u;
  1377. r = __IUSAT((((x << 24) >> 24) - ((y << 24) >> 24)), 8) & 0x000000FF;
  1378. s = __IUSAT((((x << 16) >> 24) - ((y << 16) >> 24)), 8) & 0x000000FF;
  1379. t = __IUSAT((((x << 8) >> 24) - ((y << 8) >> 24)), 8) & 0x000000FF;
  1380. u = __IUSAT((((x) >> 24) - ((y) >> 24)), 8) & 0x000000FF;
  1381. return ((u << 24) | (t << 16) | (s << 8) | (r));
  1382. }
  1383. /**
  1384. \brief Quad 8-bit signed subtraction.
  1385. \details This function enables you to perform four 8-bit signed integer subtractions.
  1386. \param [in] x first four 8-bit operands of each subtraction.
  1387. \param [in] y second four 8-bit operands of each subtraction.
  1388. \return the subtraction of the first bytes from each operand, in the first byte of the return value.\n
  1389. the subtraction of the second bytes of each operand, in the second byte of the return value.\n
  1390. the subtraction of the third bytes of each operand, in the third byte of the return value.\n
  1391. the subtraction of the fourth bytes of each operand, in the fourth byte of the return value.
  1392. \remark
  1393. res[7:0] = val1[7:0] - val2[7:0] \n
  1394. res[15:8] = val1[15:8] - val2[15:8] \n
  1395. res[23:16] = val1[23:16] - val2[23:16] \n
  1396. res[31:24] = val1[31:24] - val2[31:24]
  1397. */
  1398. __ALWAYS_STATIC_INLINE uint32_t __SSUB8(uint32_t x, uint32_t y)
  1399. {
  1400. int32_t r, s, t, u;
  1401. r = ((((int32_t)x << 24) >> 24) - (((int32_t)y << 24) >> 24)) & (int32_t)0x000000FF;
  1402. s = ((((int32_t)x << 16) >> 24) - (((int32_t)y << 16) >> 24)) & (int32_t)0x000000FF;
  1403. t = ((((int32_t)x << 8) >> 24) - (((int32_t)y << 8) >> 24)) & (int32_t)0x000000FF;
  1404. u = ((((int32_t)x) >> 24) - (((int32_t)y) >> 24)) & (int32_t)0x000000FF;
  1405. return ((uint32_t)((u << 24) | (t << 16) | (s << 8) | (r)));
  1406. }
  1407. /**
  1408. \brief Quad 8-bit unsigned subtract.
  1409. \details This function enables you to perform four 8-bit unsigned integer subtractions.
  1410. \param [in] x first four 8-bit operands of each subtraction.
  1411. \param [in] y second four 8-bit operands of each subtraction.
  1412. \return the subtraction of the first bytes from each operand, in the first byte of the return value.\n
  1413. the subtraction of the second bytes of each operand, in the second byte of the return value.\n
  1414. the subtraction of the third bytes of each operand, in the third byte of the return value.\n
  1415. the subtraction of the fourth bytes of each operand, in the fourth byte of the return value.
  1416. \remark
  1417. res[7:0] = val1[7:0] - val2[7:0] \n
  1418. res[15:8] = val1[15:8] - val2[15:8] \n
  1419. res[23:16] = val1[23:16] - val2[23:16] \n
  1420. res[31:24] = val1[31:24] - val2[31:24]
  1421. */
  1422. __ALWAYS_STATIC_INLINE uint32_t __USUB8(uint32_t x, uint32_t y)
  1423. {
  1424. int32_t r, s, t, u;
  1425. r = (((x << 24) >> 24) - ((y << 24) >> 24)) & 0x000000FF;
  1426. s = (((x << 16) >> 24) - ((y << 16) >> 24)) & 0x000000FF;
  1427. t = (((x << 8) >> 24) - ((y << 8) >> 24)) & 0x000000FF;
  1428. u = (((x) >> 24) - ((y) >> 24)) & 0x000000FF;
  1429. return ((u << 24) | (t << 16) | (s << 8) | (r));
  1430. }
  1431. /**
  1432. \brief Unsigned sum of quad 8-bit unsigned absolute difference.
  1433. \details This function enables you to perform four unsigned 8-bit subtractions, and add the absolute values
  1434. of the differences together, returning the result as a single unsigned integer.
  1435. \param [in] x first four 8-bit operands of each subtraction.
  1436. \param [in] y second four 8-bit operands of each subtraction.
  1437. \return the subtraction of the first bytes from each operand, in the first byte of the return value.\n
  1438. the subtraction of the second bytes of each operand, in the second byte of the return value.\n
  1439. the subtraction of the third bytes of each operand, in the third byte of the return value.\n
  1440. the subtraction of the fourth bytes of each operand, in the fourth byte of the return value.\n
  1441. The sum is returned as a single unsigned integer.
  1442. \remark
  1443. absdiff1 = val1[7:0] - val2[7:0] \n
  1444. absdiff2 = val1[15:8] - val2[15:8] \n
  1445. absdiff3 = val1[23:16] - val2[23:16] \n
  1446. absdiff4 = val1[31:24] - val2[31:24] \n
  1447. res[31:0] = absdiff1 + absdiff2 + absdiff3 + absdiff4
  1448. */
  1449. __ALWAYS_STATIC_INLINE uint32_t __USAD8(uint32_t x, uint32_t y)
  1450. {
  1451. int32_t r, s, t, u;
  1452. r = (((x << 24) >> 24) - ((y << 24) >> 24)) & 0x000000FF;
  1453. s = (((x << 16) >> 24) - ((y << 16) >> 24)) & 0x000000FF;
  1454. t = (((x << 8) >> 24) - ((y << 8) >> 24)) & 0x000000FF;
  1455. u = (((x) >> 24) - ((y) >> 24)) & 0x000000FF;
  1456. return (u + t + s + r);
  1457. }
  1458. /**
  1459. \brief Unsigned sum of quad 8-bit unsigned absolute difference with 32-bit accumulate.
  1460. \details This function enables you to perform four unsigned 8-bit subtractions, and add the absolute values
  1461. of the differences to a 32-bit accumulate operand.
  1462. \param [in] x first four 8-bit operands of each subtraction.
  1463. \param [in] y second four 8-bit operands of each subtraction.
  1464. \param [in] sum accumulation value.
  1465. \return the sum of the absolute differences of the following bytes, added to the accumulation value:
  1466. the subtraction of the first bytes from each operand, in the first byte of the return value.\n
  1467. the subtraction of the second bytes of each operand, in the second byte of the return value.\n
  1468. the subtraction of the third bytes of each operand, in the third byte of the return value.\n
  1469. the subtraction of the fourth bytes of each operand, in the fourth byte of the return value.
  1470. \remark
  1471. absdiff1 = val1[7:0] - val2[7:0] \n
  1472. absdiff2 = val1[15:8] - val2[15:8] \n
  1473. absdiff3 = val1[23:16] - val2[23:16] \n
  1474. absdiff4 = val1[31:24] - val2[31:24] \n
  1475. sum = absdiff1 + absdiff2 + absdiff3 + absdiff4 \n
  1476. res[31:0] = sum[31:0] + val3[31:0]
  1477. */
  1478. __ALWAYS_STATIC_INLINE uint32_t __USADA8(uint32_t x, uint32_t y, uint32_t sum)
  1479. {
  1480. int32_t r, s, t, u;
  1481. #ifdef __cplusplus
  1482. r = (abs((long long)((x << 24) >> 24) - ((y << 24) >> 24))) & 0x000000FF;
  1483. s = (abs((long long)((x << 16) >> 24) - ((y << 16) >> 24))) & 0x000000FF;
  1484. t = (abs((long long)((x << 8) >> 24) - ((y << 8) >> 24))) & 0x000000FF;
  1485. u = (abs((long long)((x) >> 24) - ((y) >> 24))) & 0x000000FF;
  1486. #else
  1487. r = (abs(((x << 24) >> 24) - ((y << 24) >> 24))) & 0x000000FF;
  1488. s = (abs(((x << 16) >> 24) - ((y << 16) >> 24))) & 0x000000FF;
  1489. t = (abs(((x << 8) >> 24) - ((y << 8) >> 24))) & 0x000000FF;
  1490. u = (abs(((x) >> 24) - ((y) >> 24))) & 0x000000FF;
  1491. #endif
  1492. return (u + t + s + r + sum);
  1493. }
  1494. /**
  1495. \brief Dual 16-bit saturating addition.
  1496. \details This function enables you to perform two 16-bit integer arithmetic additions in parallel,
  1497. saturating the results to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1498. \param [in] x first two 16-bit summands.
  1499. \param [in] y second two 16-bit summands.
  1500. \return the saturated addition of the low halfwords, in the low halfword of the return value.\n
  1501. the saturated addition of the high halfwords, in the high halfword of the return value.\n
  1502. The returned results are saturated to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1503. \remark
  1504. res[15:0] = val1[15:0] + val2[15:0] \n
  1505. res[31:16] = val1[31:16] + val2[31:16]
  1506. */
  1507. __ALWAYS_STATIC_INLINE uint32_t __QADD16(uint32_t x, uint32_t y)
  1508. {
  1509. int32_t r = 0, s = 0;
  1510. r = __SSAT(((((int32_t)x << 16) >> 16) + (((int32_t)y << 16) >> 16)), 16) & (int32_t)0x0000FFFF;
  1511. s = __SSAT(((((int32_t)x) >> 16) + (((int32_t)y) >> 16)), 16) & (int32_t)0x0000FFFF;
  1512. return ((uint32_t)((s << 16) | (r)));
  1513. }
  1514. /**
  1515. \brief Dual 16-bit unsigned saturating addition.
  1516. \details This function enables you to perform two unsigned 16-bit integer additions, saturating
  1517. the results to the 16-bit unsigned integer range 0 < x < 2^16 - 1.
  1518. \param [in] x first two 16-bit summands.
  1519. \param [in] y second two 16-bit summands.
  1520. \return the saturated addition of the low halfwords, in the low halfword of the return value.\n
  1521. the saturated addition of the high halfwords, in the high halfword of the return value.\n
  1522. The results are saturated to the 16-bit unsigned integer range 0 < x < 2^16 - 1.
  1523. \remark
  1524. res[15:0] = val1[15:0] + val2[15:0] \n
  1525. res[31:16] = val1[31:16] + val2[31:16]
  1526. */
  1527. __ALWAYS_STATIC_INLINE uint32_t __UQADD16(uint32_t x, uint32_t y)
  1528. {
  1529. int32_t r = 0, s = 0;
  1530. r = __IUSAT((((x << 16) >> 16) + ((y << 16) >> 16)), 16) & 0x0000FFFF;
  1531. s = __IUSAT((((x) >> 16) + ((y) >> 16)), 16) & 0x0000FFFF;
  1532. return ((s << 16) | (r));
  1533. }
  1534. /**
  1535. \brief Dual 16-bit signed addition.
  1536. \details This function enables you to perform two 16-bit signed integer additions.
  1537. \param [in] x first two 16-bit summands.
  1538. \param [in] y second two 16-bit summands.
  1539. \return the addition of the low halfwords in the low halfword of the return value.\n
  1540. the addition of the high halfwords in the high halfword of the return value.
  1541. \remark
  1542. res[15:0] = val1[15:0] + val2[15:0] \n
  1543. res[31:16] = val1[31:16] + val2[31:16]
  1544. */
  1545. __ALWAYS_STATIC_INLINE uint32_t __SADD16(uint32_t x, uint32_t y)
  1546. {
  1547. int32_t r = 0, s = 0;
  1548. r = ((((int32_t)x << 16) >> 16) + (((int32_t)y << 16) >> 16)) & (int32_t)0x0000FFFF;
  1549. s = ((((int32_t)x) >> 16) + (((int32_t)y) >> 16)) & (int32_t)0x0000FFFF;
  1550. return ((uint32_t)((s << 16) | (r)));
  1551. }
  1552. /**
  1553. \brief Dual 16-bit unsigned addition
  1554. \details This function enables you to perform two 16-bit unsigned integer additions.
  1555. \param [in] x first two 16-bit summands for each addition.
  1556. \param [in] y second two 16-bit summands for each addition.
  1557. \return the addition of the low halfwords in the low halfword of the return value.\n
  1558. the addition of the high halfwords in the high halfword of the return value.
  1559. \remark
  1560. res[15:0] = val1[15:0] + val2[15:0] \n
  1561. res[31:16] = val1[31:16] + val2[31:16]
  1562. */
  1563. __ALWAYS_STATIC_INLINE uint32_t __UADD16(uint32_t x, uint32_t y)
  1564. {
  1565. int32_t r = 0, s = 0;
  1566. r = (((x << 16) >> 16) + ((y << 16) >> 16)) & 0x0000FFFF;
  1567. s = (((x) >> 16) + ((y) >> 16)) & 0x0000FFFF;
  1568. return ((s << 16) | (r));
  1569. }
  1570. /**
  1571. \brief Dual 16-bit signed addition with halved results.
  1572. \details This function enables you to perform two signed 16-bit integer additions, halving the results.
  1573. \param [in] x first two 16-bit summands.
  1574. \param [in] y second two 16-bit summands.
  1575. \return the halved addition of the low halfwords, in the low halfword of the return value.\n
  1576. the halved addition of the high halfwords, in the high halfword of the return value.
  1577. \remark
  1578. res[15:0] = (val1[15:0] + val2[15:0]) >> 1 \n
  1579. res[31:16] = (val1[31:16] + val2[31:16]) >> 1
  1580. */
  1581. __ALWAYS_STATIC_INLINE uint32_t __SHADD16(uint32_t x, uint32_t y)
  1582. {
  1583. int32_t r, s;
  1584. r = (((((int32_t)x << 16) >> 16) + (((int32_t)y << 16) >> 16)) >> 1) & (int32_t)0x0000FFFF;
  1585. s = (((((int32_t)x) >> 16) + (((int32_t)y) >> 16)) >> 1) & (int32_t)0x0000FFFF;
  1586. return ((uint32_t)((s << 16) | (r)));
  1587. }
  1588. /**
  1589. \brief Dual 16-bit unsigned addition with halved results.
  1590. \details This function enables you to perform two unsigned 16-bit integer additions, halving the results.
  1591. \param [in] x first two 16-bit summands.
  1592. \param [in] y second two 16-bit summands.
  1593. \return the halved addition of the low halfwords, in the low halfword of the return value.\n
  1594. the halved addition of the high halfwords, in the high halfword of the return value.
  1595. \remark
  1596. res[15:0] = (val1[15:0] + val2[15:0]) >> 1 \n
  1597. res[31:16] = (val1[31:16] + val2[31:16]) >> 1
  1598. */
  1599. __ALWAYS_STATIC_INLINE uint32_t __UHADD16(uint32_t x, uint32_t y)
  1600. {
  1601. int32_t r, s;
  1602. r = ((((x << 16) >> 16) + ((y << 16) >> 16)) >> 1) & 0x0000FFFF;
  1603. s = ((((x) >> 16) + ((y) >> 16)) >> 1) & 0x0000FFFF;
  1604. return ((s << 16) | (r));
  1605. }
  1606. /**
  1607. \brief Quad 8-bit signed addition with halved results.
  1608. \details This function enables you to perform four signed 8-bit integer additions, halving the results.
  1609. \param [in] x first four 8-bit summands.
  1610. \param [in] y second four 8-bit summands.
  1611. \return the halved addition of the first bytes from each operand, in the first byte of the return value.\n
  1612. the halved addition of the second bytes from each operand, in the second byte of the return value.\n
  1613. the halved addition of the third bytes from each operand, in the third byte of the return value.\n
  1614. the halved addition of the fourth bytes from each operand, in the fourth byte of the return value.
  1615. \remark
  1616. res[7:0] = (val1[7:0] + val2[7:0] ) >> 1 \n
  1617. res[15:8] = (val1[15:8] + val2[15:8] ) >> 1 \n
  1618. res[23:16] = (val1[23:16] + val2[23:16]) >> 1 \n
  1619. res[31:24] = (val1[31:24] + val2[31:24]) >> 1
  1620. */
  1621. __ALWAYS_STATIC_INLINE uint32_t __SHADD8(uint32_t x, uint32_t y)
  1622. {
  1623. int32_t r, s, t, u;
  1624. r = (((((int32_t)x << 24) >> 24) + (((int32_t)y << 24) >> 24)) >> 1) & (int32_t)0x000000FF;
  1625. s = (((((int32_t)x << 16) >> 24) + (((int32_t)y << 16) >> 24)) >> 1) & (int32_t)0x000000FF;
  1626. t = (((((int32_t)x << 8) >> 24) + (((int32_t)y << 8) >> 24)) >> 1) & (int32_t)0x000000FF;
  1627. u = (((((int32_t)x) >> 24) + (((int32_t)y) >> 24)) >> 1) & (int32_t)0x000000FF;
  1628. return ((uint32_t)((u << 24) | (t << 16) | (s << 8) | (r)));
  1629. }
  1630. /**
  1631. \brief Quad 8-bit unsigned addition with halved results.
  1632. \details This function enables you to perform four unsigned 8-bit integer additions, halving the results.
  1633. \param [in] x first four 8-bit summands.
  1634. \param [in] y second four 8-bit summands.
  1635. \return the halved addition of the first bytes from each operand, in the first byte of the return value.\n
  1636. the halved addition of the second bytes from each operand, in the second byte of the return value.\n
  1637. the halved addition of the third bytes from each operand, in the third byte of the return value.\n
  1638. the halved addition of the fourth bytes from each operand, in the fourth byte of the return value.
  1639. \remark
  1640. res[7:0] = (val1[7:0] + val2[7:0] ) >> 1 \n
  1641. res[15:8] = (val1[15:8] + val2[15:8] ) >> 1 \n
  1642. res[23:16] = (val1[23:16] + val2[23:16]) >> 1 \n
  1643. res[31:24] = (val1[31:24] + val2[31:24]) >> 1
  1644. */
  1645. __ALWAYS_STATIC_INLINE uint32_t __UHADD8(uint32_t x, uint32_t y)
  1646. {
  1647. int32_t r, s, t, u;
  1648. r = ((((x << 24) >> 24) + ((y << 24) >> 24)) >> 1) & 0x000000FF;
  1649. s = ((((x << 16) >> 24) + ((y << 16) >> 24)) >> 1) & 0x000000FF;
  1650. t = ((((x << 8) >> 24) + ((y << 8) >> 24)) >> 1) & 0x000000FF;
  1651. u = ((((x) >> 24) + ((y) >> 24)) >> 1) & 0x000000FF;
  1652. return ((u << 24) | (t << 16) | (s << 8) | (r));
  1653. }
  1654. /**
  1655. \brief Dual 16-bit saturating subtract.
  1656. \details This function enables you to perform two 16-bit integer subtractions in parallel,
  1657. saturating the results to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1658. \param [in] x first two 16-bit summands.
  1659. \param [in] y second two 16-bit summands.
  1660. \return the saturated subtraction of the low halfwords, in the low halfword of the return value.\n
  1661. the saturated subtraction of the high halfwords, in the high halfword of the return value.\n
  1662. The returned results are saturated to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1663. \remark
  1664. res[15:0] = val1[15:0] - val2[15:0] \n
  1665. res[31:16] = val1[31:16] - val2[31:16]
  1666. */
  1667. __ALWAYS_STATIC_INLINE uint32_t __QSUB16(uint32_t x, uint32_t y)
  1668. {
  1669. int32_t r, s;
  1670. r = __SSAT(((((int32_t)x << 16) >> 16) - (((int32_t)y << 16) >> 16)), 16) & (int32_t)0x0000FFFF;
  1671. s = __SSAT(((((int32_t)x) >> 16) - (((int32_t)y) >> 16)), 16) & (int32_t)0x0000FFFF;
  1672. return ((uint32_t)((s << 16) | (r)));
  1673. }
  1674. /**
  1675. \brief Dual 16-bit unsigned saturating subtraction.
  1676. \details This function enables you to perform two unsigned 16-bit integer subtractions,
  1677. saturating the results to the 16-bit unsigned integer range 0 < x < 2^16 - 1.
  1678. \param [in] x first two 16-bit operands for each subtraction.
  1679. \param [in] y second two 16-bit operands for each subtraction.
  1680. \return the saturated subtraction of the low halfwords, in the low halfword of the return value.\n
  1681. the saturated subtraction of the high halfwords, in the high halfword of the return value.\n
  1682. The returned results are saturated to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1683. \remark
  1684. res[15:0] = val1[15:0] - val2[15:0] \n
  1685. res[31:16] = val1[31:16] - val2[31:16]
  1686. */
  1687. __ALWAYS_STATIC_INLINE uint32_t __UQSUB16(uint32_t x, uint32_t y)
  1688. {
  1689. int32_t r, s;
  1690. r = __IUSAT((((x << 16) >> 16) - ((y << 16) >> 16)), 16) & 0x0000FFFF;
  1691. s = __IUSAT((((x) >> 16) - ((y) >> 16)), 16) & 0x0000FFFF;
  1692. return ((s << 16) | (r));
  1693. }
  1694. /**
  1695. \brief Dual 16-bit signed subtraction.
  1696. \details This function enables you to perform two 16-bit signed integer subtractions.
  1697. \param [in] x first two 16-bit operands of each subtraction.
  1698. \param [in] y second two 16-bit operands of each subtraction.
  1699. \return the subtraction of the low halfword in the second operand from the low
  1700. halfword in the first operand, in the low halfword of the return value. \n
  1701. the subtraction of the high halfword in the second operand from the high
  1702. halfword in the first operand, in the high halfword of the return value.
  1703. \remark
  1704. res[15:0] = val1[15:0] - val2[15:0] \n
  1705. res[31:16] = val1[31:16] - val2[31:16]
  1706. */
  1707. __ALWAYS_STATIC_INLINE uint32_t __SSUB16(uint32_t x, uint32_t y)
  1708. {
  1709. int32_t r, s;
  1710. r = ((((int32_t)x << 16) >> 16) - (((int32_t)y << 16) >> 16)) & (int32_t)0x0000FFFF;
  1711. s = ((((int32_t)x) >> 16) - (((int32_t)y) >> 16)) & (int32_t)0x0000FFFF;
  1712. return ((uint32_t)((s << 16) | (r)));
  1713. }
  1714. /**
  1715. \brief Dual 16-bit unsigned subtract.
  1716. \details This function enables you to perform two 16-bit unsigned integer subtractions.
  1717. \param [in] x first two 16-bit operands of each subtraction.
  1718. \param [in] y second two 16-bit operands of each subtraction.
  1719. \return the subtraction of the low halfword in the second operand from the low
  1720. halfword in the first operand, in the low halfword of the return value. \n
  1721. the subtraction of the high halfword in the second operand from the high
  1722. halfword in the first operand, in the high halfword of the return value.
  1723. \remark
  1724. res[15:0] = val1[15:0] - val2[15:0] \n
  1725. res[31:16] = val1[31:16] - val2[31:16]
  1726. */
  1727. __ALWAYS_STATIC_INLINE uint32_t __USUB16(uint32_t x, uint32_t y)
  1728. {
  1729. int32_t r, s;
  1730. r = (((x << 16) >> 16) - ((y << 16) >> 16)) & 0x0000FFFF;
  1731. s = (((x) >> 16) - ((y) >> 16)) & 0x0000FFFF;
  1732. return ((s << 16) | (r));
  1733. }
  1734. /**
  1735. \brief Dual 16-bit signed subtraction with halved results.
  1736. \details This function enables you to perform two signed 16-bit integer subtractions, halving the results.
  1737. \param [in] x first two 16-bit summands.
  1738. \param [in] y second two 16-bit summands.
  1739. \return the halved subtraction of the low halfwords, in the low halfword of the return value.\n
  1740. the halved subtraction of the high halfwords, in the high halfword of the return value.
  1741. \remark
  1742. res[15:0] = (val1[15:0] - val2[15:0]) >> 1 \n
  1743. res[31:16] = (val1[31:16] - val2[31:16]) >> 1
  1744. */
  1745. __ALWAYS_STATIC_INLINE uint32_t __SHSUB16(uint32_t x, uint32_t y)
  1746. {
  1747. int32_t r, s;
  1748. r = (((((int32_t)x << 16) >> 16) - (((int32_t)y << 16) >> 16)) >> 1) & (int32_t)0x0000FFFF;
  1749. s = (((((int32_t)x) >> 16) - (((int32_t)y) >> 16)) >> 1) & (int32_t)0x0000FFFF;
  1750. return ((uint32_t)((s << 16) | (r)));
  1751. }
  1752. /**
  1753. \brief Dual 16-bit unsigned subtraction with halved results.
  1754. \details This function enables you to perform two unsigned 16-bit integer subtractions, halving the results.
  1755. \param [in] x first two 16-bit summands.
  1756. \param [in] y second two 16-bit summands.
  1757. \return the halved subtraction of the low halfwords, in the low halfword of the return value.\n
  1758. the halved subtraction of the high halfwords, in the high halfword of the return value.
  1759. \remark
  1760. res[15:0] = (val1[15:0] - val2[15:0]) >> 1 \n
  1761. res[31:16] = (val1[31:16] - val2[31:16]) >> 1
  1762. */
  1763. __ALWAYS_STATIC_INLINE uint32_t __UHSUB16(uint32_t x, uint32_t y)
  1764. {
  1765. int32_t r, s;
  1766. r = ((((x << 16) >> 16) - ((y << 16) >> 16)) >> 1) & 0x0000FFFF;
  1767. s = ((((x) >> 16) - ((y) >> 16)) >> 1) & 0x0000FFFF;
  1768. return ((s << 16) | (r));
  1769. }
  1770. /**
  1771. \brief Quad 8-bit signed addition with halved results.
  1772. \details This function enables you to perform four signed 8-bit integer subtractions, halving the results.
  1773. \param [in] x first four 8-bit summands.
  1774. \param [in] y second four 8-bit summands.
  1775. \return the halved subtraction of the first bytes from each operand, in the first byte of the return value.\n
  1776. the halved subtraction of the second bytes from each operand, in the second byte of the return value.\n
  1777. the halved subtraction of the third bytes from each operand, in the third byte of the return value.\n
  1778. the halved subtraction of the fourth bytes from each operand, in the fourth byte of the return value.
  1779. \remark
  1780. res[7:0] = (val1[7:0] - val2[7:0] ) >> 1 \n
  1781. res[15:8] = (val1[15:8] - val2[15:8] ) >> 1 \n
  1782. res[23:16] = (val1[23:16] - val2[23:16]) >> 1 \n
  1783. res[31:24] = (val1[31:24] - val2[31:24]) >> 1
  1784. */
  1785. __ALWAYS_STATIC_INLINE uint32_t __SHSUB8(uint32_t x, uint32_t y)
  1786. {
  1787. int32_t r, s, t, u;
  1788. r = (((((int32_t)x << 24) >> 24) - (((int32_t)y << 24) >> 24)) >> 1) & (int32_t)0x000000FF;
  1789. s = (((((int32_t)x << 16) >> 24) - (((int32_t)y << 16) >> 24)) >> 1) & (int32_t)0x000000FF;
  1790. t = (((((int32_t)x << 8) >> 24) - (((int32_t)y << 8) >> 24)) >> 1) & (int32_t)0x000000FF;
  1791. u = (((((int32_t)x) >> 24) - (((int32_t)y) >> 24)) >> 1) & (int32_t)0x000000FF;
  1792. return ((uint32_t)((u << 24) | (t << 16) | (s << 8) | (r)));
  1793. }
  1794. /**
  1795. \brief Quad 8-bit unsigned subtraction with halved results.
  1796. \details This function enables you to perform four unsigned 8-bit integer subtractions, halving the results.
  1797. \param [in] x first four 8-bit summands.
  1798. \param [in] y second four 8-bit summands.
  1799. \return the halved subtraction of the first bytes from each operand, in the first byte of the return value.\n
  1800. the halved subtraction of the second bytes from each operand, in the second byte of the return value.\n
  1801. the halved subtraction of the third bytes from each operand, in the third byte of the return value.\n
  1802. the halved subtraction of the fourth bytes from each operand, in the fourth byte of the return value.
  1803. \remark
  1804. res[7:0] = (val1[7:0] - val2[7:0] ) >> 1 \n
  1805. res[15:8] = (val1[15:8] - val2[15:8] ) >> 1 \n
  1806. res[23:16] = (val1[23:16] - val2[23:16]) >> 1 \n
  1807. res[31:24] = (val1[31:24] - val2[31:24]) >> 1
  1808. */
  1809. __ALWAYS_STATIC_INLINE uint32_t __UHSUB8(uint32_t x, uint32_t y)
  1810. {
  1811. int32_t r, s, t, u;
  1812. r = ((((x << 24) >> 24) - ((y << 24) >> 24)) >> 1) & 0x000000FF;
  1813. s = ((((x << 16) >> 24) - ((y << 16) >> 24)) >> 1) & 0x000000FF;
  1814. t = ((((x << 8) >> 24) - ((y << 8) >> 24)) >> 1) & 0x000000FF;
  1815. u = ((((x) >> 24) - ((y) >> 24)) >> 1) & 0x000000FF;
  1816. return ((u << 24) | (t << 16) | (s << 8) | (r));
  1817. }
  1818. /**
  1819. \brief Dual 16-bit add and subtract with exchange.
  1820. \details This function enables you to exchange the halfwords of the one operand,
  1821. then add the high halfwords and subtract the low halfwords,
  1822. saturating the results to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1823. \param [in] x first operand for the subtraction in the low halfword,
  1824. and the first operand for the addition in the high halfword.
  1825. \param [in] y second operand for the subtraction in the high halfword,
  1826. and the second operand for the addition in the low halfword.
  1827. \return the saturated subtraction of the high halfword in the second operand from the
  1828. low halfword in the first operand, in the low halfword of the return value.\n
  1829. the saturated addition of the high halfword in the first operand and the
  1830. low halfword in the second operand, in the high halfword of the return value.\n
  1831. The returned results are saturated to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1832. \remark
  1833. res[15:0] = val1[15:0] - val2[31:16] \n
  1834. res[31:16] = val1[31:16] + val2[15:0]
  1835. */
  1836. __ALWAYS_STATIC_INLINE uint32_t __QASX(uint32_t x, uint32_t y)
  1837. {
  1838. int32_t r, s;
  1839. r = __SSAT(((((int32_t)x << 16) >> 16) - (((int32_t)y) >> 16)), 16) & (int32_t)0x0000FFFF;
  1840. s = __SSAT(((((int32_t)x) >> 16) + (((int32_t)y << 16) >> 16)), 16) & (int32_t)0x0000FFFF;
  1841. return ((uint32_t)((s << 16) | (r)));
  1842. }
  1843. /**
  1844. \brief Dual 16-bit unsigned saturating addition and subtraction with exchange.
  1845. \details This function enables you to exchange the halfwords of the second operand and
  1846. perform one unsigned 16-bit integer addition and one unsigned 16-bit subtraction,
  1847. saturating the results to the 16-bit unsigned integer range 0 <= x <= 2^16 - 1.
  1848. \param [in] x first operand for the subtraction in the low halfword,
  1849. and the first operand for the addition in the high halfword.
  1850. \param [in] y second operand for the subtraction in the high halfword,
  1851. and the second operand for the addition in the low halfword.
  1852. \return the saturated subtraction of the high halfword in the second operand from the
  1853. low halfword in the first operand, in the low halfword of the return value.\n
  1854. the saturated addition of the high halfword in the first operand and the
  1855. low halfword in the second operand, in the high halfword of the return value.\n
  1856. The returned results are saturated to the 16-bit unsigned integer range 0 <= x <= 2^16 - 1.
  1857. \remark
  1858. res[15:0] = val1[15:0] - val2[31:16] \n
  1859. res[31:16] = val1[31:16] + val2[15:0]
  1860. */
  1861. __ALWAYS_STATIC_INLINE uint32_t __UQASX(uint32_t x, uint32_t y)
  1862. {
  1863. int32_t r, s;
  1864. r = __IUSAT((((x << 16) >> 16) - ((y) >> 16)), 16) & 0x0000FFFF;
  1865. s = __IUSAT((((x) >> 16) + ((y << 16) >> 16)), 16) & 0x0000FFFF;
  1866. return ((s << 16) | (r));
  1867. }
  1868. /**
  1869. \brief Dual 16-bit addition and subtraction with exchange.
  1870. \details It enables you to exchange the halfwords of the second operand, add the high halfwords
  1871. and subtract the low halfwords.
  1872. \param [in] x first operand for the subtraction in the low halfword,
  1873. and the first operand for the addition in the high halfword.
  1874. \param [in] y second operand for the subtraction in the high halfword,
  1875. and the second operand for the addition in the low halfword.
  1876. \return the subtraction of the high halfword in the second operand from the
  1877. low halfword in the first operand, in the low halfword of the return value.\n
  1878. the addition of the high halfword in the first operand and the
  1879. low halfword in the second operand, in the high halfword of the return value.
  1880. \remark
  1881. res[15:0] = val1[15:0] - val2[31:16] \n
  1882. res[31:16] = val1[31:16] + val2[15:0]
  1883. */
  1884. __ALWAYS_STATIC_INLINE uint32_t __SASX(uint32_t x, uint32_t y)
  1885. {
  1886. int32_t r, s;
  1887. r = ((((int32_t)x << 16) >> 16) - (((int32_t)y) >> 16)) & (int32_t)0x0000FFFF;
  1888. s = ((((int32_t)x) >> 16) + (((int32_t)y << 16) >> 16)) & (int32_t)0x0000FFFF;
  1889. return ((uint32_t)((s << 16) | (r)));
  1890. }
  1891. /**
  1892. \brief Dual 16-bit unsigned addition and subtraction with exchange.
  1893. \details This function enables you to exchange the two halfwords of the second operand,
  1894. add the high halfwords and subtract the low halfwords.
  1895. \param [in] x first operand for the subtraction in the low halfword,
  1896. and the first operand for the addition in the high halfword.
  1897. \param [in] y second operand for the subtraction in the high halfword,
  1898. and the second operand for the addition in the low halfword.
  1899. \return the subtraction of the high halfword in the second operand from the
  1900. low halfword in the first operand, in the low halfword of the return value.\n
  1901. the addition of the high halfword in the first operand and the
  1902. low halfword in the second operand, in the high halfword of the return value.
  1903. \remark
  1904. res[15:0] = val1[15:0] - val2[31:16] \n
  1905. res[31:16] = val1[31:16] + val2[15:0]
  1906. */
  1907. __ALWAYS_STATIC_INLINE uint32_t __UASX(uint32_t x, uint32_t y)
  1908. {
  1909. int32_t r, s;
  1910. r = (((x << 16) >> 16) - ((y) >> 16)) & 0x0000FFFF;
  1911. s = (((x) >> 16) + ((y << 16) >> 16)) & 0x0000FFFF;
  1912. return ((s << 16) | (r));
  1913. }
  1914. /**
  1915. \brief Dual 16-bit signed addition and subtraction with halved results.
  1916. \details This function enables you to exchange the two halfwords of one operand, perform one
  1917. signed 16-bit integer addition and one signed 16-bit subtraction, and halve the results.
  1918. \param [in] x first 16-bit operands.
  1919. \param [in] y second 16-bit operands.
  1920. \return the halved subtraction of the high halfword in the second operand from the
  1921. low halfword in the first operand, in the low halfword of the return value.\n
  1922. the halved addition of the low halfword in the second operand from the high
  1923. halfword in the first operand, in the high halfword of the return value.
  1924. \remark
  1925. res[15:0] = (val1[15:0] - val2[31:16]) >> 1 \n
  1926. res[31:16] = (val1[31:16] + val2[15:0]) >> 1
  1927. */
  1928. __ALWAYS_STATIC_INLINE uint32_t __SHASX(uint32_t x, uint32_t y)
  1929. {
  1930. int32_t r, s;
  1931. r = (((((int32_t)x << 16) >> 16) - (((int32_t)y) >> 16)) >> 1) & (int32_t)0x0000FFFF;
  1932. s = (((((int32_t)x) >> 16) + (((int32_t)y << 16) >> 16)) >> 1) & (int32_t)0x0000FFFF;
  1933. return ((uint32_t)((s << 16) | (r)));
  1934. }
  1935. /**
  1936. \brief Dual 16-bit unsigned addition and subtraction with halved results and exchange.
  1937. \details This function enables you to exchange the halfwords of the second operand,
  1938. add the high halfwords and subtract the low halfwords, halving the results.
  1939. \param [in] x first operand for the subtraction in the low halfword, and
  1940. the first operand for the addition in the high halfword.
  1941. \param [in] y second operand for the subtraction in the high halfword, and
  1942. the second operand for the addition in the low halfword.
  1943. \return the halved subtraction of the high halfword in the second operand from the
  1944. low halfword in the first operand, in the low halfword of the return value.\n
  1945. the halved addition of the low halfword in the second operand from the high
  1946. halfword in the first operand, in the high halfword of the return value.
  1947. \remark
  1948. res[15:0] = (val1[15:0] - val2[31:16]) >> 1 \n
  1949. res[31:16] = (val1[31:16] + val2[15:0]) >> 1
  1950. */
  1951. __ALWAYS_STATIC_INLINE uint32_t __UHASX(uint32_t x, uint32_t y)
  1952. {
  1953. int32_t r, s;
  1954. r = ((((x << 16) >> 16) - ((y) >> 16)) >> 1) & 0x0000FFFF;
  1955. s = ((((x) >> 16) + ((y << 16) >> 16)) >> 1) & 0x0000FFFF;
  1956. return ((s << 16) | (r));
  1957. }
  1958. /**
  1959. \brief Dual 16-bit subtract and add with exchange.
  1960. \details This function enables you to exchange the halfwords of one operand,
  1961. then subtract the high halfwords and add the low halfwords,
  1962. saturating the results to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1963. \param [in] x first operand for the addition in the low halfword,
  1964. and the first operand for the subtraction in the high halfword.
  1965. \param [in] y second operand for the addition in the high halfword,
  1966. and the second operand for the subtraction in the low halfword.
  1967. \return the saturated addition of the low halfword of the first operand and the high
  1968. halfword of the second operand, in the low halfword of the return value.\n
  1969. the saturated subtraction of the low halfword of the second operand from the
  1970. high halfword of the first operand, in the high halfword of the return value.\n
  1971. The returned results are saturated to the 16-bit signed integer range -2^15 <= x <= 2^15 - 1.
  1972. \remark
  1973. res[15:0] = val1[15:0] + val2[31:16] \n
  1974. res[31:16] = val1[31:16] - val2[15:0]
  1975. */
  1976. __ALWAYS_STATIC_INLINE uint32_t __QSAX(uint32_t x, uint32_t y)
  1977. {
  1978. int32_t r, s;
  1979. r = __SSAT(((((int32_t)x << 16) >> 16) + (((int32_t)y) >> 16)), 16) & (int32_t)0x0000FFFF;
  1980. s = __SSAT(((((int32_t)x) >> 16) - (((int32_t)y << 16) >> 16)), 16) & (int32_t)0x0000FFFF;
  1981. return ((uint32_t)((s << 16) | (r)));
  1982. }
  1983. /**
  1984. \brief Dual 16-bit unsigned saturating subtraction and addition with exchange.
  1985. \details This function enables you to exchange the halfwords of the second operand and perform
  1986. one unsigned 16-bit integer subtraction and one unsigned 16-bit addition, saturating
  1987. the results to the 16-bit unsigned integer range 0 <= x <= 2^16 - 1.
  1988. \param [in] x first operand for the addition in the low halfword,
  1989. and the first operand for the subtraction in the high halfword.
  1990. \param [in] y second operand for the addition in the high halfword,
  1991. and the second operand for the subtraction in the low halfword.
  1992. \return the saturated addition of the low halfword of the first operand and the high
  1993. halfword of the second operand, in the low halfword of the return value.\n
  1994. the saturated subtraction of the low halfword of the second operand from the
  1995. high halfword of the first operand, in the high halfword of the return value.\n
  1996. The returned results are saturated to the 16-bit unsigned integer range 0 <= x <= 2^16 - 1.
  1997. \remark
  1998. res[15:0] = val1[15:0] + val2[31:16] \n
  1999. res[31:16] = val1[31:16] - val2[15:0]
  2000. */
  2001. __ALWAYS_STATIC_INLINE uint32_t __UQSAX(uint32_t x, uint32_t y)
  2002. {
  2003. int32_t r, s;
  2004. r = __IUSAT((((x << 16) >> 16) + ((y) >> 16)), 16) & 0x0000FFFF;
  2005. s = __IUSAT((((x) >> 16) - ((y << 16) >> 16)), 16) & 0x0000FFFF;
  2006. return ((s << 16) | (r));
  2007. }
  2008. /**
  2009. \brief Dual 16-bit unsigned subtract and add with exchange.
  2010. \details This function enables you to exchange the halfwords of the second operand,
  2011. subtract the high halfwords and add the low halfwords.
  2012. \param [in] x first operand for the addition in the low halfword,
  2013. and the first operand for the subtraction in the high halfword.
  2014. \param [in] y second operand for the addition in the high halfword,
  2015. and the second operand for the subtraction in the low halfword.
  2016. \return the addition of the low halfword of the first operand and the high
  2017. halfword of the second operand, in the low halfword of the return value.\n
  2018. the subtraction of the low halfword of the second operand from the
  2019. high halfword of the first operand, in the high halfword of the return value.\n
  2020. \remark
  2021. res[15:0] = val1[15:0] + val2[31:16] \n
  2022. res[31:16] = val1[31:16] - val2[15:0]
  2023. */
  2024. __ALWAYS_STATIC_INLINE uint32_t __USAX(uint32_t x, uint32_t y)
  2025. {
  2026. int32_t r, s;
  2027. r = (((x << 16) >> 16) + ((y) >> 16)) & 0x0000FFFF;
  2028. s = (((x) >> 16) - ((y << 16) >> 16)) & 0x0000FFFF;
  2029. return ((s << 16) | (r));
  2030. }
  2031. /**
  2032. \brief Dual 16-bit signed subtraction and addition with exchange.
  2033. \details This function enables you to exchange the two halfwords of one operand and perform one
  2034. 16-bit integer subtraction and one 16-bit addition.
  2035. \param [in] x first operand for the addition in the low halfword, and the first operand
  2036. for the subtraction in the high halfword.
  2037. \param [in] y second operand for the addition in the high halfword, and the second
  2038. operand for the subtraction in the low halfword.
  2039. \return the addition of the low halfword of the first operand and the high
  2040. halfword of the second operand, in the low halfword of the return value.\n
  2041. the subtraction of the low halfword of the second operand from the
  2042. high halfword of the first operand, in the high halfword of the return value.\n
  2043. \remark
  2044. res[15:0] = val1[15:0] + val2[31:16] \n
  2045. res[31:16] = val1[31:16] - val2[15:0]
  2046. */
  2047. __ALWAYS_STATIC_INLINE uint32_t __SSAX(uint32_t x, uint32_t y)
  2048. {
  2049. int32_t r, s;
  2050. r = ((((int32_t)x << 16) >> 16) + (((int32_t)y) >> 16)) & (int32_t)0x0000FFFF;
  2051. s = ((((int32_t)x) >> 16) - (((int32_t)y << 16) >> 16)) & (int32_t)0x0000FFFF;
  2052. return ((uint32_t)((s << 16) | (r)));
  2053. }
  2054. /**
  2055. \brief Dual 16-bit signed subtraction and addition with halved results.
  2056. \details This function enables you to exchange the two halfwords of one operand, perform one signed
  2057. 16-bit integer subtraction and one signed 16-bit addition, and halve the results.
  2058. \param [in] x first 16-bit operands.
  2059. \param [in] y second 16-bit operands.
  2060. \return the halved addition of the low halfword in the first operand and the
  2061. high halfword in the second operand, in the low halfword of the return value.\n
  2062. the halved subtraction of the low halfword in the second operand from the
  2063. high halfword in the first operand, in the high halfword of the return value.
  2064. \remark
  2065. res[15:0] = (val1[15:0] + val2[31:16]) >> 1 \n
  2066. res[31:16] = (val1[31:16] - val2[15:0]) >> 1
  2067. */
  2068. __ALWAYS_STATIC_INLINE uint32_t __SHSAX(uint32_t x, uint32_t y)
  2069. {
  2070. int32_t r, s;
  2071. r = (((((int32_t)x << 16) >> 16) + (((int32_t)y) >> 16)) >> 1) & (int32_t)0x0000FFFF;
  2072. s = (((((int32_t)x) >> 16) - (((int32_t)y << 16) >> 16)) >> 1) & (int32_t)0x0000FFFF;
  2073. return ((uint32_t)((s << 16) | (r)));
  2074. }
  2075. /**
  2076. \brief Dual 16-bit unsigned subtraction and addition with halved results and exchange.
  2077. \details This function enables you to exchange the halfwords of the second operand,
  2078. subtract the high halfwords and add the low halfwords, halving the results.
  2079. \param [in] x first operand for the addition in the low halfword, and
  2080. the first operand for the subtraction in the high halfword.
  2081. \param [in] y second operand for the addition in the high halfword, and
  2082. the second operand for the subtraction in the low halfword.
  2083. \return the halved addition of the low halfword in the first operand and the
  2084. high halfword in the second operand, in the low halfword of the return value.\n
  2085. the halved subtraction of the low halfword in the second operand from the
  2086. high halfword in the first operand, in the high halfword of the return value.
  2087. \remark
  2088. res[15:0] = (val1[15:0] + val2[31:16]) >> 1 \n
  2089. res[31:16] = (val1[31:16] - val2[15:0]) >> 1
  2090. */
  2091. __ALWAYS_STATIC_INLINE uint32_t __UHSAX(uint32_t x, uint32_t y)
  2092. {
  2093. int32_t r, s;
  2094. r = ((((x << 16) >> 16) + ((y) >> 16)) >> 1) & 0x0000FFFF;
  2095. s = ((((x) >> 16) - ((y << 16) >> 16)) >> 1) & 0x0000FFFF;
  2096. return ((s << 16) | (r));
  2097. }
  2098. /**
  2099. \brief Dual 16-bit signed multiply with exchange returning difference.
  2100. \details This function enables you to perform two 16-bit signed multiplications, subtracting
  2101. one of the products from the other. The halfwords of the second operand are exchanged
  2102. before performing the arithmetic. This produces top * bottom and bottom * top multiplication.
  2103. \param [in] x first 16-bit operands for each multiplication.
  2104. \param [in] y second 16-bit operands for each multiplication.
  2105. \return the difference of the products of the two 16-bit signed multiplications.
  2106. \remark
  2107. p1 = val1[15:0] * val2[31:16] \n
  2108. p2 = val1[31:16] * val2[15:0] \n
  2109. res[31:0] = p1 - p2
  2110. */
  2111. __ALWAYS_STATIC_INLINE uint32_t __SMUSDX(uint32_t x, uint32_t y)
  2112. {
  2113. return ((uint32_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y) >> 16)) -
  2114. ((((int32_t)x) >> 16) * (((int32_t)y << 16) >> 16))));
  2115. }
  2116. /**
  2117. \brief Sum of dual 16-bit signed multiply with exchange.
  2118. \details This function enables you to perform two 16-bit signed multiplications with exchanged
  2119. halfwords of the second operand, adding the products together.
  2120. \param [in] x first 16-bit operands for each multiplication.
  2121. \param [in] y second 16-bit operands for each multiplication.
  2122. \return the sum of the products of the two 16-bit signed multiplications with exchanged halfwords of the second operand.
  2123. \remark
  2124. p1 = val1[15:0] * val2[31:16] \n
  2125. p2 = val1[31:16] * val2[15:0] \n
  2126. res[31:0] = p1 + p2
  2127. */
  2128. __ALWAYS_STATIC_INLINE uint32_t __SMUADX(uint32_t x, uint32_t y)
  2129. {
  2130. return ((uint32_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y) >> 16)) +
  2131. ((((int32_t)x) >> 16) * (((int32_t)y << 16) >> 16))));
  2132. }
  2133. /**
  2134. \brief Saturating add.
  2135. \details This function enables you to obtain the saturating add of two integers.
  2136. \param [in] x first summand of the saturating add operation.
  2137. \param [in] y second summand of the saturating add operation.
  2138. \return the saturating addition of val1 and val2.
  2139. \remark
  2140. res[31:0] = SAT(val1 + SAT(val2))
  2141. */
  2142. __ALWAYS_STATIC_INLINE int32_t __QADD(int32_t x, int32_t y)
  2143. {
  2144. int32_t result;
  2145. if (y >= 0) {
  2146. if ((int32_t)((uint32_t)x + (uint32_t)y) >= x) {
  2147. result = x + y;
  2148. } else {
  2149. result = 0x7FFFFFFF;
  2150. }
  2151. } else {
  2152. if ((int32_t)((uint32_t)x + (uint32_t)y) < x) {
  2153. result = x + y;
  2154. } else {
  2155. result = 0x80000000;
  2156. }
  2157. }
  2158. return result;
  2159. }
  2160. /**
  2161. \brief Saturating subtract.
  2162. \details This function enables you to obtain the saturating add of two integers.
  2163. \param [in] x first summand of the saturating add operation.
  2164. \param [in] y second summand of the saturating add operation.
  2165. \return the saturating addition of val1 and val2.
  2166. \remark
  2167. res[31:0] = SAT(val1 - SAT(val2))
  2168. */
  2169. __ALWAYS_STATIC_INLINE int32_t __QSUB(int32_t x, int32_t y)
  2170. {
  2171. int64_t tmp;
  2172. int32_t result;
  2173. tmp = (int64_t)x - (int64_t)y;
  2174. if (tmp > 0x7fffffff) {
  2175. tmp = 0x7fffffff;
  2176. } else if (tmp < (-2147483647 - 1)) {
  2177. tmp = -2147483647 - 1;
  2178. }
  2179. result = tmp;
  2180. return result;
  2181. }
  2182. /**
  2183. \brief Dual 16-bit signed multiply with single 32-bit accumulator.
  2184. \details This function enables you to perform two signed 16-bit multiplications,
  2185. adding both results to a 32-bit accumulate operand.
  2186. \param [in] x first 16-bit operands for each multiplication.
  2187. \param [in] y second 16-bit operands for each multiplication.
  2188. \param [in] sum accumulate value.
  2189. \return the product of each multiplication added to the accumulate value, as a 32-bit integer.
  2190. \remark
  2191. p1 = val1[15:0] * val2[15:0] \n
  2192. p2 = val1[31:16] * val2[31:16] \n
  2193. res[31:0] = p1 + p2 + val3[31:0]
  2194. */
  2195. __ALWAYS_STATIC_INLINE uint32_t __SMLAD(uint32_t x, uint32_t y, uint32_t sum)
  2196. {
  2197. return ((uint32_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y << 16) >> 16)) +
  2198. ((((int32_t)x) >> 16) * (((int32_t)y) >> 16)) +
  2199. (((int32_t)sum))));
  2200. }
  2201. /**
  2202. \brief Pre-exchanged dual 16-bit signed multiply with single 32-bit accumulator.
  2203. \details This function enables you to perform two signed 16-bit multiplications with exchanged
  2204. halfwords of the second operand, adding both results to a 32-bit accumulate operand.
  2205. \param [in] x first 16-bit operands for each multiplication.
  2206. \param [in] y second 16-bit operands for each multiplication.
  2207. \param [in] sum accumulate value.
  2208. \return the product of each multiplication with exchanged halfwords of the second
  2209. operand added to the accumulate value, as a 32-bit integer.
  2210. \remark
  2211. p1 = val1[15:0] * val2[31:16] \n
  2212. p2 = val1[31:16] * val2[15:0] \n
  2213. res[31:0] = p1 + p2 + val3[31:0]
  2214. */
  2215. __ALWAYS_STATIC_INLINE uint32_t __SMLADX(uint32_t x, uint32_t y, uint32_t sum)
  2216. {
  2217. return ((uint32_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y) >> 16)) +
  2218. ((((int32_t)x) >> 16) * (((int32_t)y << 16) >> 16)) +
  2219. (((int32_t)sum))));
  2220. }
  2221. /**
  2222. \brief Dual 16-bit signed multiply with exchange subtract with 32-bit accumulate.
  2223. \details This function enables you to perform two 16-bit signed multiplications, take the
  2224. difference of the products, subtracting the high halfword product from the low
  2225. halfword product, and add the difference to a 32-bit accumulate operand.
  2226. \param [in] x first 16-bit operands for each multiplication.
  2227. \param [in] y second 16-bit operands for each multiplication.
  2228. \param [in] sum accumulate value.
  2229. \return the difference of the product of each multiplication, added to the accumulate value.
  2230. \remark
  2231. p1 = val1[15:0] * val2[15:0] \n
  2232. p2 = val1[31:16] * val2[31:16] \n
  2233. res[31:0] = p1 - p2 + val3[31:0]
  2234. */
  2235. __ALWAYS_STATIC_INLINE uint32_t __SMLSD(uint32_t x, uint32_t y, uint32_t sum)
  2236. {
  2237. return ((uint32_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y << 16) >> 16)) -
  2238. ((((int32_t)x) >> 16) * (((int32_t)y) >> 16)) +
  2239. (((int32_t)sum))));
  2240. }
  2241. /**
  2242. \brief Dual 16-bit signed multiply with exchange subtract with 32-bit accumulate.
  2243. \details This function enables you to exchange the halfwords in the second operand, then perform two 16-bit
  2244. signed multiplications. The difference of the products is added to a 32-bit accumulate operand.
  2245. \param [in] x first 16-bit operands for each multiplication.
  2246. \param [in] y second 16-bit operands for each multiplication.
  2247. \param [in] sum accumulate value.
  2248. \return the difference of the product of each multiplication, added to the accumulate value.
  2249. \remark
  2250. p1 = val1[15:0] * val2[31:16] \n
  2251. p2 = val1[31:16] * val2[15:0] \n
  2252. res[31:0] = p1 - p2 + val3[31:0]
  2253. */
  2254. __ALWAYS_STATIC_INLINE uint32_t __SMLSDX(uint32_t x, uint32_t y, uint32_t sum)
  2255. {
  2256. return ((uint32_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y) >> 16)) -
  2257. ((((int32_t)x) >> 16) * (((int32_t)y << 16) >> 16)) +
  2258. (((int32_t)sum))));
  2259. }
  2260. /**
  2261. \brief Dual 16-bit signed multiply with single 64-bit accumulator.
  2262. \details This function enables you to perform two signed 16-bit multiplications, adding both results
  2263. to a 64-bit accumulate operand. Overflow is only possible as a result of the 64-bit addition.
  2264. This overflow is not detected if it occurs. Instead, the result wraps around modulo2^64.
  2265. \param [in] x first 16-bit operands for each multiplication.
  2266. \param [in] y second 16-bit operands for each multiplication.
  2267. \param [in] sum accumulate value.
  2268. \return the product of each multiplication added to the accumulate value.
  2269. \remark
  2270. p1 = val1[15:0] * val2[15:0] \n
  2271. p2 = val1[31:16] * val2[31:16] \n
  2272. sum = p1 + p2 + val3[63:32][31:0] \n
  2273. res[63:32] = sum[63:32] \n
  2274. res[31:0] = sum[31:0]
  2275. */
  2276. __ALWAYS_STATIC_INLINE uint64_t __SMLALD(uint32_t x, uint32_t y, uint64_t sum)
  2277. {
  2278. return ((uint64_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y << 16) >> 16)) +
  2279. ((((int32_t)x) >> 16) * (((int32_t)y) >> 16)) +
  2280. (((uint64_t)sum))));
  2281. }
  2282. /**
  2283. \brief Dual 16-bit signed multiply with exchange with single 64-bit accumulator.
  2284. \details This function enables you to exchange the halfwords of the second operand, and perform two
  2285. signed 16-bit multiplications, adding both results to a 64-bit accumulate operand. Overflow
  2286. is only possible as a result of the 64-bit addition. This overflow is not detected if it occurs.
  2287. Instead, the result wraps around modulo2^64.
  2288. \param [in] x first 16-bit operands for each multiplication.
  2289. \param [in] y second 16-bit operands for each multiplication.
  2290. \param [in] sum accumulate value.
  2291. \return the product of each multiplication added to the accumulate value.
  2292. \remark
  2293. p1 = val1[15:0] * val2[31:16] \n
  2294. p2 = val1[31:16] * val2[15:0] \n
  2295. sum = p1 + p2 + val3[63:32][31:0] \n
  2296. res[63:32] = sum[63:32] \n
  2297. res[31:0] = sum[31:0]
  2298. */
  2299. __ALWAYS_STATIC_INLINE uint64_t __SMLALDX(uint32_t x, uint32_t y, uint64_t sum)
  2300. {
  2301. return ((uint64_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y) >> 16)) +
  2302. ((((int32_t)x) >> 16) * (((int32_t)y << 16) >> 16)) +
  2303. (((uint64_t)sum))));
  2304. }
  2305. /**
  2306. \brief dual 16-bit signed multiply subtract with 64-bit accumulate.
  2307. \details This function It enables you to perform two 16-bit signed multiplications, take the difference
  2308. of the products, subtracting the high halfword product from the low halfword product, and add the
  2309. difference to a 64-bit accumulate operand. Overflow cannot occur during the multiplications or the
  2310. subtraction. Overflow can occur as a result of the 64-bit addition, and this overflow is not
  2311. detected. Instead, the result wraps round to modulo2^64.
  2312. \param [in] x first 16-bit operands for each multiplication.
  2313. \param [in] y second 16-bit operands for each multiplication.
  2314. \param [in] sum accumulate value.
  2315. \return the difference of the product of each multiplication, added to the accumulate value.
  2316. \remark
  2317. p1 = val1[15:0] * val2[15:0] \n
  2318. p2 = val1[31:16] * val2[31:16] \n
  2319. res[63:32][31:0] = p1 - p2 + val3[63:32][31:0]
  2320. */
  2321. __ALWAYS_STATIC_INLINE uint64_t __SMLSLD(uint32_t x, uint32_t y, uint64_t sum)
  2322. {
  2323. return ((uint64_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y << 16) >> 16)) -
  2324. ((((int32_t)x) >> 16) * (((int32_t)y) >> 16)) +
  2325. (((uint64_t)sum))));
  2326. }
  2327. /**
  2328. \brief Dual 16-bit signed multiply with exchange subtract with 64-bit accumulate.
  2329. \details This function enables you to exchange the halfwords of the second operand, perform two 16-bit multiplications,
  2330. adding the difference of the products to a 64-bit accumulate operand. Overflow cannot occur during the
  2331. multiplications or the subtraction. Overflow can occur as a result of the 64-bit addition, and this overflow
  2332. is not detected. Instead, the result wraps round to modulo2^64.
  2333. \param [in] x first 16-bit operands for each multiplication.
  2334. \param [in] y second 16-bit operands for each multiplication.
  2335. \param [in] sum accumulate value.
  2336. \return the difference of the product of each multiplication, added to the accumulate value.
  2337. \remark
  2338. p1 = val1[15:0] * val2[31:16] \n
  2339. p2 = val1[31:16] * val2[15:0] \n
  2340. res[63:32][31:0] = p1 - p2 + val3[63:32][31:0]
  2341. */
  2342. __ALWAYS_STATIC_INLINE uint64_t __SMLSLDX(uint32_t x, uint32_t y, uint64_t sum)
  2343. {
  2344. return ((uint64_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y) >> 16)) -
  2345. ((((int32_t)x) >> 16) * (((int32_t)y << 16) >> 16)) +
  2346. (((uint64_t)sum))));
  2347. }
  2348. /**
  2349. \brief 32-bit signed multiply with 32-bit truncated accumulator.
  2350. \details This function enables you to perform a signed 32-bit multiplications, adding the most
  2351. significant 32 bits of the 64-bit result to a 32-bit accumulate operand.
  2352. \param [in] x first operand for multiplication.
  2353. \param [in] y second operand for multiplication.
  2354. \param [in] sum accumulate value.
  2355. \return the product of multiplication (most significant 32 bits) is added to the accumulate value, as a 32-bit integer.
  2356. \remark
  2357. p = val1 * val2 \n
  2358. res[31:0] = p[63:32] + val3[31:0]
  2359. */
  2360. __ALWAYS_STATIC_INLINE uint32_t __SMMLA(int32_t x, int32_t y, int32_t sum)
  2361. {
  2362. return (uint32_t)((int32_t)((int64_t)((int64_t)x * (int64_t)y) >> 32) + sum);
  2363. }
  2364. /**
  2365. \brief Sum of dual 16-bit signed multiply.
  2366. \details This function enables you to perform two 16-bit signed multiplications, adding the products together.
  2367. \param [in] x first 16-bit operands for each multiplication.
  2368. \param [in] y second 16-bit operands for each multiplication.
  2369. \return the sum of the products of the two 16-bit signed multiplications.
  2370. \remark
  2371. p1 = val1[15:0] * val2[15:0] \n
  2372. p2 = val1[31:16] * val2[31:16] \n
  2373. res[31:0] = p1 + p2
  2374. */
  2375. __ALWAYS_STATIC_INLINE uint32_t __SMUAD(uint32_t x, uint32_t y)
  2376. {
  2377. return ((uint32_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y << 16) >> 16)) +
  2378. ((((int32_t)x) >> 16) * (((int32_t)y) >> 16))));
  2379. }
  2380. /**
  2381. \brief Dual 16-bit signed multiply returning difference.
  2382. \details This function enables you to perform two 16-bit signed multiplications, taking the difference
  2383. of the products by subtracting the high halfword product from the low halfword product.
  2384. \param [in] x first 16-bit operands for each multiplication.
  2385. \param [in] y second 16-bit operands for each multiplication.
  2386. \return the difference of the products of the two 16-bit signed multiplications.
  2387. \remark
  2388. p1 = val1[15:0] * val2[15:0] \n
  2389. p2 = val1[31:16] * val2[31:16] \n
  2390. res[31:0] = p1 - p2
  2391. */
  2392. __ALWAYS_STATIC_INLINE uint32_t __SMUSD(uint32_t x, uint32_t y)
  2393. {
  2394. return ((uint32_t)(((((int32_t)x << 16) >> 16) * (((int32_t)y << 16) >> 16)) -
  2395. ((((int32_t)x) >> 16) * (((int32_t)y) >> 16))));
  2396. }
  2397. /**
  2398. \brief Dual extracted 8-bit to 16-bit signed addition.
  2399. \details This function enables you to extract two 8-bit values from the second operand (at bit positions
  2400. [7:0] and [23:16]), sign-extend them to 16-bits each, and add the results to the first operand.
  2401. \param [in] x values added to the sign-extended to 16-bit values.
  2402. \param [in] y two 8-bit values to be extracted and sign-extended.
  2403. \return the addition of val1 and val2, where the 8-bit values in val2[7:0] and
  2404. val2[23:16] have been extracted and sign-extended prior to the addition.
  2405. \remark
  2406. res[15:0] = val1[15:0] + SignExtended(val2[7:0]) \n
  2407. res[31:16] = val1[31:16] + SignExtended(val2[23:16])
  2408. */
  2409. __ALWAYS_STATIC_INLINE uint32_t __SXTAB16(uint32_t x, uint32_t y)
  2410. {
  2411. return ((uint32_t)((((((int32_t)y << 24) >> 24) + (((int32_t)x << 16) >> 16)) & (int32_t)0x0000FFFF) |
  2412. (((((int32_t)y << 8) >> 8) + (((int32_t)x >> 16) << 16)) & (int32_t)0xFFFF0000)));
  2413. }
  2414. /**
  2415. \brief Extracted 16-bit to 32-bit unsigned addition.
  2416. \details This function enables you to extract two 8-bit values from one operand, zero-extend
  2417. them to 16 bits each, and add the results to two 16-bit values from another operand.
  2418. \param [in] x values added to the zero-extended to 16-bit values.
  2419. \param [in] y two 8-bit values to be extracted and zero-extended.
  2420. \return the addition of val1 and val2, where the 8-bit values in val2[7:0] and
  2421. val2[23:16] have been extracted and zero-extended prior to the addition.
  2422. \remark
  2423. res[15:0] = ZeroExt(val2[7:0] to 16 bits) + val1[15:0] \n
  2424. res[31:16] = ZeroExt(val2[31:16] to 16 bits) + val1[31:16]
  2425. */
  2426. __ALWAYS_STATIC_INLINE uint32_t __UXTAB16(uint32_t x, uint32_t y)
  2427. {
  2428. return ((uint32_t)(((((y << 24) >> 24) + ((x << 16) >> 16)) & 0x0000FFFF) |
  2429. ((((y << 8) >> 8) + ((x >> 16) << 16)) & 0xFFFF0000)));
  2430. }
  2431. /**
  2432. \brief Dual extract 8-bits and sign extend each to 16-bits.
  2433. \details This function enables you to extract two 8-bit values from an operand and sign-extend them to 16 bits each.
  2434. \param [in] x two 8-bit values in val[7:0] and val[23:16] to be sign-extended.
  2435. \return the 8-bit values sign-extended to 16-bit values.\n
  2436. sign-extended value of val[7:0] in the low halfword of the return value.\n
  2437. sign-extended value of val[23:16] in the high halfword of the return value.
  2438. \remark
  2439. res[15:0] = SignExtended(val[7:0]) \n
  2440. res[31:16] = SignExtended(val[23:16])
  2441. */
  2442. __ALWAYS_STATIC_INLINE uint32_t __SXTB16(uint32_t x)
  2443. {
  2444. return ((uint32_t)(((((int32_t)x << 24) >> 24) & (int32_t)0x0000FFFF) |
  2445. ((((int32_t)x << 8) >> 8) & (int32_t)0xFFFF0000)));
  2446. }
  2447. /**
  2448. \brief Dual extract 8-bits and zero-extend to 16-bits.
  2449. \details This function enables you to extract two 8-bit values from an operand and zero-extend them to 16 bits each.
  2450. \param [in] x two 8-bit values in val[7:0] and val[23:16] to be zero-extended.
  2451. \return the 8-bit values sign-extended to 16-bit values.\n
  2452. sign-extended value of val[7:0] in the low halfword of the return value.\n
  2453. sign-extended value of val[23:16] in the high halfword of the return value.
  2454. \remark
  2455. res[15:0] = SignExtended(val[7:0]) \n
  2456. res[31:16] = SignExtended(val[23:16])
  2457. */
  2458. __ALWAYS_STATIC_INLINE uint32_t __UXTB16(uint32_t x)
  2459. {
  2460. return ((uint32_t)((((x << 24) >> 24) & 0x0000FFFF) |
  2461. (((x << 8) >> 8) & 0xFFFF0000)));
  2462. }
  2463. #endif /* _CSI_RV32_GCC_H_ */