csi_rv64_gcc.h 113 KB

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