Pseudocode.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. // https://github.com/LDj3SNuD/ARM_v8-A_AArch64_Instructions_Tester/blob/master/Tester/Pseudocode.cs
  2. // https://developer.arm.com/products/architecture/a-profile/exploration-tools
  3. // ..\A64_v83A_ISA_xml_00bet6.1\ISA_v83A_A64_xml_00bet6.1_OPT\xhtml\
  4. // https://alastairreid.github.io/asl-lexical-syntax/
  5. // | ------------------------|-------------------------------- |
  6. // | ASL | C# |
  7. // | ------------------------|-------------------------------- |
  8. // | bit, bits(1); boolean | bool |
  9. // | bits | Bits |
  10. // | integer | BigInteger, int |
  11. // | real | decimal; double, float |
  12. // | ------------------------|-------------------------------- |
  13. // | '0'; FALSE | false |
  14. // | '1'; TRUE | true |
  15. // | '010' | "010" |
  16. // | DIV, MOD | /, % |
  17. // | ------------------------|-------------------------------- |
  18. using System;
  19. using System.Numerics;
  20. namespace Ryujinx.Tests.Cpu.Tester
  21. {
  22. using Types;
  23. using static Shared;
  24. internal static class AArch64
  25. {
  26. #region "exceptions/exceptions/"
  27. /* shared_pseudocode.html#AArch64.ResetControlRegisters.1 */
  28. public static void ResetControlRegisters(bool cold_reset)
  29. {
  30. PSTATE.N = cold_reset;
  31. PSTATE.Z = cold_reset;
  32. PSTATE.C = cold_reset;
  33. PSTATE.V = cold_reset;
  34. }
  35. /* */
  36. public static void TakeReset(bool cold_reset)
  37. {
  38. /* assert !HighestELUsingAArch32(); */
  39. // Enter the highest implemented Exception level in AArch64 state
  40. if (HaveEL(EL3))
  41. {
  42. PSTATE.EL = EL3;
  43. }
  44. else if (HaveEL(EL2))
  45. {
  46. PSTATE.EL = EL2;
  47. }
  48. else
  49. {
  50. PSTATE.EL = EL1;
  51. }
  52. // Reset the system registers and other system components
  53. AArch64.ResetControlRegisters(cold_reset);
  54. // Reset all other PSTATE fields
  55. PSTATE.SP = true; // Select stack pointer
  56. // All registers, bits and fields not reset by the above pseudocode or by the BranchTo() call
  57. // below are UNKNOWN bitstrings after reset. In particular, the return information registers
  58. // ELR_ELx and SPSR_ELx have UNKNOWN values, so that it
  59. // is impossible to return from a reset in an architecturally defined way.
  60. AArch64.ResetGeneralRegisters();
  61. AArch64.ResetSIMDFPRegisters();
  62. AArch64.ResetSpecialRegisters();
  63. }
  64. #endregion
  65. #region "functions/registers/"
  66. /* shared_pseudocode.html#AArch64.ResetGeneralRegisters.0 */
  67. public static void ResetGeneralRegisters()
  68. {
  69. for (int i = 0; i <= 30; i++)
  70. {
  71. /* X[i] = bits(64) UNKNOWN; */
  72. _R[i].SetAll(false);
  73. }
  74. }
  75. /* shared_pseudocode.html#AArch64.ResetSIMDFPRegisters.0 */
  76. public static void ResetSIMDFPRegisters()
  77. {
  78. for (int i = 0; i <= 31; i++)
  79. {
  80. /* V[i] = bits(128) UNKNOWN; */
  81. _V[i].SetAll(false);
  82. }
  83. }
  84. /* shared_pseudocode.html#AArch64.ResetSpecialRegisters.0 */
  85. public static void ResetSpecialRegisters()
  86. {
  87. // AArch64 special registers
  88. /* SP_EL0 = bits(64) UNKNOWN; */
  89. SP_EL0.SetAll(false);
  90. /* SP_EL1 = bits(64) UNKNOWN; */
  91. SP_EL1.SetAll(false);
  92. FPCR.SetAll(false); // TODO: Add named fields.
  93. FPSR.SetAll(false); // TODO: Add named fields.
  94. }
  95. // shared_pseudocode.html#impl-aarch64.SP.write.0
  96. public static void SP(Bits value)
  97. {
  98. /* int width = value.Count; */
  99. /* assert width IN {32,64}; */
  100. if (!PSTATE.SP)
  101. {
  102. SP_EL0 = ZeroExtend(64, value);
  103. }
  104. else
  105. {
  106. switch (PSTATE.EL)
  107. {
  108. case Bits bits when bits == EL0:
  109. SP_EL0 = ZeroExtend(64, value);
  110. break;
  111. default:
  112. case Bits bits when bits == EL1:
  113. SP_EL1 = ZeroExtend(64, value);
  114. break;/*
  115. case Bits bits when bits == EL2:
  116. SP_EL2 = ZeroExtend(64, value);
  117. break;
  118. case Bits bits when bits == EL3:
  119. SP_EL3 = ZeroExtend(64, value);
  120. break;*/
  121. }
  122. }
  123. }
  124. // shared_pseudocode.html#impl-aarch64.SP.read.0
  125. public static Bits SP(int width)
  126. {
  127. /* assert width IN {8,16,32,64}; */
  128. if (!PSTATE.SP)
  129. {
  130. return SP_EL0[width - 1, 0];
  131. }
  132. else
  133. {
  134. switch (PSTATE.EL)
  135. {
  136. case Bits bits when bits == EL0:
  137. return SP_EL0[width - 1, 0];
  138. default:
  139. case Bits bits when bits == EL1:
  140. return SP_EL1[width - 1, 0];/*
  141. case Bits bits when bits == EL2:
  142. return SP_EL2[width - 1, 0];
  143. case Bits bits when bits == EL3:
  144. return SP_EL3[width - 1, 0];*/
  145. }
  146. }
  147. }
  148. // shared_pseudocode.html#impl-aarch64.V.write.1
  149. public static void V(int n, Bits value)
  150. {
  151. /* int width = value.Count; */
  152. /* assert n >= 0 && n <= 31; */
  153. /* assert width IN {8,16,32,64,128}; */
  154. _V[n] = ZeroExtend(128, value);
  155. }
  156. /* shared_pseudocode.html#impl-aarch64.V.read.1 */
  157. public static Bits V(int width, int n)
  158. {
  159. /* assert n >= 0 && n <= 31; */
  160. /* assert width IN {8,16,32,64,128}; */
  161. return _V[n][width - 1, 0];
  162. }
  163. /* shared_pseudocode.html#impl-aarch64.Vpart.read.2 */
  164. public static Bits Vpart(int width, int n, int part)
  165. {
  166. /* assert n >= 0 && n <= 31; */
  167. /* assert part IN {0, 1}; */
  168. if (part == 0)
  169. {
  170. /* assert width IN {8,16,32,64}; */
  171. return _V[n][width - 1, 0];
  172. }
  173. else
  174. {
  175. /* assert width == 64; */
  176. return _V[n][(width * 2) - 1, width];
  177. }
  178. }
  179. // shared_pseudocode.html#impl-aarch64.Vpart.write.2
  180. public static void Vpart(int n, int part, Bits value)
  181. {
  182. int width = value.Count;
  183. /* assert n >= 0 && n <= 31; */
  184. /* assert part IN {0, 1}; */
  185. if (part == 0)
  186. {
  187. /* assert width IN {8,16,32,64}; */
  188. _V[n] = ZeroExtend(128, value);
  189. }
  190. else
  191. {
  192. /* assert width == 64; */
  193. _V[n][(width * 2) - 1, width] = value[width - 1, 0];
  194. }
  195. }
  196. // shared_pseudocode.html#impl-aarch64.X.write.1
  197. public static void X(int n, Bits value)
  198. {
  199. /* int width = value.Count; */
  200. /* assert n >= 0 && n <= 31; */
  201. /* assert width IN {32,64}; */
  202. if (n != 31)
  203. {
  204. _R[n] = ZeroExtend(64, value);
  205. }
  206. }
  207. /* shared_pseudocode.html#impl-aarch64.X.read.1 */
  208. public static Bits X(int width, int n)
  209. {
  210. /* assert n >= 0 && n <= 31; */
  211. /* assert width IN {8,16,32,64}; */
  212. if (n != 31)
  213. {
  214. return _R[n][width - 1, 0];
  215. }
  216. else
  217. {
  218. return Zeros(width);
  219. }
  220. }
  221. #endregion
  222. #region "instrs/countop/"
  223. // shared_pseudocode.html#CountOp
  224. public enum CountOp {CountOp_CLZ, CountOp_CLS, CountOp_CNT};
  225. #endregion
  226. #region "instrs/extendreg/"
  227. /* shared_pseudocode.html#impl-aarch64.DecodeRegExtend.1 */
  228. public static ExtendType DecodeRegExtend(Bits op)
  229. {
  230. switch (op)
  231. {
  232. default:
  233. case Bits bits when bits == "000":
  234. return ExtendType.ExtendType_UXTB;
  235. case Bits bits when bits == "001":
  236. return ExtendType.ExtendType_UXTH;
  237. case Bits bits when bits == "010":
  238. return ExtendType.ExtendType_UXTW;
  239. case Bits bits when bits == "011":
  240. return ExtendType.ExtendType_UXTX;
  241. case Bits bits when bits == "100":
  242. return ExtendType.ExtendType_SXTB;
  243. case Bits bits when bits == "101":
  244. return ExtendType.ExtendType_SXTH;
  245. case Bits bits when bits == "110":
  246. return ExtendType.ExtendType_SXTW;
  247. case Bits bits when bits == "111":
  248. return ExtendType.ExtendType_SXTX;
  249. }
  250. }
  251. /* shared_pseudocode.html#impl-aarch64.ExtendReg.3 */
  252. public static Bits ExtendReg(int N, int reg, ExtendType type, int shift)
  253. {
  254. /* assert shift >= 0 && shift <= 4; */
  255. Bits val = X(N, reg);
  256. bool unsigned;
  257. int len;
  258. switch (type)
  259. {
  260. default:
  261. case ExtendType.ExtendType_SXTB:
  262. unsigned = false; len = 8;
  263. break;
  264. case ExtendType.ExtendType_SXTH:
  265. unsigned = false; len = 16;
  266. break;
  267. case ExtendType.ExtendType_SXTW:
  268. unsigned = false; len = 32;
  269. break;
  270. case ExtendType.ExtendType_SXTX:
  271. unsigned = false; len = 64;
  272. break;
  273. case ExtendType.ExtendType_UXTB:
  274. unsigned = true; len = 8;
  275. break;
  276. case ExtendType.ExtendType_UXTH:
  277. unsigned = true; len = 16;
  278. break;
  279. case ExtendType.ExtendType_UXTW:
  280. unsigned = true; len = 32;
  281. break;
  282. case ExtendType.ExtendType_UXTX:
  283. unsigned = true; len = 64;
  284. break;
  285. }
  286. // Note the extended width of the intermediate value and
  287. // that sign extension occurs from bit <len+shift-1>, not
  288. // from bit <len-1>. This is equivalent to the instruction
  289. // [SU]BFIZ Rtmp, Rreg, #shift, #len
  290. // It may also be seen as a sign/zero extend followed by a shift:
  291. // LSL(Extend(val<len-1:0>, N, unsigned), shift);
  292. len = Min(len, N - shift);
  293. return Extend(Bits.Concat(val[len - 1, 0], Zeros(shift)), N, unsigned);
  294. }
  295. // shared_pseudocode.html#ExtendType
  296. public enum ExtendType {ExtendType_SXTB, ExtendType_SXTH, ExtendType_SXTW, ExtendType_SXTX,
  297. ExtendType_UXTB, ExtendType_UXTH, ExtendType_UXTW, ExtendType_UXTX};
  298. #endregion
  299. #region "instrs/integer/bitmasks/"
  300. /* shared_pseudocode.html#impl-aarch64.DecodeBitMasks.4 */
  301. public static (Bits, Bits) DecodeBitMasks(int M, bool immN, Bits imms, Bits immr, bool immediate)
  302. {
  303. Bits tmask, wmask;
  304. Bits tmask_and, wmask_and;
  305. Bits tmask_or, wmask_or;
  306. Bits levels;
  307. // Compute log2 of element size
  308. // 2^len must be in range [2, M]
  309. int len = HighestSetBit(Bits.Concat(immN, NOT(imms)));
  310. /* if len < 1 then ReservedValue(); */
  311. /* assert M >= (1 << len); */
  312. // Determine S, R and S - R parameters
  313. levels = ZeroExtend(Ones(len), 6);
  314. // For logical immediates an all-ones value of S is reserved
  315. // since it would generate a useless all-ones result (many times)
  316. /* if immediate && (imms AND levels) == levels then ReservedValue(); */
  317. BigInteger S = UInt(AND(imms, levels));
  318. BigInteger R = UInt(AND(immr, levels));
  319. BigInteger diff = S - R; // 6-bit subtract with borrow
  320. // Compute "top mask"
  321. tmask_and = OR(diff.SubBigInteger(5, 0), NOT(levels));
  322. tmask_or = AND(diff.SubBigInteger(5, 0), levels);
  323. tmask = Ones(64);
  324. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[0], 1), Ones( 1)), 32)), Replicate(Bits.Concat(Zeros( 1), Replicate(tmask_or[0], 1)), 32));
  325. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[1], 2), Ones( 2)), 16)), Replicate(Bits.Concat(Zeros( 2), Replicate(tmask_or[1], 2)), 16));
  326. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[2], 4), Ones( 4)), 8)), Replicate(Bits.Concat(Zeros( 4), Replicate(tmask_or[2], 4)), 8));
  327. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[3], 8), Ones( 8)), 4)), Replicate(Bits.Concat(Zeros( 8), Replicate(tmask_or[3], 8)), 4));
  328. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[4], 16), Ones(16)), 2)), Replicate(Bits.Concat(Zeros(16), Replicate(tmask_or[4], 16)), 2));
  329. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[5], 32), Ones(32)), 1)), Replicate(Bits.Concat(Zeros(32), Replicate(tmask_or[5], 32)), 1));
  330. // Compute "wraparound mask"
  331. wmask_and = OR(immr, NOT(levels));
  332. wmask_or = AND(immr, levels);
  333. wmask = Zeros(64);
  334. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones( 1), Replicate(wmask_and[0], 1)), 32)), Replicate(Bits.Concat(Replicate(wmask_or[0], 1), Zeros( 1)), 32));
  335. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones( 2), Replicate(wmask_and[1], 2)), 16)), Replicate(Bits.Concat(Replicate(wmask_or[1], 2), Zeros( 2)), 16));
  336. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones( 4), Replicate(wmask_and[2], 4)), 8)), Replicate(Bits.Concat(Replicate(wmask_or[2], 4), Zeros( 4)), 8));
  337. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones( 8), Replicate(wmask_and[3], 8)), 4)), Replicate(Bits.Concat(Replicate(wmask_or[3], 8), Zeros( 8)), 4));
  338. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones(16), Replicate(wmask_and[4], 16)), 2)), Replicate(Bits.Concat(Replicate(wmask_or[4], 16), Zeros(16)), 2));
  339. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones(32), Replicate(wmask_and[5], 32)), 1)), Replicate(Bits.Concat(Replicate(wmask_or[5], 32), Zeros(32)), 1));
  340. if (diff.SubBigInteger(6)) // borrow from S - R
  341. {
  342. wmask = AND(wmask, tmask);
  343. }
  344. else
  345. {
  346. wmask = OR(wmask, tmask);
  347. }
  348. return (wmask[M - 1, 0], tmask[M - 1, 0]);
  349. }
  350. #endregion
  351. #region "instrs/integer/shiftreg/"
  352. /* shared_pseudocode.html#impl-aarch64.DecodeShift.1 */
  353. public static ShiftType DecodeShift(Bits op)
  354. {
  355. switch (op)
  356. {
  357. default:
  358. case Bits bits when bits == "00":
  359. return ShiftType.ShiftType_LSL;
  360. case Bits bits when bits == "01":
  361. return ShiftType.ShiftType_LSR;
  362. case Bits bits when bits == "10":
  363. return ShiftType.ShiftType_ASR;
  364. case Bits bits when bits == "11":
  365. return ShiftType.ShiftType_ROR;
  366. }
  367. }
  368. /* shared_pseudocode.html#impl-aarch64.ShiftReg.3 */
  369. public static Bits ShiftReg(int N, int reg, ShiftType type, int amount)
  370. {
  371. Bits result = X(N, reg);
  372. switch (type)
  373. {
  374. default:
  375. case ShiftType.ShiftType_LSL:
  376. result = LSL(result, amount);
  377. break;
  378. case ShiftType.ShiftType_LSR:
  379. result = LSR(result, amount);
  380. break;
  381. case ShiftType.ShiftType_ASR:
  382. result = ASR(result, amount);
  383. break;
  384. case ShiftType.ShiftType_ROR:
  385. result = ROR(result, amount);
  386. break;
  387. }
  388. return result;
  389. }
  390. // shared_pseudocode.html#ShiftType
  391. public enum ShiftType {ShiftType_LSL, ShiftType_LSR, ShiftType_ASR, ShiftType_ROR};
  392. #endregion
  393. #region "instrs/vector/arithmetic/unary/cmp/compareop/"
  394. // shared_pseudocode.html#CompareOp
  395. public enum CompareOp {CompareOp_GT, CompareOp_GE, CompareOp_EQ, CompareOp_LE, CompareOp_LT};
  396. #endregion
  397. #region "instrs/vector/reduce/reduceop/"
  398. // shared_pseudocode.html#impl-aarch64.Reduce.3
  399. public static Bits Reduce(ReduceOp op, Bits input, int esize)
  400. {
  401. int N = input.Count;
  402. int half;
  403. Bits hi;
  404. Bits lo;
  405. Bits result = new Bits(esize);
  406. if (N == esize)
  407. {
  408. return new Bits(input); // Clone.
  409. }
  410. half = N / 2;
  411. hi = Reduce(op, input[N - 1, half], esize);
  412. lo = Reduce(op, input[half - 1, 0], esize);
  413. switch (op)
  414. {
  415. case ReduceOp.ReduceOp_FMINNUM:
  416. /* result = FPMinNum(lo, hi, FPCR); */
  417. break;
  418. case ReduceOp.ReduceOp_FMAXNUM:
  419. /* result = FPMaxNum(lo, hi, FPCR); */
  420. break;
  421. case ReduceOp.ReduceOp_FMIN:
  422. /* result = FPMin(lo, hi, FPCR); */
  423. break;
  424. case ReduceOp.ReduceOp_FMAX:
  425. /* result = FPMax(lo, hi, FPCR); */
  426. break;
  427. case ReduceOp.ReduceOp_FADD:
  428. /* result = FPAdd(lo, hi, FPCR); */
  429. break;
  430. default:
  431. case ReduceOp.ReduceOp_ADD:
  432. result = lo + hi;
  433. break;
  434. }
  435. return result;
  436. }
  437. // shared_pseudocode.html#ReduceOp
  438. public enum ReduceOp {ReduceOp_FMINNUM, ReduceOp_FMAXNUM,
  439. ReduceOp_FMIN, ReduceOp_FMAX,
  440. ReduceOp_FADD, ReduceOp_ADD};
  441. #endregion
  442. }
  443. internal static class Shared
  444. {
  445. static Shared()
  446. {
  447. _R = new Bits[31];
  448. for (int i = 0; i <= 30; i++)
  449. {
  450. _R[i] = new Bits(64, false);
  451. }
  452. _V = new Bits[32];
  453. for (int i = 0; i <= 31; i++)
  454. {
  455. _V[i] = new Bits(128, false);
  456. }
  457. SP_EL0 = new Bits(64, false);
  458. SP_EL1 = new Bits(64, false);
  459. FPCR = new Bits(32, false); // TODO: Add named fields.
  460. FPSR = new Bits(32, false); // TODO: Add named fields.
  461. PSTATE.N = false;
  462. PSTATE.Z = false;
  463. PSTATE.C = false;
  464. PSTATE.V = false;
  465. PSTATE.EL = EL1;
  466. PSTATE.SP = true;
  467. }
  468. #region "functions/common/"
  469. /* */
  470. public static Bits AND(Bits x, Bits y)
  471. {
  472. return x.And(y);
  473. }
  474. // shared_pseudocode.html#impl-shared.ASR.2
  475. public static Bits ASR(Bits x, int shift)
  476. {
  477. int N = x.Count;
  478. /* assert shift >= 0; */
  479. Bits result;
  480. if (shift == 0)
  481. {
  482. result = new Bits(x); // Clone.
  483. }
  484. else
  485. {
  486. (result, _) = ASR_C(x, shift);
  487. }
  488. return result;
  489. }
  490. // shared_pseudocode.html#impl-shared.ASR_C.2
  491. public static (Bits, bool) ASR_C(Bits x, int shift)
  492. {
  493. int N = x.Count;
  494. /* assert shift > 0; */
  495. Bits extended_x = SignExtend(x, shift + N);
  496. Bits result = extended_x[shift + N - 1, shift];
  497. bool carry_out = extended_x[shift - 1];
  498. return (result, carry_out);
  499. }
  500. // shared_pseudocode.html#impl-shared.Abs.1
  501. public static BigInteger Abs(BigInteger x)
  502. {
  503. return (x >= 0 ? x : -x);
  504. }
  505. // shared_pseudocode.html#impl-shared.BitCount.1
  506. public static int BitCount(Bits x)
  507. {
  508. int N = x.Count;
  509. int result = 0;
  510. for (int i = 0; i <= N - 1; i++)
  511. {
  512. if (x[i])
  513. {
  514. result = result + 1;
  515. }
  516. }
  517. return result;
  518. }
  519. // shared_pseudocode.html#impl-shared.CountLeadingSignBits.1
  520. public static int CountLeadingSignBits(Bits x)
  521. {
  522. int N = x.Count;
  523. return CountLeadingZeroBits(EOR(x[N - 1, 1], x[N - 2, 0]));
  524. }
  525. // shared_pseudocode.html#impl-shared.CountLeadingZeroBits.1
  526. public static int CountLeadingZeroBits(Bits x)
  527. {
  528. int N = x.Count;
  529. return (N - 1 - HighestSetBit(x));
  530. }
  531. // shared_pseudocode.html#impl-shared.Elem.read.3
  532. public static Bits Elem(/*in */Bits vector, int e, int size)
  533. {
  534. /* int N = vector.Count; */
  535. /* assert e >= 0 && (e+1)*size <= N; */
  536. return vector[e * size + size - 1, e * size];
  537. }
  538. // shared_pseudocode.html#impl-shared.Elem.write.3
  539. public static void Elem(/*out */Bits vector, int e, int size, Bits value)
  540. {
  541. /* int N = vector.Count; */
  542. /* assert e >= 0 && (e+1)*size <= N; */
  543. vector[(e + 1) * size - 1, e * size] = value;
  544. }
  545. /* */
  546. public static Bits EOR(Bits x, Bits y)
  547. {
  548. return x.Xor(y);
  549. }
  550. // shared_pseudocode.html#impl-shared.Extend.3
  551. public static Bits Extend(Bits x, int N, bool unsigned)
  552. {
  553. if (unsigned)
  554. {
  555. return ZeroExtend(x, N);
  556. }
  557. else
  558. {
  559. return SignExtend(x, N);
  560. }
  561. }
  562. /* shared_pseudocode.html#impl-shared.Extend.2 */
  563. public static Bits Extend(int N, Bits x, bool unsigned)
  564. {
  565. return Extend(x, N, unsigned);
  566. }
  567. // shared_pseudocode.html#impl-shared.HighestSetBit.1
  568. public static int HighestSetBit(Bits x)
  569. {
  570. int N = x.Count;
  571. for (int i = N - 1; i >= 0; i--)
  572. {
  573. if (x[i])
  574. {
  575. return i;
  576. }
  577. }
  578. return -1;
  579. }
  580. // shared_pseudocode.html#impl-shared.Int.2
  581. public static BigInteger Int(Bits x, bool unsigned)
  582. {
  583. return (unsigned ? UInt(x) : SInt(x));
  584. }
  585. // shared_pseudocode.html#impl-shared.IsOnes.1
  586. public static bool IsOnes(Bits x)
  587. {
  588. int N = x.Count;
  589. return (x == Ones(N));
  590. }
  591. // shared_pseudocode.html#impl-shared.IsZero.1
  592. public static bool IsZero(Bits x)
  593. {
  594. int N = x.Count;
  595. return (x == Zeros(N));
  596. }
  597. // shared_pseudocode.html#impl-shared.IsZeroBit.1
  598. public static bool IsZeroBit(Bits x)
  599. {
  600. return IsZero(x);
  601. }
  602. // shared_pseudocode.html#impl-shared.LSL.2
  603. public static Bits LSL(Bits x, int shift)
  604. {
  605. int N = x.Count;
  606. /* assert shift >= 0; */
  607. Bits result;
  608. if (shift == 0)
  609. {
  610. result = new Bits(x); // Clone.
  611. }
  612. else
  613. {
  614. (result, _) = LSL_C(x, shift);
  615. }
  616. return result;
  617. }
  618. // shared_pseudocode.html#impl-shared.LSL_C.2
  619. public static (Bits, bool) LSL_C(Bits x, int shift)
  620. {
  621. int N = x.Count;
  622. /* assert shift > 0; */
  623. Bits extended_x = Bits.Concat(x, Zeros(shift));
  624. Bits result = extended_x[N - 1, 0];
  625. bool carry_out = extended_x[N];
  626. return (result, carry_out);
  627. }
  628. // shared_pseudocode.html#impl-shared.LSR.2
  629. public static Bits LSR(Bits x, int shift)
  630. {
  631. int N = x.Count;
  632. /* assert shift >= 0; */
  633. Bits result;
  634. if (shift == 0)
  635. {
  636. result = new Bits(x); // Clone.
  637. }
  638. else
  639. {
  640. (result, _) = LSR_C(x, shift);
  641. }
  642. return result;
  643. }
  644. // shared_pseudocode.html#impl-shared.LSR_C.2
  645. public static (Bits, bool) LSR_C(Bits x, int shift)
  646. {
  647. int N = x.Count;
  648. /* assert shift > 0; */
  649. Bits extended_x = ZeroExtend(x, shift + N);
  650. Bits result = extended_x[shift + N - 1, shift];
  651. bool carry_out = extended_x[shift - 1];
  652. return (result, carry_out);
  653. }
  654. // shared_pseudocode.html#impl-shared.Min.2
  655. public static int Min(int a, int b)
  656. {
  657. if (a <= b)
  658. {
  659. return a;
  660. }
  661. else
  662. {
  663. return b;
  664. }
  665. }
  666. /* shared_pseudocode.html#impl-shared.NOT.1 */
  667. public static Bits NOT(Bits x)
  668. {
  669. return x.Not();
  670. }
  671. // shared_pseudocode.html#impl-shared.Ones.1
  672. /* shared_pseudocode.html#impl-shared.Ones.0 */
  673. public static Bits Ones(int N)
  674. {
  675. return Replicate(true, N);
  676. }
  677. /* */
  678. public static Bits OR(Bits x, Bits y)
  679. {
  680. return x.Or(y);
  681. }
  682. /* */
  683. public static decimal Real(BigInteger value)
  684. {
  685. return (decimal)value;
  686. }
  687. /* */
  688. public static float Real_32(BigInteger value)
  689. {
  690. if (value == BigInteger.Pow((BigInteger)2.0f, 1000))
  691. {
  692. return float.PositiveInfinity;
  693. }
  694. if (value == -BigInteger.Pow((BigInteger)2.0f, 1000))
  695. {
  696. return float.NegativeInfinity;
  697. }
  698. return (float)value;
  699. }
  700. /* */
  701. public static double Real_64(BigInteger value)
  702. {
  703. if (value == BigInteger.Pow((BigInteger)2.0, 10000))
  704. {
  705. return double.PositiveInfinity;
  706. }
  707. if (value == -BigInteger.Pow((BigInteger)2.0, 10000))
  708. {
  709. return double.NegativeInfinity;
  710. }
  711. return (double)value;
  712. }
  713. // shared_pseudocode.html#impl-shared.ROR.2
  714. public static Bits ROR(Bits x, int shift)
  715. {
  716. /* assert shift >= 0; */
  717. Bits result;
  718. if (shift == 0)
  719. {
  720. result = new Bits(x); // Clone.
  721. }
  722. else
  723. {
  724. (result, _) = ROR_C(x, shift);
  725. }
  726. return result;
  727. }
  728. // shared_pseudocode.html#impl-shared.ROR_C.2
  729. public static (Bits, bool) ROR_C(Bits x, int shift)
  730. {
  731. int N = x.Count;
  732. /* assert shift != 0; */
  733. int m = shift % N;
  734. Bits result = OR(LSR(x, m), LSL(x, N - m));
  735. bool carry_out = result[N - 1];
  736. return (result, carry_out);
  737. }
  738. /* shared_pseudocode.html#impl-shared.Replicate.1 */
  739. public static Bits Replicate(int N, Bits x)
  740. {
  741. int M = x.Count;
  742. /* assert N MOD M == 0; */
  743. return Replicate(x, N / M);
  744. }
  745. /* shared_pseudocode.html#impl-shared.Replicate.2 */
  746. public static Bits Replicate(Bits x, int N)
  747. {
  748. int M = x.Count;
  749. bool[] dst = new bool[M * N];
  750. for (int i = 0; i < N; i++)
  751. {
  752. x.CopyTo(dst, i * M);
  753. }
  754. return new Bits(dst);
  755. }
  756. /* shared_pseudocode.html#impl-shared.RoundDown.1 */
  757. public static BigInteger RoundDown(decimal x)
  758. {
  759. return (BigInteger)Decimal.Floor(x);
  760. }
  761. /* */
  762. public static BigInteger RoundDown_32(float x)
  763. {
  764. if (float.IsPositiveInfinity(x))
  765. {
  766. return BigInteger.Pow((BigInteger)2.0f, 1000);
  767. }
  768. if (float.IsNegativeInfinity(x))
  769. {
  770. return -BigInteger.Pow((BigInteger)2.0f, 1000);
  771. }
  772. return (BigInteger)MathF.Floor(x);
  773. }
  774. /* */
  775. public static BigInteger RoundDown_64(double x)
  776. {
  777. if (double.IsPositiveInfinity(x))
  778. {
  779. return BigInteger.Pow((BigInteger)2.0, 10000);
  780. }
  781. if (double.IsNegativeInfinity(x))
  782. {
  783. return -BigInteger.Pow((BigInteger)2.0, 10000);
  784. }
  785. return (BigInteger)Math.Floor(x);
  786. }
  787. // shared_pseudocode.html#impl-shared.RoundTowardsZero.1
  788. public static BigInteger RoundTowardsZero(decimal x)
  789. {
  790. if (x == 0.0m)
  791. {
  792. return (BigInteger)0m;
  793. }
  794. else if (x >= 0.0m)
  795. {
  796. return RoundDown(x);
  797. }
  798. else
  799. {
  800. return RoundUp(x);
  801. }
  802. }
  803. /* shared_pseudocode.html#impl-shared.RoundUp.1 */
  804. public static BigInteger RoundUp(decimal x)
  805. {
  806. return (BigInteger)Decimal.Ceiling(x);
  807. }
  808. // shared_pseudocode.html#impl-shared.SInt.1
  809. public static BigInteger SInt(Bits x)
  810. {
  811. int N = x.Count;
  812. BigInteger result = 0;
  813. for (int i = 0; i <= N - 1; i++)
  814. {
  815. if (x[i])
  816. {
  817. result = result + BigInteger.Pow(2, i);
  818. }
  819. }
  820. if (x[N - 1])
  821. {
  822. result = result - BigInteger.Pow(2, N);
  823. }
  824. return result;
  825. }
  826. // shared_pseudocode.html#impl-shared.SignExtend.2
  827. public static Bits SignExtend(Bits x, int N)
  828. {
  829. int M = x.Count;
  830. /* assert N >= M; */
  831. return Bits.Concat(Replicate(x[M - 1], N - M), x);
  832. }
  833. /* shared_pseudocode.html#impl-shared.SignExtend.1 */
  834. public static Bits SignExtend(int N, Bits x)
  835. {
  836. return SignExtend(x, N);
  837. }
  838. // shared_pseudocode.html#impl-shared.UInt.1
  839. public static BigInteger UInt(Bits x)
  840. {
  841. int N = x.Count;
  842. BigInteger result = 0;
  843. for (int i = 0; i <= N - 1; i++)
  844. {
  845. if (x[i])
  846. {
  847. result = result + BigInteger.Pow(2, i);
  848. }
  849. }
  850. return result;
  851. }
  852. // shared_pseudocode.html#impl-shared.ZeroExtend.2
  853. public static Bits ZeroExtend(Bits x, int N)
  854. {
  855. int M = x.Count;
  856. /* assert N >= M; */
  857. return Bits.Concat(Zeros(N - M), x);
  858. }
  859. /* shared_pseudocode.html#impl-shared.ZeroExtend.1 */
  860. public static Bits ZeroExtend(int N, Bits x)
  861. {
  862. return ZeroExtend(x, N);
  863. }
  864. // shared_pseudocode.html#impl-shared.Zeros.1
  865. /* shared_pseudocode.html#impl-shared.Zeros.0 */
  866. public static Bits Zeros(int N)
  867. {
  868. return Replicate(false, N);
  869. }
  870. #endregion
  871. #region "functions/crc/"
  872. // shared_pseudocode.html#impl-shared.BitReverse.1
  873. public static Bits BitReverse(Bits data)
  874. {
  875. int N = data.Count;
  876. Bits result = new Bits(N);
  877. for (int i = 0; i <= N - 1; i++)
  878. {
  879. result[N - i - 1] = data[i];
  880. }
  881. return result;
  882. }
  883. // shared_pseudocode.html#impl-shared.Poly32Mod2.2
  884. public static Bits Poly32Mod2(Bits _data, Bits poly)
  885. {
  886. int N = _data.Count;
  887. /* assert N > 32; */
  888. Bits data = new Bits(_data); // Clone.
  889. for (int i = N - 1; i >= 32; i--)
  890. {
  891. if (data[i])
  892. {
  893. data[i - 1, 0] = EOR(data[i - 1, 0], Bits.Concat(poly, Zeros(i - 32)));
  894. }
  895. }
  896. return data[31, 0];
  897. }
  898. #endregion
  899. #region "functions/crypto/"
  900. // shared_pseudocode.html#impl-shared.ROL.2
  901. public static Bits ROL(Bits x, int shift)
  902. {
  903. int N = x.Count;
  904. /* assert shift >= 0 && shift <= N; */
  905. if (shift == 0)
  906. {
  907. return new Bits(x); // Clone.
  908. }
  909. return ROR(x, N - shift);
  910. }
  911. // shared_pseudocode.html#impl-shared.SHA256hash.4
  912. public static Bits SHA256hash(Bits _X, Bits _Y, Bits W, bool part1)
  913. {
  914. Bits X = new Bits(_X); // Clone.
  915. Bits Y = new Bits(_Y); // Clone.
  916. Bits chs, maj, t; // bits(32)
  917. for (int e = 0; e <= 3; e++)
  918. {
  919. chs = SHAchoose(Y[31, 0], Y[63, 32], Y[95, 64]);
  920. maj = SHAmajority(X[31, 0], X[63, 32], X[95, 64]);
  921. t = Y[127, 96] + SHAhashSIGMA1(Y[31, 0]) + chs + Elem(W, e, 32);
  922. X[127, 96] = t + X[127, 96];
  923. Y[127, 96] = t + SHAhashSIGMA0(X[31, 0]) + maj;
  924. // TODO: Implement ASL: "<,>" as C#: "Bits.Split()".
  925. /* <Y, X> = ROL(Y : X, 32); */
  926. Bits YX = ROL(Bits.Concat(Y, X), 32);
  927. Y = YX[255, 128];
  928. X = YX[127, 0];
  929. }
  930. return (part1 ? X : Y);
  931. }
  932. // shared_pseudocode.html#impl-shared.SHAchoose.3
  933. public static Bits SHAchoose(Bits x, Bits y, Bits z)
  934. {
  935. return EOR(AND(EOR(y, z), x), z);
  936. }
  937. // shared_pseudocode.html#impl-shared.SHAhashSIGMA0.1
  938. public static Bits SHAhashSIGMA0(Bits x)
  939. {
  940. return EOR(EOR(ROR(x, 2), ROR(x, 13)), ROR(x, 22));
  941. }
  942. // shared_pseudocode.html#impl-shared.SHAhashSIGMA1.1
  943. public static Bits SHAhashSIGMA1(Bits x)
  944. {
  945. return EOR(EOR(ROR(x, 6), ROR(x, 11)), ROR(x, 25));
  946. }
  947. // shared_pseudocode.html#impl-shared.SHAmajority.3
  948. public static Bits SHAmajority(Bits x, Bits y, Bits z)
  949. {
  950. return OR(AND(x, y), AND(OR(x, y), z));
  951. }
  952. #endregion
  953. #region "functions/float/fpdecoderounding/"
  954. /* shared_pseudocode.html#impl-shared.FPDecodeRounding.1 */
  955. public static FPRounding FPDecodeRounding(Bits rmode)
  956. {
  957. switch (rmode)
  958. {
  959. default:
  960. case Bits bits when bits == "00":
  961. return FPRounding.FPRounding_TIEEVEN; // N
  962. case Bits bits when bits == "01":
  963. return FPRounding.FPRounding_POSINF; // P
  964. case Bits bits when bits == "10":
  965. return FPRounding.FPRounding_NEGINF; // M
  966. case Bits bits when bits == "11":
  967. return FPRounding.FPRounding_ZERO; // Z
  968. }
  969. }
  970. #endregion
  971. #region "functions/float/fpexc/"
  972. // shared_pseudocode.html#FPExc
  973. public enum FPExc {FPExc_InvalidOp, FPExc_DivideByZero, FPExc_Overflow,
  974. FPExc_Underflow, FPExc_Inexact, FPExc_InputDenorm};
  975. #endregion
  976. #region "functions/float/fpprocessexception/"
  977. // shared_pseudocode.html#impl-shared.FPProcessException.2
  978. public static void FPProcessException(FPExc exception, Bits _fpcr)
  979. {
  980. Bits fpcr = new Bits(_fpcr); // Clone.
  981. int cumul;
  982. // Determine the cumulative exception bit number
  983. switch (exception)
  984. {
  985. default:
  986. case FPExc.FPExc_InvalidOp: cumul = 0; break;
  987. case FPExc.FPExc_DivideByZero: cumul = 1; break;
  988. case FPExc.FPExc_Overflow: cumul = 2; break;
  989. case FPExc.FPExc_Underflow: cumul = 3; break;
  990. case FPExc.FPExc_Inexact: cumul = 4; break;
  991. case FPExc.FPExc_InputDenorm: cumul = 7; break;
  992. }
  993. int enable = cumul + 8;
  994. if (fpcr[enable])
  995. {
  996. // Trapping of the exception enabled.
  997. // It is IMPLEMENTATION DEFINED whether the enable bit may be set at all, and
  998. // if so then how exceptions may be accumulated before calling FPTrapException()
  999. /* IMPLEMENTATION_DEFINED "floating-point trap handling"; */
  1000. throw new NotImplementedException();
  1001. }/*
  1002. else if (UsingAArch32())
  1003. {
  1004. // Set the cumulative exception bit
  1005. FPSCR<cumul> = '1';
  1006. }*/
  1007. else
  1008. {
  1009. // Set the cumulative exception bit
  1010. FPSR[cumul] = true;
  1011. }
  1012. }
  1013. #endregion
  1014. #region "functions/float/fprounding/"
  1015. // shared_pseudocode.html#FPRounding
  1016. public enum FPRounding {FPRounding_TIEEVEN, FPRounding_POSINF,
  1017. FPRounding_NEGINF, FPRounding_ZERO,
  1018. FPRounding_TIEAWAY, FPRounding_ODD};
  1019. #endregion
  1020. #region "functions/float/fptofixed/"
  1021. /* shared_pseudocode.html#impl-shared.FPToFixed.5 */
  1022. public static Bits FPToFixed(int M, Bits op, int fbits, bool unsigned, Bits _fpcr, FPRounding rounding)
  1023. {
  1024. int N = op.Count;
  1025. /* assert N IN {16,32,64}; */
  1026. /* assert M IN {16,32,64}; */
  1027. /* assert fbits >= 0; */
  1028. /* assert rounding != FPRounding_ODD; */
  1029. Bits fpcr = new Bits(_fpcr); // Clone.
  1030. if (N == 16)
  1031. {
  1032. throw new NotImplementedException();
  1033. }
  1034. else if (N == 32)
  1035. {
  1036. // Unpack using fpcr to determine if subnormals are flushed-to-zero
  1037. (FPType type, bool sign, float value) = FPUnpack_32(op, fpcr);
  1038. // If NaN, set cumulative flag or take exception
  1039. if (type == FPType.FPType_SNaN || type == FPType.FPType_QNaN)
  1040. {
  1041. FPProcessException(FPExc.FPExc_InvalidOp, fpcr);
  1042. }
  1043. // Scale by fractional bits and produce integer rounded towards minus-infinity
  1044. value = value * MathF.Pow(2.0f, fbits);
  1045. BigInteger int_result = RoundDown_32(value);
  1046. float error = value - Real_32(int_result);
  1047. if (float.IsNaN(error))
  1048. {
  1049. error = 0.0f;
  1050. }
  1051. // Determine whether supplied rounding mode requires an increment
  1052. bool round_up;
  1053. switch (rounding)
  1054. {
  1055. default:
  1056. case FPRounding.FPRounding_TIEEVEN:
  1057. round_up = (error > 0.5f || (error == 0.5f && int_result.SubBigInteger(0)));
  1058. break;
  1059. case FPRounding.FPRounding_POSINF:
  1060. round_up = (error != 0.0f);
  1061. break;
  1062. case FPRounding.FPRounding_NEGINF:
  1063. round_up = false;
  1064. break;
  1065. case FPRounding.FPRounding_ZERO:
  1066. round_up = (error != 0.0f && int_result < (BigInteger)0);
  1067. break;
  1068. case FPRounding.FPRounding_TIEAWAY:
  1069. round_up = (error > 0.5f || (error == 0.5f && int_result >= (BigInteger)0));
  1070. break;
  1071. }
  1072. if (round_up)
  1073. {
  1074. int_result = int_result + 1;
  1075. }
  1076. // Generate saturated result and exceptions
  1077. (Bits result, bool overflow) = SatQ(int_result, M, unsigned);
  1078. if (overflow)
  1079. {
  1080. FPProcessException(FPExc.FPExc_InvalidOp, fpcr);
  1081. }
  1082. else if (error != 0.0f)
  1083. {
  1084. FPProcessException(FPExc.FPExc_Inexact, fpcr);
  1085. }
  1086. return result;
  1087. }
  1088. else /* if (N == 64) */
  1089. {
  1090. // Unpack using fpcr to determine if subnormals are flushed-to-zero
  1091. (FPType type, bool sign, double value) = FPUnpack_64(op, fpcr);
  1092. // If NaN, set cumulative flag or take exception
  1093. if (type == FPType.FPType_SNaN || type == FPType.FPType_QNaN)
  1094. {
  1095. FPProcessException(FPExc.FPExc_InvalidOp, fpcr);
  1096. }
  1097. // Scale by fractional bits and produce integer rounded towards minus-infinity
  1098. value = value * Math.Pow(2.0, fbits);
  1099. BigInteger int_result = RoundDown_64(value);
  1100. double error = value - Real_64(int_result);
  1101. if (double.IsNaN(error))
  1102. {
  1103. error = 0.0;
  1104. }
  1105. // Determine whether supplied rounding mode requires an increment
  1106. bool round_up;
  1107. switch (rounding)
  1108. {
  1109. default:
  1110. case FPRounding.FPRounding_TIEEVEN:
  1111. round_up = (error > 0.5 || (error == 0.5 && int_result.SubBigInteger(0)));
  1112. break;
  1113. case FPRounding.FPRounding_POSINF:
  1114. round_up = (error != 0.0);
  1115. break;
  1116. case FPRounding.FPRounding_NEGINF:
  1117. round_up = false;
  1118. break;
  1119. case FPRounding.FPRounding_ZERO:
  1120. round_up = (error != 0.0 && int_result < (BigInteger)0);
  1121. break;
  1122. case FPRounding.FPRounding_TIEAWAY:
  1123. round_up = (error > 0.5 || (error == 0.5 && int_result >= (BigInteger)0));
  1124. break;
  1125. }
  1126. if (round_up)
  1127. {
  1128. int_result = int_result + 1;
  1129. }
  1130. // Generate saturated result and exceptions
  1131. (Bits result, bool overflow) = SatQ(int_result, M, unsigned);
  1132. if (overflow)
  1133. {
  1134. FPProcessException(FPExc.FPExc_InvalidOp, fpcr);
  1135. }
  1136. else if (error != 0.0)
  1137. {
  1138. FPProcessException(FPExc.FPExc_Inexact, fpcr);
  1139. }
  1140. return result;
  1141. }
  1142. }
  1143. #endregion
  1144. #region "functions/float/fptype/"
  1145. // shared_pseudocode.html#FPType
  1146. public enum FPType {FPType_Nonzero, FPType_Zero, FPType_Infinity,
  1147. FPType_QNaN, FPType_SNaN};
  1148. #endregion
  1149. #region "functions/float/fpunpack/"
  1150. /* shared_pseudocode.html#impl-shared.FPUnpack.2 */
  1151. /* shared_pseudocode.html#impl-shared.FPUnpackBase.2 */
  1152. /*public static (FPType, bool, real) FPUnpack_16(Bits fpval, Bits _fpcr)
  1153. {
  1154. int N = fpval.Count;
  1155. // assert N == 16;
  1156. Bits fpcr = new Bits(_fpcr); // Clone.
  1157. fpcr[26] = false;
  1158. return FPUnpackBase_16(fpval, fpcr);
  1159. }*/
  1160. public static (FPType, bool, float) FPUnpack_32(Bits fpval, Bits _fpcr)
  1161. {
  1162. int N = fpval.Count;
  1163. /* assert N == 32; */
  1164. Bits fpcr = new Bits(_fpcr); // Clone.
  1165. FPType type;
  1166. float value;
  1167. bool sign = fpval[31];
  1168. Bits exp32 = fpval[30, 23];
  1169. Bits frac32 = fpval[22, 0];
  1170. if (IsZero(exp32))
  1171. {
  1172. // Produce zero if value is zero or flush-to-zero is selected.
  1173. if (IsZero(frac32) || fpcr[24])
  1174. {
  1175. type = FPType.FPType_Zero;
  1176. value = 0.0f;
  1177. // Denormalized input flushed to zero
  1178. if (!IsZero(frac32))
  1179. {
  1180. FPProcessException(FPExc.FPExc_InputDenorm, fpcr);
  1181. }
  1182. }
  1183. else
  1184. {
  1185. type = FPType.FPType_Nonzero;
  1186. value = MathF.Pow(2.0f, -126) * (Real_32(UInt(frac32)) * MathF.Pow(2.0f, -23));
  1187. }
  1188. }
  1189. else if (IsOnes(exp32))
  1190. {
  1191. if (IsZero(frac32))
  1192. {
  1193. type = FPType.FPType_Infinity;
  1194. /* value = 2.0^1000000; */
  1195. value = MathF.Pow(2.0f, 1000);
  1196. }
  1197. else
  1198. {
  1199. type = frac32[22] ? FPType.FPType_QNaN : FPType.FPType_SNaN;
  1200. value = 0.0f;
  1201. }
  1202. }
  1203. else
  1204. {
  1205. type = FPType.FPType_Nonzero;
  1206. value = MathF.Pow(2.0f, (int)UInt(exp32) - 127) * (1.0f + Real_32(UInt(frac32)) * MathF.Pow(2.0f, -23));
  1207. }
  1208. if (sign)
  1209. {
  1210. value = -value;
  1211. }
  1212. return (type, sign, value);
  1213. }
  1214. public static (FPType, bool, double) FPUnpack_64(Bits fpval, Bits _fpcr)
  1215. {
  1216. int N = fpval.Count;
  1217. /* assert N == 64; */
  1218. Bits fpcr = new Bits(_fpcr); // Clone.
  1219. FPType type;
  1220. double value;
  1221. bool sign = fpval[63];
  1222. Bits exp64 = fpval[62, 52];
  1223. Bits frac64 = fpval[51, 0];
  1224. if (IsZero(exp64))
  1225. {
  1226. // Produce zero if value is zero or flush-to-zero is selected.
  1227. if (IsZero(frac64) || fpcr[24])
  1228. {
  1229. type = FPType.FPType_Zero;
  1230. value = 0.0;
  1231. // Denormalized input flushed to zero
  1232. if (!IsZero(frac64))
  1233. {
  1234. FPProcessException(FPExc.FPExc_InputDenorm, fpcr);
  1235. }
  1236. }
  1237. else
  1238. {
  1239. type = FPType.FPType_Nonzero;
  1240. value = Math.Pow(2.0, -1022) * (Real_64(UInt(frac64)) * Math.Pow(2.0, -52));
  1241. }
  1242. }
  1243. else if (IsOnes(exp64))
  1244. {
  1245. if (IsZero(frac64))
  1246. {
  1247. type = FPType.FPType_Infinity;
  1248. /* value = 2.0^1000000; */
  1249. value = Math.Pow(2.0, 10000);
  1250. }
  1251. else
  1252. {
  1253. type = frac64[51] ? FPType.FPType_QNaN : FPType.FPType_SNaN;
  1254. value = 0.0;
  1255. }
  1256. }
  1257. else
  1258. {
  1259. type = FPType.FPType_Nonzero;
  1260. value = Math.Pow(2.0, (int)UInt(exp64) - 1023) * (1.0 + Real_64(UInt(frac64)) * Math.Pow(2.0, -52));
  1261. }
  1262. if (sign)
  1263. {
  1264. value = -value;
  1265. }
  1266. return (type, sign, value);
  1267. }
  1268. /* shared_pseudocode.html#impl-shared.FPUnpackCV.2 */
  1269. /* shared_pseudocode.html#impl-shared.FPUnpackBase.2 */
  1270. /*public static (FPType, bool, real) FPUnpackCV_16(Bits fpval, Bits _fpcr)
  1271. {
  1272. int N = fpval.Count;
  1273. // assert N == 16;
  1274. Bits fpcr = new Bits(_fpcr); // Clone.
  1275. fpcr[19] = false;
  1276. return FPUnpackBase_16(fpval, fpcr);
  1277. }*/
  1278. public static (FPType, bool, float) FPUnpackCV_32(Bits fpval, Bits _fpcr)
  1279. {
  1280. return FPUnpack_32(fpval, _fpcr);
  1281. }
  1282. public static (FPType, bool, double) FPUnpackCV_64(Bits fpval, Bits _fpcr)
  1283. {
  1284. return FPUnpack_64(fpval, _fpcr);
  1285. }
  1286. #endregion
  1287. #region "functions/integer/"
  1288. /* shared_pseudocode.html#impl-shared.AddWithCarry.3 */
  1289. public static (Bits, Bits) AddWithCarry(int N, Bits x, Bits y, bool carry_in)
  1290. {
  1291. BigInteger unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
  1292. BigInteger signed_sum = SInt(x) + SInt(y) + UInt(carry_in);
  1293. Bits result = unsigned_sum.SubBigInteger(N - 1, 0); // same value as signed_sum<N-1:0>
  1294. bool n = result[N - 1];
  1295. bool z = IsZero(result);
  1296. bool c = !(UInt(result) == unsigned_sum);
  1297. bool v = !(SInt(result) == signed_sum);
  1298. return (result, Bits.Concat(n, z, c, v));
  1299. }
  1300. #endregion
  1301. #region "functions/registers/"
  1302. public static readonly Bits[] _R;
  1303. public static readonly Bits[] _V;
  1304. public static Bits SP_EL0;
  1305. public static Bits SP_EL1;
  1306. public static Bits FPCR; // TODO: Add named fields.
  1307. // [ 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 22 | 21 20 | 19 | 18 17 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 ]
  1308. // [ 0 | 0 | 0 | 0 | 0 | AHP | DN | FZ | RMode | Stride | FZ16 | Len | IDE | 0 | 0 | IXE | UFE | OFE | DZE | IOE | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 ]
  1309. public static Bits FPSR; // TODO: Add named fields.
  1310. // [ 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 ]
  1311. // [ N | Z | C | V | QC | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | IDC | 0 | 0 | IXC | UFC | OFC | DZC | IOC ]
  1312. #endregion
  1313. #region "functions/system/"
  1314. // shared_pseudocode.html#impl-shared.ConditionHolds.1
  1315. public static bool ConditionHolds(Bits cond)
  1316. {
  1317. bool result;
  1318. // Evaluate base condition.
  1319. switch (cond[3, 1])
  1320. {
  1321. case Bits bits when bits == "000":
  1322. result = (PSTATE.Z == true); // EQ or NE
  1323. break;
  1324. case Bits bits when bits == "001":
  1325. result = (PSTATE.C == true); // CS or CC
  1326. break;
  1327. case Bits bits when bits == "010":
  1328. result = (PSTATE.N == true); // MI or PL
  1329. break;
  1330. case Bits bits when bits == "011":
  1331. result = (PSTATE.V == true); // VS or VC
  1332. break;
  1333. case Bits bits when bits == "100":
  1334. result = (PSTATE.C == true && PSTATE.Z == false); // HI or LS
  1335. break;
  1336. case Bits bits when bits == "101":
  1337. result = (PSTATE.N == PSTATE.V); // GE or LT
  1338. break;
  1339. case Bits bits when bits == "110":
  1340. result = (PSTATE.N == PSTATE.V && PSTATE.Z == false); // GT or LE
  1341. break;
  1342. default:
  1343. case Bits bits when bits == "111":
  1344. result = true; // AL
  1345. break;
  1346. }
  1347. // Condition flag values in the set '111x' indicate always true
  1348. // Otherwise, invert condition if necessary.
  1349. if (cond[0] == true && cond != "1111")
  1350. {
  1351. result = !result;
  1352. }
  1353. return result;
  1354. }
  1355. // shared_pseudocode.html#EL3
  1356. public static readonly Bits EL3 = "11";
  1357. // shared_pseudocode.html#EL2
  1358. public static readonly Bits EL2 = "10";
  1359. // shared_pseudocode.html#EL1
  1360. public static readonly Bits EL1 = "01";
  1361. // shared_pseudocode.html#EL0
  1362. public static readonly Bits EL0 = "00";
  1363. /* shared_pseudocode.html#impl-shared.HaveEL.1 */
  1364. public static bool HaveEL(Bits el)
  1365. {
  1366. // TODO: Implement ASL: "IN" as C#: "Bits.In()".
  1367. /* if el IN {EL1,EL0} then */
  1368. if (el == EL1 || el == EL0)
  1369. {
  1370. return true; // EL1 and EL0 must exist
  1371. }
  1372. /* return boolean IMPLEMENTATION_DEFINED; */
  1373. return false;
  1374. }
  1375. public static ProcState PSTATE;
  1376. /* shared_pseudocode.html#ProcState */
  1377. internal struct ProcState
  1378. {
  1379. public void NZCV(Bits nzcv) // ASL: ".<,,,>".
  1380. {
  1381. N = nzcv[3];
  1382. Z = nzcv[2];
  1383. C = nzcv[1];
  1384. V = nzcv[0];
  1385. }
  1386. public void NZCV(bool n, bool z, bool c, bool v) // ASL: ".<,,,>".
  1387. {
  1388. N = n;
  1389. Z = z;
  1390. C = c;
  1391. V = v;
  1392. }
  1393. public bool N; // Negative condition flag
  1394. public bool Z; // Zero condition flag
  1395. public bool C; // Carry condition flag
  1396. public bool V; // oVerflow condition flag
  1397. public Bits EL; // Exception Level
  1398. public bool SP; // Stack pointer select: 0=SP0, 1=SPx [AArch64 only]
  1399. }
  1400. #endregion
  1401. #region "functions/vector/"
  1402. // shared_pseudocode.html#impl-shared.SatQ.3
  1403. public static (Bits, bool) SatQ(BigInteger i, int N, bool unsigned)
  1404. {
  1405. (Bits result, bool sat) = (unsigned ? UnsignedSatQ(i, N) : SignedSatQ(i, N));
  1406. return (result, sat);
  1407. }
  1408. // shared_pseudocode.html#impl-shared.SignedSatQ.2
  1409. public static (Bits, bool) SignedSatQ(BigInteger i, int N)
  1410. {
  1411. BigInteger result;
  1412. bool saturated;
  1413. if (i > BigInteger.Pow(2, N - 1) - 1)
  1414. {
  1415. result = BigInteger.Pow(2, N - 1) - 1;
  1416. saturated = true;
  1417. }
  1418. else if (i < -(BigInteger.Pow(2, N - 1)))
  1419. {
  1420. result = -(BigInteger.Pow(2, N - 1));
  1421. saturated = true;
  1422. }
  1423. else
  1424. {
  1425. result = i;
  1426. saturated = false;
  1427. }
  1428. return (result.SubBigInteger(N - 1, 0), saturated);
  1429. }
  1430. // shared_pseudocode.html#impl-shared.UnsignedSatQ.2
  1431. public static (Bits, bool) UnsignedSatQ(BigInteger i, int N)
  1432. {
  1433. BigInteger result;
  1434. bool saturated;
  1435. if (i > BigInteger.Pow(2, N) - 1)
  1436. {
  1437. result = BigInteger.Pow(2, N) - 1;
  1438. saturated = true;
  1439. }
  1440. else if (i < (BigInteger)0)
  1441. {
  1442. result = (BigInteger)0;
  1443. saturated = true;
  1444. }
  1445. else
  1446. {
  1447. result = i;
  1448. saturated = false;
  1449. }
  1450. return (result.SubBigInteger(N - 1, 0), saturated);
  1451. }
  1452. #endregion
  1453. }
  1454. }