csi_gcc.h 105 KB

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