Instructions.cs 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  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/neg_advsimd.xml#NEG_asisdmisc_R
  1389. public static void Neg_S(Bits size, Bits Rn, Bits Rd)
  1390. {
  1391. bool U = true;
  1392. /* Decode Scalar */
  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 = esize;
  1398. int elements = 1;
  1399. bool neg = (U == true);
  1400. /* Operation */
  1401. /* CheckFPAdvSIMDEnabled64(); */
  1402. Bits result = new Bits(datasize);
  1403. Bits operand = V(datasize, n);
  1404. BigInteger element;
  1405. for (int e = 0; e <= elements - 1; e++)
  1406. {
  1407. element = SInt(Elem(operand, e, esize));
  1408. if (neg)
  1409. {
  1410. element = -element;
  1411. }
  1412. else
  1413. {
  1414. element = Abs(element);
  1415. }
  1416. Elem(result, e, esize, element.SubBigInteger(esize - 1, 0));
  1417. }
  1418. V(d, result);
  1419. }
  1420. // https://meriac.github.io/archex/A64_v83A_ISA/neg_advsimd.xml#NEG_asimdmisc_R
  1421. public static void Neg_V(bool Q, Bits size, Bits Rn, Bits Rd)
  1422. {
  1423. bool U = true;
  1424. /* Decode Vector */
  1425. int d = (int)UInt(Rd);
  1426. int n = (int)UInt(Rn);
  1427. /* if size:Q == '110' then ReservedValue(); */
  1428. int esize = 8 << (int)UInt(size);
  1429. int datasize = (Q ? 128 : 64);
  1430. int elements = datasize / esize;
  1431. bool neg = (U == true);
  1432. /* Operation */
  1433. /* CheckFPAdvSIMDEnabled64(); */
  1434. Bits result = new Bits(datasize);
  1435. Bits operand = V(datasize, n);
  1436. BigInteger element;
  1437. for (int e = 0; e <= elements - 1; e++)
  1438. {
  1439. element = SInt(Elem(operand, e, esize));
  1440. if (neg)
  1441. {
  1442. element = -element;
  1443. }
  1444. else
  1445. {
  1446. element = Abs(element);
  1447. }
  1448. Elem(result, e, esize, element.SubBigInteger(esize - 1, 0));
  1449. }
  1450. V(d, result);
  1451. }
  1452. #endregion
  1453. #region "SimdReg"
  1454. // https://meriac.github.io/archex/A64_v83A_ISA/add_advsimd.xml#ADD_asisdsame_only
  1455. public static void Add_S(Bits size, Bits Rm, Bits Rn, Bits Rd)
  1456. {
  1457. bool U = false;
  1458. /* Decode Scalar */
  1459. int d = (int)UInt(Rd);
  1460. int n = (int)UInt(Rn);
  1461. int m = (int)UInt(Rm);
  1462. /* if size != '11' then ReservedValue(); */
  1463. int esize = 8 << (int)UInt(size);
  1464. int datasize = esize;
  1465. int elements = 1;
  1466. bool sub_op = (U == true);
  1467. /* Operation */
  1468. /* CheckFPAdvSIMDEnabled64(); */
  1469. Bits result = new Bits(datasize);
  1470. Bits operand1 = V(datasize, n);
  1471. Bits operand2 = V(datasize, m);
  1472. Bits element1;
  1473. Bits element2;
  1474. for (int e = 0; e <= elements - 1; e++)
  1475. {
  1476. element1 = Elem(operand1, e, esize);
  1477. element2 = Elem(operand2, e, esize);
  1478. if (sub_op)
  1479. {
  1480. Elem(result, e, esize, element1 - element2);
  1481. }
  1482. else
  1483. {
  1484. Elem(result, e, esize, element1 + element2);
  1485. }
  1486. }
  1487. V(d, result);
  1488. }
  1489. // https://meriac.github.io/archex/A64_v83A_ISA/add_advsimd.xml#ADD_asimdsame_only
  1490. public static void Add_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1491. {
  1492. bool U = false;
  1493. /* Decode Vector */
  1494. int d = (int)UInt(Rd);
  1495. int n = (int)UInt(Rn);
  1496. int m = (int)UInt(Rm);
  1497. /* if size:Q == '110' then ReservedValue(); */
  1498. int esize = 8 << (int)UInt(size);
  1499. int datasize = (Q ? 128 : 64);
  1500. int elements = datasize / esize;
  1501. bool sub_op = (U == true);
  1502. /* Operation */
  1503. /* CheckFPAdvSIMDEnabled64(); */
  1504. Bits result = new Bits(datasize);
  1505. Bits operand1 = V(datasize, n);
  1506. Bits operand2 = V(datasize, m);
  1507. Bits element1;
  1508. Bits element2;
  1509. for (int e = 0; e <= elements - 1; e++)
  1510. {
  1511. element1 = Elem(operand1, e, esize);
  1512. element2 = Elem(operand2, e, esize);
  1513. if (sub_op)
  1514. {
  1515. Elem(result, e, esize, element1 - element2);
  1516. }
  1517. else
  1518. {
  1519. Elem(result, e, esize, element1 + element2);
  1520. }
  1521. }
  1522. V(d, result);
  1523. }
  1524. // https://meriac.github.io/archex/A64_v83A_ISA/addhn_advsimd.xml
  1525. public static void Addhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1526. {
  1527. bool U = false;
  1528. bool o1 = false;
  1529. /* Decode */
  1530. int d = (int)UInt(Rd);
  1531. int n = (int)UInt(Rn);
  1532. int m = (int)UInt(Rm);
  1533. /* if size == '11' then ReservedValue(); */
  1534. int esize = 8 << (int)UInt(size);
  1535. int datasize = 64;
  1536. int part = (int)UInt(Q);
  1537. int elements = datasize / esize;
  1538. bool sub_op = (o1 == true);
  1539. bool round = (U == true);
  1540. /* Operation */
  1541. /* CheckFPAdvSIMDEnabled64(); */
  1542. Bits result = new Bits(datasize);
  1543. Bits operand1 = V(2 * datasize, n);
  1544. Bits operand2 = V(2 * datasize, m);
  1545. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1546. Bits sum;
  1547. Bits element1;
  1548. Bits element2;
  1549. for (int e = 0; e <= elements - 1; e++)
  1550. {
  1551. element1 = Elem(operand1, e, 2 * esize);
  1552. element2 = Elem(operand2, e, 2 * esize);
  1553. if (sub_op)
  1554. {
  1555. sum = element1 - element2;
  1556. }
  1557. else
  1558. {
  1559. sum = element1 + element2;
  1560. }
  1561. sum = sum + round_const;
  1562. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1563. }
  1564. Vpart(d, part, result);
  1565. }
  1566. // https://meriac.github.io/archex/A64_v83A_ISA/addp_advsimd_vec.xml
  1567. public static void Addp_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1568. {
  1569. /* Decode Vector */
  1570. int d = (int)UInt(Rd);
  1571. int n = (int)UInt(Rn);
  1572. int m = (int)UInt(Rm);
  1573. /* if size:Q == '110' then ReservedValue(); */
  1574. int esize = 8 << (int)UInt(size);
  1575. int datasize = (Q ? 128 : 64);
  1576. int elements = datasize / esize;
  1577. /* Operation */
  1578. /* CheckFPAdvSIMDEnabled64(); */
  1579. Bits result = new Bits(datasize);
  1580. Bits operand1 = V(datasize, n);
  1581. Bits operand2 = V(datasize, m);
  1582. Bits concat = Bits.Concat(operand2, operand1);
  1583. Bits element1;
  1584. Bits element2;
  1585. for (int e = 0; e <= elements - 1; e++)
  1586. {
  1587. element1 = Elem(concat, 2 * e, esize);
  1588. element2 = Elem(concat, (2 * e) + 1, esize);
  1589. Elem(result, e, esize, element1 + element2);
  1590. }
  1591. V(d, result);
  1592. }
  1593. // https://meriac.github.io/archex/A64_v83A_ISA/raddhn_advsimd.xml
  1594. public static void Raddhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1595. {
  1596. bool U = true;
  1597. bool o1 = false;
  1598. /* Decode */
  1599. int d = (int)UInt(Rd);
  1600. int n = (int)UInt(Rn);
  1601. int m = (int)UInt(Rm);
  1602. /* if size == '11' then ReservedValue(); */
  1603. int esize = 8 << (int)UInt(size);
  1604. int datasize = 64;
  1605. int part = (int)UInt(Q);
  1606. int elements = datasize / esize;
  1607. bool sub_op = (o1 == true);
  1608. bool round = (U == true);
  1609. /* Operation */
  1610. /* CheckFPAdvSIMDEnabled64(); */
  1611. Bits result = new Bits(datasize);
  1612. Bits operand1 = V(2 * datasize, n);
  1613. Bits operand2 = V(2 * datasize, m);
  1614. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1615. Bits sum;
  1616. Bits element1;
  1617. Bits element2;
  1618. for (int e = 0; e <= elements - 1; e++)
  1619. {
  1620. element1 = Elem(operand1, e, 2 * esize);
  1621. element2 = Elem(operand2, e, 2 * esize);
  1622. if (sub_op)
  1623. {
  1624. sum = element1 - element2;
  1625. }
  1626. else
  1627. {
  1628. sum = element1 + element2;
  1629. }
  1630. sum = sum + round_const;
  1631. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1632. }
  1633. Vpart(d, part, result);
  1634. }
  1635. // https://meriac.github.io/archex/A64_v83A_ISA/rsubhn_advsimd.xml
  1636. public static void Rsubhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1637. {
  1638. bool U = true;
  1639. bool o1 = true;
  1640. /* Decode */
  1641. int d = (int)UInt(Rd);
  1642. int n = (int)UInt(Rn);
  1643. int m = (int)UInt(Rm);
  1644. /* if size == '11' then ReservedValue(); */
  1645. int esize = 8 << (int)UInt(size);
  1646. int datasize = 64;
  1647. int part = (int)UInt(Q);
  1648. int elements = datasize / esize;
  1649. bool sub_op = (o1 == true);
  1650. bool round = (U == true);
  1651. /* Operation */
  1652. /* CheckFPAdvSIMDEnabled64(); */
  1653. Bits result = new Bits(datasize);
  1654. Bits operand1 = V(2 * datasize, n);
  1655. Bits operand2 = V(2 * datasize, m);
  1656. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1657. Bits sum;
  1658. Bits element1;
  1659. Bits element2;
  1660. for (int e = 0; e <= elements - 1; e++)
  1661. {
  1662. element1 = Elem(operand1, e, 2 * esize);
  1663. element2 = Elem(operand2, e, 2 * esize);
  1664. if (sub_op)
  1665. {
  1666. sum = element1 - element2;
  1667. }
  1668. else
  1669. {
  1670. sum = element1 + element2;
  1671. }
  1672. sum = sum + round_const;
  1673. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1674. }
  1675. Vpart(d, part, result);
  1676. }
  1677. // https://meriac.github.io/archex/A64_v83A_ISA/sub_advsimd.xml#SUB_asisdsame_only
  1678. public static void Sub_S(Bits size, Bits Rm, Bits Rn, Bits Rd)
  1679. {
  1680. bool U = true;
  1681. /* Decode Scalar */
  1682. int d = (int)UInt(Rd);
  1683. int n = (int)UInt(Rn);
  1684. int m = (int)UInt(Rm);
  1685. /* if size != '11' then ReservedValue(); */
  1686. int esize = 8 << (int)UInt(size);
  1687. int datasize = esize;
  1688. int elements = 1;
  1689. bool sub_op = (U == true);
  1690. /* Operation */
  1691. /* CheckFPAdvSIMDEnabled64(); */
  1692. Bits result = new Bits(datasize);
  1693. Bits operand1 = V(datasize, n);
  1694. Bits operand2 = V(datasize, m);
  1695. Bits element1;
  1696. Bits element2;
  1697. for (int e = 0; e <= elements - 1; e++)
  1698. {
  1699. element1 = Elem(operand1, e, esize);
  1700. element2 = Elem(operand2, e, esize);
  1701. if (sub_op)
  1702. {
  1703. Elem(result, e, esize, element1 - element2);
  1704. }
  1705. else
  1706. {
  1707. Elem(result, e, esize, element1 + element2);
  1708. }
  1709. }
  1710. V(d, result);
  1711. }
  1712. // https://meriac.github.io/archex/A64_v83A_ISA/sub_advsimd.xml#SUB_asimdsame_only
  1713. public static void Sub_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1714. {
  1715. bool U = true;
  1716. /* Decode Vector */
  1717. int d = (int)UInt(Rd);
  1718. int n = (int)UInt(Rn);
  1719. int m = (int)UInt(Rm);
  1720. /* if size:Q == '110' then ReservedValue(); */
  1721. int esize = 8 << (int)UInt(size);
  1722. int datasize = (Q ? 128 : 64);
  1723. int elements = datasize / esize;
  1724. bool sub_op = (U == true);
  1725. /* Operation */
  1726. /* CheckFPAdvSIMDEnabled64(); */
  1727. Bits result = new Bits(datasize);
  1728. Bits operand1 = V(datasize, n);
  1729. Bits operand2 = V(datasize, m);
  1730. Bits element1;
  1731. Bits element2;
  1732. for (int e = 0; e <= elements - 1; e++)
  1733. {
  1734. element1 = Elem(operand1, e, esize);
  1735. element2 = Elem(operand2, e, esize);
  1736. if (sub_op)
  1737. {
  1738. Elem(result, e, esize, element1 - element2);
  1739. }
  1740. else
  1741. {
  1742. Elem(result, e, esize, element1 + element2);
  1743. }
  1744. }
  1745. V(d, result);
  1746. }
  1747. // https://meriac.github.io/archex/A64_v83A_ISA/subhn_advsimd.xml
  1748. public static void Subhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1749. {
  1750. bool U = false;
  1751. bool o1 = true;
  1752. /* Decode */
  1753. int d = (int)UInt(Rd);
  1754. int n = (int)UInt(Rn);
  1755. int m = (int)UInt(Rm);
  1756. /* if size == '11' then ReservedValue(); */
  1757. int esize = 8 << (int)UInt(size);
  1758. int datasize = 64;
  1759. int part = (int)UInt(Q);
  1760. int elements = datasize / esize;
  1761. bool sub_op = (o1 == true);
  1762. bool round = (U == true);
  1763. /* Operation */
  1764. /* CheckFPAdvSIMDEnabled64(); */
  1765. Bits result = new Bits(datasize);
  1766. Bits operand1 = V(2 * datasize, n);
  1767. Bits operand2 = V(2 * datasize, m);
  1768. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1769. Bits sum;
  1770. Bits element1;
  1771. Bits element2;
  1772. for (int e = 0; e <= elements - 1; e++)
  1773. {
  1774. element1 = Elem(operand1, e, 2 * esize);
  1775. element2 = Elem(operand2, e, 2 * esize);
  1776. if (sub_op)
  1777. {
  1778. sum = element1 - element2;
  1779. }
  1780. else
  1781. {
  1782. sum = element1 + element2;
  1783. }
  1784. sum = sum + round_const;
  1785. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1786. }
  1787. Vpart(d, part, result);
  1788. }
  1789. #endregion
  1790. }
  1791. }