Instructions.cs 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810
  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. // https://meriac.github.io/archex/A64_v83A_ISA/sqxtn_advsimd.xml#SQXTN_asisdmisc_N
  1515. public static void Sqxtn_S(Bits size, Bits Rn, Bits Rd)
  1516. {
  1517. bool U = false;
  1518. /* Decode Scalar */
  1519. int d = (int)UInt(Rd);
  1520. int n = (int)UInt(Rn);
  1521. /* if size == '11' then ReservedValue(); */
  1522. int esize = 8 << (int)UInt(size);
  1523. int datasize = esize;
  1524. int part = 0;
  1525. int elements = 1;
  1526. bool unsigned = (U == true);
  1527. /* Operation */
  1528. /* CheckFPAdvSIMDEnabled64(); */
  1529. Bits result = new Bits(datasize);
  1530. Bits operand = V(2 * datasize, n);
  1531. Bits element;
  1532. bool sat;
  1533. for (int e = 0; e <= elements - 1; e++)
  1534. {
  1535. element = Elem(operand, e, 2 * esize);
  1536. (Bits _result, bool _sat) = SatQ(Int(element, unsigned), esize, unsigned);
  1537. Elem(result, e, esize, _result);
  1538. sat = _sat;
  1539. if (sat)
  1540. {
  1541. /* FPSR.QC = '1'; */
  1542. FPSR[27] = true; // FIXME: Temporary solution.
  1543. }
  1544. }
  1545. Vpart(d, part, result);
  1546. }
  1547. // https://meriac.github.io/archex/A64_v83A_ISA/sqxtn_advsimd.xml#SQXTN_asimdmisc_N
  1548. public static void Sqxtn_V(bool Q, Bits size, Bits Rn, Bits Rd)
  1549. {
  1550. bool U = false;
  1551. /* Decode Vector */
  1552. int d = (int)UInt(Rd);
  1553. int n = (int)UInt(Rn);
  1554. /* if size == '11' then ReservedValue(); */
  1555. int esize = 8 << (int)UInt(size);
  1556. int datasize = 64;
  1557. int part = (int)UInt(Q);
  1558. int elements = datasize / esize;
  1559. bool unsigned = (U == true);
  1560. /* Operation */
  1561. /* CheckFPAdvSIMDEnabled64(); */
  1562. Bits result = new Bits(datasize);
  1563. Bits operand = V(2 * datasize, n);
  1564. Bits element;
  1565. bool sat;
  1566. for (int e = 0; e <= elements - 1; e++)
  1567. {
  1568. element = Elem(operand, e, 2 * esize);
  1569. (Bits _result, bool _sat) = SatQ(Int(element, unsigned), esize, unsigned);
  1570. Elem(result, e, esize, _result);
  1571. sat = _sat;
  1572. if (sat)
  1573. {
  1574. /* FPSR.QC = '1'; */
  1575. FPSR[27] = true; // FIXME: Temporary solution.
  1576. }
  1577. }
  1578. Vpart(d, part, result);
  1579. }
  1580. // https://meriac.github.io/archex/A64_v83A_ISA/uqxtn_advsimd.xml#UQXTN_asisdmisc_N
  1581. public static void Uqxtn_S(Bits size, Bits Rn, Bits Rd)
  1582. {
  1583. bool U = true;
  1584. /* Decode Scalar */
  1585. int d = (int)UInt(Rd);
  1586. int n = (int)UInt(Rn);
  1587. /* if size == '11' then ReservedValue(); */
  1588. int esize = 8 << (int)UInt(size);
  1589. int datasize = esize;
  1590. int part = 0;
  1591. int elements = 1;
  1592. bool unsigned = (U == true);
  1593. /* Operation */
  1594. /* CheckFPAdvSIMDEnabled64(); */
  1595. Bits result = new Bits(datasize);
  1596. Bits operand = V(2 * datasize, n);
  1597. Bits element;
  1598. bool sat;
  1599. for (int e = 0; e <= elements - 1; e++)
  1600. {
  1601. element = Elem(operand, e, 2 * esize);
  1602. (Bits _result, bool _sat) = SatQ(Int(element, unsigned), esize, unsigned);
  1603. Elem(result, e, esize, _result);
  1604. sat = _sat;
  1605. if (sat)
  1606. {
  1607. /* FPSR.QC = '1'; */
  1608. FPSR[27] = true; // FIXME: Temporary solution.
  1609. }
  1610. }
  1611. Vpart(d, part, result);
  1612. }
  1613. // https://meriac.github.io/archex/A64_v83A_ISA/uqxtn_advsimd.xml#UQXTN_asimdmisc_N
  1614. public static void Uqxtn_V(bool Q, Bits size, Bits Rn, Bits Rd)
  1615. {
  1616. bool U = true;
  1617. /* Decode Vector */
  1618. int d = (int)UInt(Rd);
  1619. int n = (int)UInt(Rn);
  1620. /* if size == '11' then ReservedValue(); */
  1621. int esize = 8 << (int)UInt(size);
  1622. int datasize = 64;
  1623. int part = (int)UInt(Q);
  1624. int elements = datasize / esize;
  1625. bool unsigned = (U == true);
  1626. /* Operation */
  1627. /* CheckFPAdvSIMDEnabled64(); */
  1628. Bits result = new Bits(datasize);
  1629. Bits operand = V(2 * datasize, n);
  1630. Bits element;
  1631. bool sat;
  1632. for (int e = 0; e <= elements - 1; e++)
  1633. {
  1634. element = Elem(operand, e, 2 * esize);
  1635. (Bits _result, bool _sat) = SatQ(Int(element, unsigned), esize, unsigned);
  1636. Elem(result, e, esize, _result);
  1637. sat = _sat;
  1638. if (sat)
  1639. {
  1640. /* FPSR.QC = '1'; */
  1641. FPSR[27] = true; // FIXME: Temporary solution.
  1642. }
  1643. }
  1644. Vpart(d, part, result);
  1645. }
  1646. #endregion
  1647. #region "SimdReg"
  1648. // https://meriac.github.io/archex/A64_v83A_ISA/add_advsimd.xml#ADD_asisdsame_only
  1649. public static void Add_S(Bits size, Bits Rm, Bits Rn, Bits Rd)
  1650. {
  1651. bool U = false;
  1652. /* Decode Scalar */
  1653. int d = (int)UInt(Rd);
  1654. int n = (int)UInt(Rn);
  1655. int m = (int)UInt(Rm);
  1656. /* if size != '11' then ReservedValue(); */
  1657. int esize = 8 << (int)UInt(size);
  1658. int datasize = esize;
  1659. int elements = 1;
  1660. bool sub_op = (U == true);
  1661. /* Operation */
  1662. /* CheckFPAdvSIMDEnabled64(); */
  1663. Bits result = new Bits(datasize);
  1664. Bits operand1 = V(datasize, n);
  1665. Bits operand2 = V(datasize, m);
  1666. Bits element1;
  1667. Bits element2;
  1668. for (int e = 0; e <= elements - 1; e++)
  1669. {
  1670. element1 = Elem(operand1, e, esize);
  1671. element2 = Elem(operand2, e, esize);
  1672. if (sub_op)
  1673. {
  1674. Elem(result, e, esize, element1 - element2);
  1675. }
  1676. else
  1677. {
  1678. Elem(result, e, esize, element1 + element2);
  1679. }
  1680. }
  1681. V(d, result);
  1682. }
  1683. // https://meriac.github.io/archex/A64_v83A_ISA/add_advsimd.xml#ADD_asimdsame_only
  1684. public static void Add_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1685. {
  1686. bool U = false;
  1687. /* Decode Vector */
  1688. int d = (int)UInt(Rd);
  1689. int n = (int)UInt(Rn);
  1690. int m = (int)UInt(Rm);
  1691. /* if size:Q == '110' then ReservedValue(); */
  1692. int esize = 8 << (int)UInt(size);
  1693. int datasize = (Q ? 128 : 64);
  1694. int elements = datasize / esize;
  1695. bool sub_op = (U == true);
  1696. /* Operation */
  1697. /* CheckFPAdvSIMDEnabled64(); */
  1698. Bits result = new Bits(datasize);
  1699. Bits operand1 = V(datasize, n);
  1700. Bits operand2 = V(datasize, m);
  1701. Bits element1;
  1702. Bits element2;
  1703. for (int e = 0; e <= elements - 1; e++)
  1704. {
  1705. element1 = Elem(operand1, e, esize);
  1706. element2 = Elem(operand2, e, esize);
  1707. if (sub_op)
  1708. {
  1709. Elem(result, e, esize, element1 - element2);
  1710. }
  1711. else
  1712. {
  1713. Elem(result, e, esize, element1 + element2);
  1714. }
  1715. }
  1716. V(d, result);
  1717. }
  1718. // https://meriac.github.io/archex/A64_v83A_ISA/addhn_advsimd.xml
  1719. public static void Addhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1720. {
  1721. bool U = false;
  1722. bool o1 = false;
  1723. /* Decode */
  1724. int d = (int)UInt(Rd);
  1725. int n = (int)UInt(Rn);
  1726. int m = (int)UInt(Rm);
  1727. /* if size == '11' then ReservedValue(); */
  1728. int esize = 8 << (int)UInt(size);
  1729. int datasize = 64;
  1730. int part = (int)UInt(Q);
  1731. int elements = datasize / esize;
  1732. bool sub_op = (o1 == true);
  1733. bool round = (U == true);
  1734. /* Operation */
  1735. /* CheckFPAdvSIMDEnabled64(); */
  1736. Bits result = new Bits(datasize);
  1737. Bits operand1 = V(2 * datasize, n);
  1738. Bits operand2 = V(2 * datasize, m);
  1739. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1740. Bits sum;
  1741. Bits element1;
  1742. Bits element2;
  1743. for (int e = 0; e <= elements - 1; e++)
  1744. {
  1745. element1 = Elem(operand1, e, 2 * esize);
  1746. element2 = Elem(operand2, e, 2 * esize);
  1747. if (sub_op)
  1748. {
  1749. sum = element1 - element2;
  1750. }
  1751. else
  1752. {
  1753. sum = element1 + element2;
  1754. }
  1755. sum = sum + round_const;
  1756. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1757. }
  1758. Vpart(d, part, result);
  1759. }
  1760. // https://meriac.github.io/archex/A64_v83A_ISA/addp_advsimd_vec.xml
  1761. public static void Addp_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1762. {
  1763. /* Decode Vector */
  1764. int d = (int)UInt(Rd);
  1765. int n = (int)UInt(Rn);
  1766. int m = (int)UInt(Rm);
  1767. /* if size:Q == '110' then ReservedValue(); */
  1768. int esize = 8 << (int)UInt(size);
  1769. int datasize = (Q ? 128 : 64);
  1770. int elements = datasize / esize;
  1771. /* Operation */
  1772. /* CheckFPAdvSIMDEnabled64(); */
  1773. Bits result = new Bits(datasize);
  1774. Bits operand1 = V(datasize, n);
  1775. Bits operand2 = V(datasize, m);
  1776. Bits concat = Bits.Concat(operand2, operand1);
  1777. Bits element1;
  1778. Bits element2;
  1779. for (int e = 0; e <= elements - 1; e++)
  1780. {
  1781. element1 = Elem(concat, 2 * e, esize);
  1782. element2 = Elem(concat, (2 * e) + 1, esize);
  1783. Elem(result, e, esize, element1 + element2);
  1784. }
  1785. V(d, result);
  1786. }
  1787. // https://meriac.github.io/archex/A64_v83A_ISA/and_advsimd.xml
  1788. public static void And_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1789. {
  1790. /* Decode */
  1791. int d = (int)UInt(Rd);
  1792. int n = (int)UInt(Rn);
  1793. int m = (int)UInt(Rm);
  1794. int datasize = (Q ? 128 : 64);
  1795. /* Operation */
  1796. /* CheckFPAdvSIMDEnabled64(); */
  1797. Bits operand1 = V(datasize, n);
  1798. Bits operand2 = V(datasize, m);
  1799. Bits result = AND(operand1, operand2);
  1800. V(d, result);
  1801. }
  1802. // https://meriac.github.io/archex/A64_v83A_ISA/bic_advsimd_reg.xml
  1803. public static void Bic_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1804. {
  1805. /* Decode */
  1806. int d = (int)UInt(Rd);
  1807. int n = (int)UInt(Rn);
  1808. int m = (int)UInt(Rm);
  1809. int datasize = (Q ? 128 : 64);
  1810. /* Operation */
  1811. /* CheckFPAdvSIMDEnabled64(); */
  1812. Bits operand1 = V(datasize, n);
  1813. Bits operand2 = V(datasize, m);
  1814. operand2 = NOT(operand2);
  1815. Bits result = AND(operand1, operand2);
  1816. V(d, result);
  1817. }
  1818. // https://meriac.github.io/archex/A64_v83A_ISA/bif_advsimd.xml
  1819. public static void Bif_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1820. {
  1821. /* Decode */
  1822. int d = (int)UInt(Rd);
  1823. int n = (int)UInt(Rn);
  1824. int m = (int)UInt(Rm);
  1825. int datasize = (Q ? 128 : 64);
  1826. /* Operation */
  1827. /* CheckFPAdvSIMDEnabled64(); */
  1828. Bits operand1;
  1829. Bits operand3;
  1830. Bits operand4 = V(datasize, n);
  1831. operand1 = V(datasize, d);
  1832. operand3 = NOT(V(datasize, m));
  1833. V(d, EOR(operand1, AND(EOR(operand1, operand4), operand3)));
  1834. }
  1835. // https://meriac.github.io/archex/A64_v83A_ISA/bit_advsimd.xml
  1836. public static void Bit_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1837. {
  1838. /* Decode */
  1839. int d = (int)UInt(Rd);
  1840. int n = (int)UInt(Rn);
  1841. int m = (int)UInt(Rm);
  1842. int datasize = (Q ? 128 : 64);
  1843. /* Operation */
  1844. /* CheckFPAdvSIMDEnabled64(); */
  1845. Bits operand1;
  1846. Bits operand3;
  1847. Bits operand4 = V(datasize, n);
  1848. operand1 = V(datasize, d);
  1849. operand3 = V(datasize, m);
  1850. V(d, EOR(operand1, AND(EOR(operand1, operand4), operand3)));
  1851. }
  1852. // https://meriac.github.io/archex/A64_v83A_ISA/bsl_advsimd.xml
  1853. public static void Bsl_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1854. {
  1855. /* Decode */
  1856. int d = (int)UInt(Rd);
  1857. int n = (int)UInt(Rn);
  1858. int m = (int)UInt(Rm);
  1859. int datasize = (Q ? 128 : 64);
  1860. /* Operation */
  1861. /* CheckFPAdvSIMDEnabled64(); */
  1862. Bits operand1;
  1863. Bits operand3;
  1864. Bits operand4 = V(datasize, n);
  1865. operand1 = V(datasize, m);
  1866. operand3 = V(datasize, d);
  1867. V(d, EOR(operand1, AND(EOR(operand1, operand4), operand3)));
  1868. }
  1869. // https://meriac.github.io/archex/A64_v83A_ISA/orn_advsimd.xml
  1870. public static void Orn_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1871. {
  1872. /* Decode */
  1873. int d = (int)UInt(Rd);
  1874. int n = (int)UInt(Rn);
  1875. int m = (int)UInt(Rm);
  1876. int datasize = (Q ? 128 : 64);
  1877. /* Operation */
  1878. /* CheckFPAdvSIMDEnabled64(); */
  1879. Bits operand1 = V(datasize, n);
  1880. Bits operand2 = V(datasize, m);
  1881. operand2 = NOT(operand2);
  1882. Bits result = OR(operand1, operand2);
  1883. V(d, result);
  1884. }
  1885. // https://meriac.github.io/archex/A64_v83A_ISA/orr_advsimd_reg.xml
  1886. public static void Orr_V(bool Q, Bits Rm, Bits Rn, Bits Rd)
  1887. {
  1888. /* Decode */
  1889. int d = (int)UInt(Rd);
  1890. int n = (int)UInt(Rn);
  1891. int m = (int)UInt(Rm);
  1892. int datasize = (Q ? 128 : 64);
  1893. /* Operation */
  1894. /* CheckFPAdvSIMDEnabled64(); */
  1895. Bits operand1 = V(datasize, n);
  1896. Bits operand2 = V(datasize, m);
  1897. Bits result = OR(operand1, operand2);
  1898. V(d, result);
  1899. }
  1900. // https://meriac.github.io/archex/A64_v83A_ISA/raddhn_advsimd.xml
  1901. public static void Raddhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1902. {
  1903. bool U = true;
  1904. bool o1 = false;
  1905. /* Decode */
  1906. int d = (int)UInt(Rd);
  1907. int n = (int)UInt(Rn);
  1908. int m = (int)UInt(Rm);
  1909. /* if size == '11' then ReservedValue(); */
  1910. int esize = 8 << (int)UInt(size);
  1911. int datasize = 64;
  1912. int part = (int)UInt(Q);
  1913. int elements = datasize / esize;
  1914. bool sub_op = (o1 == true);
  1915. bool round = (U == true);
  1916. /* Operation */
  1917. /* CheckFPAdvSIMDEnabled64(); */
  1918. Bits result = new Bits(datasize);
  1919. Bits operand1 = V(2 * datasize, n);
  1920. Bits operand2 = V(2 * datasize, m);
  1921. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1922. Bits sum;
  1923. Bits element1;
  1924. Bits element2;
  1925. for (int e = 0; e <= elements - 1; e++)
  1926. {
  1927. element1 = Elem(operand1, e, 2 * esize);
  1928. element2 = Elem(operand2, e, 2 * esize);
  1929. if (sub_op)
  1930. {
  1931. sum = element1 - element2;
  1932. }
  1933. else
  1934. {
  1935. sum = element1 + element2;
  1936. }
  1937. sum = sum + round_const;
  1938. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1939. }
  1940. Vpart(d, part, result);
  1941. }
  1942. // https://meriac.github.io/archex/A64_v83A_ISA/rsubhn_advsimd.xml
  1943. public static void Rsubhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  1944. {
  1945. bool U = true;
  1946. bool o1 = true;
  1947. /* Decode */
  1948. int d = (int)UInt(Rd);
  1949. int n = (int)UInt(Rn);
  1950. int m = (int)UInt(Rm);
  1951. /* if size == '11' then ReservedValue(); */
  1952. int esize = 8 << (int)UInt(size);
  1953. int datasize = 64;
  1954. int part = (int)UInt(Q);
  1955. int elements = datasize / esize;
  1956. bool sub_op = (o1 == true);
  1957. bool round = (U == true);
  1958. /* Operation */
  1959. /* CheckFPAdvSIMDEnabled64(); */
  1960. Bits result = new Bits(datasize);
  1961. Bits operand1 = V(2 * datasize, n);
  1962. Bits operand2 = V(2 * datasize, m);
  1963. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  1964. Bits sum;
  1965. Bits element1;
  1966. Bits element2;
  1967. for (int e = 0; e <= elements - 1; e++)
  1968. {
  1969. element1 = Elem(operand1, e, 2 * esize);
  1970. element2 = Elem(operand2, e, 2 * esize);
  1971. if (sub_op)
  1972. {
  1973. sum = element1 - element2;
  1974. }
  1975. else
  1976. {
  1977. sum = element1 + element2;
  1978. }
  1979. sum = sum + round_const;
  1980. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  1981. }
  1982. Vpart(d, part, result);
  1983. }
  1984. // https://meriac.github.io/archex/A64_v83A_ISA/sub_advsimd.xml#SUB_asisdsame_only
  1985. public static void Sub_S(Bits size, Bits Rm, Bits Rn, Bits Rd)
  1986. {
  1987. bool U = true;
  1988. /* Decode Scalar */
  1989. int d = (int)UInt(Rd);
  1990. int n = (int)UInt(Rn);
  1991. int m = (int)UInt(Rm);
  1992. /* if size != '11' then ReservedValue(); */
  1993. int esize = 8 << (int)UInt(size);
  1994. int datasize = esize;
  1995. int elements = 1;
  1996. bool sub_op = (U == true);
  1997. /* Operation */
  1998. /* CheckFPAdvSIMDEnabled64(); */
  1999. Bits result = new Bits(datasize);
  2000. Bits operand1 = V(datasize, n);
  2001. Bits operand2 = V(datasize, m);
  2002. Bits element1;
  2003. Bits element2;
  2004. for (int e = 0; e <= elements - 1; e++)
  2005. {
  2006. element1 = Elem(operand1, e, esize);
  2007. element2 = Elem(operand2, e, esize);
  2008. if (sub_op)
  2009. {
  2010. Elem(result, e, esize, element1 - element2);
  2011. }
  2012. else
  2013. {
  2014. Elem(result, e, esize, element1 + element2);
  2015. }
  2016. }
  2017. V(d, result);
  2018. }
  2019. // https://meriac.github.io/archex/A64_v83A_ISA/sub_advsimd.xml#SUB_asimdsame_only
  2020. public static void Sub_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  2021. {
  2022. bool U = true;
  2023. /* Decode Vector */
  2024. int d = (int)UInt(Rd);
  2025. int n = (int)UInt(Rn);
  2026. int m = (int)UInt(Rm);
  2027. /* if size:Q == '110' then ReservedValue(); */
  2028. int esize = 8 << (int)UInt(size);
  2029. int datasize = (Q ? 128 : 64);
  2030. int elements = datasize / esize;
  2031. bool sub_op = (U == true);
  2032. /* Operation */
  2033. /* CheckFPAdvSIMDEnabled64(); */
  2034. Bits result = new Bits(datasize);
  2035. Bits operand1 = V(datasize, n);
  2036. Bits operand2 = V(datasize, m);
  2037. Bits element1;
  2038. Bits element2;
  2039. for (int e = 0; e <= elements - 1; e++)
  2040. {
  2041. element1 = Elem(operand1, e, esize);
  2042. element2 = Elem(operand2, e, esize);
  2043. if (sub_op)
  2044. {
  2045. Elem(result, e, esize, element1 - element2);
  2046. }
  2047. else
  2048. {
  2049. Elem(result, e, esize, element1 + element2);
  2050. }
  2051. }
  2052. V(d, result);
  2053. }
  2054. // https://meriac.github.io/archex/A64_v83A_ISA/subhn_advsimd.xml
  2055. public static void Subhn_V(bool Q, Bits size, Bits Rm, Bits Rn, Bits Rd)
  2056. {
  2057. bool U = false;
  2058. bool o1 = true;
  2059. /* Decode */
  2060. int d = (int)UInt(Rd);
  2061. int n = (int)UInt(Rn);
  2062. int m = (int)UInt(Rm);
  2063. /* if size == '11' then ReservedValue(); */
  2064. int esize = 8 << (int)UInt(size);
  2065. int datasize = 64;
  2066. int part = (int)UInt(Q);
  2067. int elements = datasize / esize;
  2068. bool sub_op = (o1 == true);
  2069. bool round = (U == true);
  2070. /* Operation */
  2071. /* CheckFPAdvSIMDEnabled64(); */
  2072. Bits result = new Bits(datasize);
  2073. Bits operand1 = V(2 * datasize, n);
  2074. Bits operand2 = V(2 * datasize, m);
  2075. BigInteger round_const = (round ? (BigInteger)1 << (esize - 1) : 0);
  2076. Bits sum;
  2077. Bits element1;
  2078. Bits element2;
  2079. for (int e = 0; e <= elements - 1; e++)
  2080. {
  2081. element1 = Elem(operand1, e, 2 * esize);
  2082. element2 = Elem(operand2, e, 2 * esize);
  2083. if (sub_op)
  2084. {
  2085. sum = element1 - element2;
  2086. }
  2087. else
  2088. {
  2089. sum = element1 + element2;
  2090. }
  2091. sum = sum + round_const;
  2092. Elem(result, e, esize, sum[2 * esize - 1, esize]);
  2093. }
  2094. Vpart(d, part, result);
  2095. }
  2096. #endregion
  2097. }
  2098. }