csi_rv32_gcc.h 105 KB

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