cpu_core.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387
  1. /*
  2. *********************************************************************************************************
  3. * uC/CPU
  4. * CPU CONFIGURATION & PORT LAYER
  5. *
  6. * (c) Copyright 2004-2015; Micrium, Inc.; Weston, FL
  7. *
  8. * All rights reserved. Protected by international copyright laws.
  9. *
  10. * uC/CPU is provided in source form to registered licensees ONLY. It is
  11. * illegal to distribute this source code to any third party unless you receive
  12. * written permission by an authorized Micrium representative. Knowledge of
  13. * the source code may NOT be used to develop a similar product.
  14. *
  15. * Please help us continue to provide the Embedded community with the finest
  16. * software available. Your honesty is greatly appreciated.
  17. *
  18. * You can find our product's user manual, API reference, release notes and
  19. * more information at https://doc.micrium.com.
  20. * You can contact us at www.micrium.com.
  21. *********************************************************************************************************
  22. */
  23. /*
  24. *********************************************************************************************************
  25. *
  26. * CORE CPU MODULE
  27. *
  28. * Filename : cpu_core.c
  29. * Version : V1.30.02
  30. * Programmer(s) : SR
  31. * ITJ
  32. *********************************************************************************************************
  33. */
  34. /*
  35. *********************************************************************************************************
  36. * INCLUDE FILES
  37. *********************************************************************************************************
  38. */
  39. #define MICRIUM_SOURCE
  40. #define CPU_CORE_MODULE
  41. #include "cpu_core.h"
  42. #if defined(CPU_CFG_CACHE_MGMT_EN)
  43. #if (CPU_CFG_CACHE_MGMT_EN == DEF_ENABLED)
  44. #include "cpu_cache.h"
  45. #endif
  46. #endif
  47. /*
  48. *********************************************************************************************************
  49. * LOCAL DEFINES
  50. *********************************************************************************************************
  51. */
  52. /* Pop cnt algorithm csts. */
  53. #define CRC_UTIL_POPCNT_MASK01010101_32 0x55555555u
  54. #define CRC_UTIL_POPCNT_MASK00110011_32 0x33333333u
  55. #define CRC_UTIL_POPCNT_MASK00001111_32 0x0F0F0F0Fu
  56. #define CRC_UTIL_POPCNT_POWERSOF256_32 0x01010101u
  57. /*
  58. *********************************************************************************************************
  59. * LOCAL CONSTANTS
  60. *********************************************************************************************************
  61. */
  62. /*
  63. *********************************************************************************************************
  64. * LOCAL DATA TYPES
  65. *********************************************************************************************************
  66. */
  67. /*
  68. *********************************************************************************************************
  69. * LOCAL TABLES
  70. *********************************************************************************************************
  71. */
  72. /*
  73. *********************************************************************************************************
  74. * CPU COUNT LEAD ZEROs LOOKUP TABLE
  75. *
  76. * Note(s) : (1) Index into bit pattern table determines the number of leading zeros in an 8-bit value :
  77. *
  78. * b07 b06 b05 b04 b03 b02 b01 b00 # Leading Zeros
  79. * --- --- --- --- --- --- --- --- ---------------
  80. * 1 x x x x x x x 0
  81. * 0 1 x x x x x x 1
  82. * 0 0 1 x x x x x 2
  83. * 0 0 0 1 x x x x 3
  84. * 0 0 0 0 1 x x x 4
  85. * 0 0 0 0 0 1 x x 5
  86. * 0 0 0 0 0 0 1 x 6
  87. * 0 0 0 0 0 0 0 1 7
  88. * 0 0 0 0 0 0 0 0 8
  89. *********************************************************************************************************
  90. */
  91. #if (!(defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) || \
  92. (CPU_CFG_DATA_SIZE_MAX > CPU_CFG_DATA_SIZE))
  93. static const CPU_INT08U CPU_CntLeadZerosTbl[256] = { /* Data vals : */
  94. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  95. 8u, 7u, 6u, 6u, 5u, 5u, 5u, 5u, 4u, 4u, 4u, 4u, 4u, 4u, 4u, 4u, /* 0x00 to 0x0F */
  96. 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u, /* 0x10 to 0x1F */
  97. 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, /* 0x20 to 0x2F */
  98. 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, /* 0x30 to 0x3F */
  99. 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, /* 0x40 to 0x4F */
  100. 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, /* 0x50 to 0x5F */
  101. 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, /* 0x60 to 0x6F */
  102. 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, /* 0x70 to 0x7F */
  103. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0x80 to 0x8F */
  104. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0x90 to 0x9F */
  105. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xA0 to 0xAF */
  106. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xB0 to 0xBF */
  107. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xC0 to 0xCF */
  108. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xD0 to 0xDF */
  109. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, /* 0xE0 to 0xEF */
  110. 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u /* 0xF0 to 0xFF */
  111. };
  112. #endif
  113. /*
  114. *********************************************************************************************************
  115. * LOCAL GLOBAL VARIABLES
  116. *********************************************************************************************************
  117. */
  118. CPU_INT32U const CPU_EndiannessTest = 0x12345678LU; /* Variable to test CPU endianness. */
  119. /*
  120. *********************************************************************************************************
  121. * LOCAL FUNCTION PROTOTYPES
  122. *********************************************************************************************************
  123. */
  124. #if (CPU_CFG_NAME_EN == DEF_ENABLED) /* ---------------- CPU NAME FNCTS ---------------- */
  125. static void CPU_NameInit (void);
  126. #endif
  127. /* ----------------- CPU TS FNCTS ----------------- */
  128. #if ((CPU_CFG_TS_EN == DEF_ENABLED) || \
  129. (CPU_CFG_TS_TMR_EN == DEF_ENABLED))
  130. static void CPU_TS_Init (void);
  131. #endif
  132. #ifdef CPU_CFG_INT_DIS_MEAS_EN /* ---------- CPU INT DIS TIME MEAS FNCTS --------- */
  133. static void CPU_IntDisMeasInit (void);
  134. static CPU_TS_TMR CPU_IntDisMeasMaxCalc(CPU_TS_TMR time_tot_cnts);
  135. #endif
  136. /*
  137. *********************************************************************************************************
  138. * LOCAL CONFIGURATION ERRORS
  139. *********************************************************************************************************
  140. */
  141. /*
  142. *********************************************************************************************************
  143. * CPU_Init()
  144. *
  145. * Description : (1) Initialize CPU module :
  146. *
  147. * (a) Initialize CPU timestamps
  148. * (b) Initialize CPU interrupts disabled time measurements
  149. * (c) Initialize CPU host name
  150. *
  151. *
  152. * Argument(s) : none.
  153. *
  154. * Return(s) : none.
  155. *
  156. * Caller(s) : Your Product's Application.
  157. *
  158. * This function is a CPU initialization function & MAY be called by application/
  159. * initialization function(s).
  160. *
  161. * Note(s) : (2) CPU_Init() MUST be called ... :
  162. *
  163. * (a) ONLY ONCE from a product's application; ...
  164. * (b) BEFORE product's application calls any core CPU module function(s)
  165. *
  166. * (3) The following initialization functions MUST be sequenced as follows :
  167. *
  168. * (a) CPU_TS_Init() SHOULD precede ALL calls to other CPU timestamp functions
  169. *
  170. * (b) CPU_IntDisMeasInit() SHOULD precede ALL calls to CPU_CRITICAL_ENTER()/CPU_CRITICAL_EXIT()
  171. * & other CPU interrupts disabled time measurement functions
  172. *********************************************************************************************************
  173. */
  174. void CPU_Init (void)
  175. {
  176. /* --------------------- INIT TS ---------------------- */
  177. #if ((CPU_CFG_TS_EN == DEF_ENABLED) || \
  178. (CPU_CFG_TS_TMR_EN == DEF_ENABLED))
  179. CPU_TS_Init(); /* See Note #3a. */
  180. #endif
  181. /* -------------- INIT INT DIS TIME MEAS -------------- */
  182. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  183. CPU_IntDisMeasInit(); /* See Note #3b. */
  184. #endif
  185. /* ------------------ INIT CPU NAME ------------------- */
  186. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  187. CPU_NameInit();
  188. #endif
  189. #if (CPU_CFG_CACHE_MGMT_EN == DEF_ENABLED)
  190. CPU_Cache_Init();
  191. #endif
  192. }
  193. /*
  194. *********************************************************************************************************
  195. * CPU_SW_Exception()
  196. *
  197. * Description : Trap unrecoverable software exception.
  198. *
  199. * Argument(s) : none.
  200. *
  201. * Return(s) : none.
  202. *
  203. * Caller(s) : various.
  204. *
  205. * Note(s) : (1) CPU_SW_Exception() deadlocks the current code execution -- whether multi-tasked/
  206. * -processed/-threaded or single-threaded -- when the current code execution cannot
  207. * gracefully recover or report a fault or exception condition.
  208. *
  209. * See also 'cpu_core.h CPU_SW_EXCEPTION() Note #1'.
  210. *********************************************************************************************************
  211. */
  212. void CPU_SW_Exception (void)
  213. {
  214. while (DEF_ON) {
  215. ;
  216. }
  217. }
  218. /*
  219. *********************************************************************************************************
  220. * CPU_NameClr()
  221. *
  222. * Description : Clear CPU Name.
  223. *
  224. * Argument(s) : none.
  225. *
  226. * Return(s) : none.
  227. *
  228. * Caller(s) : CPU_NameInit(),
  229. * Application.
  230. *
  231. * This function is a CPU module application programming interface (API) function & MAY be
  232. * called by application function(s).
  233. *
  234. * Note(s) : none.
  235. *********************************************************************************************************
  236. */
  237. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  238. void CPU_NameClr (void)
  239. {
  240. CPU_SR_ALLOC();
  241. CPU_CRITICAL_ENTER();
  242. Mem_Clr((void *)&CPU_Name[0],
  243. (CPU_SIZE_T) CPU_CFG_NAME_SIZE);
  244. CPU_CRITICAL_EXIT();
  245. }
  246. #endif
  247. /*
  248. *********************************************************************************************************
  249. * CPU_NameGet()
  250. *
  251. * Description : Get CPU host name.
  252. *
  253. * Argument(s) : p_name Pointer to an ASCII character array that will receive the return CPU host
  254. * name ASCII string from this function (see Note #1).
  255. *
  256. * p_err Pointer to variable that will receive the return error code from this function :
  257. *
  258. * CPU_ERR_NONE CPU host name successfully returned.
  259. * CPU_ERR_NULL_PTR Argument 'p_name' passed a NULL pointer.
  260. *
  261. * Return(s) : none.
  262. *
  263. * Caller(s) : Application.
  264. *
  265. * This function is a CPU module application programming interface (API) function & MAY
  266. * be called by application function(s).
  267. *
  268. * Note(s) : (1) The size of the ASCII character array that will receive the return CPU host name
  269. * ASCII string :
  270. *
  271. * (a) MUST be greater than or equal to the current CPU host name's ASCII string
  272. * size including the terminating NULL character;
  273. * (b) SHOULD be greater than or equal to CPU_CFG_NAME_SIZE
  274. *********************************************************************************************************
  275. */
  276. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  277. void CPU_NameGet (CPU_CHAR *p_name,
  278. CPU_ERR *p_err)
  279. {
  280. CPU_SR_ALLOC();
  281. if (p_err == (CPU_ERR *)0) {
  282. CPU_SW_EXCEPTION(;);
  283. }
  284. if (p_name == (CPU_CHAR *)0) {
  285. *p_err = CPU_ERR_NULL_PTR;
  286. return;
  287. }
  288. CPU_CRITICAL_ENTER();
  289. (void)Str_Copy_N(p_name,
  290. &CPU_Name[0],
  291. CPU_CFG_NAME_SIZE);
  292. CPU_CRITICAL_EXIT();
  293. *p_err = CPU_ERR_NONE;
  294. }
  295. #endif
  296. /*
  297. *********************************************************************************************************
  298. * CPU_NameSet()
  299. *
  300. * Description : Set CPU host name.
  301. *
  302. * Argument(s) : p_name Pointer to CPU host name to set.
  303. *
  304. * p_err Pointer to variable that will receive the return error code from this function :
  305. *
  306. * CPU_ERR_NONE CPU host name successfully set.
  307. * CPU_ERR_NULL_PTR Argument 'p_name' passed a NULL pointer.
  308. * CPU_ERR_NAME_SIZE Invalid CPU host name size (see Note #1).
  309. *
  310. * Return(s) : none.
  311. *
  312. * Caller(s) : Application.
  313. *
  314. * This function is a CPU module application programming interface (API) function & MAY be
  315. * called by application function(s).
  316. *
  317. * Note(s) : (1) 'p_name' ASCII string size, including the terminating NULL character, MUST be less
  318. * than or equal to CPU_CFG_NAME_SIZE.
  319. *********************************************************************************************************
  320. */
  321. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  322. void CPU_NameSet (const CPU_CHAR *p_name,
  323. CPU_ERR *p_err)
  324. {
  325. CPU_SIZE_T len;
  326. CPU_SR_ALLOC();
  327. if (p_err == (CPU_ERR *)0) {
  328. CPU_SW_EXCEPTION(;);
  329. }
  330. if (p_name == (const CPU_CHAR *)0) {
  331. *p_err = CPU_ERR_NULL_PTR;
  332. return;
  333. }
  334. len = Str_Len_N(p_name,
  335. CPU_CFG_NAME_SIZE);
  336. if (len < CPU_CFG_NAME_SIZE) { /* If cfg name len < max name size, ... */
  337. CPU_CRITICAL_ENTER();
  338. (void)Str_Copy_N(&CPU_Name[0], /* ... copy cfg name to CPU host name. */
  339. p_name,
  340. CPU_CFG_NAME_SIZE);
  341. CPU_CRITICAL_EXIT();
  342. *p_err = CPU_ERR_NONE;
  343. } else {
  344. *p_err = CPU_ERR_NAME_SIZE;
  345. }
  346. }
  347. #endif
  348. /*
  349. *********************************************************************************************************
  350. * CPU_TS_Get32()
  351. *
  352. * Description : Get current 32-bit CPU timestamp.
  353. *
  354. * Argument(s) : none.
  355. *
  356. * Return(s) : Current 32-bit CPU timestamp (in timestamp timer counts).
  357. *
  358. * Caller(s) : Application.
  359. *
  360. * This function is a CPU module application programming interface (API) function & MAY
  361. * be called by application function(s).
  362. *
  363. * Note(s) : (1) When applicable, the amount of time measured by CPU timestamps is calculated by
  364. * either of the following equations :
  365. *
  366. * (a) Time measured = Number timer counts * Timer period
  367. *
  368. * where
  369. *
  370. * Number timer counts Number of timer counts measured
  371. * Timer period Timer's period in some units of
  372. * (fractional) seconds
  373. * Time measured Amount of time measured, in same
  374. * units of (fractional) seconds
  375. * as the Timer period
  376. *
  377. * Number timer counts
  378. * (b) Time measured = ---------------------
  379. * Timer frequency
  380. *
  381. * where
  382. *
  383. * Number timer counts Number of timer counts measured
  384. * Timer frequency Timer's frequency in some units
  385. * of counts per second
  386. * Time measured Amount of time measured, in seconds
  387. *
  388. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c1'.
  389. *
  390. * (2) In case the CPU timestamp timer has lower precision than the 32-bit CPU timestamp;
  391. * its precision is extended via periodic updates by accumulating the deltas of the
  392. * timestamp timer count values into the higher-precision 32-bit CPU timestamp.
  393. *
  394. * (3) After initialization, 'CPU_TS_32_Accum' & 'CPU_TS_32_TmrPrev' MUST ALWAYS
  395. * be accessed AND updated exclusively with interrupts disabled -- but NOT
  396. * with critical sections.
  397. *********************************************************************************************************
  398. */
  399. #if (CPU_CFG_TS_32_EN == DEF_ENABLED)
  400. CPU_TS32 CPU_TS_Get32 (void)
  401. {
  402. CPU_TS32 ts;
  403. #if (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32)
  404. CPU_TS_TMR tmr_cur;
  405. CPU_TS_TMR tmr_delta;
  406. CPU_SR_ALLOC();
  407. #endif
  408. #if (CPU_CFG_TS_TMR_SIZE >= CPU_WORD_SIZE_32)
  409. ts = (CPU_TS32)CPU_TS_TmrRd(); /* Get cur ts tmr val (in 32-bit ts cnts). */
  410. #else
  411. CPU_INT_DIS();
  412. tmr_cur = (CPU_TS_TMR) CPU_TS_TmrRd(); /* Get cur ts tmr val (in ts tmr cnts). */
  413. tmr_delta = (CPU_TS_TMR)(tmr_cur - CPU_TS_32_TmrPrev); /* Calc delta ts tmr cnts. */
  414. CPU_TS_32_Accum += (CPU_TS32 ) tmr_delta; /* Inc ts by delta ts tmr cnts (see Note #2). */
  415. CPU_TS_32_TmrPrev = (CPU_TS_TMR) tmr_cur; /* Save cur ts tmr cnts for next update. */
  416. ts = (CPU_TS32 ) CPU_TS_32_Accum;
  417. CPU_INT_EN();
  418. #endif
  419. return (ts);
  420. }
  421. #endif
  422. /*
  423. *********************************************************************************************************
  424. * CPU_TS_Get64()
  425. *
  426. * Description : Get current 64-bit CPU timestamp.
  427. *
  428. * Argument(s) : none.
  429. *
  430. * Return(s) : Current 64-bit CPU timestamp (in timestamp timer counts).
  431. *
  432. * Caller(s) : Application.
  433. *
  434. * This function is a CPU module application programming interface (API) function & MAY
  435. * be called by application function(s).
  436. *
  437. * Note(s) : (1) When applicable, the amount of time measured by CPU timestamps is calculated by
  438. * either of the following equations :
  439. *
  440. * (a) Time measured = Number timer counts * Timer period
  441. *
  442. * where
  443. *
  444. * Number timer counts Number of timer counts measured
  445. * Timer period Timer's period in some units of
  446. * (fractional) seconds
  447. * Time measured Amount of time measured, in same
  448. * units of (fractional) seconds
  449. * as the Timer period
  450. *
  451. * Number timer counts
  452. * (b) Time measured = ---------------------
  453. * Timer frequency
  454. *
  455. * where
  456. *
  457. * Number timer counts Number of timer counts measured
  458. * Timer frequency Timer's frequency in some units
  459. * of counts per second
  460. * Time measured Amount of time measured, in seconds
  461. *
  462. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c1'.
  463. *
  464. * (2) In case the CPU timestamp timer has lower precision than the 64-bit CPU timestamp;
  465. * its precision is extended via periodic updates by accumulating the deltas of the
  466. * timestamp timer count values into the higher-precision 64-bit CPU timestamp.
  467. *
  468. * (3) After initialization, 'CPU_TS_64_Accum' & 'CPU_TS_64_TmrPrev' MUST ALWAYS
  469. * be accessed AND updated exclusively with interrupts disabled -- but NOT
  470. * with critical sections.
  471. *********************************************************************************************************
  472. */
  473. #if (CPU_CFG_TS_64_EN == DEF_ENABLED)
  474. CPU_TS64 CPU_TS_Get64 (void)
  475. {
  476. CPU_TS64 ts;
  477. #if (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64)
  478. CPU_TS_TMR tmr_cur;
  479. CPU_TS_TMR tmr_delta;
  480. CPU_SR_ALLOC();
  481. #endif
  482. #if (CPU_CFG_TS_TMR_SIZE >= CPU_WORD_SIZE_64)
  483. ts = (CPU_TS64)CPU_TS_TmrRd(); /* Get cur ts tmr val (in 64-bit ts cnts). */
  484. #else
  485. CPU_INT_DIS();
  486. tmr_cur = (CPU_TS_TMR) CPU_TS_TmrRd(); /* Get cur ts tmr val (in ts tmr cnts). */
  487. tmr_delta = (CPU_TS_TMR)(tmr_cur - CPU_TS_64_TmrPrev); /* Calc delta ts tmr cnts. */
  488. CPU_TS_64_Accum += (CPU_TS64 ) tmr_delta; /* Inc ts by delta ts tmr cnts (see Note #2). */
  489. CPU_TS_64_TmrPrev = (CPU_TS_TMR) tmr_cur; /* Save cur ts tmr cnts for next update. */
  490. ts = (CPU_TS64 ) CPU_TS_64_Accum;
  491. CPU_INT_EN();
  492. #endif
  493. return (ts);
  494. }
  495. #endif
  496. /*
  497. *********************************************************************************************************
  498. * CPU_TS_Update()
  499. *
  500. * Description : Update current CPU timestamp(s).
  501. *
  502. * Argument(s) : none.
  503. *
  504. * Return(s) : none.
  505. *
  506. * Caller(s) : Application/BSP periodic time handler (see Note #1).
  507. *
  508. * This function is a CPU timestamp BSP function & SHOULD be called only by appropriate
  509. * application/BSP function(s).
  510. *
  511. * Note(s) : (1) (a) CPU timestamp(s) MUST be updated periodically by some application (or BSP) time
  512. * handler in order to (adequately) maintain CPU timestamp(s)' time.
  513. *
  514. * (b) CPU timestamp(s) MUST be updated more frequently than the CPU timestamp timer
  515. * overflows; otherwise, CPU timestamp(s) will lose time.
  516. *
  517. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c2'.
  518. *********************************************************************************************************
  519. */
  520. #if (CPU_CFG_TS_EN == DEF_ENABLED)
  521. void CPU_TS_Update (void)
  522. {
  523. #if ((CPU_CFG_TS_32_EN == DEF_ENABLED) && \
  524. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32))
  525. (void)CPU_TS_Get32();
  526. #endif
  527. #if ((CPU_CFG_TS_64_EN == DEF_ENABLED) && \
  528. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64))
  529. (void)CPU_TS_Get64();
  530. #endif
  531. }
  532. #endif
  533. /*
  534. *********************************************************************************************************
  535. * CPU_TS_TmrFreqGet()
  536. *
  537. * Description : Get CPU timestamp's timer frequency.
  538. *
  539. * Argument(s) : p_err Pointer to variable that will receive the return error code from this function :
  540. *
  541. * CPU_ERR_NONE CPU timestamp's timer frequency successfully
  542. * returned.
  543. * CPU_ERR_TS_FREQ_INVALID CPU timestamp's timer frequency invalid &/or
  544. * NOT yet configured.
  545. *
  546. * Return(s) : CPU timestamp's timer frequency (in Hertz), if NO error(s).
  547. *
  548. * 0, otherwise.
  549. *
  550. * Caller(s) : Application.
  551. *
  552. * This function is a CPU module application programming interface (API) function & MAY be
  553. * called by application function(s).
  554. *
  555. * Note(s) : none.
  556. *********************************************************************************************************
  557. */
  558. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  559. CPU_TS_TMR_FREQ CPU_TS_TmrFreqGet (CPU_ERR *p_err)
  560. {
  561. CPU_TS_TMR_FREQ freq_hz;
  562. if (p_err == (CPU_ERR *)0) {
  563. CPU_SW_EXCEPTION(;);
  564. }
  565. freq_hz = CPU_TS_TmrFreq_Hz;
  566. *p_err = (freq_hz != 0u) ? CPU_ERR_NONE : CPU_ERR_TS_FREQ_INVALID;
  567. return (freq_hz);
  568. }
  569. #endif
  570. /*
  571. *********************************************************************************************************
  572. * CPU_TS_TmrFreqSet()
  573. *
  574. * Description : Set CPU timestamp's timer frequency.
  575. *
  576. * Argument(s) : freq_hz Frequency (in Hertz) to set for CPU timestamp's timer.
  577. *
  578. * Return(s) : none.
  579. *
  580. * Caller(s) : CPU_TS_TmrInit(),
  581. * Application/BSP initialization function(s).
  582. *
  583. * This function is a CPU module BSP function & SHOULD be called only by appropriate
  584. * application/BSP function(s) [see Note #1].
  585. *
  586. * Note(s) : (1) (a) (1) CPU timestamp timer frequency is NOT required for internal CPU timestamp
  587. * operations but may OPTIONALLY be configured by CPU_TS_TmrInit() or other
  588. * application/BSP initialization functions.
  589. *
  590. * (2) CPU timestamp timer frequency MAY be used with optional CPU_TSxx_to_uSec()
  591. * to convert CPU timestamps from timer counts into microseconds.
  592. *
  593. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2a'.
  594. *
  595. * (b) CPU timestamp timer period SHOULD be less than the typical measured time but MUST
  596. * be less than the maximum measured time; otherwise, timer resolution inadequate to
  597. * measure desired times.
  598. *
  599. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2b'.
  600. *********************************************************************************************************
  601. */
  602. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  603. void CPU_TS_TmrFreqSet (CPU_TS_TMR_FREQ freq_hz)
  604. {
  605. CPU_TS_TmrFreq_Hz = freq_hz;
  606. }
  607. #endif
  608. /*
  609. *********************************************************************************************************
  610. * CPU_IntDisMeasMaxCurReset()
  611. *
  612. * Description : Reset current maximum interrupts disabled time.
  613. *
  614. * Argument(s) : none.
  615. *
  616. * Return(s) : Maximum interrupts disabled time (in CPU timestamp timer counts) before resetting.
  617. *
  618. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c'
  619. * & 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2'.
  620. *
  621. * Caller(s) : Application.
  622. *
  623. * This function is a CPU module application programming interface (API) function
  624. * & MAY be called by application function(s).
  625. *
  626. * Note(s) : (1) After initialization, 'CPU_IntDisMeasMaxCur_cnts' MUST ALWAYS be accessed
  627. * exclusively with interrupts disabled -- but NOT with critical sections.
  628. *********************************************************************************************************
  629. */
  630. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  631. CPU_TS_TMR CPU_IntDisMeasMaxCurReset (void)
  632. {
  633. CPU_TS_TMR time_max_cnts;
  634. CPU_SR_ALLOC();
  635. time_max_cnts = CPU_IntDisMeasMaxCurGet();
  636. CPU_INT_DIS();
  637. CPU_IntDisMeasMaxCur_cnts = 0u;
  638. CPU_INT_EN();
  639. return (time_max_cnts);
  640. }
  641. #endif
  642. /*
  643. *********************************************************************************************************
  644. * CPU_IntDisMeasMaxCurGet()
  645. *
  646. * Description : Get current maximum interrupts disabled time.
  647. *
  648. * Argument(s) : none.
  649. *
  650. * Return(s) : Current maximum interrupts disabled time (in CPU timestamp timer counts).
  651. *
  652. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c'
  653. * & 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2'.
  654. *
  655. * Caller(s) : CPU_IntDisMeasMaxCurReset(),
  656. * Application.
  657. *
  658. * This function is a CPU module application programming interface (API) function
  659. * & MAY be called by application function(s).
  660. *
  661. * Note(s) : (1) After initialization, 'CPU_IntDisMeasMaxCur_cnts' MUST ALWAYS be accessed
  662. * exclusively with interrupts disabled -- but NOT with critical sections.
  663. *********************************************************************************************************
  664. */
  665. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  666. CPU_TS_TMR CPU_IntDisMeasMaxCurGet (void)
  667. {
  668. CPU_TS_TMR time_tot_cnts;
  669. CPU_TS_TMR time_max_cnts;
  670. CPU_SR_ALLOC();
  671. CPU_INT_DIS();
  672. time_tot_cnts = CPU_IntDisMeasMaxCur_cnts;
  673. CPU_INT_EN();
  674. time_max_cnts = CPU_IntDisMeasMaxCalc(time_tot_cnts);
  675. return (time_max_cnts);
  676. }
  677. #endif
  678. /*
  679. *********************************************************************************************************
  680. * CPU_IntDisMeasMaxGet()
  681. *
  682. * Description : Get (non-resetable) maximum interrupts disabled time.
  683. *
  684. * Argument(s) : none.
  685. *
  686. * Return(s) : (Non-resetable) maximum interrupts disabled time (in CPU timestamp timer counts).
  687. *
  688. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c'
  689. * & 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2'.
  690. *
  691. * Caller(s) : CPU_IntDisMeasInit(),
  692. * Application.
  693. *
  694. * This function is a CPU module application programming interface (API) function
  695. * & MAY be called by application function(s).
  696. *
  697. * Note(s) : (1) After initialization, 'CPU_IntDisMeasMax_cnts' MUST ALWAYS be accessed
  698. * exclusively with interrupts disabled -- but NOT with critical sections.
  699. *********************************************************************************************************
  700. */
  701. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  702. CPU_TS_TMR CPU_IntDisMeasMaxGet (void)
  703. {
  704. CPU_TS_TMR time_tot_cnts;
  705. CPU_TS_TMR time_max_cnts;
  706. CPU_SR_ALLOC();
  707. CPU_INT_DIS();
  708. time_tot_cnts = CPU_IntDisMeasMax_cnts;
  709. CPU_INT_EN();
  710. time_max_cnts = CPU_IntDisMeasMaxCalc(time_tot_cnts);
  711. return (time_max_cnts);
  712. }
  713. #endif
  714. /*
  715. *********************************************************************************************************
  716. * CPU_IntDisMeasStart()
  717. *
  718. * Description : Start interrupts disabled time measurement.
  719. *
  720. * Argument(s) : none.
  721. *
  722. * Return(s) : none.
  723. *
  724. * Caller(s) : CPU_CRITICAL_ENTER().
  725. *
  726. * This function is an INTERNAL CPU module function & MUST NOT be called by application
  727. * function(s).
  728. *
  729. * Note(s) : none.
  730. *********************************************************************************************************
  731. */
  732. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  733. void CPU_IntDisMeasStart (void)
  734. {
  735. CPU_IntDisMeasCtr++;
  736. if (CPU_IntDisNestCtr == 0u) { /* If ints NOT yet dis'd, ... */
  737. CPU_IntDisMeasStart_cnts = CPU_TS_TmrRd(); /* ... get ints dis'd start time. */
  738. }
  739. CPU_IntDisNestCtr++;
  740. }
  741. #endif
  742. /*
  743. *********************************************************************************************************
  744. * CPU_IntDisMeasStop()
  745. *
  746. * Description : Stop interrupts disabled time measurement.
  747. *
  748. * Argument(s) : none.
  749. *
  750. * Return(s) : none.
  751. *
  752. * Caller(s) : CPU_CRITICAL_EXIT().
  753. *
  754. * This function is an INTERNAL CPU module function & MUST NOT be called by application
  755. * function(s).
  756. *
  757. * Note(s) : (1) (a) The total amount of time interrupts are disabled by system &/or application code
  758. * during critical sections is calculated by the following equations :
  759. *
  760. * (1) When interrupts disabled time measurements are disabled :
  761. *
  762. *
  763. * | CRITICAL | | CRITICAL |
  764. * |<- SECTION ->| |<- SECTION ->|
  765. * | ENTER | | EXIT |
  766. *
  767. * Disable Enable
  768. * Interrupts Interrupts
  769. *
  770. * || || || ||
  771. * || || || ||
  772. * || | ||<------------------------->|| | ||
  773. * || |<->|| | ||<----->| ||
  774. * || | | || | || | | ||
  775. * | | | | |
  776. * interrupts time interrupts
  777. * disabled interrupts |enabled
  778. * | disabled |
  779. * | (via application) |
  780. * time time
  781. * interrupts interrupts
  782. * disabled ovrhd enabled ovrhd
  783. *
  784. *
  785. * (A) time = [ time - time ] - time
  786. * interrupts [ interrupts interrupts ] total
  787. * disabled [ enabled disabled ] ovrhd
  788. * (via application)
  789. *
  790. *
  791. * (B) time = time + time
  792. * total interrupts interrupts
  793. * ovrhd enabled ovrhd disabled ovrhd
  794. *
  795. *
  796. * where
  797. *
  798. * time time interrupts are disabled between
  799. * interrupts first critical section enter &
  800. * disabled last critical section exit (i.e.
  801. * (via application) minus total overhead time)
  802. *
  803. * time time when interrupts are disabled
  804. * interrupts
  805. * disabled
  806. *
  807. * time time when interrupts are enabled
  808. * interrupts
  809. * enabled
  810. *
  811. *
  812. * time total overhead time to disable/enable
  813. * total interrupts during critical section
  814. * ovrhd enter & exit
  815. *
  816. * time total overhead time to disable interrupts
  817. * interrupts during critical section enter
  818. * disabled ovrhd
  819. *
  820. * time total overhead time to enable interrupts
  821. * interrupts during critical section exit
  822. * enabled ovrhd
  823. *
  824. *
  825. * (2) When interrupts disabled time measurements are enabled :
  826. *
  827. *
  828. * | | | |
  829. * |<----- CRITICAL SECTION ENTER ----->| |<------- CRITICAL SECTION EXIT ------->|
  830. * | | | |
  831. *
  832. * Time Time
  833. * Disable Measurement Measurement Enable
  834. * Interrupts Start Stop Interrupts
  835. *
  836. * || | || || | ||
  837. * || | || || | ||
  838. * || | | ||<------------------------->|| | | ||
  839. * || | | |<----------->|| | ||<------------->| | | ||
  840. * || | | | | || | || | | | | ||
  841. * | | | | | | |
  842. * interrupts get | time | get interrupts
  843. * disabled start time | interrupts | stop time enabled
  844. * meas | disabled | meas
  845. * time (via application) time
  846. * start meas stop meas
  847. * ovrhd ovrhd
  848. *
  849. *
  850. * (A) time = [ time - time ] - time
  851. * interrupts [ stop start ] total meas
  852. * disabled [ meas meas ] ovrhd
  853. * (via application)
  854. *
  855. *
  856. * (B) time = time + time
  857. * total meas start meas stop meas
  858. * ovrhd ovrhd ovrhd
  859. *
  860. *
  861. * where
  862. *
  863. * time time interrupts are disabled between first
  864. * interrupts critical section enter & last critical
  865. * disabled section exit (i.e. minus measurement
  866. * (via application) overhead time; however, this does NOT
  867. * include any overhead time to disable
  868. * or enable interrupts during critical
  869. * section enter & exit)
  870. *
  871. * time time of disable interrupts start time
  872. * start measurement (in timer counts)
  873. * meas
  874. *
  875. * time time of disable interrupts stop time
  876. * stop measurement (in timer counts)
  877. * meas
  878. *
  879. *
  880. * time total overhead time to start/stop disabled
  881. * total meas interrupts time measurements (in timer
  882. * ovrhd counts)
  883. *
  884. * time total overhead time after getting start
  885. * start meas time until end of start measurement
  886. * ovrhd function (in timer counts)
  887. *
  888. * time total overhead time from beginning of stop
  889. * stop meas measurement function until after getting
  890. * ovrhd stop time (in timer counts)
  891. *
  892. *
  893. * (b) (1) (A) In order to correctly handle unsigned subtraction overflows of start times
  894. * from stop times, CPU timestamp timer count values MUST be returned via
  895. * word-size-configurable 'CPU_TS_TMR' data type.
  896. *
  897. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2a'.
  898. *
  899. * (B) Since unsigned subtraction of start times from stop times assumes increasing
  900. * values, timestamp timer count values MUST increase with each time count.
  901. *
  902. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2b'.
  903. *
  904. * (2) (A) To expedite & reduce interrupts disabled time measurement overhead; only the
  905. * subtraction of start times from stop times is performed.
  906. *
  907. * (B) The final calculations to subtract the interrupts disabled time measurement
  908. * overhead is performed asynchronously in appropriate API functions.
  909. *
  910. * See also 'CPU_IntDisMeasMaxCalc() Note #1b'.
  911. *********************************************************************************************************
  912. */
  913. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  914. void CPU_IntDisMeasStop (void)
  915. {
  916. CPU_TS_TMR time_ints_disd_cnts;
  917. CPU_IntDisNestCtr--;
  918. if (CPU_IntDisNestCtr == 0u) { /* If ints NO longer dis'd, ... */
  919. CPU_IntDisMeasStop_cnts = CPU_TS_TmrRd(); /* ... get ints dis'd stop time & ... */
  920. /* ... calc ints dis'd tot time (see Note #1b2A). */
  921. time_ints_disd_cnts = CPU_IntDisMeasStop_cnts -
  922. CPU_IntDisMeasStart_cnts;
  923. /* Calc max ints dis'd times. */
  924. if (CPU_IntDisMeasMaxCur_cnts < time_ints_disd_cnts) {
  925. CPU_IntDisMeasMaxCur_cnts = time_ints_disd_cnts;
  926. }
  927. if (CPU_IntDisMeasMax_cnts < time_ints_disd_cnts) {
  928. CPU_IntDisMeasMax_cnts = time_ints_disd_cnts;
  929. }
  930. }
  931. }
  932. #endif
  933. /*
  934. *********************************************************************************************************
  935. * CPU_CntLeadZeros()
  936. *
  937. * Description : Count the number of contiguous, most-significant, leading zero bits in a data value.
  938. *
  939. * Argument(s) : val Data value to count leading zero bits.
  940. *
  941. * Return(s) : Number of contiguous, most-significant, leading zero bits in 'val', if NO error(s).
  942. *
  943. * DEF_INT_CPU_U_MAX_VAL, otherwise.
  944. *
  945. * Caller(s) : CPU_CntTrailZeros(),
  946. * Application.
  947. *
  948. * This function is a CPU module application programming interface (API) function & MAY
  949. * be called by application function(s).
  950. *
  951. * Note(s) : (1) (a) Supports the following data value sizes :
  952. *
  953. * (1) 8-bits
  954. * (2) 16-bits
  955. * (3) 32-bits
  956. * (4) 64-bits
  957. *
  958. * See also 'cpu_def.h CPU WORD CONFIGURATION Note #1'.
  959. *
  960. * (b) (1) For 8-bit values :
  961. *
  962. * b07 b06 b05 b04 b03 b02 b01 b00 # Leading Zeros
  963. * --- --- --- --- --- --- --- --- ---------------
  964. * 1 x x x x x x x 0
  965. * 0 1 x x x x x x 1
  966. * 0 0 1 x x x x x 2
  967. * 0 0 0 1 x x x x 3
  968. * 0 0 0 0 1 x x x 4
  969. * 0 0 0 0 0 1 x x 5
  970. * 0 0 0 0 0 0 1 x 6
  971. * 0 0 0 0 0 0 0 1 7
  972. * 0 0 0 0 0 0 0 0 8
  973. *
  974. *
  975. * (2) For 16-bit values :
  976. *
  977. * b15 b14 b13 ... b04 b03 b02 b01 b00 # Leading Zeros
  978. * --- --- --- --- --- --- --- --- ---------------
  979. * 1 x x x x x x x 0
  980. * 0 1 x x x x x x 1
  981. * 0 0 1 x x x x x 2
  982. * : : : : : : : : :
  983. * : : : : : : : : :
  984. * 0 0 0 1 x x x x 11
  985. * 0 0 0 0 1 x x x 12
  986. * 0 0 0 0 0 1 x x 13
  987. * 0 0 0 0 0 0 1 x 14
  988. * 0 0 0 0 0 0 0 1 15
  989. * 0 0 0 0 0 0 0 0 16
  990. *
  991. * (3) For 32-bit values :
  992. *
  993. * b31 b30 b29 ... b04 b03 b02 b01 b00 # Leading Zeros
  994. * --- --- --- --- --- --- --- --- ---------------
  995. * 1 x x x x x x x 0
  996. * 0 1 x x x x x x 1
  997. * 0 0 1 x x x x x 2
  998. * : : : : : : : : :
  999. * : : : : : : : : :
  1000. * 0 0 0 1 x x x x 27
  1001. * 0 0 0 0 1 x x x 28
  1002. * 0 0 0 0 0 1 x x 29
  1003. * 0 0 0 0 0 0 1 x 30
  1004. * 0 0 0 0 0 0 0 1 31
  1005. * 0 0 0 0 0 0 0 0 32
  1006. *
  1007. *
  1008. * (4) For 64-bit values :
  1009. *
  1010. * b63 b62 b61 ... b04 b03 b02 b01 b00 # Leading Zeros
  1011. * --- --- --- --- --- --- --- --- ---------------
  1012. * 1 x x x x x x x 0
  1013. * 0 1 x x x x x x 1
  1014. * 0 0 1 x x x x x 2
  1015. * : : : : : : : : :
  1016. * : : : : : : : : :
  1017. * 0 0 0 1 x x x x 59
  1018. * 0 0 0 0 1 x x x 60
  1019. * 0 0 0 0 0 1 x x 61
  1020. * 0 0 0 0 0 0 1 x 62
  1021. * 0 0 0 0 0 0 0 1 63
  1022. * 0 0 0 0 0 0 0 0 64
  1023. *
  1024. *
  1025. * See also 'CPU COUNT LEAD ZEROs LOOKUP TABLE Note #1'.
  1026. *********************************************************************************************************
  1027. */
  1028. #ifndef CPU_CFG_LEAD_ZEROS_ASM_PRESENT
  1029. CPU_DATA CPU_CntLeadZeros (CPU_DATA val)
  1030. {
  1031. CPU_DATA nbr_lead_zeros;
  1032. #if (CPU_CFG_DATA_SIZE == CPU_WORD_SIZE_08)
  1033. nbr_lead_zeros = CPU_CntLeadZeros08((CPU_INT08U)val);
  1034. #elif (CPU_CFG_DATA_SIZE == CPU_WORD_SIZE_16)
  1035. nbr_lead_zeros = CPU_CntLeadZeros16((CPU_INT16U)val);
  1036. #elif (CPU_CFG_DATA_SIZE == CPU_WORD_SIZE_32)
  1037. nbr_lead_zeros = CPU_CntLeadZeros32((CPU_INT32U)val);
  1038. #elif (CPU_CFG_DATA_SIZE == CPU_WORD_SIZE_64)
  1039. nbr_lead_zeros = CPU_CntLeadZeros64((CPU_INT64U)val);
  1040. #else /* See Note #1a. */
  1041. nbr_lead_zeros = DEF_INT_CPU_U_MAX_VAL;
  1042. #endif
  1043. return (nbr_lead_zeros);
  1044. }
  1045. #endif
  1046. /*
  1047. *********************************************************************************************************
  1048. * CPU_CntLeadZeros08()
  1049. *
  1050. * Description : Count the number of contiguous, most-significant, leading zero bits in an 8-bit data value.
  1051. *
  1052. * Argument(s) : val Data value to count leading zero bits.
  1053. *
  1054. * Return(s) : Number of contiguous, most-significant, leading zero bits in 'val'.
  1055. *
  1056. * Caller(s) : CPU_CntLeadZeros(),
  1057. * CPU_CntTrailZeros08(),
  1058. * Application.
  1059. *
  1060. * This function is a CPU module application programming interface (API) function & MAY be
  1061. * called by application function(s).
  1062. *
  1063. * Note(s) : (1) Supports 8-bit values :
  1064. *
  1065. * b07 b06 b05 b04 b03 b02 b01 b00 # Leading Zeros
  1066. * --- --- --- --- --- --- --- --- ---------------
  1067. * 1 x x x x x x x 0
  1068. * 0 1 x x x x x x 1
  1069. * 0 0 1 x x x x x 2
  1070. * 0 0 0 1 x x x x 3
  1071. * 0 0 0 0 1 x x x 4
  1072. * 0 0 0 0 0 1 x x 5
  1073. * 0 0 0 0 0 0 1 x 6
  1074. * 0 0 0 0 0 0 0 1 7
  1075. * 0 0 0 0 0 0 0 0 8
  1076. *
  1077. *
  1078. * See also 'CPU COUNT LEAD ZEROs LOOKUP TABLE Note #1'.
  1079. *********************************************************************************************************
  1080. */
  1081. #if (CPU_CFG_DATA_SIZE_MAX >= CPU_WORD_SIZE_08)
  1082. CPU_DATA CPU_CntLeadZeros08 (CPU_INT08U val)
  1083. {
  1084. #if (!((defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) && \
  1085. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_08)))
  1086. CPU_DATA ix;
  1087. #endif
  1088. CPU_DATA nbr_lead_zeros;
  1089. /* ---------- ASM-OPTIMIZED ----------- */
  1090. #if ((defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) && \
  1091. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_08))
  1092. nbr_lead_zeros = CPU_CntLeadZeros((CPU_DATA)val);
  1093. nbr_lead_zeros -= (CPU_CFG_DATA_SIZE - CPU_WORD_SIZE_08) * DEF_OCTET_NBR_BITS;
  1094. #else /* ----------- C-OPTIMIZED ------------ */
  1095. /* Chk bits [07:00] : */
  1096. /* .. Nbr lead zeros = .. */
  1097. ix = (CPU_DATA)(val); /* .. lookup tbl ix = 'val' >> 0 bits */
  1098. nbr_lead_zeros = (CPU_DATA)(CPU_CntLeadZerosTbl[ix]); /* .. plus nbr msb lead zeros = 0 bits.*/
  1099. #endif
  1100. return (nbr_lead_zeros);
  1101. }
  1102. #endif
  1103. /*
  1104. *********************************************************************************************************
  1105. * CPU_CntLeadZeros16()
  1106. *
  1107. * Description : Count the number of contiguous, most-significant, leading zero bits in a 16-bit data value.
  1108. *
  1109. * Argument(s) : val Data value to count leading zero bits.
  1110. *
  1111. * Return(s) : Number of contiguous, most-significant, leading zero bits in 'val'.
  1112. *
  1113. * Caller(s) : CPU_CntLeadZeros(),
  1114. * CPU_CntTrailZeros16(),
  1115. * Application.
  1116. *
  1117. * This function is a CPU module application programming interface (API) function & MAY be
  1118. * called by application function(s).
  1119. *
  1120. * Note(s) : (1) Supports 16-bit values :
  1121. *
  1122. * b15 b14 b13 ... b04 b03 b02 b01 b00 # Leading Zeros
  1123. * --- --- --- --- --- --- --- --- ---------------
  1124. * 1 x x x x x x x 0
  1125. * 0 1 x x x x x x 1
  1126. * 0 0 1 x x x x x 2
  1127. * : : : : : : : : :
  1128. * : : : : : : : : :
  1129. * 0 0 0 1 x x x x 11
  1130. * 0 0 0 0 1 x x x 12
  1131. * 0 0 0 0 0 1 x x 13
  1132. * 0 0 0 0 0 0 1 x 14
  1133. * 0 0 0 0 0 0 0 1 15
  1134. * 0 0 0 0 0 0 0 0 16
  1135. *
  1136. *
  1137. * See also 'CPU COUNT LEAD ZEROs LOOKUP TABLE Note #1'.
  1138. *********************************************************************************************************
  1139. */
  1140. #if (CPU_CFG_DATA_SIZE_MAX >= CPU_WORD_SIZE_16)
  1141. CPU_DATA CPU_CntLeadZeros16 (CPU_INT16U val)
  1142. {
  1143. #if (!((defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) && \
  1144. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_16)))
  1145. CPU_DATA ix;
  1146. #endif
  1147. CPU_DATA nbr_lead_zeros;
  1148. /* ---------- ASM-OPTIMIZED ----------- */
  1149. #if ((defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) && \
  1150. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_16))
  1151. nbr_lead_zeros = CPU_CntLeadZeros((CPU_DATA)val);
  1152. nbr_lead_zeros -= (CPU_CFG_DATA_SIZE - CPU_WORD_SIZE_16) * DEF_OCTET_NBR_BITS;
  1153. #else /* ----------- C-OPTIMIZED ------------ */
  1154. if (val > 0x00FFu) { /* Chk bits [15:08] : */
  1155. /* .. Nbr lead zeros = .. */
  1156. ix = (CPU_DATA)((CPU_DATA)val >> 8u); /* .. lookup tbl ix = 'val' >> 8 bits */
  1157. nbr_lead_zeros = (CPU_DATA)(CPU_CntLeadZerosTbl[ix]); /* .. plus nbr msb lead zeros = 0 bits.*/
  1158. } else { /* Chk bits [07:00] : */
  1159. /* .. Nbr lead zeros = .. */
  1160. ix = (CPU_DATA)(val); /* .. lookup tbl ix = 'val' >> 0 bits */
  1161. nbr_lead_zeros = (CPU_DATA)((CPU_DATA)CPU_CntLeadZerosTbl[ix] + 8u); /* .. plus nbr msb lead zeros = 8 bits.*/
  1162. }
  1163. #endif
  1164. return (nbr_lead_zeros);
  1165. }
  1166. #endif
  1167. /*
  1168. *********************************************************************************************************
  1169. * CPU_CntLeadZeros32()
  1170. *
  1171. * Description : Count the number of contiguous, most-significant, leading zero bits in a 32-bit data value.
  1172. *
  1173. * Argument(s) : val Data value to count leading zero bits.
  1174. *
  1175. * Return(s) : Number of contiguous, most-significant, leading zero bits in 'val'.
  1176. *
  1177. * Caller(s) : CPU_CntLeadZeros(),
  1178. * CPU_CntTrailZeros32(),
  1179. * Application.
  1180. *
  1181. * This function is a CPU module application programming interface (API) function & MAY be
  1182. * called by application function(s).
  1183. *
  1184. * Note(s) : (1) Supports 32-bit values :
  1185. *
  1186. * b31 b30 b29 ... b04 b03 b02 b01 b00 # Leading Zeros
  1187. * --- --- --- --- --- --- --- --- ---------------
  1188. * 1 x x x x x x x 0
  1189. * 0 1 x x x x x x 1
  1190. * 0 0 1 x x x x x 2
  1191. * : : : : : : : : :
  1192. * : : : : : : : : :
  1193. * 0 0 0 1 x x x x 27
  1194. * 0 0 0 0 1 x x x 28
  1195. * 0 0 0 0 0 1 x x 29
  1196. * 0 0 0 0 0 0 1 x 30
  1197. * 0 0 0 0 0 0 0 1 31
  1198. * 0 0 0 0 0 0 0 0 32
  1199. *
  1200. *
  1201. * See also 'CPU COUNT LEAD ZEROs LOOKUP TABLE Note #1'.
  1202. *********************************************************************************************************
  1203. */
  1204. #if (CPU_CFG_DATA_SIZE_MAX >= CPU_WORD_SIZE_32)
  1205. CPU_DATA CPU_CntLeadZeros32 (CPU_INT32U val)
  1206. {
  1207. #if (!((defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) && \
  1208. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_32)))
  1209. CPU_DATA ix;
  1210. #endif
  1211. CPU_DATA nbr_lead_zeros;
  1212. /* ---------- ASM-OPTIMIZED ----------- */
  1213. #if ((defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) && \
  1214. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_32))
  1215. nbr_lead_zeros = CPU_CntLeadZeros((CPU_DATA)val);
  1216. nbr_lead_zeros -= (CPU_CFG_DATA_SIZE - CPU_WORD_SIZE_32) * DEF_OCTET_NBR_BITS;
  1217. #else /* ----------- C-OPTIMIZED ------------ */
  1218. if (val > 0x0000FFFFu) {
  1219. if (val > 0x00FFFFFFu) { /* Chk bits [31:24] : */
  1220. /* .. Nbr lead zeros = .. */
  1221. ix = (CPU_DATA)((CPU_DATA)(val >> 24u)); /* .. lookup tbl ix = 'val' >> 24 bits */
  1222. nbr_lead_zeros = (CPU_DATA)(CPU_CntLeadZerosTbl[ix]); /* .. plus nbr msb lead zeros = 0 bits.*/
  1223. } else { /* Chk bits [23:16] : */
  1224. /* .. Nbr lead zeros = .. */
  1225. ix = (CPU_DATA)((CPU_DATA)(val >> 16u)); /* .. lookup tbl ix = 'val' >> 16 bits */
  1226. nbr_lead_zeros = (CPU_DATA)((CPU_DATA)CPU_CntLeadZerosTbl[ix] + 8u);/* .. plus nbr msb lead zeros = 8 bits.*/
  1227. }
  1228. } else {
  1229. if (val > 0x000000FFu) { /* Chk bits [15:08] : */
  1230. /* .. Nbr lead zeros = .. */
  1231. ix = (CPU_DATA)((CPU_DATA)(val >> 8u)); /* .. lookup tbl ix = 'val' >> 8 bits */
  1232. nbr_lead_zeros = (CPU_DATA)((CPU_DATA)CPU_CntLeadZerosTbl[ix] + 16u);/* .. plus nbr msb lead zeros = 16 bits.*/
  1233. } else { /* Chk bits [07:00] : */
  1234. /* .. Nbr lead zeros = .. */
  1235. ix = (CPU_DATA)((CPU_DATA)(val >> 0u)); /* .. lookup tbl ix = 'val' >> 0 bits */
  1236. nbr_lead_zeros = (CPU_DATA)((CPU_DATA)CPU_CntLeadZerosTbl[ix] + 24u);/* .. plus nbr msb lead zeros = 24 bits.*/
  1237. }
  1238. }
  1239. #endif
  1240. return (nbr_lead_zeros);
  1241. }
  1242. #endif
  1243. /*
  1244. *********************************************************************************************************
  1245. * CPU_CntLeadZeros64()
  1246. *
  1247. * Description : Count the number of contiguous, most-significant, leading zero bits in a 64-bit data value.
  1248. *
  1249. * Argument(s) : val Data value to count leading zero bits.
  1250. *
  1251. * Return(s) : Number of contiguous, most-significant, leading zero bits in 'val'.
  1252. *
  1253. * Caller(s) : CPU_CntLeadZeros(),
  1254. * CPU_CntTrailZeros64(),
  1255. * Application.
  1256. *
  1257. * This function is a CPU module application programming interface (API) function & MAY be
  1258. * called by application function(s).
  1259. *
  1260. * Note(s) : (1) Supports 64-bit values :
  1261. *
  1262. * b63 b62 b61 ... b04 b03 b02 b01 b00 # Leading Zeros
  1263. * --- --- --- --- --- --- --- --- ---------------
  1264. * 1 x x x x x x x 0
  1265. * 0 1 x x x x x x 1
  1266. * 0 0 1 x x x x x 2
  1267. * : : : : : : : : :
  1268. * : : : : : : : : :
  1269. * 0 0 0 1 x x x x 59
  1270. * 0 0 0 0 1 x x x 60
  1271. * 0 0 0 0 0 1 x x 61
  1272. * 0 0 0 0 0 0 1 x 62
  1273. * 0 0 0 0 0 0 0 1 63
  1274. * 0 0 0 0 0 0 0 0 64
  1275. *
  1276. *
  1277. * See also 'CPU COUNT LEAD ZEROs LOOKUP TABLE Note #1'.
  1278. *********************************************************************************************************
  1279. */
  1280. #if (CPU_CFG_DATA_SIZE_MAX >= CPU_WORD_SIZE_64)
  1281. CPU_DATA CPU_CntLeadZeros64 (CPU_INT64U val)
  1282. {
  1283. #if (!((defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) && \
  1284. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_64)))
  1285. CPU_DATA ix;
  1286. #endif
  1287. CPU_DATA nbr_lead_zeros;
  1288. /* ---------- ASM-OPTIMIZED ----------- */
  1289. #if ((defined(CPU_CFG_LEAD_ZEROS_ASM_PRESENT)) && \
  1290. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_64))
  1291. nbr_lead_zeros = CPU_CntLeadZeros((CPU_DATA)val);
  1292. nbr_lead_zeros -= (CPU_CFG_DATA_SIZE - CPU_WORD_SIZE_64) * DEF_OCTET_NBR_BITS;
  1293. #else /* ----------- C-OPTIMIZED ------------ */
  1294. if (val > 0x00000000FFFFFFFFu) {
  1295. if (val > 0x0000FFFFFFFFFFFFu) {
  1296. if (val > 0x00FFFFFFFFFFFFFFu) { /* Chk bits [63:56] : */
  1297. /* .. Nbr lead zeros = .. */
  1298. ix = (CPU_DATA)((CPU_INT64U)val >> 56u); /* .. lookup tbl ix = 'val' >> 56 bits */
  1299. nbr_lead_zeros = (CPU_DATA)(CPU_CntLeadZerosTbl[ix]); /* .. plus nbr msb lead zeros = 0 bits.*/
  1300. } else { /* Chk bits [55:48] : */
  1301. /* .. Nbr lead zeros = .. */
  1302. ix = (CPU_DATA)((CPU_INT64U)val >> 48u); /* .. lookup tbl ix = 'val' >> 48 bits */
  1303. nbr_lead_zeros = (CPU_DATA)((CPU_INT64U)CPU_CntLeadZerosTbl[ix] + 8u);/* .. plus nbr msb lead zeros = 8 bits.*/
  1304. }
  1305. } else {
  1306. if (val > 0x000000FFFFFFFFFFu) { /* Chk bits [47:40] : */
  1307. /* .. Nbr lead zeros = .. */
  1308. ix = (CPU_DATA)((CPU_INT64U)val >> 40u); /* .. lookup tbl ix = 'val' >> 40 bits */
  1309. nbr_lead_zeros = (CPU_DATA)((CPU_INT64U)CPU_CntLeadZerosTbl[ix] + 16u);/* .. plus nbr msb lead zeros = 16 bits.*/
  1310. } else { /* Chk bits [39:32] : */
  1311. /* .. Nbr lead zeros = .. */
  1312. ix = (CPU_DATA)((CPU_INT64U)val >> 32u); /* .. lookup tbl ix = 'val' >> 32 bits */
  1313. nbr_lead_zeros = (CPU_DATA)((CPU_INT64U)CPU_CntLeadZerosTbl[ix] + 24u);/* .. plus nbr msb lead zeros = 24 bits.*/
  1314. }
  1315. }
  1316. } else {
  1317. if (val > 0x000000000000FFFFu) {
  1318. if (val > 0x0000000000FFFFFFu) { /* Chk bits [31:24] : */
  1319. /* .. Nbr lead zeros = .. */
  1320. ix = (CPU_DATA)((CPU_INT64U)val >> 24u); /* .. lookup tbl ix = 'val' >> 24 bits */
  1321. nbr_lead_zeros = (CPU_DATA)((CPU_INT64U)CPU_CntLeadZerosTbl[ix] + 32u);/* .. plus nbr msb lead zeros = 32 bits.*/
  1322. } else { /* Chk bits [23:16] : */
  1323. /* .. Nbr lead zeros = .. */
  1324. ix = (CPU_DATA)((CPU_INT64U)val >> 16u); /* .. lookup tbl ix = 'val' >> 16 bits */
  1325. nbr_lead_zeros = (CPU_DATA)((CPU_INT64U)CPU_CntLeadZerosTbl[ix] + 40u);/* .. plus nbr msb lead zeros = 40 bits.*/
  1326. }
  1327. } else {
  1328. if (val > 0x00000000000000FFu) { /* Chk bits [15:08] : */
  1329. /* .. Nbr lead zeros = .. */
  1330. ix = (CPU_DATA)((CPU_INT64U)val >> 8u); /* .. lookup tbl ix = 'val' >> 8 bits */
  1331. nbr_lead_zeros = (CPU_DATA)((CPU_INT64U)CPU_CntLeadZerosTbl[ix] + 48u);/* .. plus nbr msb lead zeros = 48 bits.*/
  1332. } else { /* Chk bits [07:00] : */
  1333. /* .. Nbr lead zeros = .. */
  1334. ix = (CPU_DATA)(val); /* .. lookup tbl ix = 'val' >> 0 bits */
  1335. nbr_lead_zeros = (CPU_DATA)((CPU_INT64U)CPU_CntLeadZerosTbl[ix] + 56u);/* .. plus nbr msb lead zeros = 56 bits.*/
  1336. }
  1337. }
  1338. }
  1339. #endif
  1340. return (nbr_lead_zeros);
  1341. }
  1342. #endif
  1343. /*
  1344. *********************************************************************************************************
  1345. * CPU_CntTrailZeros()
  1346. *
  1347. * Description : Count the number of contiguous, least-significant, trailing zero bits in a data value.
  1348. *
  1349. * Argument(s) : val Data value to count trailing zero bits.
  1350. *
  1351. * Return(s) : Number of contiguous, least-significant, trailing zero bits in 'val'.
  1352. *
  1353. * Caller(s) : Application.
  1354. *
  1355. * This function is a CPU module application programming interface (API) function & MAY
  1356. * be called by application function(s).
  1357. *
  1358. * Note(s) : (1) (a) Supports the following data value sizes :
  1359. *
  1360. * (1) 8-bits
  1361. * (2) 16-bits
  1362. * (3) 32-bits
  1363. * (4) 64-bits
  1364. *
  1365. * See also 'cpu_def.h CPU WORD CONFIGURATION Note #1'.
  1366. *
  1367. * (b) (1) For 8-bit values :
  1368. *
  1369. * b07 b06 b05 b04 b03 b02 b01 b00 # Trailing Zeros
  1370. * --- --- --- --- --- --- --- --- ----------------
  1371. * x x x x x x x 1 0
  1372. * x x x x x x 1 0 1
  1373. * x x x x x 1 0 0 2
  1374. * x x x x 1 0 0 0 3
  1375. * x x x 1 0 0 0 0 4
  1376. * x x 1 0 0 0 0 0 5
  1377. * x 1 0 0 0 0 0 0 6
  1378. * 1 0 0 0 0 0 0 0 7
  1379. * 0 0 0 0 0 0 0 0 8
  1380. *
  1381. *
  1382. * (2) For 16-bit values :
  1383. *
  1384. * b15 b14 b13 b12 b11 ... b02 b01 b00 # Trailing Zeros
  1385. * --- --- --- --- --- --- --- --- ----------------
  1386. * x x x x x x x 1 0
  1387. * x x x x x x 1 0 1
  1388. * x x x x x 1 0 0 2
  1389. * : : : : : : : : :
  1390. * : : : : : : : : :
  1391. * x x x x 1 0 0 0 11
  1392. * x x x 1 0 0 0 0 12
  1393. * x x 1 0 0 0 0 0 13
  1394. * x 1 0 0 0 0 0 0 14
  1395. * 1 0 0 0 0 0 0 0 15
  1396. * 0 0 0 0 0 0 0 0 16
  1397. *
  1398. *
  1399. * (3) For 32-bit values :
  1400. *
  1401. * b31 b30 b29 b28 b27 ... b02 b01 b00 # Trailing Zeros
  1402. * --- --- --- --- --- --- --- --- ----------------
  1403. * x x x x x x x 1 0
  1404. * x x x x x x 1 0 1
  1405. * x x x x x 1 0 0 2
  1406. * : : : : : : : : :
  1407. * : : : : : : : : :
  1408. * x x x x 1 0 0 0 27
  1409. * x x x 1 0 0 0 0 28
  1410. * x x 1 0 0 0 0 0 29
  1411. * x 1 0 0 0 0 0 0 30
  1412. * 1 0 0 0 0 0 0 0 31
  1413. * 0 0 0 0 0 0 0 0 32
  1414. *
  1415. *
  1416. * (4) For 64-bit values :
  1417. *
  1418. * b63 b62 b61 b60 b59 ... b02 b01 b00 # Trailing Zeros
  1419. * --- --- --- --- --- --- --- --- ----------------
  1420. * x x x x x x x 1 0
  1421. * x x x x x x 1 0 1
  1422. * x x x x x 1 0 0 2
  1423. * : : : : : : : : :
  1424. * : : : : : : : : :
  1425. * x x x x 1 0 0 0 59
  1426. * x x x 1 0 0 0 0 60
  1427. * x x 1 0 0 0 0 0 61
  1428. * x 1 0 0 0 0 0 0 62
  1429. * 1 0 0 0 0 0 0 0 63
  1430. * 0 0 0 0 0 0 0 0 64
  1431. *
  1432. * (2) For non-zero values, the returned number of contiguous, least-significant, trailing
  1433. * zero bits is also equivalent to the bit position of the least-significant set bit.
  1434. *
  1435. * (3) 'val' SHOULD be validated for non-'0' PRIOR to all other counting zero calculations :
  1436. *
  1437. * (a) CPU_CntTrailZeros()'s final conditional statement calculates 'val's number of
  1438. * trailing zeros based on its return data size, 'CPU_CFG_DATA_SIZE', & 'val's
  1439. * calculated number of lead zeros ONLY if the initial 'val' is non-'0' :
  1440. *
  1441. * if (val != 0u) {
  1442. * nbr_trail_zeros = ((CPU_CFG_DATA_SIZE * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1443. * } else {
  1444. * nbr_trail_zeros = nbr_lead_zeros;
  1445. * }
  1446. *
  1447. * Therefore, initially validating all non-'0' values avoids having to conditionally
  1448. * execute the final 'if' statement.
  1449. *********************************************************************************************************
  1450. */
  1451. #ifndef CPU_CFG_TRAIL_ZEROS_ASM_PRESENT
  1452. CPU_DATA CPU_CntTrailZeros (CPU_DATA val)
  1453. {
  1454. CPU_DATA val_bit_mask;
  1455. CPU_DATA nbr_lead_zeros;
  1456. CPU_DATA nbr_trail_zeros;
  1457. if (val == 0u) { /* Rtn ALL val bits as zero'd (see Note #3). */
  1458. return (CPU_CFG_DATA_SIZE * DEF_OCTET_NBR_BITS);
  1459. }
  1460. val_bit_mask = val & ((CPU_DATA)~val + 1u); /* Zero/clr all bits EXCEPT least-sig set bit. */
  1461. nbr_lead_zeros = CPU_CntLeadZeros(val_bit_mask); /* Cnt nbr lead 0s. */
  1462. /* Calc nbr trail 0s = (nbr val bits - 1) - nbr lead 0s.*/
  1463. nbr_trail_zeros = ((CPU_CFG_DATA_SIZE * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1464. return (nbr_trail_zeros);
  1465. }
  1466. #endif
  1467. /*
  1468. *********************************************************************************************************
  1469. * CPU_CntTrailZeros08()
  1470. *
  1471. * Description : Count the number of contiguous, least-significant, trailing zero bits in an 8-bit data value.
  1472. *
  1473. * Argument(s) : val Data value to count trailing zero bits.
  1474. *
  1475. * Return(s) : Number of contiguous, least-significant, trailing zero bits in 'val'.
  1476. *
  1477. * Caller(s) : Application.
  1478. *
  1479. * This function is a CPU module application programming interface (API) function & MAY be
  1480. * called by application function(s).
  1481. *
  1482. * Note(s) : (1) Supports 8-bit values :
  1483. *
  1484. * b07 b06 b05 b04 b03 b02 b01 b00 # Trailing Zeros
  1485. * --- --- --- --- --- --- --- --- ----------------
  1486. * x x x x x x x 1 0
  1487. * x x x x x x 1 0 1
  1488. * x x x x x 1 0 0 2
  1489. * x x x x 1 0 0 0 3
  1490. * x x x 1 0 0 0 0 4
  1491. * x x 1 0 0 0 0 0 5
  1492. * x 1 0 0 0 0 0 0 6
  1493. * 1 0 0 0 0 0 0 0 7
  1494. * 0 0 0 0 0 0 0 0 8
  1495. *
  1496. *
  1497. * (2) For non-zero values, the returned number of contiguous, least-significant, trailing
  1498. * zero bits is also equivalent to the bit position of the least-significant set bit.
  1499. *
  1500. * (3) 'val' SHOULD be validated for non-'0' PRIOR to all other counting zero calculations :
  1501. *
  1502. * (a) For assembly-optimized implementations, CPU_CntTrailZeros() returns 'val's
  1503. * number of trailing zeros via CPU's native data size, 'CPU_CFG_DATA_SIZE'.
  1504. * If the returned number of zeros exceeds CPU_CntTrailZeros08()'s 8-bit return
  1505. * data size, then the returned number of zeros must be offset by the difference
  1506. * between CPU_CntTrailZeros()'s & CPU_CntTrailZeros08()'s return data size :
  1507. *
  1508. * nbr_trail_zeros = CPU_CntTrailZeros((CPU_DATA)val);
  1509. * if (nbr_trail_zeros > (CPU_WORD_SIZE_08 * DEF_OCTET_NBR_BITS)) {
  1510. * nbr_trail_zeros -= (CPU_CFG_DATA_SIZE - CPU_WORD_SIZE_08) * DEF_OCTET_NBR_BITS;
  1511. * }
  1512. *
  1513. * However, this ONLY occurs for an initial 'val' of '0' since all non-'0' 8-bit
  1514. * values would return a number of trailing zeros less than or equal to 8 bits.
  1515. *
  1516. * Therefore, initially validating all non-'0' values prior to calling assembly-
  1517. * optimized CPU_CntTrailZeros() avoids having to offset the number of returned
  1518. * trailing zeros by the difference in CPU data size and 8-bit data value bits.
  1519. *
  1520. * (b) For CPU_CntTrailZeros08()'s C implementation, the final conditional statement
  1521. * calculates 'val's number of trailing zeros based on CPU_CntTrailZeros08()'s
  1522. * 8-bit return data size & 'val's calculated number of lead zeros ONLY if the
  1523. * initial 'val' is non-'0' :
  1524. *
  1525. * if (val != 0u) {
  1526. * nbr_trail_zeros = ((CPU_WORD_SIZE_08 * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1527. * } else {
  1528. * nbr_trail_zeros = nbr_lead_zeros;
  1529. * }
  1530. *
  1531. * Therefore, initially validating all non-'0' values avoids having to conditionally
  1532. * execute the final 'if' statement.
  1533. *********************************************************************************************************
  1534. */
  1535. #if (CPU_CFG_DATA_SIZE_MAX >= CPU_WORD_SIZE_08)
  1536. CPU_DATA CPU_CntTrailZeros08 (CPU_INT08U val)
  1537. {
  1538. #if (!((defined(CPU_CFG_TRAIL_ZEROS_ASM_PRESENT)) && \
  1539. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_08)))
  1540. CPU_INT08U val_bit_mask;
  1541. CPU_DATA nbr_lead_zeros;
  1542. #endif
  1543. CPU_DATA nbr_trail_zeros;
  1544. if (val == 0u) { /* Rtn ALL val bits as zero'd (see Note #3). */
  1545. return (CPU_WORD_SIZE_08 * DEF_OCTET_NBR_BITS);
  1546. }
  1547. /* ------------------ ASM-OPTIMIZED ------------------- */
  1548. #if ((defined(CPU_CFG_TRAIL_ZEROS_ASM_PRESENT)) && \
  1549. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_08))
  1550. nbr_trail_zeros = CPU_CntTrailZeros((CPU_DATA)val);
  1551. #else /* ------------------- C-OPTIMIZED -------------------- */
  1552. val_bit_mask = val & ((CPU_INT08U)~val + 1u); /* Zero/clr all bits EXCEPT least-sig set bit. */
  1553. nbr_lead_zeros = CPU_CntLeadZeros08(val_bit_mask); /* Cnt nbr lead 0s. */
  1554. /* Calc nbr trail 0s = (nbr val bits - 1) - nbr lead 0s.*/
  1555. nbr_trail_zeros = ((CPU_WORD_SIZE_08 * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1556. #endif
  1557. return (nbr_trail_zeros);
  1558. }
  1559. #endif
  1560. /*
  1561. *********************************************************************************************************
  1562. * CPU_CntTrailZeros16()
  1563. *
  1564. * Description : Count the number of contiguous, least-significant, trailing zero bits in a 16-bit data value.
  1565. *
  1566. * Argument(s) : val Data value to count trailing zero bits.
  1567. *
  1568. * Return(s) : Number of contiguous, least-significant, trailing zero bits in 'val'.
  1569. *
  1570. * Caller(s) : Application.
  1571. *
  1572. * This function is a CPU module application programming interface (API) function & MAY be
  1573. * called by application function(s).
  1574. *
  1575. * Note(s) : (1) Supports 16-bit values :
  1576. *
  1577. * b15 b14 b13 b12 b11 ... b02 b01 b00 # Trailing Zeros
  1578. * --- --- --- --- --- --- --- --- ----------------
  1579. * x x x x x x x 1 0
  1580. * x x x x x x 1 0 1
  1581. * x x x x x 1 0 0 2
  1582. * : : : : : : : : :
  1583. * : : : : : : : : :
  1584. * x x x x 1 0 0 0 11
  1585. * x x x 1 0 0 0 0 12
  1586. * x x 1 0 0 0 0 0 13
  1587. * x 1 0 0 0 0 0 0 14
  1588. * 1 0 0 0 0 0 0 0 15
  1589. * 0 0 0 0 0 0 0 0 16
  1590. *
  1591. *
  1592. * (2) For non-zero values, the returned number of contiguous, least-significant, trailing
  1593. * zero bits is also equivalent to the bit position of the least-significant set bit.
  1594. *
  1595. * (3) 'val' SHOULD be validated for non-'0' PRIOR to all other counting zero calculations :
  1596. *
  1597. * (a) For assembly-optimized implementations, CPU_CntTrailZeros() returns 'val's
  1598. * number of trailing zeros via CPU's native data size, 'CPU_CFG_DATA_SIZE'.
  1599. * If the returned number of zeros exceeds CPU_CntTrailZeros16()'s 16-bit return
  1600. * data size, then the returned number of zeros must be offset by the difference
  1601. * between CPU_CntTrailZeros()'s & CPU_CntTrailZeros16()'s return data size :
  1602. *
  1603. * nbr_trail_zeros = CPU_CntTrailZeros((CPU_DATA)val);
  1604. * if (nbr_trail_zeros > (CPU_WORD_SIZE_16 * DEF_OCTET_NBR_BITS)) {
  1605. * nbr_trail_zeros -= (CPU_CFG_DATA_SIZE - CPU_WORD_SIZE_16) * DEF_OCTET_NBR_BITS;
  1606. * }
  1607. *
  1608. * However, this ONLY occurs for an initial 'val' of '0' since all non-'0' 16-bit
  1609. * values would return a number of trailing zeros less than or equal to 16 bits.
  1610. *
  1611. * Therefore, initially validating all non-'0' values prior to calling assembly-
  1612. * optimized CPU_CntTrailZeros() avoids having to offset the number of returned
  1613. * trailing zeros by the difference in CPU data size and 16-bit data value bits.
  1614. *
  1615. * (b) For CPU_CntTrailZeros16()'s C implementation, the final conditional statement
  1616. * calculates 'val's number of trailing zeros based on CPU_CntTrailZeros16()'s
  1617. * 16-bit return data size & 'val's calculated number of lead zeros ONLY if the
  1618. * initial 'val' is non-'0' :
  1619. *
  1620. * if (val != 0u) {
  1621. * nbr_trail_zeros = ((CPU_WORD_SIZE_16 * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1622. * } else {
  1623. * nbr_trail_zeros = nbr_lead_zeros;
  1624. * }
  1625. *
  1626. * Therefore, initially validating all non-'0' values avoids having to conditionally
  1627. * execute the final 'if' statement.
  1628. *********************************************************************************************************
  1629. */
  1630. #if (CPU_CFG_DATA_SIZE_MAX >= CPU_WORD_SIZE_16)
  1631. CPU_DATA CPU_CntTrailZeros16 (CPU_INT16U val)
  1632. {
  1633. #if (!((defined(CPU_CFG_TRAIL_ZEROS_ASM_PRESENT)) && \
  1634. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_16)))
  1635. CPU_INT16U val_bit_mask;
  1636. CPU_DATA nbr_lead_zeros;
  1637. #endif
  1638. CPU_DATA nbr_trail_zeros;
  1639. if (val == 0u) { /* Rtn ALL val bits as zero'd (see Note #3). */
  1640. return (CPU_WORD_SIZE_16 * DEF_OCTET_NBR_BITS);
  1641. }
  1642. /* ------------------ ASM-OPTIMIZED ------------------- */
  1643. #if ((defined(CPU_CFG_TRAIL_ZEROS_ASM_PRESENT)) && \
  1644. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_16))
  1645. nbr_trail_zeros = CPU_CntTrailZeros((CPU_DATA)val);
  1646. #else /* ------------------- C-OPTIMIZED -------------------- */
  1647. val_bit_mask = val & ((CPU_INT16U)~val + 1u); /* Zero/clr all bits EXCEPT least-sig set bit. */
  1648. nbr_lead_zeros = CPU_CntLeadZeros16(val_bit_mask); /* Cnt nbr lead 0s. */
  1649. /* Calc nbr trail 0s = (nbr val bits - 1) - nbr lead 0s.*/
  1650. nbr_trail_zeros = ((CPU_WORD_SIZE_16 * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1651. #endif
  1652. return (nbr_trail_zeros);
  1653. }
  1654. #endif
  1655. /*
  1656. *********************************************************************************************************
  1657. * CPU_CntTrailZeros32()
  1658. *
  1659. * Description : Count the number of contiguous, least-significant, trailing zero bits in a 32-bit data value.
  1660. *
  1661. * Argument(s) : val Data value to count trailing zero bits.
  1662. *
  1663. * Return(s) : Number of contiguous, least-significant, trailing zero bits in 'val'.
  1664. *
  1665. * Caller(s) : Application.
  1666. *
  1667. * This function is a CPU module application programming interface (API) function & MAY be
  1668. * called by application function(s).
  1669. *
  1670. * Note(s) : (1) Supports 32-bit values :
  1671. *
  1672. * b31 b30 b29 b28 b27 ... b02 b01 b00 # Trailing Zeros
  1673. * --- --- --- --- --- --- --- --- ----------------
  1674. * x x x x x x x 1 0
  1675. * x x x x x x 1 0 1
  1676. * x x x x x 1 0 0 2
  1677. * : : : : : : : : :
  1678. * : : : : : : : : :
  1679. * x x x x 1 0 0 0 27
  1680. * x x x 1 0 0 0 0 28
  1681. * x x 1 0 0 0 0 0 29
  1682. * x 1 0 0 0 0 0 0 30
  1683. * 1 0 0 0 0 0 0 0 31
  1684. * 0 0 0 0 0 0 0 0 32
  1685. *
  1686. *
  1687. * (2) For non-zero values, the returned number of contiguous, least-significant, trailing
  1688. * zero bits is also equivalent to the bit position of the least-significant set bit.
  1689. *
  1690. * (3) 'val' SHOULD be validated for non-'0' PRIOR to all other counting zero calculations :
  1691. *
  1692. * (a) For assembly-optimized implementations, CPU_CntTrailZeros() returns 'val's
  1693. * number of trailing zeros via CPU's native data size, 'CPU_CFG_DATA_SIZE'.
  1694. * If the returned number of zeros exceeds CPU_CntTrailZeros32()'s 32-bit return
  1695. * data size, then the returned number of zeros must be offset by the difference
  1696. * between CPU_CntTrailZeros()'s & CPU_CntTrailZeros32()'s return data size :
  1697. *
  1698. * nbr_trail_zeros = CPU_CntTrailZeros((CPU_DATA)val);
  1699. * if (nbr_trail_zeros > (CPU_WORD_SIZE_32 * DEF_OCTET_NBR_BITS)) {
  1700. * nbr_trail_zeros -= (CPU_CFG_DATA_SIZE - CPU_WORD_SIZE_32) * DEF_OCTET_NBR_BITS;
  1701. * }
  1702. *
  1703. * However, this ONLY occurs for an initial 'val' of '0' since all non-'0' 32-bit
  1704. * values would return a number of trailing zeros less than or equal to 32 bits.
  1705. *
  1706. * Therefore, initially validating all non-'0' values prior to calling assembly-
  1707. * optimized CPU_CntTrailZeros() avoids having to offset the number of returned
  1708. * trailing zeros by the difference in CPU data size and 32-bit data value bits.
  1709. *
  1710. * (b) For CPU_CntTrailZeros32()'s C implementation, the final conditional statement
  1711. * calculates 'val's number of trailing zeros based on CPU_CntTrailZeros32()'s
  1712. * 32-bit return data size & 'val's calculated number of lead zeros ONLY if the
  1713. * initial 'val' is non-'0' :
  1714. *
  1715. * if (val != 0u) {
  1716. * nbr_trail_zeros = ((CPU_WORD_SIZE_32 * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1717. * } else {
  1718. * nbr_trail_zeros = nbr_lead_zeros;
  1719. * }
  1720. *
  1721. * Therefore, initially validating all non-'0' values avoids having to conditionally
  1722. * execute the final 'if' statement.
  1723. *********************************************************************************************************
  1724. */
  1725. #if (CPU_CFG_DATA_SIZE_MAX >= CPU_WORD_SIZE_32)
  1726. CPU_DATA CPU_CntTrailZeros32 (CPU_INT32U val)
  1727. {
  1728. #if (!((defined(CPU_CFG_TRAIL_ZEROS_ASM_PRESENT)) && \
  1729. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_32)))
  1730. CPU_INT32U val_bit_mask;
  1731. CPU_DATA nbr_lead_zeros;
  1732. #endif
  1733. CPU_DATA nbr_trail_zeros;
  1734. if (val == 0u) { /* Rtn ALL val bits as zero'd (see Note #3). */
  1735. return (CPU_WORD_SIZE_32 * DEF_OCTET_NBR_BITS);
  1736. }
  1737. /* ------------------ ASM-OPTIMIZED ------------------- */
  1738. #if ((defined(CPU_CFG_TRAIL_ZEROS_ASM_PRESENT)) && \
  1739. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_32))
  1740. nbr_trail_zeros = CPU_CntTrailZeros((CPU_DATA)val);
  1741. #else /* ------------------- C-OPTIMIZED -------------------- */
  1742. val_bit_mask = val & ((CPU_INT32U)~val + 1u); /* Zero/clr all bits EXCEPT least-sig set bit. */
  1743. nbr_lead_zeros = CPU_CntLeadZeros32(val_bit_mask); /* Cnt nbr lead 0s. */
  1744. /* Calc nbr trail 0s = (nbr val bits - 1) - nbr lead 0s.*/
  1745. nbr_trail_zeros = ((CPU_WORD_SIZE_32 * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1746. #endif
  1747. return (nbr_trail_zeros);
  1748. }
  1749. #endif
  1750. /*
  1751. *********************************************************************************************************
  1752. * CPU_CntTrailZeros64()
  1753. *
  1754. * Description : Count the number of contiguous, least-significant, trailing zero bits in a 64-bit data value.
  1755. *
  1756. * Argument(s) : val Data value to count trailing zero bits.
  1757. *
  1758. * Return(s) : Number of contiguous, least-significant, trailing zero bits in 'val'.
  1759. *
  1760. * Caller(s) : Application.
  1761. *
  1762. * This function is a CPU module application programming interface (API) function & MAY be
  1763. * called by application function(s).
  1764. *
  1765. * Note(s) : (1) Supports 64-bit values :
  1766. *
  1767. * b63 b62 b61 b60 b59 ... b02 b01 b00 # Trailing Zeros
  1768. * --- --- --- --- --- --- --- --- ----------------
  1769. * x x x x x x x 1 0
  1770. * x x x x x x 1 0 1
  1771. * x x x x x 1 0 0 2
  1772. * : : : : : : : : :
  1773. * : : : : : : : : :
  1774. * x x x x 1 0 0 0 59
  1775. * x x x 1 0 0 0 0 60
  1776. * x x 1 0 0 0 0 0 61
  1777. * x 1 0 0 0 0 0 0 62
  1778. * 1 0 0 0 0 0 0 0 63
  1779. * 0 0 0 0 0 0 0 0 64
  1780. *
  1781. *
  1782. * (2) For non-zero values, the returned number of contiguous, least-significant, trailing
  1783. * zero bits is also equivalent to the bit position of the least-significant set bit.
  1784. *
  1785. * (3) 'val' SHOULD be validated for non-'0' PRIOR to all other counting zero calculations :
  1786. *
  1787. * (a) For assembly-optimized implementations, CPU_CntTrailZeros() returns 'val's
  1788. * number of trailing zeros via CPU's native data size, 'CPU_CFG_DATA_SIZE'.
  1789. * If the returned number of zeros exceeds CPU_CntTrailZeros64()'s 64-bit return
  1790. * data size, then the returned number of zeros must be offset by the difference
  1791. * between CPU_CntTrailZeros()'s & CPU_CntTrailZeros64()'s return data size :
  1792. *
  1793. * nbr_trail_zeros = CPU_CntTrailZeros((CPU_DATA)val);
  1794. * if (nbr_trail_zeros > (CPU_WORD_SIZE_64 * DEF_OCTET_NBR_BITS)) {
  1795. * nbr_trail_zeros -= (CPU_CFG_DATA_SIZE - CPU_WORD_SIZE_64) * DEF_OCTET_NBR_BITS;
  1796. * }
  1797. *
  1798. * However, this ONLY occurs for an initial 'val' of '0' since all non-'0' 64-bit
  1799. * values would return a number of trailing zeros less than or equal to 64 bits.
  1800. *
  1801. * Therefore, initially validating all non-'0' values prior to calling assembly-
  1802. * optimized CPU_CntTrailZeros() avoids having to offset the number of returned
  1803. * trailing zeros by the difference in CPU data size and 64-bit data value bits.
  1804. *
  1805. * (b) For CPU_CntTrailZeros64()'s C implementation, the final conditional statement
  1806. * calculates 'val's number of trailing zeros based on CPU_CntTrailZeros64()'s
  1807. * 64-bit return data size & 'val's calculated number of lead zeros ONLY if the
  1808. * initial 'val' is non-'0' :
  1809. *
  1810. * if (val != 0u) {
  1811. * nbr_trail_zeros = ((CPU_WORD_SIZE_64 * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1812. * } else {
  1813. * nbr_trail_zeros = nbr_lead_zeros;
  1814. * }
  1815. *
  1816. * Therefore, initially validating all non-'0' values avoids having to conditionally
  1817. * execute the final 'if' statement.
  1818. *********************************************************************************************************
  1819. */
  1820. #if (CPU_CFG_DATA_SIZE_MAX >= CPU_WORD_SIZE_64)
  1821. CPU_DATA CPU_CntTrailZeros64 (CPU_INT64U val)
  1822. {
  1823. #if (!((defined(CPU_CFG_TRAIL_ZEROS_ASM_PRESENT)) && \
  1824. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_64)))
  1825. CPU_INT64U val_bit_mask;
  1826. CPU_DATA nbr_lead_zeros;
  1827. #endif
  1828. CPU_DATA nbr_trail_zeros;
  1829. if (val == 0u) { /* Rtn ALL val bits as zero'd (see Note #3). */
  1830. return (CPU_WORD_SIZE_64 * DEF_OCTET_NBR_BITS);
  1831. }
  1832. /* ------------------ ASM-OPTIMIZED ------------------- */
  1833. #if ((defined(CPU_CFG_TRAIL_ZEROS_ASM_PRESENT)) && \
  1834. (CPU_CFG_DATA_SIZE >= CPU_WORD_SIZE_64))
  1835. nbr_trail_zeros = CPU_CntTrailZeros((CPU_DATA)val);
  1836. #else /* ------------------- C-OPTIMIZED -------------------- */
  1837. val_bit_mask = val & ((CPU_INT64U)~val + 1u); /* Zero/clr all bits EXCEPT least-sig set bit. */
  1838. nbr_lead_zeros = CPU_CntLeadZeros64(val_bit_mask); /* Cnt nbr lead 0s. */
  1839. /* Calc nbr trail 0s = (nbr val bits - 1) - nbr lead 0s.*/
  1840. nbr_trail_zeros = ((CPU_WORD_SIZE_64 * DEF_OCTET_NBR_BITS) - 1u) - nbr_lead_zeros;
  1841. #endif
  1842. return (nbr_trail_zeros);
  1843. }
  1844. #endif
  1845. /*
  1846. *********************************************************************************************************
  1847. * CRCUtil_PopCnt_32()
  1848. *
  1849. * Description : Compute population count (hamming weight) for value (number of bits set).
  1850. *
  1851. * Argument(s) : value Value to compute population count on.
  1852. *
  1853. *
  1854. * Return(s) : value's population count.
  1855. *
  1856. * Caller(s) : various.
  1857. *
  1858. * Note(s) : (1) Algorithm taken from http://en.wikipedia.org/wiki/Hamming_weight
  1859. *********************************************************************************************************
  1860. */
  1861. CPU_INT08U CPU_PopCnt32 (CPU_INT32U value)
  1862. {
  1863. CPU_INT32U even_cnt;
  1864. CPU_INT32U odd_cnt;
  1865. CPU_INT32U result;
  1866. odd_cnt = (value >> 1u) & CRC_UTIL_POPCNT_MASK01010101_32; /* 2-bits pieces. */
  1867. result = value - odd_cnt; /* Same result as result=odd_cnt+(value & 0x55555555). */
  1868. even_cnt = result & CRC_UTIL_POPCNT_MASK00110011_32; /* 4-bits pieces. */
  1869. odd_cnt = (result >> 2u) & CRC_UTIL_POPCNT_MASK00110011_32;
  1870. result = even_cnt + odd_cnt;
  1871. even_cnt = result & CRC_UTIL_POPCNT_MASK00001111_32; /* 8-bits pieces. */
  1872. odd_cnt = (result >> 4u) & CRC_UTIL_POPCNT_MASK00001111_32;
  1873. result = even_cnt + odd_cnt;
  1874. result = (result * CRC_UTIL_POPCNT_POWERSOF256_32) >> 24u;
  1875. return (result);
  1876. }
  1877. /*
  1878. *********************************************************************************************************
  1879. *********************************************************************************************************
  1880. * LOCAL FUNCTIONS
  1881. *********************************************************************************************************
  1882. *********************************************************************************************************
  1883. */
  1884. /*
  1885. *********************************************************************************************************
  1886. * CPU_NameInit()
  1887. *
  1888. * Description : Initialize CPU Name.
  1889. *
  1890. * Argument(s) : none.
  1891. *
  1892. * Return(s) : none.
  1893. *
  1894. * Caller(s) : CPU_Init().
  1895. *
  1896. * Note(s) : none.
  1897. *********************************************************************************************************
  1898. */
  1899. #if (CPU_CFG_NAME_EN == DEF_ENABLED)
  1900. static void CPU_NameInit (void)
  1901. {
  1902. CPU_NameClr();
  1903. }
  1904. #endif
  1905. /*
  1906. *********************************************************************************************************
  1907. * CPU_TS_Init()
  1908. *
  1909. * Description : (1) Initialize CPU timestamp :
  1910. *
  1911. * (a) Initialize/start CPU timestamp timer See Note #1
  1912. * (b) Initialize CPU timestamp controls
  1913. *
  1914. *
  1915. * Argument(s) : none.
  1916. *
  1917. * Return(s) : none.
  1918. *
  1919. * Caller(s) : CPU_Init().
  1920. *
  1921. * Note(s) : (1) The following initialization MUST be sequenced as follows :
  1922. *
  1923. * (a) CPU_TS_TmrFreq_Hz MUST be initialized prior to CPU_TS_TmrInit()
  1924. * (b) CPU_TS_TmrInit() SHOULD precede calls to all other CPU timestamp functions;
  1925. * otherwise, invalid time measurements may be calculated/
  1926. * returned.
  1927. *
  1928. * See also 'CPU_Init() Note #3a'.
  1929. *********************************************************************************************************
  1930. */
  1931. #if ((CPU_CFG_TS_EN == DEF_ENABLED) || \
  1932. (CPU_CFG_TS_TMR_EN == DEF_ENABLED))
  1933. static void CPU_TS_Init (void)
  1934. {
  1935. #if (((CPU_CFG_TS_32_EN == DEF_ENABLED ) && \
  1936. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32)) || \
  1937. ((CPU_CFG_TS_64_EN == DEF_ENABLED ) && \
  1938. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64)))
  1939. CPU_TS_TMR ts_tmr_cnts;
  1940. #endif
  1941. /* ----------------- INIT CPU TS TMR ------------------ */
  1942. #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
  1943. CPU_TS_TmrFreq_Hz = 0u; /* Init/clr ts tmr freq (see Note #1a). */
  1944. CPU_TS_TmrInit(); /* Init & start ts tmr (see Note #1b). */
  1945. #endif
  1946. /* ------------------- INIT CPU TS -------------------- */
  1947. #if (((CPU_CFG_TS_32_EN == DEF_ENABLED ) && \
  1948. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32)) || \
  1949. ((CPU_CFG_TS_64_EN == DEF_ENABLED ) && \
  1950. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64)))
  1951. ts_tmr_cnts = CPU_TS_TmrRd(); /* Get init ts tmr val (in ts tmr cnts). */
  1952. #endif
  1953. #if ((CPU_CFG_TS_32_EN == DEF_ENABLED) && \
  1954. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_32))
  1955. CPU_TS_32_Accum = 0u; /* Init 32-bit accum'd ts. */
  1956. CPU_TS_32_TmrPrev = ts_tmr_cnts; /* Init 32-bit ts prev tmr val. */
  1957. #endif
  1958. #if ((CPU_CFG_TS_64_EN == DEF_ENABLED) && \
  1959. (CPU_CFG_TS_TMR_SIZE < CPU_WORD_SIZE_64))
  1960. CPU_TS_64_Accum = 0u; /* Init 64-bit accum'd ts. */
  1961. CPU_TS_64_TmrPrev = ts_tmr_cnts; /* Init 64-bit ts prev tmr val. */
  1962. #endif
  1963. }
  1964. #endif
  1965. /*
  1966. *********************************************************************************************************
  1967. * CPU_IntDisMeasInit()
  1968. *
  1969. * Description : (1) Initialize interrupts disabled time measurements feature :
  1970. *
  1971. * (a) Initialize interrupts disabled time measurement controls
  1972. * (b) Calculate interrupts disabled time measurement overhead
  1973. *
  1974. *
  1975. * Argument(s) : none.
  1976. *
  1977. * Return(s) : none.
  1978. *
  1979. * Caller(s) : CPU_Init().
  1980. *
  1981. * Note(s) : (2) CPU_IntDisMeasInit() SHOULD precede ALL calls to CPU_CRITICAL_ENTER()/CPU_CRITICAL_EXIT()
  1982. * & other CPU interrupts disabled time measurement functions; otherwise, invalid interrupts
  1983. * disabled time measurements may be calculated/returned.
  1984. *
  1985. * See also 'CPU_Init() Note #3b'.
  1986. *
  1987. * (3) (a) (1) Interrupts disabled time measurement overhead performed multiple times to calculate
  1988. * a rounded average with better accuracy, hopefully of +/- one timer count.
  1989. *
  1990. * (2) However, a single overhead time measurement is recommended, even for instruction-
  1991. * cache-enabled CPUs, since critical sections are NOT typically called within
  1992. * instruction-cached loops. Thus a single non-cached/non-averaged time measurement
  1993. * is a more realistic overhead for the majority of non-cached interrupts disabled
  1994. * time measurements.
  1995. *
  1996. * (b) Interrupts MUST be disabled while measuring the interrupts disabled time measurement
  1997. * overhead; otherwise, overhead measurements could be interrupted which would incorrectly
  1998. * calculate an inflated overhead time which would then incorrectly calculate deflated
  1999. * interrupts disabled times.
  2000. *********************************************************************************************************
  2001. */
  2002. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  2003. static void CPU_IntDisMeasInit (void)
  2004. {
  2005. CPU_TS_TMR time_meas_tot_cnts;
  2006. CPU_INT16U i;
  2007. CPU_SR_ALLOC();
  2008. /* ----------- INIT INT DIS TIME MEAS CTRLS ----------- */
  2009. CPU_IntDisMeasCtr = 0u;
  2010. CPU_IntDisNestCtr = 0u;
  2011. CPU_IntDisMeasStart_cnts = 0u;
  2012. CPU_IntDisMeasStop_cnts = 0u;
  2013. CPU_IntDisMeasMaxCur_cnts = 0u;
  2014. CPU_IntDisMeasMax_cnts = 0u;
  2015. CPU_IntDisMeasOvrhd_cnts = 0u;
  2016. /* ----------- CALC INT DIS TIME MEAS OVRHD ----------- */
  2017. time_meas_tot_cnts = 0u;
  2018. CPU_INT_DIS(); /* Ints MUST be dis'd for ovrhd calc (see Note #3b). */
  2019. for (i = 0u; i < CPU_CFG_INT_DIS_MEAS_OVRHD_NBR; i++) {
  2020. CPU_IntDisMeasMaxCur_cnts = 0u;
  2021. CPU_IntDisMeasStart(); /* Perform multiple consecutive start/stop time meas's */
  2022. CPU_IntDisMeasStop();
  2023. time_meas_tot_cnts += CPU_IntDisMeasMaxCur_cnts; /* ... & sum time meas max's ... */
  2024. }
  2025. /* ... to calc avg time meas ovrhd (see Note #3a). */
  2026. CPU_IntDisMeasOvrhd_cnts = (time_meas_tot_cnts + (CPU_CFG_INT_DIS_MEAS_OVRHD_NBR / 2u))
  2027. / CPU_CFG_INT_DIS_MEAS_OVRHD_NBR;
  2028. CPU_IntDisMeasMaxCur_cnts = 0u; /* Reset max ints dis'd times. */
  2029. CPU_IntDisMeasMax_cnts = 0u;
  2030. CPU_INT_EN();
  2031. }
  2032. #endif
  2033. /*
  2034. *********************************************************************************************************
  2035. * CPU_IntDisMeasMaxCalc()
  2036. *
  2037. * Description : Calculate maximum interrupts disabled time.
  2038. *
  2039. * Argument(s) : time_tot_cnts Total interrupt disabled time, in timer counts.
  2040. *
  2041. * Return(s) : Maximum interrupts disabled time (in CPU timestamp timer counts).
  2042. *
  2043. * Caller(s) : CPU_IntDisMeasMaxCurGet(),
  2044. * CPU_IntDisMeasMaxGet().
  2045. *
  2046. * Note(s) : (1) (a) The total amount of time interrupts are disabled by system &/or application code
  2047. * during critical sections is calculated by the following equations :
  2048. *
  2049. * (1) time = [ time - time ] - time
  2050. * interrupts [ stop start ] total meas
  2051. * disabled [ meas meas ] ovrhd
  2052. * (via application)
  2053. *
  2054. *
  2055. * (2) time = time + time
  2056. * total meas start meas stop meas
  2057. * ovrhd ovrhd ovrhd
  2058. *
  2059. *
  2060. * where
  2061. *
  2062. * time time interrupts are disabled between
  2063. * interrupts first critical section enter &
  2064. * disabled last critical section exit minus
  2065. * (via application) time measurement overhead
  2066. *
  2067. * time time of disable interrupts start time
  2068. * start measurement (in timer counts)
  2069. * meas
  2070. *
  2071. * time time of disable interrupts stop time
  2072. * stop measurement (in timer counts)
  2073. * meas
  2074. *
  2075. * time total overhead time to start/stop disabled
  2076. * total meas interrupts time measurements (in timer
  2077. * ovrhd counts)
  2078. *
  2079. * time total overhead time after getting start
  2080. * start meas time until end of start measurement
  2081. * ovrhd function (in timer counts)
  2082. *
  2083. * time total overhead time from beginning of stop
  2084. * stop meas measurement function until after getting
  2085. * ovrhd stop time (in timer counts)
  2086. *
  2087. *
  2088. * (b) To expedite & reduce interrupts disabled time measurement overhead, the final
  2089. * calculations to subtract the interrupts disabled time measurement overhead is
  2090. * performed asynchronously in API functions.
  2091. *
  2092. * See also 'CPU_IntDisMeasStop() Note #1b2'.
  2093. *
  2094. * (c) The amount of time interrupts are disabled is calculated by either of the
  2095. * following equations :
  2096. *
  2097. * (1) Interrupts disabled time = Number timer counts * Timer period
  2098. *
  2099. * where
  2100. *
  2101. * Number timer counts Number of timer counts measured
  2102. * Timer period Timer's period in some units of
  2103. * (fractional) seconds
  2104. * Interrupts disabled time Amount of time interrupts are
  2105. * disabled, in same units of
  2106. * (fractional) seconds as the
  2107. * Timer period
  2108. *
  2109. * Number timer counts
  2110. * (2) Interrupts disabled time = ---------------------
  2111. * Timer frequency
  2112. *
  2113. * where
  2114. *
  2115. * Number timer counts Number of timer counts measured
  2116. * Timer frequency Timer's frequency in some units
  2117. * of counts per second
  2118. * Interrupts disabled time Amount of time interrupts are
  2119. * disabled, in seconds
  2120. *
  2121. * See also 'cpu_core.h FUNCTION PROTOTYPES CPU_TS_TmrRd() Note #2c'
  2122. * & 'cpu_core.h FUNCTION PROTOTYPES CPU_TSxx_to_uSec() Note #2'.
  2123. *
  2124. * (2) Although it is not typical, it is possible for an interrupts disabled time
  2125. * measurement to be less than the interrupts disabled time measurement overhead;
  2126. * especially if the overhead was calculated with a single, non-cached measurement
  2127. * & critical sections are called within instruction-cached loops.
  2128. *********************************************************************************************************
  2129. */
  2130. #ifdef CPU_CFG_INT_DIS_MEAS_EN
  2131. static CPU_TS_TMR CPU_IntDisMeasMaxCalc (CPU_TS_TMR time_tot_cnts)
  2132. {
  2133. CPU_TS_TMR time_max_cnts;
  2134. time_max_cnts = time_tot_cnts;
  2135. if (time_max_cnts > CPU_IntDisMeasOvrhd_cnts) { /* If max ints dis'd time > ovrhd time, ... */
  2136. time_max_cnts -= CPU_IntDisMeasOvrhd_cnts; /* ... adj max ints dis'd time by ovrhd time; ... */
  2137. } else { /* ... else max ints dis'd time < ovrhd time, ... */
  2138. time_max_cnts = 0u; /* ... clr max ints dis'd time (see Note #2). */
  2139. }
  2140. return (time_max_cnts);
  2141. }
  2142. #endif