Instructions.cs 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. // https://github.com/LDj3SNuD/ARM_v8-A_AArch64_Instructions_Tester/blob/master/Tester/Instructions.cs
  2. // https://meriac.github.io/archex/A64_v83A_ISA/index.xml
  3. // https://meriac.github.io/archex/A64_v83A_ISA/fpsimdindex.xml
  4. using System.Numerics;
  5. namespace Ryujinx.Tests.Cpu.Tester
  6. {
  7. using Types;
  8. using static AArch64;
  9. using static Shared;
  10. internal static class Base
  11. {
  12. #region "Alu"
  13. // https://meriac.github.io/archex/A64_v83A_ISA/cls_int.xml
  14. public static void Cls(bool sf, Bits Rn, Bits Rd)
  15. {
  16. /* Decode */
  17. int d = (int)UInt(Rd);
  18. int n = (int)UInt(Rn);
  19. int datasize = (sf ? 64 : 32);
  20. /* Operation */
  21. Bits operand1 = X(datasize, n);
  22. BigInteger result = (BigInteger)CountLeadingSignBits(operand1);
  23. X(d, result.SubBigInteger(datasize - 1, 0));
  24. }
  25. // https://meriac.github.io/archex/A64_v83A_ISA/clz_int.xml
  26. public static void Clz(bool sf, Bits Rn, Bits Rd)
  27. {
  28. /* Decode */
  29. int d = (int)UInt(Rd);
  30. int n = (int)UInt(Rn);
  31. int datasize = (sf ? 64 : 32);
  32. /* Operation */
  33. Bits operand1 = X(datasize, n);
  34. BigInteger result = (BigInteger)CountLeadingZeroBits(operand1);
  35. X(d, result.SubBigInteger(datasize - 1, 0));
  36. }
  37. // https://meriac.github.io/archex/A64_v83A_ISA/rbit_int.xml
  38. public static void Rbit(bool sf, Bits Rn, Bits Rd)
  39. {
  40. /* Decode */
  41. int d = (int)UInt(Rd);
  42. int n = (int)UInt(Rn);
  43. int datasize = (sf ? 64 : 32);
  44. /* Operation */
  45. Bits result = new Bits(datasize);
  46. Bits operand = X(datasize, n);
  47. for (int i = 0; i <= datasize - 1; i++)
  48. {
  49. result[datasize - 1 - i] = operand[i];
  50. }
  51. X(d, result);
  52. }
  53. // https://meriac.github.io/archex/A64_v83A_ISA/rev16_int.xml
  54. public static void Rev16(bool sf, Bits Rn, Bits Rd)
  55. {
  56. /* Bits opc = "01"; */
  57. /* Decode */
  58. int d = (int)UInt(Rd);
  59. int n = (int)UInt(Rn);
  60. int datasize = (sf ? 64 : 32);
  61. int container_size = 16;
  62. /* Operation */
  63. Bits result = new Bits(datasize);
  64. Bits operand = X(datasize, n);
  65. int containers = datasize / container_size;
  66. int elements_per_container = container_size / 8;
  67. int index = 0;
  68. int rev_index;
  69. for (int c = 0; c <= containers - 1; c++)
  70. {
  71. rev_index = index + ((elements_per_container - 1) * 8);
  72. for (int e = 0; e <= elements_per_container - 1; e++)
  73. {
  74. result[rev_index + 7, rev_index] = operand[index + 7, index];
  75. index = index + 8;
  76. rev_index = rev_index - 8;
  77. }
  78. }
  79. X(d, result);
  80. }
  81. // https://meriac.github.io/archex/A64_v83A_ISA/rev32_int.xml
  82. // (https://meriac.github.io/archex/A64_v83A_ISA/rev.xml)
  83. public static void Rev32(bool sf, Bits Rn, Bits Rd)
  84. {
  85. /* Bits opc = "10"; */
  86. /* Decode */
  87. int d = (int)UInt(Rd);
  88. int n = (int)UInt(Rn);
  89. int datasize = (sf ? 64 : 32);
  90. int container_size = 32;
  91. /* Operation */
  92. Bits result = new Bits(datasize);
  93. Bits operand = X(datasize, n);
  94. int containers = datasize / container_size;
  95. int elements_per_container = container_size / 8;
  96. int index = 0;
  97. int rev_index;
  98. for (int c = 0; c <= containers - 1; c++)
  99. {
  100. rev_index = index + ((elements_per_container - 1) * 8);
  101. for (int e = 0; e <= elements_per_container - 1; e++)
  102. {
  103. result[rev_index + 7, rev_index] = operand[index + 7, index];
  104. index = index + 8;
  105. rev_index = rev_index - 8;
  106. }
  107. }
  108. X(d, result);
  109. }
  110. // https://meriac.github.io/archex/A64_v83A_ISA/rev64_rev.xml
  111. // (https://meriac.github.io/archex/A64_v83A_ISA/rev.xml)
  112. public static void Rev64(Bits Rn, Bits Rd)
  113. {
  114. /* Bits opc = "11"; */
  115. /* Decode */
  116. int d = (int)UInt(Rd);
  117. int n = (int)UInt(Rn);
  118. int container_size = 64;
  119. /* Operation */
  120. Bits result = new Bits(64);
  121. Bits operand = X(64, n);
  122. int containers = 64 / container_size;
  123. int elements_per_container = container_size / 8;
  124. int index = 0;
  125. int rev_index;
  126. for (int c = 0; c <= containers - 1; c++)
  127. {
  128. rev_index = index + ((elements_per_container - 1) * 8);
  129. for (int e = 0; e <= elements_per_container - 1; e++)
  130. {
  131. result[rev_index + 7, rev_index] = operand[index + 7, index];
  132. index = index + 8;
  133. rev_index = rev_index - 8;
  134. }
  135. }
  136. X(d, result);
  137. }
  138. #endregion
  139. #region "AluImm"
  140. // https://meriac.github.io/archex/A64_v83A_ISA/add_addsub_imm.xml
  141. public static void Add_Imm(bool sf, Bits shift, Bits imm12, Bits Rn, Bits Rd)
  142. {
  143. /* Decode */
  144. int d = (int)UInt(Rd);
  145. int n = (int)UInt(Rn);
  146. int datasize = (sf ? 64 : 32);
  147. Bits imm;
  148. switch (shift)
  149. {
  150. default:
  151. case Bits bits when bits == "00":
  152. imm = ZeroExtend(imm12, datasize);
  153. break;
  154. case Bits bits when bits == "01":
  155. imm = ZeroExtend(Bits.Concat(imm12, Zeros(12)), datasize);
  156. break;
  157. /* when '1x' ReservedValue(); */
  158. }
  159. /* Operation */
  160. Bits result;
  161. Bits operand1 = (n == 31 ? SP(datasize) : X(datasize, n));
  162. (result, _) = AddWithCarry(datasize, operand1, imm, false);
  163. if (d == 31)
  164. {
  165. SP(result);
  166. }
  167. else
  168. {
  169. X(d, result);
  170. }
  171. }
  172. // https://meriac.github.io/archex/A64_v83A_ISA/adds_addsub_imm.xml
  173. public static void Adds_Imm(bool sf, Bits shift, Bits imm12, Bits Rn, Bits Rd)
  174. {
  175. /* Decode */
  176. int d = (int)UInt(Rd);
  177. int n = (int)UInt(Rn);
  178. int datasize = (sf ? 64 : 32);
  179. Bits imm;
  180. switch (shift)
  181. {
  182. default:
  183. case Bits bits when bits == "00":
  184. imm = ZeroExtend(imm12, datasize);
  185. break;
  186. case Bits bits when bits == "01":
  187. imm = ZeroExtend(Bits.Concat(imm12, Zeros(12)), datasize);
  188. break;
  189. /* when '1x' ReservedValue(); */
  190. }
  191. /* Operation */
  192. Bits result;
  193. Bits operand1 = (n == 31 ? SP(datasize) : X(datasize, n));
  194. Bits nzcv;
  195. (result, nzcv) = AddWithCarry(datasize, operand1, imm, false);
  196. PSTATE.NZCV(nzcv);
  197. X(d, result);
  198. }
  199. // https://meriac.github.io/archex/A64_v83A_ISA/and_log_imm.xml
  200. public static void And_Imm(bool sf, bool N, Bits immr, Bits imms, Bits Rn, Bits Rd)
  201. {
  202. /* Decode */
  203. int d = (int)UInt(Rd);
  204. int n = (int)UInt(Rn);
  205. int datasize = (sf ? 64 : 32);
  206. Bits imm;
  207. /* if sf == '0' && N != '0' then ReservedValue(); */
  208. (imm, _) = DecodeBitMasks(datasize, N, imms, immr, true);
  209. /* Operation */
  210. Bits operand1 = X(datasize, n);
  211. Bits result = AND(operand1, imm);
  212. if (d == 31)
  213. {
  214. SP(result);
  215. }
  216. else
  217. {
  218. X(d, result);
  219. }
  220. }
  221. // https://meriac.github.io/archex/A64_v83A_ISA/ands_log_imm.xml
  222. public static void Ands_Imm(bool sf, bool N, Bits immr, Bits imms, Bits Rn, Bits Rd)
  223. {
  224. /* Decode */
  225. int d = (int)UInt(Rd);
  226. int n = (int)UInt(Rn);
  227. int datasize = (sf ? 64 : 32);
  228. Bits imm;
  229. /* if sf == '0' && N != '0' then ReservedValue(); */
  230. (imm, _) = DecodeBitMasks(datasize, N, imms, immr, true);
  231. /* Operation */
  232. Bits operand1 = X(datasize, n);
  233. Bits result = AND(operand1, imm);
  234. PSTATE.NZCV(result[datasize - 1], IsZeroBit(result), false, false);
  235. X(d, result);
  236. }
  237. // https://meriac.github.io/archex/A64_v83A_ISA/eor_log_imm.xml
  238. public static void Eor_Imm(bool sf, bool N, Bits immr, Bits imms, Bits Rn, Bits Rd)
  239. {
  240. /* Decode */
  241. int d = (int)UInt(Rd);
  242. int n = (int)UInt(Rn);
  243. int datasize = (sf ? 64 : 32);
  244. Bits imm;
  245. /* if sf == '0' && N != '0' then ReservedValue(); */
  246. (imm, _) = DecodeBitMasks(datasize, N, imms, immr, true);
  247. /* Operation */
  248. Bits operand1 = X(datasize, n);
  249. Bits result = EOR(operand1, imm);
  250. if (d == 31)
  251. {
  252. SP(result);
  253. }
  254. else
  255. {
  256. X(d, result);
  257. }
  258. }
  259. // https://meriac.github.io/archex/A64_v83A_ISA/orr_log_imm.xml
  260. public static void Orr_Imm(bool sf, bool N, Bits immr, Bits imms, Bits Rn, Bits Rd)
  261. {
  262. /* Decode */
  263. int d = (int)UInt(Rd);
  264. int n = (int)UInt(Rn);
  265. int datasize = (sf ? 64 : 32);
  266. Bits imm;
  267. /* if sf == '0' && N != '0' then ReservedValue(); */
  268. (imm, _) = DecodeBitMasks(datasize, N, imms, immr, true);
  269. /* Operation */
  270. Bits operand1 = X(datasize, n);
  271. Bits result = OR(operand1, imm);
  272. if (d == 31)
  273. {
  274. SP(result);
  275. }
  276. else
  277. {
  278. X(d, result);
  279. }
  280. }
  281. // https://meriac.github.io/archex/A64_v83A_ISA/sub_addsub_imm.xml
  282. public static void Sub_Imm(bool sf, Bits shift, Bits imm12, Bits Rn, Bits Rd)
  283. {
  284. /* Decode */
  285. int d = (int)UInt(Rd);
  286. int n = (int)UInt(Rn);
  287. int datasize = (sf ? 64 : 32);
  288. Bits imm;
  289. switch (shift)
  290. {
  291. default:
  292. case Bits bits when bits == "00":
  293. imm = ZeroExtend(imm12, datasize);
  294. break;
  295. case Bits bits when bits == "01":
  296. imm = ZeroExtend(Bits.Concat(imm12, Zeros(12)), datasize);
  297. break;
  298. /* when '1x' ReservedValue(); */
  299. }
  300. /* Operation */
  301. Bits result;
  302. Bits operand1 = (n == 31 ? SP(datasize) : X(datasize, n));
  303. Bits operand2 = NOT(imm);
  304. (result, _) = AddWithCarry(datasize, operand1, operand2, true);
  305. if (d == 31)
  306. {
  307. SP(result);
  308. }
  309. else
  310. {
  311. X(d, result);
  312. }
  313. }
  314. // https://meriac.github.io/archex/A64_v83A_ISA/subs_addsub_imm.xml
  315. public static void Subs_Imm(bool sf, Bits shift, Bits imm12, Bits Rn, Bits Rd)
  316. {
  317. /* Decode */
  318. int d = (int)UInt(Rd);
  319. int n = (int)UInt(Rn);
  320. int datasize = (sf ? 64 : 32);
  321. Bits imm;
  322. switch (shift)
  323. {
  324. default:
  325. case Bits bits when bits == "00":
  326. imm = ZeroExtend(imm12, datasize);
  327. break;
  328. case Bits bits when bits == "01":
  329. imm = ZeroExtend(Bits.Concat(imm12, Zeros(12)), datasize);
  330. break;
  331. /* when '1x' ReservedValue(); */
  332. }
  333. /* Operation */
  334. Bits result;
  335. Bits operand1 = (n == 31 ? SP(datasize) : X(datasize, n));
  336. Bits operand2 = NOT(imm);
  337. Bits nzcv;
  338. (result, nzcv) = AddWithCarry(datasize, operand1, operand2, true);
  339. PSTATE.NZCV(nzcv);
  340. X(d, result);
  341. }
  342. #endregion
  343. #region "AluRs"
  344. // https://meriac.github.io/archex/A64_v83A_ISA/adc.xml
  345. public static void Adc(bool sf, Bits Rm, Bits Rn, Bits Rd)
  346. {
  347. /* Decode */
  348. int d = (int)UInt(Rd);
  349. int n = (int)UInt(Rn);
  350. int m = (int)UInt(Rm);
  351. int datasize = (sf ? 64 : 32);
  352. /* Operation */
  353. Bits result;
  354. Bits operand1 = X(datasize, n);
  355. Bits operand2 = X(datasize, m);
  356. (result, _) = AddWithCarry(datasize, operand1, operand2, PSTATE.C);
  357. X(d, result);
  358. }
  359. // https://meriac.github.io/archex/A64_v83A_ISA/adcs.xml
  360. public static void Adcs(bool sf, Bits Rm, Bits Rn, Bits Rd)
  361. {
  362. /* Decode */
  363. int d = (int)UInt(Rd);
  364. int n = (int)UInt(Rn);
  365. int m = (int)UInt(Rm);
  366. int datasize = (sf ? 64 : 32);
  367. /* Operation */
  368. Bits result;
  369. Bits operand1 = X(datasize, n);
  370. Bits operand2 = X(datasize, m);
  371. Bits nzcv;
  372. (result, nzcv) = AddWithCarry(datasize, operand1, operand2, PSTATE.C);
  373. PSTATE.NZCV(nzcv);
  374. X(d, result);
  375. }
  376. // https://meriac.github.io/archex/A64_v83A_ISA/add_addsub_shift.xml
  377. public static void Add_Rs(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  378. {
  379. /* Decode */
  380. int d = (int)UInt(Rd);
  381. int n = (int)UInt(Rn);
  382. int m = (int)UInt(Rm);
  383. int datasize = (sf ? 64 : 32);
  384. /* if shift == '11' then ReservedValue(); */
  385. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  386. ShiftType shift_type = DecodeShift(shift);
  387. int shift_amount = (int)UInt(imm6);
  388. /* Operation */
  389. Bits result;
  390. Bits operand1 = X(datasize, n);
  391. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  392. (result, _) = AddWithCarry(datasize, operand1, operand2, false);
  393. X(d, result);
  394. }
  395. // https://meriac.github.io/archex/A64_v83A_ISA/adds_addsub_shift.xml
  396. public static void Adds_Rs(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  397. {
  398. /* Decode */
  399. int d = (int)UInt(Rd);
  400. int n = (int)UInt(Rn);
  401. int m = (int)UInt(Rm);
  402. int datasize = (sf ? 64 : 32);
  403. /* if shift == '11' then ReservedValue(); */
  404. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  405. ShiftType shift_type = DecodeShift(shift);
  406. int shift_amount = (int)UInt(imm6);
  407. /* Operation */
  408. Bits result;
  409. Bits operand1 = X(datasize, n);
  410. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  411. Bits nzcv;
  412. (result, nzcv) = AddWithCarry(datasize, operand1, operand2, false);
  413. PSTATE.NZCV(nzcv);
  414. X(d, result);
  415. }
  416. // https://meriac.github.io/archex/A64_v83A_ISA/and_log_shift.xml
  417. public static void And_Rs(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  418. {
  419. /* Decode */
  420. int d = (int)UInt(Rd);
  421. int n = (int)UInt(Rn);
  422. int m = (int)UInt(Rm);
  423. int datasize = (sf ? 64 : 32);
  424. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  425. ShiftType shift_type = DecodeShift(shift);
  426. int shift_amount = (int)UInt(imm6);
  427. /* Operation */
  428. Bits operand1 = X(datasize, n);
  429. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  430. Bits result = AND(operand1, operand2);
  431. X(d, result);
  432. }
  433. // https://meriac.github.io/archex/A64_v83A_ISA/ands_log_shift.xml
  434. public static void Ands_Rs(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  435. {
  436. /* Decode */
  437. int d = (int)UInt(Rd);
  438. int n = (int)UInt(Rn);
  439. int m = (int)UInt(Rm);
  440. int datasize = (sf ? 64 : 32);
  441. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  442. ShiftType shift_type = DecodeShift(shift);
  443. int shift_amount = (int)UInt(imm6);
  444. /* Operation */
  445. Bits operand1 = X(datasize, n);
  446. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  447. Bits result = AND(operand1, operand2);
  448. PSTATE.NZCV(result[datasize - 1], IsZeroBit(result), false, false);
  449. X(d, result);
  450. }
  451. // https://meriac.github.io/archex/A64_v83A_ISA/asrv.xml
  452. public static void Asrv(bool sf, Bits Rm, Bits Rn, Bits Rd)
  453. {
  454. Bits op2 = "10";
  455. /* Decode */
  456. int d = (int)UInt(Rd);
  457. int n = (int)UInt(Rn);
  458. int m = (int)UInt(Rm);
  459. int datasize = (sf ? 64 : 32);
  460. ShiftType shift_type = DecodeShift(op2);
  461. /* Operation */
  462. Bits operand2 = X(datasize, m);
  463. Bits result = ShiftReg(datasize, n, shift_type, (int)(UInt(operand2) % datasize)); // BigInteger.Modulus Operator (BigInteger, BigInteger)
  464. X(d, result);
  465. }
  466. // https://meriac.github.io/archex/A64_v83A_ISA/bic_log_shift.xml
  467. public static void Bic(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  468. {
  469. /* Decode */
  470. int d = (int)UInt(Rd);
  471. int n = (int)UInt(Rn);
  472. int m = (int)UInt(Rm);
  473. int datasize = (sf ? 64 : 32);
  474. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  475. ShiftType shift_type = DecodeShift(shift);
  476. int shift_amount = (int)UInt(imm6);
  477. /* Operation */
  478. Bits operand1 = X(datasize, n);
  479. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  480. operand2 = NOT(operand2);
  481. Bits result = AND(operand1, operand2);
  482. X(d, result);
  483. }
  484. // https://meriac.github.io/archex/A64_v83A_ISA/bics.xml
  485. public static void Bics(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  486. {
  487. /* Decode */
  488. int d = (int)UInt(Rd);
  489. int n = (int)UInt(Rn);
  490. int m = (int)UInt(Rm);
  491. int datasize = (sf ? 64 : 32);
  492. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  493. ShiftType shift_type = DecodeShift(shift);
  494. int shift_amount = (int)UInt(imm6);
  495. /* Operation */
  496. Bits operand1 = X(datasize, n);
  497. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  498. operand2 = NOT(operand2);
  499. Bits result = AND(operand1, operand2);
  500. PSTATE.NZCV(result[datasize - 1], IsZeroBit(result), false, false);
  501. X(d, result);
  502. }
  503. // https://meriac.github.io/archex/A64_v83A_ISA/crc32.xml
  504. public static void Crc32(bool sf, Bits Rm, Bits sz, Bits Rn, Bits Rd)
  505. {
  506. /* Decode */
  507. int d = (int)UInt(Rd);
  508. int n = (int)UInt(Rn);
  509. int m = (int)UInt(Rm);
  510. /* if sf == '1' && sz != '11' then UnallocatedEncoding(); */
  511. /* if sf == '0' && sz == '11' then UnallocatedEncoding(); */
  512. int size = 8 << (int)UInt(sz);
  513. /* Operation */
  514. /* if !HaveCRCExt() then UnallocatedEncoding(); */
  515. Bits acc = X(32, n); // accumulator
  516. Bits val = X(size, m); // input value
  517. Bits poly = new Bits(0x04C11DB7u);
  518. Bits tempacc = Bits.Concat(BitReverse(acc), Zeros(size));
  519. Bits tempval = Bits.Concat(BitReverse(val), Zeros(32));
  520. // Poly32Mod2 on a bitstring does a polynomial Modulus over {0,1} operation
  521. X(d, BitReverse(Poly32Mod2(EOR(tempacc, tempval), poly)));
  522. }
  523. // https://meriac.github.io/archex/A64_v83A_ISA/crc32c.xml
  524. public static void Crc32c(bool sf, Bits Rm, Bits sz, Bits Rn, Bits Rd)
  525. {
  526. /* Decode */
  527. int d = (int)UInt(Rd);
  528. int n = (int)UInt(Rn);
  529. int m = (int)UInt(Rm);
  530. /* if sf == '1' && sz != '11' then UnallocatedEncoding(); */
  531. /* if sf == '0' && sz == '11' then UnallocatedEncoding(); */
  532. int size = 8 << (int)UInt(sz);
  533. /* Operation */
  534. /* if !HaveCRCExt() then UnallocatedEncoding(); */
  535. Bits acc = X(32, n); // accumulator
  536. Bits val = X(size, m); // input value
  537. Bits poly = new Bits(0x1EDC6F41u);
  538. Bits tempacc = Bits.Concat(BitReverse(acc), Zeros(size));
  539. Bits tempval = Bits.Concat(BitReverse(val), Zeros(32));
  540. // Poly32Mod2 on a bitstring does a polynomial Modulus over {0,1} operation
  541. X(d, BitReverse(Poly32Mod2(EOR(tempacc, tempval), poly)));
  542. }
  543. // https://meriac.github.io/archex/A64_v83A_ISA/eon.xml
  544. public static void Eon(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  545. {
  546. /* Decode */
  547. int d = (int)UInt(Rd);
  548. int n = (int)UInt(Rn);
  549. int m = (int)UInt(Rm);
  550. int datasize = (sf ? 64 : 32);
  551. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  552. ShiftType shift_type = DecodeShift(shift);
  553. int shift_amount = (int)UInt(imm6);
  554. /* Operation */
  555. Bits operand1 = X(datasize, n);
  556. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  557. operand2 = NOT(operand2);
  558. Bits result = EOR(operand1, operand2);
  559. X(d, result);
  560. }
  561. // https://meriac.github.io/archex/A64_v83A_ISA/eor_log_shift.xml
  562. public static void Eor_Rs(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  563. {
  564. /* Decode */
  565. int d = (int)UInt(Rd);
  566. int n = (int)UInt(Rn);
  567. int m = (int)UInt(Rm);
  568. int datasize = (sf ? 64 : 32);
  569. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  570. ShiftType shift_type = DecodeShift(shift);
  571. int shift_amount = (int)UInt(imm6);
  572. /* Operation */
  573. Bits operand1 = X(datasize, n);
  574. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  575. Bits result = EOR(operand1, operand2);
  576. X(d, result);
  577. }
  578. // https://meriac.github.io/archex/A64_v83A_ISA/extr.xml
  579. public static void Extr(bool sf, bool N, Bits Rm, Bits imms, Bits Rn, Bits Rd)
  580. {
  581. /* Decode */
  582. int d = (int)UInt(Rd);
  583. int n = (int)UInt(Rn);
  584. int m = (int)UInt(Rm);
  585. int datasize = (sf ? 64 : 32);
  586. /* if N != sf then UnallocatedEncoding(); */
  587. /* if sf == '0' && imms<5> == '1' then ReservedValue(); */
  588. int lsb = (int)UInt(imms);
  589. /* Operation */
  590. Bits operand1 = X(datasize, n);
  591. Bits operand2 = X(datasize, m);
  592. Bits concat = Bits.Concat(operand1, operand2);
  593. Bits result = concat[lsb + datasize - 1, lsb];
  594. X(d, result);
  595. }
  596. // https://meriac.github.io/archex/A64_v83A_ISA/lslv.xml
  597. public static void Lslv(bool sf, Bits Rm, Bits Rn, Bits Rd)
  598. {
  599. Bits op2 = "00";
  600. /* Decode */
  601. int d = (int)UInt(Rd);
  602. int n = (int)UInt(Rn);
  603. int m = (int)UInt(Rm);
  604. int datasize = (sf ? 64 : 32);
  605. ShiftType shift_type = DecodeShift(op2);
  606. /* Operation */
  607. Bits operand2 = X(datasize, m);
  608. Bits result = ShiftReg(datasize, n, shift_type, (int)(UInt(operand2) % datasize)); // BigInteger.Modulus Operator (BigInteger, BigInteger)
  609. X(d, result);
  610. }
  611. // https://meriac.github.io/archex/A64_v83A_ISA/lsrv.xml
  612. public static void Lsrv(bool sf, Bits Rm, Bits Rn, Bits Rd)
  613. {
  614. Bits op2 = "01";
  615. /* Decode */
  616. int d = (int)UInt(Rd);
  617. int n = (int)UInt(Rn);
  618. int m = (int)UInt(Rm);
  619. int datasize = (sf ? 64 : 32);
  620. ShiftType shift_type = DecodeShift(op2);
  621. /* Operation */
  622. Bits operand2 = X(datasize, m);
  623. Bits result = ShiftReg(datasize, n, shift_type, (int)(UInt(operand2) % datasize)); // BigInteger.Modulus Operator (BigInteger, BigInteger)
  624. X(d, result);
  625. }
  626. // https://meriac.github.io/archex/A64_v83A_ISA/orn_log_shift.xml
  627. public static void Orn(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  628. {
  629. /* Decode */
  630. int d = (int)UInt(Rd);
  631. int n = (int)UInt(Rn);
  632. int m = (int)UInt(Rm);
  633. int datasize = (sf ? 64 : 32);
  634. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  635. ShiftType shift_type = DecodeShift(shift);
  636. int shift_amount = (int)UInt(imm6);
  637. /* Operation */
  638. Bits operand1 = X(datasize, n);
  639. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  640. operand2 = NOT(operand2);
  641. Bits result = OR(operand1, operand2);
  642. X(d, result);
  643. }
  644. // https://meriac.github.io/archex/A64_v83A_ISA/orr_log_shift.xml
  645. public static void Orr_Rs(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  646. {
  647. /* Decode */
  648. int d = (int)UInt(Rd);
  649. int n = (int)UInt(Rn);
  650. int m = (int)UInt(Rm);
  651. int datasize = (sf ? 64 : 32);
  652. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  653. ShiftType shift_type = DecodeShift(shift);
  654. int shift_amount = (int)UInt(imm6);
  655. /* Operation */
  656. Bits operand1 = X(datasize, n);
  657. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  658. Bits result = OR(operand1, operand2);
  659. X(d, result);
  660. }
  661. // https://meriac.github.io/archex/A64_v83A_ISA/rorv.xml
  662. public static void Rorv(bool sf, Bits Rm, Bits Rn, Bits Rd)
  663. {
  664. Bits op2 = "11";
  665. /* Decode */
  666. int d = (int)UInt(Rd);
  667. int n = (int)UInt(Rn);
  668. int m = (int)UInt(Rm);
  669. int datasize = (sf ? 64 : 32);
  670. ShiftType shift_type = DecodeShift(op2);
  671. /* Operation */
  672. Bits operand2 = X(datasize, m);
  673. Bits result = ShiftReg(datasize, n, shift_type, (int)(UInt(operand2) % datasize)); // BigInteger.Modulus Operator (BigInteger, BigInteger)
  674. X(d, result);
  675. }
  676. // https://meriac.github.io/archex/A64_v83A_ISA/sbc.xml
  677. public static void Sbc(bool sf, Bits Rm, Bits Rn, Bits Rd)
  678. {
  679. /* Decode */
  680. int d = (int)UInt(Rd);
  681. int n = (int)UInt(Rn);
  682. int m = (int)UInt(Rm);
  683. int datasize = (sf ? 64 : 32);
  684. /* Operation */
  685. Bits result;
  686. Bits operand1 = X(datasize, n);
  687. Bits operand2 = X(datasize, m);
  688. operand2 = NOT(operand2);
  689. (result, _) = AddWithCarry(datasize, operand1, operand2, PSTATE.C);
  690. X(d, result);
  691. }
  692. // https://meriac.github.io/archex/A64_v83A_ISA/sbcs.xml
  693. public static void Sbcs(bool sf, Bits Rm, Bits Rn, Bits Rd)
  694. {
  695. /* Decode */
  696. int d = (int)UInt(Rd);
  697. int n = (int)UInt(Rn);
  698. int m = (int)UInt(Rm);
  699. int datasize = (sf ? 64 : 32);
  700. /* Operation */
  701. Bits result;
  702. Bits operand1 = X(datasize, n);
  703. Bits operand2 = X(datasize, m);
  704. Bits nzcv;
  705. operand2 = NOT(operand2);
  706. (result, nzcv) = AddWithCarry(datasize, operand1, operand2, PSTATE.C);
  707. PSTATE.NZCV(nzcv);
  708. X(d, result);
  709. }
  710. // https://meriac.github.io/archex/A64_v83A_ISA/sdiv.xml
  711. public static void Sdiv(bool sf, Bits Rm, Bits Rn, Bits Rd)
  712. {
  713. /* Decode */
  714. int d = (int)UInt(Rd);
  715. int n = (int)UInt(Rn);
  716. int m = (int)UInt(Rm);
  717. int datasize = (sf ? 64 : 32);
  718. /* Operation */
  719. BigInteger result;
  720. Bits operand1 = X(datasize, n);
  721. Bits operand2 = X(datasize, m);
  722. if (IsZero(operand2))
  723. {
  724. result = (BigInteger)0m;
  725. }
  726. else
  727. {
  728. result = RoundTowardsZero(Real(Int(operand1, false)) / Real(Int(operand2, false)));
  729. }
  730. X(d, result.SubBigInteger(datasize - 1, 0));
  731. }
  732. // https://meriac.github.io/archex/A64_v83A_ISA/sub_addsub_shift.xml
  733. public static void Sub_Rs(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  734. {
  735. /* Decode */
  736. int d = (int)UInt(Rd);
  737. int n = (int)UInt(Rn);
  738. int m = (int)UInt(Rm);
  739. int datasize = (sf ? 64 : 32);
  740. /* if shift == '11' then ReservedValue(); */
  741. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  742. ShiftType shift_type = DecodeShift(shift);
  743. int shift_amount = (int)UInt(imm6);
  744. /* Operation */
  745. Bits result;
  746. Bits operand1 = X(datasize, n);
  747. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  748. operand2 = NOT(operand2);
  749. (result, _) = AddWithCarry(datasize, operand1, operand2, true);
  750. X(d, result);
  751. }
  752. // https://meriac.github.io/archex/A64_v83A_ISA/subs_addsub_shift.xml
  753. public static void Subs_Rs(bool sf, Bits shift, Bits Rm, Bits imm6, Bits Rn, Bits Rd)
  754. {
  755. /* Decode */
  756. int d = (int)UInt(Rd);
  757. int n = (int)UInt(Rn);
  758. int m = (int)UInt(Rm);
  759. int datasize = (sf ? 64 : 32);
  760. /* if shift == '11' then ReservedValue(); */
  761. /* if sf == '0' && imm6<5> == '1' then ReservedValue(); */
  762. ShiftType shift_type = DecodeShift(shift);
  763. int shift_amount = (int)UInt(imm6);
  764. /* Operation */
  765. Bits result;
  766. Bits operand1 = X(datasize, n);
  767. Bits operand2 = ShiftReg(datasize, m, shift_type, shift_amount);
  768. Bits nzcv;
  769. operand2 = NOT(operand2);
  770. (result, nzcv) = AddWithCarry(datasize, operand1, operand2, true);
  771. PSTATE.NZCV(nzcv);
  772. X(d, result);
  773. }
  774. // https://meriac.github.io/archex/A64_v83A_ISA/udiv.xml
  775. public static void Udiv(bool sf, Bits Rm, Bits Rn, Bits Rd)
  776. {
  777. /* Decode */
  778. int d = (int)UInt(Rd);
  779. int n = (int)UInt(Rn);
  780. int m = (int)UInt(Rm);
  781. int datasize = (sf ? 64 : 32);
  782. /* Operation */
  783. BigInteger result;
  784. Bits operand1 = X(datasize, n);
  785. Bits operand2 = X(datasize, m);
  786. if (IsZero(operand2))
  787. {
  788. result = (BigInteger)0m;
  789. }
  790. else
  791. {
  792. result = RoundTowardsZero(Real(Int(operand1, true)) / Real(Int(operand2, true)));
  793. }
  794. X(d, result.SubBigInteger(datasize - 1, 0));
  795. }
  796. #endregion
  797. #region "AluRx"
  798. // https://meriac.github.io/archex/A64_v83A_ISA/add_addsub_ext.xml
  799. public static void Add_Rx(bool sf, Bits Rm, Bits option, Bits imm3, Bits Rn, Bits Rd)
  800. {
  801. /* Decode */
  802. int d = (int)UInt(Rd);
  803. int n = (int)UInt(Rn);
  804. int m = (int)UInt(Rm);
  805. int datasize = (sf ? 64 : 32);
  806. ExtendType extend_type = DecodeRegExtend(option);
  807. int shift = (int)UInt(imm3);
  808. /* if shift > 4 then ReservedValue(); */
  809. /* Operation */
  810. Bits result;
  811. Bits operand1 = (n == 31 ? SP(datasize) : X(datasize, n));
  812. Bits operand2 = ExtendReg(datasize, m, extend_type, shift);
  813. (result, _) = AddWithCarry(datasize, operand1, operand2, false);
  814. if (d == 31)
  815. {
  816. SP(result);
  817. }
  818. else
  819. {
  820. X(d, result);
  821. }
  822. }
  823. // https://meriac.github.io/archex/A64_v83A_ISA/adds_addsub_ext.xml
  824. public static void Adds_Rx(bool sf, Bits Rm, Bits option, Bits imm3, Bits Rn, Bits Rd)
  825. {
  826. /* Decode */
  827. int d = (int)UInt(Rd);
  828. int n = (int)UInt(Rn);
  829. int m = (int)UInt(Rm);
  830. int datasize = (sf ? 64 : 32);
  831. ExtendType extend_type = DecodeRegExtend(option);
  832. int shift = (int)UInt(imm3);
  833. /* if shift > 4 then ReservedValue(); */
  834. /* Operation */
  835. Bits result;
  836. Bits operand1 = (n == 31 ? SP(datasize) : X(datasize, n));
  837. Bits operand2 = ExtendReg(datasize, m, extend_type, shift);
  838. Bits nzcv;
  839. (result, nzcv) = AddWithCarry(datasize, operand1, operand2, false);
  840. PSTATE.NZCV(nzcv);
  841. X(d, result);
  842. }
  843. // https://meriac.github.io/archex/A64_v83A_ISA/sub_addsub_ext.xml
  844. public static void Sub_Rx(bool sf, Bits Rm, Bits option, Bits imm3, Bits Rn, Bits Rd)
  845. {
  846. /* Decode */
  847. int d = (int)UInt(Rd);
  848. int n = (int)UInt(Rn);
  849. int m = (int)UInt(Rm);
  850. int datasize = (sf ? 64 : 32);
  851. ExtendType extend_type = DecodeRegExtend(option);
  852. int shift = (int)UInt(imm3);
  853. /* if shift > 4 then ReservedValue(); */
  854. /* Operation */
  855. Bits result;
  856. Bits operand1 = (n == 31 ? SP(datasize) : X(datasize, n));
  857. Bits operand2 = ExtendReg(datasize, m, extend_type, shift);
  858. operand2 = NOT(operand2);
  859. (result, _) = AddWithCarry(datasize, operand1, operand2, true);
  860. if (d == 31)
  861. {
  862. SP(result);
  863. }
  864. else
  865. {
  866. X(d, result);
  867. }
  868. }
  869. // https://meriac.github.io/archex/A64_v83A_ISA/subs_addsub_ext.xml
  870. public static void Subs_Rx(bool sf, Bits Rm, Bits option, Bits imm3, Bits Rn, Bits Rd)
  871. {
  872. /* Decode */
  873. int d = (int)UInt(Rd);
  874. int n = (int)UInt(Rn);
  875. int m = (int)UInt(Rm);
  876. int datasize = (sf ? 64 : 32);
  877. ExtendType extend_type = DecodeRegExtend(option);
  878. int shift = (int)UInt(imm3);
  879. /* if shift > 4 then ReservedValue(); */
  880. /* Operation */
  881. Bits result;
  882. Bits operand1 = (n == 31 ? SP(datasize) : X(datasize, n));
  883. Bits operand2 = ExtendReg(datasize, m, extend_type, shift);
  884. Bits nzcv;
  885. operand2 = NOT(operand2);
  886. (result, nzcv) = AddWithCarry(datasize, operand1, operand2, true);
  887. PSTATE.NZCV(nzcv);
  888. X(d, result);
  889. }
  890. #endregion
  891. #region "Bfm"
  892. // https://meriac.github.io/archex/A64_v83A_ISA/bfm.xml
  893. public static void Bfm(bool sf, bool N, Bits immr, Bits imms, Bits Rn, Bits Rd)
  894. {
  895. /* Decode */
  896. int d = (int)UInt(Rd);
  897. int n = (int)UInt(Rn);
  898. int datasize = (sf ? 64 : 32);
  899. int R;
  900. Bits wmask;
  901. Bits tmask;
  902. /* if sf == '1' && N != '1' then ReservedValue(); */
  903. /* if sf == '0' && (N != '0' || immr<5> != '0' || imms<5> != '0') then ReservedValue(); */
  904. R = (int)UInt(immr);
  905. (wmask, tmask) = DecodeBitMasks(datasize, N, imms, immr, false);
  906. /* Operation */
  907. Bits dst = X(datasize, d);
  908. Bits src = X(datasize, n);
  909. // perform bitfield move on low bits
  910. Bits bot = OR(AND(dst, NOT(wmask)), AND(ROR(src, R), wmask));
  911. // combine extension bits and result bits
  912. X(d, OR(AND(dst, NOT(tmask)), AND(bot, tmask)));
  913. }
  914. // https://meriac.github.io/archex/A64_v83A_ISA/sbfm.xml
  915. public static void Sbfm(bool sf, bool N, Bits immr, Bits imms, Bits Rn, Bits Rd)
  916. {
  917. /* Decode */
  918. int d = (int)UInt(Rd);
  919. int n = (int)UInt(Rn);
  920. int datasize = (sf ? 64 : 32);
  921. int R;
  922. int S;
  923. Bits wmask;
  924. Bits tmask;
  925. /* if sf == '1' && N != '1' then ReservedValue(); */
  926. /* if sf == '0' && (N != '0' || immr<5> != '0' || imms<5> != '0') then ReservedValue(); */
  927. R = (int)UInt(immr);
  928. S = (int)UInt(imms);
  929. (wmask, tmask) = DecodeBitMasks(datasize, N, imms, immr, false);
  930. /* Operation */
  931. Bits src = X(datasize, n);
  932. // perform bitfield move on low bits
  933. Bits bot = AND(ROR(src, R), wmask);
  934. // determine extension bits (sign, zero or dest register)
  935. Bits top = Replicate(datasize, src[S]);
  936. // combine extension bits and result bits
  937. X(d, OR(AND(top, NOT(tmask)), AND(bot, tmask)));
  938. }
  939. // https://meriac.github.io/archex/A64_v83A_ISA/ubfm.xml
  940. public static void Ubfm(bool sf, bool N, Bits immr, Bits imms, Bits Rn, Bits Rd)
  941. {
  942. /* Decode */
  943. int d = (int)UInt(Rd);
  944. int n = (int)UInt(Rn);
  945. int datasize = (sf ? 64 : 32);
  946. int R;
  947. Bits wmask;
  948. Bits tmask;
  949. /* if sf == '1' && N != '1' then ReservedValue(); */
  950. /* if sf == '0' && (N != '0' || immr<5> != '0' || imms<5> != '0') then ReservedValue(); */
  951. R = (int)UInt(immr);
  952. (wmask, tmask) = DecodeBitMasks(datasize, N, imms, immr, false);
  953. /* Operation */
  954. Bits src = X(datasize, n);
  955. // perform bitfield move on low bits
  956. Bits bot = AND(ROR(src, R), wmask);
  957. // combine extension bits and result bits
  958. X(d, AND(bot, tmask));
  959. }
  960. #endregion
  961. #region "CcmpImm"
  962. // https://meriac.github.io/archex/A64_v83A_ISA/ccmn_imm.xml
  963. public static void Ccmn_Imm(bool sf, Bits imm5, Bits cond, Bits Rn, Bits nzcv)
  964. {
  965. /* Decode */
  966. int n = (int)UInt(Rn);
  967. int datasize = (sf ? 64 : 32);
  968. Bits flags = nzcv;
  969. Bits imm = ZeroExtend(imm5, datasize);
  970. /* Operation */
  971. Bits operand1 = X(datasize, n);
  972. if (ConditionHolds(cond))
  973. {
  974. (_, flags) = AddWithCarry(datasize, operand1, imm, false);
  975. }
  976. PSTATE.NZCV(flags);
  977. }
  978. // https://meriac.github.io/archex/A64_v83A_ISA/ccmp_imm.xml
  979. public static void Ccmp_Imm(bool sf, Bits imm5, Bits cond, Bits Rn, Bits nzcv)
  980. {
  981. /* Decode */
  982. int n = (int)UInt(Rn);
  983. int datasize = (sf ? 64 : 32);
  984. Bits flags = nzcv;
  985. Bits imm = ZeroExtend(imm5, datasize);
  986. /* Operation */
  987. Bits operand1 = X(datasize, n);
  988. Bits operand2;
  989. if (ConditionHolds(cond))
  990. {
  991. operand2 = NOT(imm);
  992. (_, flags) = AddWithCarry(datasize, operand1, operand2, true);
  993. }
  994. PSTATE.NZCV(flags);
  995. }
  996. #endregion
  997. #region "CcmpReg"
  998. // https://meriac.github.io/archex/A64_v83A_ISA/ccmn_reg.xml
  999. public static void Ccmn_Reg(bool sf, Bits Rm, Bits cond, Bits Rn, Bits nzcv)
  1000. {
  1001. /* Decode */
  1002. int n = (int)UInt(Rn);
  1003. int m = (int)UInt(Rm);
  1004. int datasize = (sf ? 64 : 32);
  1005. Bits flags = nzcv;
  1006. /* Operation */
  1007. Bits operand1 = X(datasize, n);
  1008. Bits operand2 = X(datasize, m);
  1009. if (ConditionHolds(cond))
  1010. {
  1011. (_, flags) = AddWithCarry(datasize, operand1, operand2, false);
  1012. }
  1013. PSTATE.NZCV(flags);
  1014. }
  1015. // https://meriac.github.io/archex/A64_v83A_ISA/ccmp_reg.xml
  1016. public static void Ccmp_Reg(bool sf, Bits Rm, Bits cond, Bits Rn, Bits nzcv)
  1017. {
  1018. /* Decode */
  1019. int n = (int)UInt(Rn);
  1020. int m = (int)UInt(Rm);
  1021. int datasize = (sf ? 64 : 32);
  1022. Bits flags = nzcv;
  1023. /* Operation */
  1024. Bits operand1 = X(datasize, n);
  1025. Bits operand2 = X(datasize, m);
  1026. if (ConditionHolds(cond))
  1027. {
  1028. operand2 = NOT(operand2);
  1029. (_, flags) = AddWithCarry(datasize, operand1, operand2, true);
  1030. }
  1031. PSTATE.NZCV(flags);
  1032. }
  1033. #endregion
  1034. #region "Csel"
  1035. // https://meriac.github.io/archex/A64_v83A_ISA/csel.xml
  1036. public static void Csel(bool sf, Bits Rm, Bits cond, Bits Rn, Bits Rd)
  1037. {
  1038. /* Decode */
  1039. int d = (int)UInt(Rd);
  1040. int n = (int)UInt(Rn);
  1041. int m = (int)UInt(Rm);
  1042. int datasize = (sf ? 64 : 32);
  1043. /* Operation */
  1044. Bits result;
  1045. Bits operand1 = X(datasize, n);
  1046. Bits operand2 = X(datasize, m);
  1047. if (ConditionHolds(cond))
  1048. {
  1049. result = operand1;
  1050. }
  1051. else
  1052. {
  1053. result = operand2;
  1054. }
  1055. X(d, result);
  1056. }
  1057. // https://meriac.github.io/archex/A64_v83A_ISA/csinc.xml
  1058. public static void Csinc(bool sf, Bits Rm, Bits cond, Bits Rn, Bits Rd)
  1059. {
  1060. /* Decode */
  1061. int d = (int)UInt(Rd);
  1062. int n = (int)UInt(Rn);
  1063. int m = (int)UInt(Rm);
  1064. int datasize = (sf ? 64 : 32);
  1065. /* Operation */
  1066. Bits result;
  1067. Bits operand1 = X(datasize, n);
  1068. Bits operand2 = X(datasize, m);
  1069. if (ConditionHolds(cond))
  1070. {
  1071. result = operand1;
  1072. }
  1073. else
  1074. {
  1075. result = operand2 + 1;
  1076. }
  1077. X(d, result);
  1078. }
  1079. // https://meriac.github.io/archex/A64_v83A_ISA/csinv.xml
  1080. public static void Csinv(bool sf, Bits Rm, Bits cond, Bits Rn, Bits Rd)
  1081. {
  1082. /* Decode */
  1083. int d = (int)UInt(Rd);
  1084. int n = (int)UInt(Rn);
  1085. int m = (int)UInt(Rm);
  1086. int datasize = (sf ? 64 : 32);
  1087. /* Operation */
  1088. Bits result;
  1089. Bits operand1 = X(datasize, n);
  1090. Bits operand2 = X(datasize, m);
  1091. if (ConditionHolds(cond))
  1092. {
  1093. result = operand1;
  1094. }
  1095. else
  1096. {
  1097. result = NOT(operand2);
  1098. }
  1099. X(d, result);
  1100. }
  1101. // https://meriac.github.io/archex/A64_v83A_ISA/csneg.xml
  1102. public static void Csneg(bool sf, Bits Rm, Bits cond, Bits Rn, Bits Rd)
  1103. {
  1104. /* Decode */
  1105. int d = (int)UInt(Rd);
  1106. int n = (int)UInt(Rn);
  1107. int m = (int)UInt(Rm);
  1108. int datasize = (sf ? 64 : 32);
  1109. /* Operation */
  1110. Bits result;
  1111. Bits operand1 = X(datasize, n);
  1112. Bits operand2 = X(datasize, m);
  1113. if (ConditionHolds(cond))
  1114. {
  1115. result = operand1;
  1116. }
  1117. else
  1118. {
  1119. result = NOT(operand2);
  1120. result = result + 1;
  1121. }
  1122. X(d, result);
  1123. }
  1124. #endregion
  1125. #region "Mov"
  1126. // https://meriac.github.io/archex/A64_v83A_ISA/movk.xml
  1127. public static void Movk(bool sf, Bits hw, Bits imm16, Bits Rd)
  1128. {
  1129. /* Decode */
  1130. int d = (int)UInt(Rd);
  1131. int datasize = (sf ? 64 : 32);
  1132. /* if sf == '0' && hw<1> == '1' then UnallocatedEncoding(); */
  1133. int pos = (int)UInt(Bits.Concat(hw, "0000"));
  1134. /* Operation */
  1135. Bits result = X(datasize, d);
  1136. result[pos + 15, pos] = imm16;
  1137. X(d, result);
  1138. }
  1139. // https://meriac.github.io/archex/A64_v83A_ISA/movn.xml
  1140. public static void Movn(bool sf, Bits hw, Bits imm16, Bits Rd)
  1141. {
  1142. /* Decode */
  1143. int d = (int)UInt(Rd);
  1144. int datasize = (sf ? 64 : 32);
  1145. /* if sf == '0' && hw<1> == '1' then UnallocatedEncoding(); */
  1146. int pos = (int)UInt(Bits.Concat(hw, "0000"));
  1147. /* Operation */
  1148. Bits result = Zeros(datasize);
  1149. result[pos + 15, pos] = imm16;
  1150. result = NOT(result);
  1151. X(d, result);
  1152. }
  1153. // https://meriac.github.io/archex/A64_v83A_ISA/movz.xml
  1154. public static void Movz(bool sf, Bits hw, Bits imm16, Bits Rd)
  1155. {
  1156. /* Decode */
  1157. int d = (int)UInt(Rd);
  1158. int datasize = (sf ? 64 : 32);
  1159. /* if sf == '0' && hw<1> == '1' then UnallocatedEncoding(); */
  1160. int pos = (int)UInt(Bits.Concat(hw, "0000"));
  1161. /* Operation */
  1162. Bits result = Zeros(datasize);
  1163. result[pos + 15, pos] = imm16;
  1164. X(d, result);
  1165. }
  1166. #endregion
  1167. #region "Mul"
  1168. // https://meriac.github.io/archex/A64_v83A_ISA/madd.xml
  1169. public static void Madd(bool sf, Bits Rm, Bits Ra, Bits Rn, Bits Rd)
  1170. {
  1171. /* Decode */
  1172. int d = (int)UInt(Rd);
  1173. int n = (int)UInt(Rn);
  1174. int m = (int)UInt(Rm);
  1175. int a = (int)UInt(Ra);
  1176. int datasize = (sf ? 64 : 32);
  1177. /* Operation */
  1178. Bits operand1 = X(datasize, n);
  1179. Bits operand2 = X(datasize, m);
  1180. Bits operand3 = X(datasize, a);
  1181. BigInteger result = UInt(operand3) + (UInt(operand1) * UInt(operand2));
  1182. X(d, result.SubBigInteger(datasize - 1, 0));
  1183. }
  1184. // https://meriac.github.io/archex/A64_v83A_ISA/msub.xml
  1185. public static void Msub(bool sf, Bits Rm, Bits Ra, Bits Rn, Bits Rd)
  1186. {
  1187. /* Decode */
  1188. int d = (int)UInt(Rd);
  1189. int n = (int)UInt(Rn);
  1190. int m = (int)UInt(Rm);
  1191. int a = (int)UInt(Ra);
  1192. int datasize = (sf ? 64 : 32);
  1193. /* Operation */
  1194. Bits operand1 = X(datasize, n);
  1195. Bits operand2 = X(datasize, m);
  1196. Bits operand3 = X(datasize, a);
  1197. BigInteger result = UInt(operand3) - (UInt(operand1) * UInt(operand2));
  1198. X(d, result.SubBigInteger(datasize - 1, 0));
  1199. }
  1200. // https://meriac.github.io/archex/A64_v83A_ISA/smaddl.xml
  1201. public static void Smaddl(Bits Rm, Bits Ra, Bits Rn, Bits Rd)
  1202. {
  1203. /* Decode */
  1204. int d = (int)UInt(Rd);
  1205. int n = (int)UInt(Rn);
  1206. int m = (int)UInt(Rm);
  1207. int a = (int)UInt(Ra);
  1208. /* Operation */
  1209. Bits operand1 = X(32, n);
  1210. Bits operand2 = X(32, m);
  1211. Bits operand3 = X(64, a);
  1212. BigInteger result = Int(operand3, false) + (Int(operand1, false) * Int(operand2, false));
  1213. X(d, result.SubBigInteger(63, 0));
  1214. }
  1215. // https://meriac.github.io/archex/A64_v83A_ISA/umaddl.xml
  1216. public static void Umaddl(Bits Rm, Bits Ra, Bits Rn, Bits Rd)
  1217. {
  1218. /* Decode */
  1219. int d = (int)UInt(Rd);
  1220. int n = (int)UInt(Rn);
  1221. int m = (int)UInt(Rm);
  1222. int a = (int)UInt(Ra);
  1223. /* Operation */
  1224. Bits operand1 = X(32, n);
  1225. Bits operand2 = X(32, m);
  1226. Bits operand3 = X(64, a);
  1227. BigInteger result = Int(operand3, true) + (Int(operand1, true) * Int(operand2, true));
  1228. X(d, result.SubBigInteger(63, 0));
  1229. }
  1230. // https://meriac.github.io/archex/A64_v83A_ISA/smsubl.xml
  1231. public static void Smsubl(Bits Rm, Bits Ra, Bits Rn, Bits Rd)
  1232. {
  1233. /* Decode */
  1234. int d = (int)UInt(Rd);
  1235. int n = (int)UInt(Rn);
  1236. int m = (int)UInt(Rm);
  1237. int a = (int)UInt(Ra);
  1238. /* Operation */
  1239. Bits operand1 = X(32, n);
  1240. Bits operand2 = X(32, m);
  1241. Bits operand3 = X(64, a);
  1242. BigInteger result = Int(operand3, false) - (Int(operand1, false) * Int(operand2, false));
  1243. X(d, result.SubBigInteger(63, 0));
  1244. }
  1245. // https://meriac.github.io/archex/A64_v83A_ISA/umsubl.xml
  1246. public static void Umsubl(Bits Rm, Bits Ra, Bits Rn, Bits Rd)
  1247. {
  1248. /* Decode */
  1249. int d = (int)UInt(Rd);
  1250. int n = (int)UInt(Rn);
  1251. int m = (int)UInt(Rm);
  1252. int a = (int)UInt(Ra);
  1253. /* Operation */
  1254. Bits operand1 = X(32, n);
  1255. Bits operand2 = X(32, m);
  1256. Bits operand3 = X(64, a);
  1257. BigInteger result = Int(operand3, true) - (Int(operand1, true) * Int(operand2, true));
  1258. X(d, result.SubBigInteger(63, 0));
  1259. }
  1260. // https://meriac.github.io/archex/A64_v83A_ISA/smulh.xml
  1261. public static void Smulh(Bits Rm, Bits Rn, Bits Rd)
  1262. {
  1263. /* Decode */
  1264. int d = (int)UInt(Rd);
  1265. int n = (int)UInt(Rn);
  1266. int m = (int)UInt(Rm);
  1267. /* Operation */
  1268. Bits operand1 = X(64, n);
  1269. Bits operand2 = X(64, m);
  1270. BigInteger result = Int(operand1, false) * Int(operand2, false);
  1271. X(d, result.SubBigInteger(127, 64));
  1272. }
  1273. // https://meriac.github.io/archex/A64_v83A_ISA/umulh.xml
  1274. public static void Umulh(Bits Rm, Bits Rn, Bits Rd)
  1275. {
  1276. /* Decode */
  1277. int d = (int)UInt(Rd);
  1278. int n = (int)UInt(Rn);
  1279. int m = (int)UInt(Rm);
  1280. /* Operation */
  1281. Bits operand1 = X(64, n);
  1282. Bits operand2 = X(64, m);
  1283. BigInteger result = Int(operand1, true) * Int(operand2, true);
  1284. X(d, result.SubBigInteger(127, 64));
  1285. }
  1286. #endregion
  1287. }
  1288. internal static class SimdFp
  1289. {
  1290. #region "Simd"
  1291. // https://meriac.github.io/archex/A64_v83A_ISA/abs_advsimd.xml#ABS_asisdmisc_R
  1292. public static void Abs_S(Bits size, Bits Rn, Bits Rd)
  1293. {
  1294. bool U = false;
  1295. /* Decode Scalar */
  1296. int d = (int)UInt(Rd);
  1297. int n = (int)UInt(Rn);
  1298. /* if size != '11' then ReservedValue(); */
  1299. int esize = 8 << (int)UInt(size);
  1300. int datasize = esize;
  1301. int elements = 1;
  1302. bool neg = (U == true);
  1303. /* Operation */
  1304. /* CheckFPAdvSIMDEnabled64(); */
  1305. Bits result = new Bits(datasize);
  1306. Bits operand = V(datasize, n);
  1307. BigInteger element;
  1308. for (int e = 0; e <= elements - 1; e++)
  1309. {
  1310. element = SInt(Elem(operand, e, esize));
  1311. if (neg)
  1312. {
  1313. element = -element;
  1314. }
  1315. else
  1316. {
  1317. element = Abs(element);
  1318. }
  1319. Elem(result, e, esize, element.SubBigInteger(esize - 1, 0));
  1320. }
  1321. V(d, result);
  1322. }
  1323. // https://meriac.github.io/archex/A64_v83A_ISA/abs_advsimd.xml#ABS_asimdmisc_R
  1324. public static void Abs_V(bool Q, Bits size, Bits Rn, Bits Rd)
  1325. {
  1326. bool U = false;
  1327. /* Decode Vector */
  1328. int d = (int)UInt(Rd);
  1329. int n = (int)UInt(Rn);
  1330. /* if size:Q == '110' then ReservedValue(); */
  1331. int esize = 8 << (int)UInt(size);
  1332. int datasize = (Q ? 128 : 64);
  1333. int elements = datasize / esize;
  1334. bool neg = (U == true);
  1335. /* Operation */
  1336. /* CheckFPAdvSIMDEnabled64(); */
  1337. Bits result = new Bits(datasize);
  1338. Bits operand = V(datasize, n);
  1339. BigInteger element;
  1340. for (int e = 0; e <= elements - 1; e++)
  1341. {
  1342. element = SInt(Elem(operand, e, esize));
  1343. if (neg)
  1344. {
  1345. element = -element;
  1346. }
  1347. else
  1348. {
  1349. element = Abs(element);
  1350. }
  1351. Elem(result, e, esize, element.SubBigInteger(esize - 1, 0));
  1352. }
  1353. V(d, result);
  1354. }
  1355. // https://meriac.github.io/archex/A64_v83A_ISA/addp_advsimd_pair.xml
  1356. public static void Addp_S(Bits size, Bits Rn, Bits Rd)
  1357. {
  1358. /* Decode Scalar */
  1359. int d = (int)UInt(Rd);
  1360. int n = (int)UInt(Rn);
  1361. /* if size != '11' then ReservedValue(); */
  1362. int esize = 8 << (int)UInt(size);
  1363. int datasize = esize * 2;
  1364. // int elements = 2;
  1365. ReduceOp op = ReduceOp.ReduceOp_ADD;
  1366. /* Operation */
  1367. /* CheckFPAdvSIMDEnabled64(); */
  1368. Bits operand = V(datasize, n);
  1369. V(d, Reduce(op, operand, esize));
  1370. }
  1371. // https://meriac.github.io/archex/A64_v83A_ISA/addv_advsimd.xml
  1372. public static void Addv_V(bool Q, Bits size, Bits Rn, Bits Rd)
  1373. {
  1374. /* Decode */
  1375. int d = (int)UInt(Rd);
  1376. int n = (int)UInt(Rn);
  1377. /* if size:Q == '100' then ReservedValue(); */
  1378. /* if size == '11' then ReservedValue(); */
  1379. int esize = 8 << (int)UInt(size);
  1380. int datasize = (Q ? 128 : 64);
  1381. // int elements = datasize / esize;
  1382. ReduceOp op = ReduceOp.ReduceOp_ADD;
  1383. /* Operation */
  1384. /* CheckFPAdvSIMDEnabled64(); */
  1385. Bits operand = V(datasize, n);
  1386. V(d, Reduce(op, operand, esize));
  1387. }
  1388. // https://meriac.github.io/archex/A64_v83A_ISA/cls_advsimd.xml
  1389. public static void Cls_V(bool Q, Bits size, Bits Rn, Bits Rd)
  1390. {
  1391. bool U = false;
  1392. /* Decode */
  1393. int d = (int)UInt(Rd);
  1394. int n = (int)UInt(Rn);
  1395. /* if size == '11' then ReservedValue(); */
  1396. int esize = 8 << (int)UInt(size);
  1397. int datasize = (Q ? 128 : 64);
  1398. int elements = datasize / esize;
  1399. CountOp countop = (U ? CountOp.CountOp_CLZ : CountOp.CountOp_CLS);
  1400. /* Operation */
  1401. /* CheckFPAdvSIMDEnabled64(); */
  1402. Bits result = new Bits(datasize);
  1403. Bits operand = V(datasize, n);
  1404. BigInteger count;
  1405. for (int e = 0; e <= elements - 1; e++)
  1406. {
  1407. if (countop == CountOp.CountOp_CLS)
  1408. {
  1409. count = (BigInteger)CountLeadingSignBits(Elem(operand, e, esize));
  1410. }
  1411. else
  1412. {
  1413. count = (BigInteger)CountLeadingZeroBits(Elem(operand, e, esize));
  1414. }
  1415. Elem(result, e, esize, count.SubBigInteger(esize - 1, 0));
  1416. }
  1417. V(d, result);
  1418. }
  1419. // https://meriac.github.io/archex/A64_v83A_ISA/clz_advsimd.xml
  1420. public static void Clz_V(bool Q, Bits size, Bits Rn, Bits Rd)
  1421. {
  1422. bool U = true;
  1423. /* Decode */
  1424. int d = (int)UInt(Rd);
  1425. int n = (int)UInt(Rn);
  1426. /* if size == '11' then ReservedValue(); */
  1427. int esize = 8 << (int)UInt(size);
  1428. int datasize = (Q ? 128 : 64);
  1429. int elements = datasize / esize;
  1430. CountOp countop = (U ? CountOp.CountOp_CLZ : CountOp.CountOp_CLS);
  1431. /* Operation */
  1432. /* CheckFPAdvSIMDEnabled64(); */
  1433. Bits result = new Bits(datasize);
  1434. Bits operand = V(datasize, n);
  1435. BigInteger count;
  1436. for (int e = 0; e <= elements - 1; e++)
  1437. {
  1438. if (countop == CountOp.CountOp_CLS)
  1439. {
  1440. count = (BigInteger)CountLeadingSignBits(Elem(operand, e, esize));
  1441. }
  1442. else
  1443. {
  1444. count = (BigInteger)CountLeadingZeroBits(Elem(operand, e, esize));
  1445. }
  1446. Elem(result, e, esize, count.SubBigInteger(esize - 1, 0));
  1447. }
  1448. V(d, result);
  1449. }
  1450. // https://meriac.github.io/archex/A64_v83A_ISA/neg_advsimd.xml#NEG_asisdmisc_R
  1451. public static void Neg_S(Bits size, Bits Rn, Bits Rd)
  1452. {
  1453. bool U = true;
  1454. /* Decode Scalar */
  1455. int d = (int)UInt(Rd);
  1456. int n = (int)UInt(Rn);
  1457. /* if size != '11' then ReservedValue(); */
  1458. int esize = 8 << (int)UInt(size);
  1459. int datasize = esize;
  1460. int elements = 1;
  1461. bool neg = (U == true);
  1462. /* Operation */
  1463. /* CheckFPAdvSIMDEnabled64(); */
  1464. Bits result = new Bits(datasize);
  1465. Bits operand = V(datasize, n);
  1466. BigInteger element;
  1467. for (int e = 0; e <= elements - 1; e++)
  1468. {
  1469. element = SInt(Elem(operand, e, esize));
  1470. if (neg)
  1471. {
  1472. element = -element;
  1473. }
  1474. else
  1475. {
  1476. element = Abs(element);
  1477. }
  1478. Elem(result, e, esize, element.SubBigInteger(esize - 1, 0));
  1479. }
  1480. V(d, result);
  1481. }
  1482. // https://meriac.github.io/archex/A64_v83A_ISA/neg_advsimd.xml#NEG_asimdmisc_R
  1483. public static void Neg_V(bool Q, Bits size, Bits Rn, Bits Rd)
  1484. {
  1485. bool U = true;
  1486. /* Decode Vector */
  1487. int d = (int)UInt(Rd);
  1488. int n = (int)UInt(Rn);
  1489. /* if size:Q == '110' then ReservedValue(); */
  1490. int esize = 8 << (int)UInt(size);
  1491. int datasize = (Q ? 128 : 64);
  1492. int elements = datasize / esize;
  1493. bool neg = (U == true);
  1494. /* Operation */
  1495. /* CheckFPAdvSIMDEnabled64(); */
  1496. Bits result = new Bits(datasize);
  1497. Bits operand = V(datasize, n);
  1498. BigInteger element;
  1499. for (int e = 0; e <= elements - 1; e++)
  1500. {
  1501. element = SInt(Elem(operand, e, esize));
  1502. if (neg)
  1503. {
  1504. element = -element;
  1505. }
  1506. else
  1507. {
  1508. element = Abs(element);
  1509. }
  1510. Elem(result, e, esize, element.SubBigInteger(esize - 1, 0));
  1511. }
  1512. V(d, result);
  1513. }
  1514. #endregion
  1515. #region "SimdReg"
  1516. // https://meriac.github.io/archex/A64_v83A_ISA/add_advsimd.xml#ADD_asisdsame_only
  1517. public static void Add_S(Bits size, Bits Rm, Bits Rn, Bits Rd)
  1518. {
  1519. bool U = false;
  1520. /* Decode Scalar */
  1521. int d = (int)UInt(Rd);
  1522. int n = (int)UInt(Rn);
  1523. int m = (int)UInt(Rm);
  1524. /* if size != '11' then ReservedValue(); */
  1525. int esize = 8 << (int)UInt(size);
  1526. int datasize = esize;
  1527. int elements = 1;
  1528. bool sub_op = (U == true);
  1529. /* Operation */
  1530. /* CheckFPAdvSIMDEnabled64(); */
  1531. Bits result = new Bits(datasize);
  1532. Bits operand1 = V(datasize, n);
  1533. Bits operand2 = V(datasize, m);
  1534. Bits element1;
  1535. Bits element2;
  1536. for (int e = 0; e <= elements - 1; e++)
  1537. {
  1538. element1 = Elem(operand1, e, esize);
  1539. element2 = Elem(operand2, e, esize);
  1540. if (sub_op)
  1541. {
  1542. Elem(result, e, esize, element1 - element2);
  1543. }
  1544. else
  1545. {
  1546. Elem(result, e, esize, element1 + element2);
  1547. }
  1548. }
  1549. V(d, result);
  1550. }
  1551. // https://meriac.github.io/archex/A64_v83A_ISA/add_advsimd.xml#ADD_asimdsame_only
  1552. public static void Add_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1553. {
  1554. bool U = false;
  1555. /* Decode Vector */
  1556. int d = (int)UInt(Rd);
  1557. int n = (int)UInt(Rn);
  1558. int m = (int)UInt(Rm);
  1559. /* if size:Q == '110' then ReservedValue(); */
  1560. int esize = 8 << (int)UInt(size);
  1561. int datasize = (Q ? 128 : 64);
  1562. int elements = datasize / esize;
  1563. bool sub_op = (U == true);
  1564. /* Operation */
  1565. /* CheckFPAdvSIMDEnabled64(); */
  1566. Bits result = new Bits(datasize);
  1567. Bits operand1 = V(datasize, n);
  1568. Bits operand2 = V(datasize, m);
  1569. Bits element1;
  1570. Bits element2;
  1571. for (int e = 0; e <= elements - 1; e++)
  1572. {
  1573. element1 = Elem(operand1, e, esize);
  1574. element2 = Elem(operand2, e, esize);
  1575. if (sub_op)
  1576. {
  1577. Elem(result, e, esize, element1 - element2);
  1578. }
  1579. else
  1580. {
  1581. Elem(result, e, esize, element1 + element2);
  1582. }
  1583. }
  1584. V(d, result);
  1585. }
  1586. // https://meriac.github.io/archex/A64_v83A_ISA/addhn_advsimd.xml
  1587. public static void Addhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1588. {
  1589. bool U = false;
  1590. bool o1 = false;
  1591. /* Decode */
  1592. int d = (int)UInt(Rd);
  1593. int n = (int)UInt(Rn);
  1594. int m = (int)UInt(Rm);
  1595. /* if size == '11' then ReservedValue(); */
  1596. int esize = 8 << (int)UInt(size);
  1597. int datasize = 64;
  1598. int part = (int)UInt(Q);
  1599. int elements = datasize / esize;
  1600. bool sub_op = (o1 == true);
  1601. bool round = (U == true);
  1602. /* Operation */
  1603. /* CheckFPAdvSIMDEnabled64(); */
  1604. Bits result = new Bits(datasize);
  1605. Bits operand1 = V(2 * datasize, n);
  1606. Bits operand2 = V(2 * datasize, m);
  1607. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1608. Bits sum;
  1609. Bits element1;
  1610. Bits element2;
  1611. for (int e = 0; e <= elements - 1; e++)
  1612. {
  1613. element1 = Elem(operand1, e, 2 * esize);
  1614. element2 = Elem(operand2, e, 2 * esize);
  1615. if (sub_op)
  1616. {
  1617. sum = element1 - element2;
  1618. }
  1619. else
  1620. {
  1621. sum = element1 + element2;
  1622. }
  1623. sum = sum + round_const;
  1624. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1625. }
  1626. Vpart(d, part, result);
  1627. }
  1628. // https://meriac.github.io/archex/A64_v83A_ISA/addp_advsimd_vec.xml
  1629. public static void Addp_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1630. {
  1631. /* Decode Vector */
  1632. int d = (int)UInt(Rd);
  1633. int n = (int)UInt(Rn);
  1634. int m = (int)UInt(Rm);
  1635. /* if size:Q == '110' then ReservedValue(); */
  1636. int esize = 8 << (int)UInt(size);
  1637. int datasize = (Q ? 128 : 64);
  1638. int elements = datasize / esize;
  1639. /* Operation */
  1640. /* CheckFPAdvSIMDEnabled64(); */
  1641. Bits result = new Bits(datasize);
  1642. Bits operand1 = V(datasize, n);
  1643. Bits operand2 = V(datasize, m);
  1644. Bits concat = Bits.Concat(operand2, operand1);
  1645. Bits element1;
  1646. Bits element2;
  1647. for (int e = 0; e <= elements - 1; e++)
  1648. {
  1649. element1 = Elem(concat, 2 * e, esize);
  1650. element2 = Elem(concat, (2 * e) + 1, esize);
  1651. Elem(result, e, esize, element1 + element2);
  1652. }
  1653. V(d, result);
  1654. }
  1655. // https://meriac.github.io/archex/A64_v83A_ISA/and_advsimd.xml
  1656. public static void And_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1657. {
  1658. /* Decode */
  1659. int d = (int)UInt(Rd);
  1660. int n = (int)UInt(Rn);
  1661. int m = (int)UInt(Rm);
  1662. int datasize = (Q ? 128 : 64);
  1663. /* Operation */
  1664. /* CheckFPAdvSIMDEnabled64(); */
  1665. Bits operand1 = V(datasize, n);
  1666. Bits operand2 = V(datasize, m);
  1667. Bits result = AND(operand1, operand2);
  1668. V(d, result);
  1669. }
  1670. // https://meriac.github.io/archex/A64_v83A_ISA/bic_advsimd_reg.xml
  1671. public static void Bic_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1672. {
  1673. /* Decode */
  1674. int d = (int)UInt(Rd);
  1675. int n = (int)UInt(Rn);
  1676. int m = (int)UInt(Rm);
  1677. int datasize = (Q ? 128 : 64);
  1678. /* Operation */
  1679. /* CheckFPAdvSIMDEnabled64(); */
  1680. Bits operand1 = V(datasize, n);
  1681. Bits operand2 = V(datasize, m);
  1682. operand2 = NOT(operand2);
  1683. Bits result = AND(operand1, operand2);
  1684. V(d, result);
  1685. }
  1686. // https://meriac.github.io/archex/A64_v83A_ISA/bif_advsimd.xml
  1687. public static void Bif_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1688. {
  1689. /* Decode */
  1690. int d = (int)UInt(Rd);
  1691. int n = (int)UInt(Rn);
  1692. int m = (int)UInt(Rm);
  1693. int datasize = (Q ? 128 : 64);
  1694. /* Operation */
  1695. /* CheckFPAdvSIMDEnabled64(); */
  1696. Bits operand1;
  1697. Bits operand3;
  1698. Bits operand4 = V(datasize, n);
  1699. operand1 = V(datasize, d);
  1700. operand3 = NOT(V(datasize, m));
  1701. V(d, EOR(operand1, AND(EOR(operand1, operand4), operand3)));
  1702. }
  1703. // https://meriac.github.io/archex/A64_v83A_ISA/bit_advsimd.xml
  1704. public static void Bit_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1705. {
  1706. /* Decode */
  1707. int d = (int)UInt(Rd);
  1708. int n = (int)UInt(Rn);
  1709. int m = (int)UInt(Rm);
  1710. int datasize = (Q ? 128 : 64);
  1711. /* Operation */
  1712. /* CheckFPAdvSIMDEnabled64(); */
  1713. Bits operand1;
  1714. Bits operand3;
  1715. Bits operand4 = V(datasize, n);
  1716. operand1 = V(datasize, d);
  1717. operand3 = V(datasize, m);
  1718. V(d, EOR(operand1, AND(EOR(operand1, operand4), operand3)));
  1719. }
  1720. // https://meriac.github.io/archex/A64_v83A_ISA/bsl_advsimd.xml
  1721. public static void Bsl_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1722. {
  1723. /* Decode */
  1724. int d = (int)UInt(Rd);
  1725. int n = (int)UInt(Rn);
  1726. int m = (int)UInt(Rm);
  1727. int datasize = (Q ? 128 : 64);
  1728. /* Operation */
  1729. /* CheckFPAdvSIMDEnabled64(); */
  1730. Bits operand1;
  1731. Bits operand3;
  1732. Bits operand4 = V(datasize, n);
  1733. operand1 = V(datasize, m);
  1734. operand3 = V(datasize, d);
  1735. V(d, EOR(operand1, AND(EOR(operand1, operand4), operand3)));
  1736. }
  1737. // https://meriac.github.io/archex/A64_v83A_ISA/orn_advsimd.xml
  1738. public static void Orn_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1739. {
  1740. /* Decode */
  1741. int d = (int)UInt(Rd);
  1742. int n = (int)UInt(Rn);
  1743. int m = (int)UInt(Rm);
  1744. int datasize = (Q ? 128 : 64);
  1745. /* Operation */
  1746. /* CheckFPAdvSIMDEnabled64(); */
  1747. Bits operand1 = V(datasize, n);
  1748. Bits operand2 = V(datasize, m);
  1749. operand2 = NOT(operand2);
  1750. Bits result = OR(operand1, operand2);
  1751. V(d, result);
  1752. }
  1753. // https://meriac.github.io/archex/A64_v83A_ISA/orr_advsimd_reg.xml
  1754. public static void Orr_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1755. {
  1756. /* Decode */
  1757. int d = (int)UInt(Rd);
  1758. int n = (int)UInt(Rn);
  1759. int m = (int)UInt(Rm);
  1760. int datasize = (Q ? 128 : 64);
  1761. /* Operation */
  1762. /* CheckFPAdvSIMDEnabled64(); */
  1763. Bits operand1 = V(datasize, n);
  1764. Bits operand2 = V(datasize, m);
  1765. Bits result = OR(operand1, operand2);
  1766. V(d, result);
  1767. }
  1768. // https://meriac.github.io/archex/A64_v83A_ISA/raddhn_advsimd.xml
  1769. public static void Raddhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1770. {
  1771. bool U = true;
  1772. bool o1 = false;
  1773. /* Decode */
  1774. int d = (int)UInt(Rd);
  1775. int n = (int)UInt(Rn);
  1776. int m = (int)UInt(Rm);
  1777. /* if size == '11' then ReservedValue(); */
  1778. int esize = 8 << (int)UInt(size);
  1779. int datasize = 64;
  1780. int part = (int)UInt(Q);
  1781. int elements = datasize / esize;
  1782. bool sub_op = (o1 == true);
  1783. bool round = (U == true);
  1784. /* Operation */
  1785. /* CheckFPAdvSIMDEnabled64(); */
  1786. Bits result = new Bits(datasize);
  1787. Bits operand1 = V(2 * datasize, n);
  1788. Bits operand2 = V(2 * datasize, m);
  1789. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1790. Bits sum;
  1791. Bits element1;
  1792. Bits element2;
  1793. for (int e = 0; e <= elements - 1; e++)
  1794. {
  1795. element1 = Elem(operand1, e, 2 * esize);
  1796. element2 = Elem(operand2, e, 2 * esize);
  1797. if (sub_op)
  1798. {
  1799. sum = element1 - element2;
  1800. }
  1801. else
  1802. {
  1803. sum = element1 + element2;
  1804. }
  1805. sum = sum + round_const;
  1806. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1807. }
  1808. Vpart(d, part, result);
  1809. }
  1810. // https://meriac.github.io/archex/A64_v83A_ISA/rsubhn_advsimd.xml
  1811. public static void Rsubhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1812. {
  1813. bool U = true;
  1814. bool o1 = true;
  1815. /* Decode */
  1816. int d = (int)UInt(Rd);
  1817. int n = (int)UInt(Rn);
  1818. int m = (int)UInt(Rm);
  1819. /* if size == '11' then ReservedValue(); */
  1820. int esize = 8 << (int)UInt(size);
  1821. int datasize = 64;
  1822. int part = (int)UInt(Q);
  1823. int elements = datasize / esize;
  1824. bool sub_op = (o1 == true);
  1825. bool round = (U == true);
  1826. /* Operation */
  1827. /* CheckFPAdvSIMDEnabled64(); */
  1828. Bits result = new Bits(datasize);
  1829. Bits operand1 = V(2 * datasize, n);
  1830. Bits operand2 = V(2 * datasize, m);
  1831. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1832. Bits sum;
  1833. Bits element1;
  1834. Bits element2;
  1835. for (int e = 0; e <= elements - 1; e++)
  1836. {
  1837. element1 = Elem(operand1, e, 2 * esize);
  1838. element2 = Elem(operand2, e, 2 * esize);
  1839. if (sub_op)
  1840. {
  1841. sum = element1 - element2;
  1842. }
  1843. else
  1844. {
  1845. sum = element1 + element2;
  1846. }
  1847. sum = sum + round_const;
  1848. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1849. }
  1850. Vpart(d, part, result);
  1851. }
  1852. // https://meriac.github.io/archex/A64_v83A_ISA/sub_advsimd.xml#SUB_asisdsame_only
  1853. public static void Sub_S(Bits size, Bits Rm, Bits Rn, Bits Rd)
  1854. {
  1855. bool U = true;
  1856. /* Decode Scalar */
  1857. int d = (int)UInt(Rd);
  1858. int n = (int)UInt(Rn);
  1859. int m = (int)UInt(Rm);
  1860. /* if size != '11' then ReservedValue(); */
  1861. int esize = 8 << (int)UInt(size);
  1862. int datasize = esize;
  1863. int elements = 1;
  1864. bool sub_op = (U == true);
  1865. /* Operation */
  1866. /* CheckFPAdvSIMDEnabled64(); */
  1867. Bits result = new Bits(datasize);
  1868. Bits operand1 = V(datasize, n);
  1869. Bits operand2 = V(datasize, m);
  1870. Bits element1;
  1871. Bits element2;
  1872. for (int e = 0; e <= elements - 1; e++)
  1873. {
  1874. element1 = Elem(operand1, e, esize);
  1875. element2 = Elem(operand2, e, esize);
  1876. if (sub_op)
  1877. {
  1878. Elem(result, e, esize, element1 - element2);
  1879. }
  1880. else
  1881. {
  1882. Elem(result, e, esize, element1 + element2);
  1883. }
  1884. }
  1885. V(d, result);
  1886. }
  1887. // https://meriac.github.io/archex/A64_v83A_ISA/sub_advsimd.xml#SUB_asimdsame_only
  1888. public static void Sub_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1889. {
  1890. bool U = true;
  1891. /* Decode Vector */
  1892. int d = (int)UInt(Rd);
  1893. int n = (int)UInt(Rn);
  1894. int m = (int)UInt(Rm);
  1895. /* if size:Q == '110' then ReservedValue(); */
  1896. int esize = 8 << (int)UInt(size);
  1897. int datasize = (Q ? 128 : 64);
  1898. int elements = datasize / esize;
  1899. bool sub_op = (U == true);
  1900. /* Operation */
  1901. /* CheckFPAdvSIMDEnabled64(); */
  1902. Bits result = new Bits(datasize);
  1903. Bits operand1 = V(datasize, n);
  1904. Bits operand2 = V(datasize, m);
  1905. Bits element1;
  1906. Bits element2;
  1907. for (int e = 0; e <= elements - 1; e++)
  1908. {
  1909. element1 = Elem(operand1, e, esize);
  1910. element2 = Elem(operand2, e, esize);
  1911. if (sub_op)
  1912. {
  1913. Elem(result, e, esize, element1 - element2);
  1914. }
  1915. else
  1916. {
  1917. Elem(result, e, esize, element1 + element2);
  1918. }
  1919. }
  1920. V(d, result);
  1921. }
  1922. // https://meriac.github.io/archex/A64_v83A_ISA/subhn_advsimd.xml
  1923. public static void Subhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1924. {
  1925. bool U = false;
  1926. bool o1 = true;
  1927. /* Decode */
  1928. int d = (int)UInt(Rd);
  1929. int n = (int)UInt(Rn);
  1930. int m = (int)UInt(Rm);
  1931. /* if size == '11' then ReservedValue(); */
  1932. int esize = 8 << (int)UInt(size);
  1933. int datasize = 64;
  1934. int part = (int)UInt(Q);
  1935. int elements = datasize / esize;
  1936. bool sub_op = (o1 == true);
  1937. bool round = (U == true);
  1938. /* Operation */
  1939. /* CheckFPAdvSIMDEnabled64(); */
  1940. Bits result = new Bits(datasize);
  1941. Bits operand1 = V(2 * datasize, n);
  1942. Bits operand2 = V(2 * datasize, m);
  1943. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1944. Bits sum;
  1945. Bits element1;
  1946. Bits element2;
  1947. for (int e = 0; e <= elements - 1; e++)
  1948. {
  1949. element1 = Elem(operand1, e, 2 * esize);
  1950. element2 = Elem(operand2, e, 2 * esize);
  1951. if (sub_op)
  1952. {
  1953. sum = element1 - element2;
  1954. }
  1955. else
  1956. {
  1957. sum = element1 + element2;
  1958. }
  1959. sum = sum + round_const;
  1960. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1961. }
  1962. Vpart(d, part, result);
  1963. }
  1964. #endregion
  1965. }
  1966. }