SoftFPU.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "SoftFPU.h"
  8. #include "Emulator.h"
  9. #include "SoftCPU.h"
  10. #include "ValueWithShadow.h"
  11. #include <AK/BitCast.h>
  12. #include <AK/NumericLimits.h>
  13. #include <AK/UFixedBigInt.h>
  14. #include <unistd.h>
  15. #if defined(AK_COMPILER_GCC)
  16. # pragma GCC optimize("O3")
  17. #endif
  18. #define TODO_INSN() \
  19. do { \
  20. reportln("\n=={}== Unimplemented instruction: {}\n"sv, getpid(), __FUNCTION__); \
  21. m_emulator.dump_backtrace(); \
  22. _exit(0); \
  23. } while (0)
  24. template<typename T>
  25. ALWAYS_INLINE void warn_if_uninitialized(T value_with_shadow, char const* message)
  26. {
  27. if (value_with_shadow.is_uninitialized()) [[unlikely]] {
  28. reportln("\033[31;1mWarning! Use of uninitialized value: {}\033[0m\n"sv, message);
  29. UserspaceEmulator::Emulator::the().dump_backtrace();
  30. }
  31. }
  32. namespace UserspaceEmulator { // NOLINT(readability-implicit-bool-conversion) 0/1 to follow spec closer
  33. ALWAYS_INLINE void SoftFPU::warn_if_mmx_absolute(u8 index) const
  34. {
  35. if (m_reg_is_mmx[index]) [[unlikely]] {
  36. reportln("\033[31;1mWarning! Use of an MMX register as an FPU value ({} abs)\033[0m\n"sv, index);
  37. m_emulator.dump_backtrace();
  38. }
  39. }
  40. ALWAYS_INLINE void SoftFPU::warn_if_fpu_absolute(u8 index) const
  41. {
  42. if (!m_reg_is_mmx[index]) [[unlikely]] {
  43. reportln("\033[31;1mWarning! Use of an FPU value ({} abs) as an MMX register\033[0m\n"sv, index);
  44. m_emulator.dump_backtrace();
  45. }
  46. }
  47. ALWAYS_INLINE long double SoftFPU::fpu_get(u8 index)
  48. {
  49. VERIFY(index < 8);
  50. if (!fpu_is_set(index))
  51. fpu_set_stack_underflow();
  52. warn_if_mmx_absolute(index);
  53. u8 effective_index = (m_fpu_stack_top + index) % 8;
  54. return m_storage[effective_index].fp;
  55. }
  56. ALWAYS_INLINE void SoftFPU::fpu_set_absolute(u8 index, long double value)
  57. {
  58. VERIFY(index < 8);
  59. set_tag_from_value_absolute(index, value);
  60. m_storage[index].fp = value;
  61. m_reg_is_mmx[index] = false;
  62. }
  63. ALWAYS_INLINE void SoftFPU::fpu_set(u8 index, long double value)
  64. {
  65. VERIFY(index < 8);
  66. fpu_set_absolute((m_fpu_stack_top + index) % 8, value);
  67. }
  68. MMX SoftFPU::mmx_get(u8 index) const
  69. {
  70. VERIFY(index < 8);
  71. warn_if_fpu_absolute(index);
  72. return m_storage[index].mmx;
  73. }
  74. void SoftFPU::mmx_set(u8 index, MMX value)
  75. {
  76. m_storage[index].mmx = value;
  77. // The high bytes are set to 0b11... to make the floating-point value NaN.
  78. // This way we are technically able to find out if we are reading the wrong
  79. // type, but this is still difficult, so we use our own lookup for that
  80. m_storage[index].__high = 0xFFFFU;
  81. m_reg_is_mmx[index] = true;
  82. }
  83. ALWAYS_INLINE void SoftFPU::fpu_push(long double value)
  84. {
  85. if (fpu_is_set(7))
  86. fpu_set_stack_overflow();
  87. m_fpu_stack_top = (m_fpu_stack_top - 1u) % 8;
  88. fpu_set(0, value);
  89. }
  90. ALWAYS_INLINE long double SoftFPU::fpu_pop()
  91. {
  92. warn_if_mmx_absolute(m_fpu_stack_top);
  93. if (!fpu_is_set(0))
  94. fpu_set_stack_underflow();
  95. auto ret = fpu_get(0);
  96. fpu_set_tag(0, FPU_Tag::Empty);
  97. m_fpu_stack_top = (m_fpu_stack_top + 1u) % 8;
  98. return ret;
  99. }
  100. ALWAYS_INLINE void SoftFPU::fpu_set_exception(FPU_Exception ex)
  101. {
  102. switch (ex) {
  103. case FPU_Exception::StackFault:
  104. m_fpu_error_stackfault = 1;
  105. m_fpu_error_invalid = 1; // Implies InvalidOperation
  106. break;
  107. case FPU_Exception::InvalidOperation:
  108. m_fpu_error_invalid = 1;
  109. if (!m_fpu_cw.mask_invalid)
  110. break;
  111. return;
  112. case FPU_Exception::DenormalizedOperand:
  113. m_fpu_error_denorm = 1;
  114. if (!m_fpu_cw.mask_denorm)
  115. break;
  116. return;
  117. case FPU_Exception::ZeroDivide:
  118. m_fpu_error_zero_div = 1;
  119. if (!m_fpu_cw.mask_zero_div)
  120. break;
  121. return;
  122. case FPU_Exception::Overflow:
  123. m_fpu_error_overflow = 1;
  124. if (!m_fpu_cw.mask_overflow)
  125. break;
  126. return;
  127. case FPU_Exception::Underflow:
  128. m_fpu_error_underflow = 1;
  129. if (!m_fpu_cw.mask_underflow)
  130. break;
  131. return;
  132. case FPU_Exception::Precision:
  133. m_fpu_error_precision = 1;
  134. if (!m_fpu_cw.mask_precision)
  135. break;
  136. return;
  137. }
  138. // set exception bit
  139. m_fpu_error_summary = 1;
  140. // FIXME: set traceback
  141. // For that we need to get the currently executing instruction and
  142. // the previous eip
  143. // FIXME: Call FPU Exception handler
  144. reportln("Trying to call Exception handler from {}"sv, fpu_exception_string(ex));
  145. fpu_dump_env();
  146. m_emulator.dump_backtrace();
  147. TODO();
  148. }
  149. template<Arithmetic T>
  150. ALWAYS_INLINE T SoftFPU::round_checked(long double value)
  151. {
  152. T result = static_cast<T>(rintl(value));
  153. if (result != value)
  154. fpu_set_exception(FPU_Exception::Precision);
  155. if (result > value)
  156. set_c1(1);
  157. else
  158. set_c1(0);
  159. return result;
  160. }
  161. template<FloatingPoint T>
  162. ALWAYS_INLINE T SoftFPU::convert_checked(long double value)
  163. {
  164. T result = static_cast<T>(value);
  165. if (auto rnd = value - result) {
  166. if (rnd > 0)
  167. set_c1(1);
  168. else
  169. set_c1(0);
  170. fpu_set_exception(FPU_Exception::Precision);
  171. }
  172. return result;
  173. }
  174. // Instructions
  175. // DATA TRANSFER
  176. void SoftFPU::FLD_RM32(const X86::Instruction& insn)
  177. {
  178. if (insn.modrm().is_register()) {
  179. fpu_push(fpu_get(insn.modrm().register_index()));
  180. } else {
  181. auto new_f32 = insn.modrm().read32(m_cpu, insn);
  182. // FIXME: Respect shadow values
  183. fpu_push(bit_cast<float>(new_f32.value()));
  184. }
  185. }
  186. void SoftFPU::FLD_RM64(const X86::Instruction& insn)
  187. {
  188. VERIFY(!insn.modrm().is_register());
  189. auto new_f64 = insn.modrm().read64(m_cpu, insn);
  190. // FIXME: Respect shadow values
  191. fpu_push(bit_cast<double>(new_f64.value()));
  192. }
  193. void SoftFPU::FLD_RM80(const X86::Instruction& insn)
  194. {
  195. VERIFY(!insn.modrm().is_register());
  196. // long doubles can be up to 128 bits wide in memory for reasons (alignment) and only uses 80 bits of precision
  197. // GCC uses 12 bytes in 32 bit and 16 bytes in 64 bit mode
  198. // so in the 32 bit case we read a bit to much, but that shouldn't be an issue.
  199. // FIXME: Respect shadow values
  200. u128 new_f80 = insn.modrm().read128(m_cpu, insn).value();
  201. fpu_push(*(long double*)new_f80.bytes().data());
  202. }
  203. void SoftFPU::FST_RM32(const X86::Instruction& insn)
  204. {
  205. VERIFY(!insn.modrm().is_register());
  206. float f32 = convert_checked<float>(fpu_get(0));
  207. if (fpu_is_set(0))
  208. insn.modrm().write32(m_cpu, insn, shadow_wrap_as_initialized(bit_cast<u32>(f32)));
  209. else
  210. insn.modrm().write32(m_cpu, insn, ValueWithShadow<u32>(bit_cast<u32>(f32), 0u));
  211. }
  212. void SoftFPU::FST_RM64(const X86::Instruction& insn)
  213. {
  214. if (insn.modrm().is_register()) {
  215. fpu_set(insn.modrm().register_index(), fpu_get(0));
  216. } else {
  217. double f64 = convert_checked<double>(fpu_get(0));
  218. if (fpu_is_set(0))
  219. insn.modrm().write64(m_cpu, insn, shadow_wrap_as_initialized(bit_cast<u64>(f64)));
  220. else
  221. insn.modrm().write64(m_cpu, insn, ValueWithShadow<u64>(bit_cast<u64>(f64), 0ULL));
  222. }
  223. }
  224. void SoftFPU::FSTP_RM32(const X86::Instruction& insn)
  225. {
  226. FST_RM32(insn);
  227. fpu_pop();
  228. }
  229. void SoftFPU::FSTP_RM64(const X86::Instruction& insn)
  230. {
  231. FST_RM64(insn);
  232. fpu_pop();
  233. }
  234. void SoftFPU::FSTP_RM80(const X86::Instruction& insn)
  235. {
  236. if (insn.modrm().is_register()) {
  237. fpu_set(insn.modrm().register_index(), fpu_get(0));
  238. fpu_pop();
  239. } else {
  240. // FIXME: Respect more shadow values
  241. // long doubles can be up to 128 bits wide in memory for reasons (alignment) and only uses 80 bits of precision
  242. // gcc uses 12 byte in 32 bit and 16 byte in 64 bit mode
  243. // due to only 10 bytes being used, we just write these 10 into memory
  244. // We have to do .bytes().data() to get around static type analysis
  245. ValueWithShadow<u128> f80 { 0u, 0u };
  246. u128 value {};
  247. f80 = insn.modrm().read128(m_cpu, insn);
  248. *(long double*)value.bytes().data() = fpu_pop();
  249. memcpy(f80.value().bytes().data(), &value, 10); // copy
  250. f80.set_initialized();
  251. insn.modrm().write128(m_cpu, insn, f80);
  252. }
  253. }
  254. void SoftFPU::FILD_RM16(const X86::Instruction& insn)
  255. {
  256. VERIFY(!insn.modrm().is_register());
  257. auto m16int = insn.modrm().read16(m_cpu, insn);
  258. warn_if_uninitialized(m16int, "int16 loaded as float");
  259. fpu_push(static_cast<long double>(static_cast<i16>(m16int.value())));
  260. }
  261. void SoftFPU::FILD_RM32(const X86::Instruction& insn)
  262. {
  263. VERIFY(!insn.modrm().is_register());
  264. auto m32int = insn.modrm().read32(m_cpu, insn);
  265. warn_if_uninitialized(m32int, "int32 loaded as float");
  266. fpu_push(static_cast<long double>(static_cast<i32>(m32int.value())));
  267. }
  268. void SoftFPU::FILD_RM64(const X86::Instruction& insn)
  269. {
  270. VERIFY(!insn.modrm().is_register());
  271. auto m64int = insn.modrm().read64(m_cpu, insn);
  272. warn_if_uninitialized(m64int, "int64 loaded as float");
  273. fpu_push(static_cast<long double>(static_cast<i64>(m64int.value())));
  274. }
  275. void SoftFPU::FIST_RM16(const X86::Instruction& insn)
  276. {
  277. VERIFY(!insn.modrm().is_register());
  278. auto f = fpu_get(0);
  279. set_c1(0);
  280. auto int16 = round_checked<i16>(f);
  281. // FIXME: Respect shadow values
  282. insn.modrm().write16(m_cpu, insn, shadow_wrap_as_initialized(bit_cast<u16>(int16)));
  283. }
  284. void SoftFPU::FIST_RM32(const X86::Instruction& insn)
  285. {
  286. VERIFY(!insn.modrm().is_register());
  287. auto f = fpu_get(0);
  288. set_c1(0);
  289. auto int32 = round_checked<i32>(f);
  290. // FIXME: Respect shadow values
  291. insn.modrm().write32(m_cpu, insn, shadow_wrap_as_initialized(bit_cast<u32>(int32)));
  292. }
  293. void SoftFPU::FISTP_RM16(const X86::Instruction& insn)
  294. {
  295. FIST_RM16(insn);
  296. fpu_pop();
  297. }
  298. void SoftFPU::FISTP_RM32(const X86::Instruction& insn)
  299. {
  300. FIST_RM32(insn);
  301. fpu_pop();
  302. }
  303. void SoftFPU::FISTP_RM64(const X86::Instruction& insn)
  304. {
  305. VERIFY(!insn.modrm().is_register());
  306. auto f = fpu_pop();
  307. set_c1(0);
  308. auto i64 = round_checked<int64_t>(f);
  309. // FIXME: Respect shadow values
  310. insn.modrm().write64(m_cpu, insn, shadow_wrap_as_initialized(bit_cast<u64>(i64)));
  311. }
  312. void SoftFPU::FISTTP_RM16(const X86::Instruction& insn)
  313. {
  314. VERIFY(!insn.modrm().is_register());
  315. set_c1(0);
  316. i16 value = static_cast<i16>(fpu_pop());
  317. // FIXME: Respect shadow values
  318. insn.modrm().write16(m_cpu, insn, shadow_wrap_as_initialized(bit_cast<u16>(value)));
  319. }
  320. void SoftFPU::FISTTP_RM32(const X86::Instruction& insn)
  321. {
  322. VERIFY(!insn.modrm().is_register());
  323. i32 value = static_cast<i32>(fpu_pop());
  324. set_c1(0);
  325. // FIXME: Respect shadow values
  326. insn.modrm().write32(m_cpu, insn, shadow_wrap_as_initialized(bit_cast<u32>(value)));
  327. }
  328. void SoftFPU::FISTTP_RM64(const X86::Instruction& insn)
  329. {
  330. VERIFY(!insn.modrm().is_register());
  331. set_c1(0);
  332. i64 value = static_cast<i64>(fpu_pop());
  333. // FIXME: Respect shadow values
  334. insn.modrm().write64(m_cpu, insn, shadow_wrap_as_initialized(bit_cast<u64>(value)));
  335. }
  336. void SoftFPU::FBLD_M80(const X86::Instruction&) { TODO_INSN(); }
  337. void SoftFPU::FBSTP_M80(const X86::Instruction&) { TODO_INSN(); }
  338. void SoftFPU::FXCH(const X86::Instruction& insn)
  339. {
  340. VERIFY(insn.modrm().is_register());
  341. set_c1(0);
  342. auto tmp = fpu_get(0);
  343. fpu_set(0, fpu_get(insn.modrm().register_index()));
  344. fpu_set(insn.modrm().register_index(), tmp);
  345. }
  346. void SoftFPU::FCMOVE(const X86::Instruction& insn)
  347. {
  348. VERIFY(insn.modrm().is_register());
  349. if (m_cpu.zf())
  350. fpu_set(0, fpu_get(insn.modrm().rm()));
  351. }
  352. void SoftFPU::FCMOVNE(const X86::Instruction& insn)
  353. {
  354. VERIFY(insn.modrm().is_register());
  355. if (!m_cpu.zf())
  356. fpu_set(0, fpu_get((insn.modrm().reg_fpu())));
  357. }
  358. void SoftFPU::FCMOVB(const X86::Instruction& insn)
  359. {
  360. VERIFY(insn.modrm().is_register());
  361. if (m_cpu.cf())
  362. fpu_set(0, fpu_get(insn.modrm().rm()));
  363. }
  364. void SoftFPU::FCMOVNB(const X86::Instruction& insn)
  365. {
  366. VERIFY(insn.modrm().is_register());
  367. if (!m_cpu.cf())
  368. fpu_set(0, fpu_get(insn.modrm().rm()));
  369. }
  370. void SoftFPU::FCMOVBE(const X86::Instruction& insn)
  371. {
  372. VERIFY(insn.modrm().is_register());
  373. if (m_cpu.cf() || m_cpu.zf())
  374. fpu_set(0, fpu_get(insn.modrm().rm()));
  375. }
  376. void SoftFPU::FCMOVNBE(const X86::Instruction& insn)
  377. {
  378. VERIFY(insn.modrm().is_register());
  379. if (!(m_cpu.cf() || m_cpu.zf()))
  380. fpu_set(0, fpu_get(insn.modrm().rm()));
  381. }
  382. void SoftFPU::FCMOVU(const X86::Instruction& insn)
  383. {
  384. VERIFY(insn.modrm().is_register());
  385. if (m_cpu.pf())
  386. fpu_set(0, fpu_get((insn.modrm().reg_fpu())));
  387. }
  388. void SoftFPU::FCMOVNU(const X86::Instruction& insn)
  389. {
  390. VERIFY(insn.modrm().is_register());
  391. if (!m_cpu.pf())
  392. fpu_set(0, fpu_get((insn.modrm().reg_fpu())));
  393. }
  394. // BASIC ARITHMETIC
  395. void SoftFPU::FADD_RM32(const X86::Instruction& insn)
  396. {
  397. // FIXME look at ::INC_foo for how mem/reg stuff is handled, and use that here too to make sure this is only called for mem32 ops
  398. if (insn.modrm().is_register()) {
  399. fpu_set(0, fpu_get(insn.modrm().register_index()) + fpu_get(0));
  400. } else {
  401. auto new_f32 = insn.modrm().read32(m_cpu, insn);
  402. // FIXME: Respect shadow values
  403. auto f32 = bit_cast<float>(new_f32.value());
  404. fpu_set(0, fpu_get(0) + f32);
  405. }
  406. }
  407. void SoftFPU::FADD_RM64(const X86::Instruction& insn)
  408. {
  409. // FIXME look at ::INC_foo for how mem/reg stuff is handled, and use that here too to make sure this is only called for mem64 ops
  410. if (insn.modrm().is_register()) {
  411. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) + fpu_get(0));
  412. } else {
  413. auto new_f64 = insn.modrm().read64(m_cpu, insn);
  414. // FIXME: Respect shadow values
  415. auto f64 = bit_cast<double>(new_f64.value());
  416. fpu_set(0, fpu_get(0) + f64);
  417. }
  418. }
  419. void SoftFPU::FADDP(const X86::Instruction& insn)
  420. {
  421. VERIFY(insn.modrm().is_register());
  422. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) + fpu_get(0));
  423. fpu_pop();
  424. }
  425. void SoftFPU::FIADD_RM32(const X86::Instruction& insn)
  426. {
  427. VERIFY(!insn.modrm().is_register());
  428. auto m32int = (i32)insn.modrm().read32(m_cpu, insn).value();
  429. // FIXME: Respect shadow values
  430. fpu_set(0, fpu_get(0) + (long double)m32int);
  431. }
  432. void SoftFPU::FIADD_RM16(const X86::Instruction& insn)
  433. {
  434. VERIFY(!insn.modrm().is_register());
  435. auto m16int = (i16)insn.modrm().read16(m_cpu, insn).value();
  436. // FIXME: Respect shadow values
  437. fpu_set(0, fpu_get(0) + (long double)m16int);
  438. }
  439. void SoftFPU::FSUB_RM32(const X86::Instruction& insn)
  440. {
  441. if (insn.modrm().is_register()) {
  442. fpu_set(0, fpu_get(0) - fpu_get(insn.modrm().register_index()));
  443. } else {
  444. auto new_f32 = insn.modrm().read32(m_cpu, insn);
  445. // FIXME: Respect shadow values
  446. auto f32 = bit_cast<float>(new_f32.value());
  447. fpu_set(0, fpu_get(0) - f32);
  448. }
  449. }
  450. void SoftFPU::FSUB_RM64(const X86::Instruction& insn)
  451. {
  452. if (insn.modrm().is_register()) {
  453. // Note: This is FSUBR (DC E8+i FSUBR st(i) st(0)) in the spec
  454. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) - fpu_get(0));
  455. } else {
  456. auto new_f64 = insn.modrm().read64(m_cpu, insn);
  457. // FIXME: Respect shadow values
  458. auto f64 = bit_cast<double>(new_f64.value());
  459. fpu_set(0, fpu_get(0) - f64);
  460. }
  461. }
  462. void SoftFPU::FSUBP(const X86::Instruction& insn)
  463. {
  464. VERIFY(insn.modrm().is_register());
  465. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) - fpu_get(0));
  466. fpu_pop();
  467. }
  468. void SoftFPU::FSUBR_RM32(const X86::Instruction& insn)
  469. {
  470. if (insn.modrm().is_register()) {
  471. fpu_set(0, fpu_get(insn.modrm().register_index()) - fpu_get(0));
  472. } else {
  473. auto new_f32 = insn.modrm().read32(m_cpu, insn);
  474. // FIXME: Respect shadow values
  475. auto f32 = bit_cast<float>(new_f32.value());
  476. fpu_set(0, f32 - fpu_get(0));
  477. }
  478. }
  479. void SoftFPU::FSUBR_RM64(const X86::Instruction& insn)
  480. {
  481. if (insn.modrm().is_register()) {
  482. // Note: This is FSUB (DC E0+i FSUB st(i) st(0)) in the spec
  483. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) - fpu_get(0));
  484. } else {
  485. auto new_f64 = insn.modrm().read64(m_cpu, insn);
  486. // FIXME: Respect shadow values
  487. auto f64 = bit_cast<double>(new_f64.value());
  488. fpu_set(0, f64 - fpu_get(0));
  489. }
  490. }
  491. void SoftFPU::FSUBRP(const X86::Instruction& insn)
  492. {
  493. VERIFY(insn.modrm().is_register());
  494. fpu_set(insn.modrm().register_index(), fpu_get(0) - fpu_get(insn.modrm().register_index()));
  495. fpu_pop();
  496. }
  497. void SoftFPU::FISUB_RM32(const X86::Instruction& insn)
  498. {
  499. VERIFY(!insn.modrm().is_register());
  500. auto m32int = (i32)insn.modrm().read32(m_cpu, insn).value();
  501. // FIXME: Respect shadow values
  502. fpu_set(0, fpu_get(0) - (long double)m32int);
  503. }
  504. void SoftFPU::FISUB_RM16(const X86::Instruction& insn)
  505. {
  506. VERIFY(!insn.modrm().is_register());
  507. auto m16int = (i16)insn.modrm().read16(m_cpu, insn).value();
  508. // FIXME: Respect shadow values
  509. fpu_set(0, fpu_get(0) - (long double)m16int);
  510. }
  511. void SoftFPU::FISUBR_RM16(const X86::Instruction& insn)
  512. {
  513. VERIFY(!insn.modrm().is_register());
  514. auto m16int = (i16)insn.modrm().read16(m_cpu, insn).value();
  515. // FIXME: Respect shadow values
  516. fpu_set(0, (long double)m16int - fpu_get(0));
  517. }
  518. void SoftFPU::FISUBR_RM32(const X86::Instruction& insn)
  519. {
  520. VERIFY(!insn.modrm().is_register());
  521. auto m32int = (i32)insn.modrm().read32(m_cpu, insn).value();
  522. // FIXME: Respect shadow values
  523. fpu_set(0, (long double)m32int - fpu_get(0));
  524. }
  525. void SoftFPU::FMUL_RM32(const X86::Instruction& insn)
  526. {
  527. // FIXME look at ::INC_foo for how mem/reg stuff is handled, and use that here too to make sure this is only called for mem32 ops
  528. if (insn.modrm().is_register()) {
  529. fpu_set(0, fpu_get(0) * fpu_get(insn.modrm().register_index()));
  530. } else {
  531. auto new_f32 = insn.modrm().read32(m_cpu, insn);
  532. // FIXME: Respect shadow values
  533. auto f32 = bit_cast<float>(new_f32.value());
  534. fpu_set(0, fpu_get(0) * f32);
  535. }
  536. }
  537. void SoftFPU::FMUL_RM64(const X86::Instruction& insn)
  538. {
  539. // FIXME look at ::INC_foo for how mem/reg stuff is handled, and use that here too to make sure this is only called for mem64 ops
  540. if (insn.modrm().is_register()) {
  541. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) * fpu_get(0));
  542. } else {
  543. auto new_f64 = insn.modrm().read64(m_cpu, insn);
  544. // FIXME: Respect shadow values
  545. auto f64 = bit_cast<double>(new_f64.value());
  546. fpu_set(0, fpu_get(0) * f64);
  547. }
  548. }
  549. void SoftFPU::FMULP(const X86::Instruction& insn)
  550. {
  551. VERIFY(insn.modrm().is_register());
  552. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) * fpu_get(0));
  553. fpu_pop();
  554. }
  555. void SoftFPU::FIMUL_RM32(const X86::Instruction& insn)
  556. {
  557. VERIFY(!insn.modrm().is_register());
  558. auto m32int = (i32)insn.modrm().read32(m_cpu, insn).value();
  559. // FIXME: Respect shadow values
  560. fpu_set(0, fpu_get(0) * m32int);
  561. }
  562. void SoftFPU::FIMUL_RM16(const X86::Instruction& insn)
  563. {
  564. VERIFY(!insn.modrm().is_register());
  565. auto m16int = (i16)insn.modrm().read16(m_cpu, insn).value();
  566. // FIXME: Respect shadow values
  567. fpu_set(0, fpu_get(0) * m16int);
  568. }
  569. void SoftFPU::FDIV_RM32(const X86::Instruction& insn)
  570. {
  571. if (insn.modrm().is_register()) {
  572. fpu_set(0, fpu_get(0) / fpu_get(insn.modrm().register_index()));
  573. } else {
  574. auto new_f32 = insn.modrm().read32(m_cpu, insn);
  575. // FIXME: Respect shadow values
  576. auto f32 = bit_cast<float>(new_f32.value());
  577. // FIXME: Raise IA on + infinity / +-infinity, +-0 / +-0, raise Z on finite / +-0
  578. fpu_set(0, fpu_get(0) / f32);
  579. }
  580. }
  581. void SoftFPU::FDIV_RM64(const X86::Instruction& insn)
  582. {
  583. if (insn.modrm().is_register()) {
  584. // Note: This is FDIVR (DC F0+i FDIVR st(i) st(0)) in the spec
  585. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) / fpu_get(0));
  586. } else {
  587. auto new_f64 = insn.modrm().read64(m_cpu, insn);
  588. // FIXME: Respect shadow values
  589. auto f64 = bit_cast<double>(new_f64.value());
  590. // FIXME: Raise IA on + infinity / +-infinity, +-0 / +-0, raise Z on finite / +-0
  591. fpu_set(0, fpu_get(0) / f64);
  592. }
  593. }
  594. void SoftFPU::FDIVP(const X86::Instruction& insn)
  595. {
  596. VERIFY(insn.modrm().is_register());
  597. // FIXME: Raise IA on + infinity / +-infinity, +-0 / +-0, raise Z on finite / +-0
  598. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) / fpu_get(0));
  599. fpu_pop();
  600. }
  601. void SoftFPU::FDIVR_RM32(const X86::Instruction& insn)
  602. {
  603. if (insn.modrm().is_register()) {
  604. fpu_set(0, fpu_get(insn.modrm().register_index()) / fpu_get(0));
  605. } else {
  606. auto new_f32 = insn.modrm().read32(m_cpu, insn);
  607. // FIXME: Respect shadow values
  608. auto f32 = bit_cast<float>(new_f32.value());
  609. // FIXME: Raise IA on + infinity / +-infinity, +-0 / +-0, raise Z on finite / +-0
  610. fpu_set(0, f32 / fpu_get(0));
  611. }
  612. }
  613. void SoftFPU::FDIVR_RM64(const X86::Instruction& insn)
  614. {
  615. if (insn.modrm().is_register()) {
  616. // Note: This is FDIV (DC F8+i FDIV st(i) st(0)) in the spec
  617. fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) / fpu_get(0));
  618. } else {
  619. auto new_f64 = insn.modrm().read64(m_cpu, insn);
  620. // FIXME: Respect shadow values
  621. auto f64 = bit_cast<double>(new_f64.value());
  622. // FIXME: Raise IA on + infinity / +-infinity, +-0 / +-0, raise Z on finite / +-0
  623. fpu_set(0, f64 / fpu_get(0));
  624. }
  625. }
  626. void SoftFPU::FDIVRP(const X86::Instruction& insn)
  627. {
  628. VERIFY(insn.modrm().is_register());
  629. // FIXME: Raise IA on + infinity / +-infinity, +-0 / +-0, raise Z on finite / +-0
  630. fpu_set(insn.modrm().register_index(), fpu_get(0) / fpu_get(insn.modrm().register_index()));
  631. fpu_pop();
  632. }
  633. void SoftFPU::FIDIV_RM16(const X86::Instruction& insn)
  634. {
  635. VERIFY(!insn.modrm().is_register());
  636. auto m16int = (i16)insn.modrm().read16(m_cpu, insn).value();
  637. // FIXME: Respect shadow values
  638. // FIXME: Raise IA on 0 / _=0, raise Z on finite / +-0
  639. fpu_set(0, fpu_get(0) / m16int);
  640. }
  641. void SoftFPU::FIDIV_RM32(const X86::Instruction& insn)
  642. {
  643. VERIFY(!insn.modrm().is_register());
  644. auto m32int = (i32)insn.modrm().read32(m_cpu, insn).value();
  645. // FIXME: Respect shadow values
  646. // FIXME: Raise IA on 0 / _=0, raise Z on finite / +-0
  647. fpu_set(0, fpu_get(0) / m32int);
  648. }
  649. void SoftFPU::FIDIVR_RM16(const X86::Instruction& insn)
  650. {
  651. VERIFY(!insn.modrm().is_register());
  652. auto m16int = (i16)insn.modrm().read16(m_cpu, insn).value();
  653. // FIXME: Respect shadow values
  654. // FIXME: Raise IA on 0 / _=0, raise Z on finite / +-0
  655. fpu_set(0, m16int / fpu_get(0));
  656. }
  657. void SoftFPU::FIDIVR_RM32(const X86::Instruction& insn)
  658. {
  659. VERIFY(!insn.modrm().is_register());
  660. auto m32int = (i32)insn.modrm().read32(m_cpu, insn).value();
  661. // FIXME: Respect shadow values
  662. // FIXME: Raise IA on 0 / _=0, raise Z on finite / +-0
  663. fpu_set(0, m32int / fpu_get(0));
  664. }
  665. void SoftFPU::FPREM(const X86::Instruction&)
  666. {
  667. // FIXME: FPREM should only be able to reduce top's exponent by a maximum
  668. // amount of 32-63 (impl-specific)
  669. long double top = fpu_get(0);
  670. long double one = fpu_get(1);
  671. int Q = static_cast<int>(truncl(top / one));
  672. top = top - (one * Q);
  673. set_c2(0);
  674. set_c1(Q & 1);
  675. set_c3((Q >> 1) & 1);
  676. set_c0((Q >> 2) & 1);
  677. fpu_set(0, top);
  678. }
  679. void SoftFPU::FPREM1(const X86::Instruction&)
  680. {
  681. // FIXME: FPREM1 should only be able to reduce top's exponent by a maximum
  682. // amount of 32-63 (impl-specific)
  683. long double top = fpu_get(0);
  684. long double one = fpu_get(1);
  685. int Q = static_cast<int>(roundl(top / one));
  686. top = top - (one * Q);
  687. set_c2(0);
  688. set_c1(Q & 1);
  689. set_c3((Q >> 1) & 1);
  690. set_c0((Q >> 2) & 1);
  691. fpu_set(0, top);
  692. }
  693. void SoftFPU::FABS(const X86::Instruction&)
  694. {
  695. set_c1(0);
  696. fpu_set(0, __builtin_fabsl(fpu_get(0)));
  697. }
  698. void SoftFPU::FCHS(const X86::Instruction&)
  699. {
  700. set_c1(0);
  701. fpu_set(0, -fpu_get(0));
  702. }
  703. void SoftFPU::FRNDINT(const X86::Instruction&)
  704. {
  705. // FIXME: Raise #IA #D
  706. auto res = round_checked<long double>(fpu_get(0));
  707. fpu_set(0, res);
  708. }
  709. void SoftFPU::FSCALE(const X86::Instruction&)
  710. {
  711. // FIXME: Raise #IA #D #U #O #P
  712. fpu_set(0, fpu_get(0) * exp2l(truncl(fpu_get(1))));
  713. }
  714. void SoftFPU::FSQRT(const X86::Instruction&)
  715. {
  716. // FIXME: Raise #IA #D #P
  717. if (fpu_get(0) < 0)
  718. fpu_set_exception(FPU_Exception::InvalidOperation);
  719. fpu_set(0, sqrtl(fpu_get(0)));
  720. }
  721. void SoftFPU::FXTRACT(const X86::Instruction&) { TODO_INSN(); }
  722. // COMPARISON
  723. // FIXME: there may be an implicit argument, how is this conveyed by the insn
  724. void SoftFPU::FCOM_RM32(const X86::Instruction&) { TODO_INSN(); }
  725. void SoftFPU::FCOM_RM64(const X86::Instruction&) { TODO_INSN(); }
  726. void SoftFPU::FCOMP_RM32(const X86::Instruction&) { TODO_INSN(); }
  727. void SoftFPU::FCOMP_RM64(const X86::Instruction&) { TODO_INSN(); }
  728. void SoftFPU::FCOMPP(const X86::Instruction&)
  729. {
  730. if (fpu_isnan(0) || fpu_isnan(1)) {
  731. fpu_set_exception(FPU_Exception::InvalidOperation);
  732. if (m_fpu_cw.mask_invalid)
  733. fpu_set_unordered();
  734. } else {
  735. set_c2(0);
  736. set_c0(fpu_get(0) < fpu_get(1));
  737. set_c3(fpu_get(0) == fpu_get(1));
  738. }
  739. fpu_pop();
  740. fpu_pop();
  741. }
  742. void SoftFPU::FUCOM(const X86::Instruction&) { TODO_INSN(); } // Needs QNaN detection
  743. void SoftFPU::FUCOMP(const X86::Instruction&) { TODO_INSN(); }
  744. void SoftFPU::FUCOMPP(const X86::Instruction&) { TODO_INSN(); }
  745. void SoftFPU::FICOM_RM16(const X86::Instruction& insn)
  746. {
  747. // FIXME: Check for denormals
  748. VERIFY(insn.modrm().is_register());
  749. auto val_shd = insn.modrm().read16(m_cpu, insn);
  750. warn_if_uninitialized(val_shd, "int16 compare to float");
  751. auto val = static_cast<i16>(val_shd.value());
  752. if (fpu_isnan(0)) {
  753. fpu_set_unordered();
  754. } else {
  755. set_c0(fpu_get(0) < val);
  756. set_c2(0);
  757. set_c3(fpu_get(0) == val);
  758. }
  759. set_c1(0);
  760. }
  761. void SoftFPU::FICOM_RM32(const X86::Instruction& insn)
  762. {
  763. // FIXME: Check for denormals
  764. VERIFY(insn.modrm().is_register());
  765. auto val_shd = insn.modrm().read32(m_cpu, insn);
  766. warn_if_uninitialized(val_shd, "int32 compare to float");
  767. auto val = static_cast<i32>(val_shd.value());
  768. if (fpu_isnan(0)) {
  769. fpu_set_unordered();
  770. } else {
  771. set_c0(fpu_get(0) < val);
  772. set_c2(0);
  773. set_c3(fpu_get(0) == val);
  774. }
  775. set_c1(0);
  776. }
  777. void SoftFPU::FICOMP_RM16(const X86::Instruction& insn)
  778. {
  779. FICOM_RM16(insn);
  780. fpu_pop();
  781. }
  782. void SoftFPU::FICOMP_RM32(const X86::Instruction& insn)
  783. {
  784. FICOM_RM32(insn);
  785. fpu_pop();
  786. }
  787. void SoftFPU::FCOMI(const X86::Instruction& insn)
  788. {
  789. auto i = insn.modrm().rm();
  790. // FIXME: QNaN / exception handling.
  791. set_c0(0);
  792. if (isnan(fpu_get(0)) || isnan(fpu_get(1))) {
  793. fpu_set_exception(FPU_Exception::InvalidOperation);
  794. m_cpu.set_zf(1);
  795. m_cpu.set_pf(1);
  796. m_cpu.set_cf(1);
  797. } else {
  798. m_cpu.set_zf(fpu_get(0) == fpu_get(i));
  799. m_cpu.set_pf(false);
  800. m_cpu.set_cf(fpu_get(0) < fpu_get(i));
  801. }
  802. if (!fpu_is_set(1))
  803. fpu_set_exception(FPU_Exception::Underflow);
  804. m_cpu.set_of(false);
  805. m_cpu.set_af(false);
  806. m_cpu.set_sf(false);
  807. // FIXME: Taint should be based on ST(0) and ST(i)
  808. m_cpu.m_flags_tainted = false;
  809. }
  810. void SoftFPU::FCOMIP(const X86::Instruction& insn)
  811. {
  812. FCOMI(insn);
  813. fpu_pop();
  814. }
  815. void SoftFPU::FUCOMI(const X86::Instruction& insn)
  816. {
  817. auto i = insn.modrm().rm();
  818. // FIXME: Unordered comparison checks.
  819. // FIXME: QNaN / exception handling.
  820. set_c1(0);
  821. if (fpu_isnan(0) || fpu_isnan(i)) {
  822. m_cpu.set_zf(true);
  823. m_cpu.set_pf(true);
  824. m_cpu.set_cf(true);
  825. } else {
  826. m_cpu.set_zf(fpu_get(0) == fpu_get(i));
  827. m_cpu.set_pf(false);
  828. m_cpu.set_cf(fpu_get(0) < fpu_get(i));
  829. }
  830. m_cpu.set_of(false);
  831. m_cpu.set_af(false);
  832. m_cpu.set_sf(false);
  833. // FIXME: Taint should be based on ST(0) and ST(i)
  834. m_cpu.m_flags_tainted = false;
  835. }
  836. void SoftFPU::FUCOMIP(const X86::Instruction& insn)
  837. {
  838. FUCOMI(insn);
  839. fpu_pop();
  840. }
  841. void SoftFPU::FTST(const X86::Instruction&)
  842. {
  843. // FIXME: maybe check for denormal
  844. set_c1(0);
  845. if (fpu_isnan(0))
  846. // raise #IA?
  847. fpu_set_unordered();
  848. else {
  849. set_c0(fpu_get(0) < 0.);
  850. set_c2(0);
  851. set_c3(fpu_get(0) == 0.);
  852. }
  853. }
  854. void SoftFPU::FXAM(const X86::Instruction&)
  855. {
  856. if (m_reg_is_mmx[m_fpu_stack_top]) {
  857. // technically a subset of NaN/INF, with the Tag set to valid,
  858. // but we have our own helper for this
  859. set_c0(0);
  860. set_c2(0);
  861. set_c3(0);
  862. } else {
  863. switch (fpu_get_tag(0)) {
  864. case FPU_Tag::Valid:
  865. set_c0(0);
  866. set_c2(1);
  867. set_c3(0);
  868. break;
  869. case FPU_Tag::Zero:
  870. set_c0(1);
  871. set_c2(0);
  872. set_c3(0);
  873. break;
  874. case FPU_Tag::Special:
  875. if (isinf(fpu_get(0))) {
  876. set_c0(1);
  877. set_c2(1);
  878. set_c3(0);
  879. } else if (isnan(fpu_get(0))) {
  880. set_c0(1);
  881. set_c2(0);
  882. set_c3(0);
  883. } else {
  884. // denormalized
  885. set_c0(0);
  886. set_c2(1);
  887. set_c3(1);
  888. }
  889. break;
  890. case FPU_Tag::Empty:
  891. set_c0(1);
  892. set_c2(0);
  893. set_c3(1);
  894. break;
  895. default:
  896. VERIFY_NOT_REACHED();
  897. }
  898. }
  899. set_c1(signbit(fpu_get(0)));
  900. }
  901. // TRANSCENDENTAL
  902. void SoftFPU::FSIN(const X86::Instruction&)
  903. {
  904. // FIXME: Raise #IA #D #P
  905. // FIXME: Set C1 on when result was rounded up, cleared otherwise
  906. // FIXME: Set C2 to 1 if ST(0) is outside range of -2^63 to +2^63; else set to 0
  907. // ST(0) shall remain unchanged in this case
  908. fpu_set(0, sinl(fpu_get(0)));
  909. }
  910. void SoftFPU::FCOS(const X86::Instruction&)
  911. {
  912. // FIXME: Raise #IA #D #P
  913. // FIXME: Set C1 on when result was rounded up, cleared otherwise
  914. // FIXME: Set C2 to 1 if ST(0) is outside range of -2^63 to +2^63; else set to 0
  915. // ST(0) shall remain unchanged in this case
  916. fpu_set(0, cosl(fpu_get(0)));
  917. }
  918. void SoftFPU::FSINCOS(const X86::Instruction&)
  919. {
  920. // FIXME: Raise #IA #D #P
  921. // FIXME: Set C1 on when result was rounded up, cleared otherwise
  922. // FIXME: Set C2 to 1 if ST(0) is outside range of -2^63 to +2^63; else set to 0
  923. // ST(0) shall remain unchanged in this case
  924. long double sin = sinl(fpu_get(0));
  925. long double cos = cosl(fpu_get(0));
  926. fpu_set(0, sin);
  927. fpu_push(cos);
  928. }
  929. void SoftFPU::FPTAN(const X86::Instruction&)
  930. {
  931. // FIXME: Raise #IA #D #U #P
  932. // FIXME: Set C1 on when result was rounded up, cleared otherwise
  933. // FIXME: Set C2 to 1 if ST(0) is outside range of -2^63 to +2^63; else set to 0
  934. // ST(0) shall remain unchanged in this case
  935. fpu_set(0, tanl(fpu_get(0)));
  936. fpu_push(1.0f);
  937. }
  938. void SoftFPU::FPATAN(const X86::Instruction&)
  939. {
  940. // FIXME: Raise #IA #D #U #P
  941. // FIXME: Set C1 on when result was rounded up, cleared otherwise
  942. // Note: Not implemented 80287 quirk:
  943. // Restriction to 0 ≤ |ST(1)| < |ST(0)| < +∞
  944. fpu_set(1, atan2l(fpu_get(1), fpu_get(0)));
  945. fpu_pop();
  946. }
  947. void SoftFPU::F2XM1(const X86::Instruction&)
  948. {
  949. // FIXME: Raise #IA #D #U #P
  950. // FIXME: Set C1 on when result was rounded up, cleared otherwise
  951. // FIXME: Validate ST(0) is in range –1.0 to +1.0
  952. auto val = fpu_get(0);
  953. fpu_set(0, exp2(val) - 1.0l);
  954. }
  955. void SoftFPU::FYL2X(const X86::Instruction&)
  956. {
  957. // FIXME: Set C1 on when result was rounded up, cleared otherwise
  958. // FIXME: Raise #IA #D #U #O #P
  959. auto x = fpu_get(0);
  960. auto y = fpu_get(1);
  961. if (x < 0. && !isinf(x)) {
  962. fpu_set_exception(FPU_Exception::InvalidOperation);
  963. // FIXME: Spec does not say what to do here....
  964. // So lets just ask libm....
  965. fpu_set(1, y * log2l(x));
  966. } else if (x == 0.) {
  967. if (y == 0)
  968. fpu_set_exception(FPU_Exception::InvalidOperation);
  969. fpu_set_exception(FPU_Exception::ZeroDivide);
  970. fpu_set(1, INFINITY * (signbit(y) ? 1 : -1));
  971. } else {
  972. fpu_set(1, y * log2l(x));
  973. }
  974. fpu_pop();
  975. }
  976. void SoftFPU::FYL2XP1(const X86::Instruction&)
  977. {
  978. // FIXME: Raise #IA #O #U #P #D
  979. auto x = fpu_get(0);
  980. auto y = fpu_get(1);
  981. if (x == 0 && isinf(y))
  982. fpu_set_exception(FPU_Exception::InvalidOperation);
  983. fpu_set(1, (y * log2l(x + 1.0l)));
  984. fpu_pop();
  985. }
  986. // LOAD CONSTANT
  987. void SoftFPU::FLD1(const X86::Instruction&)
  988. {
  989. set_c1(0);
  990. fpu_push(1.0l);
  991. }
  992. void SoftFPU::FLDZ(const X86::Instruction&)
  993. {
  994. set_c1(0);
  995. fpu_push(0.0l);
  996. }
  997. void SoftFPU::FLDPI(const X86::Instruction&)
  998. {
  999. set_c1(0);
  1000. fpu_push(M_PIl);
  1001. }
  1002. void SoftFPU::FLDL2E(const X86::Instruction&)
  1003. {
  1004. set_c1(0);
  1005. fpu_push(M_LOG2El);
  1006. }
  1007. void SoftFPU::FLDLN2(const X86::Instruction&)
  1008. {
  1009. set_c1(0);
  1010. fpu_push(M_LN2l);
  1011. }
  1012. void SoftFPU::FLDL2T(const X86::Instruction&)
  1013. {
  1014. set_c1(0);
  1015. fpu_push(log2l(10.0l));
  1016. }
  1017. void SoftFPU::FLDLG2(const X86::Instruction&)
  1018. {
  1019. set_c1(0);
  1020. fpu_push(log10l(2.0l));
  1021. }
  1022. // CONTROL
  1023. void SoftFPU::FINCSTP(const X86::Instruction&)
  1024. {
  1025. m_fpu_stack_top = (m_fpu_stack_top + 1u) % 8u;
  1026. set_c1(0);
  1027. }
  1028. void SoftFPU::FDECSTP(const X86::Instruction&)
  1029. {
  1030. m_fpu_stack_top = (m_fpu_stack_top - 1u) % 8u;
  1031. set_c1(0);
  1032. }
  1033. void SoftFPU::FFREE(const X86::Instruction& insn)
  1034. {
  1035. fpu_set_tag(insn.modrm().reg_fpu(), FPU_Tag::Empty);
  1036. }
  1037. void SoftFPU::FFREEP(const X86::Instruction& insn)
  1038. {
  1039. FFREE(insn);
  1040. fpu_pop();
  1041. }
  1042. void SoftFPU::FNINIT(const X86::Instruction&)
  1043. {
  1044. m_fpu_cw.cw = 0x037F;
  1045. m_fpu_sw = 0;
  1046. m_fpu_tw = 0xFFFF;
  1047. m_fpu_ip = 0;
  1048. m_fpu_cs = 0;
  1049. m_fpu_dp = 0;
  1050. m_fpu_ds = 0;
  1051. m_fpu_iop = 0;
  1052. }
  1053. void SoftFPU::FNCLEX(const X86::Instruction&)
  1054. {
  1055. m_fpu_error_invalid = 0;
  1056. m_fpu_error_denorm = 0;
  1057. m_fpu_error_zero_div = 0;
  1058. m_fpu_error_overflow = 0;
  1059. m_fpu_error_underflow = 0;
  1060. m_fpu_error_precision = 0;
  1061. m_fpu_error_stackfault = 0;
  1062. m_fpu_busy = 0;
  1063. }
  1064. void SoftFPU::FNSTCW(const X86::Instruction& insn)
  1065. {
  1066. insn.modrm().write16(m_cpu, insn, shadow_wrap_as_initialized(m_fpu_cw.cw));
  1067. }
  1068. void SoftFPU::FLDCW(const X86::Instruction& insn)
  1069. {
  1070. m_fpu_cw.cw = insn.modrm().read16(m_cpu, insn).value();
  1071. // Just let the host's x87 handle the rounding for us
  1072. // We do not want to accedentally raise an FP-Exception on the host, so we
  1073. // mask all exceptions
  1074. AK::X87ControlWord temp = m_fpu_cw;
  1075. temp.mask_invalid = 1;
  1076. temp.mask_denorm = 1;
  1077. temp.mask_zero_div = 1;
  1078. temp.mask_overflow = 1;
  1079. temp.mask_underflow = 1;
  1080. temp.mask_precision = 1;
  1081. AK::set_cw_x87(temp);
  1082. }
  1083. void SoftFPU::FNSTENV(const X86::Instruction& insn)
  1084. {
  1085. // Assuming we are always in Protected mode
  1086. // FIXME: 16-bit Format
  1087. // 32-bit Format
  1088. /* 31--------------16---------------0
  1089. * | | CW | 0
  1090. * +----------------+---------------+
  1091. * | | SW | 4
  1092. * +----------------+---------------+
  1093. * | | TW | 8
  1094. * +----------------+---------------+
  1095. * | FIP | 12
  1096. * +----+-----------+---------------+
  1097. * |0000|fpuOp[10:0]| FIP_sel | 16
  1098. * +----+-----------+---------------+
  1099. * | FDP | 20
  1100. * +----------------+---------------+
  1101. * | | FDP_ds | 24
  1102. * +----------------|---------------+
  1103. * */
  1104. auto address = insn.modrm().resolve(m_cpu, insn);
  1105. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_cw.cw));
  1106. address.set_offset(address.offset() + 4);
  1107. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_sw));
  1108. address.set_offset(address.offset() + 4);
  1109. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_tw));
  1110. address.set_offset(address.offset() + 4);
  1111. m_cpu.write_memory32(address, shadow_wrap_as_initialized(m_fpu_ip));
  1112. address.set_offset(address.offset() + 4);
  1113. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_cs));
  1114. address.set_offset(address.offset() + 2);
  1115. m_cpu.write_memory16(address, shadow_wrap_as_initialized<u16>(m_fpu_iop & 0x3FFU));
  1116. address.set_offset(address.offset() + 2);
  1117. m_cpu.write_memory32(address, shadow_wrap_as_initialized(m_fpu_dp));
  1118. address.set_offset(address.offset() + 4);
  1119. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_ds));
  1120. }
  1121. void SoftFPU::FLDENV(const X86::Instruction& insn)
  1122. {
  1123. // Assuming we are always in Protected mode
  1124. // FIXME: 16-bit Format
  1125. auto address = insn.modrm().resolve(m_cpu, insn);
  1126. // FIXME: Shadow Values
  1127. m_fpu_cw.cw = m_cpu.read_memory16(address).value();
  1128. // See note in FLDCW
  1129. AK::X87ControlWord temp = m_fpu_cw;
  1130. temp.mask_invalid = 1;
  1131. temp.mask_denorm = 1;
  1132. temp.mask_zero_div = 1;
  1133. temp.mask_overflow = 1;
  1134. temp.mask_underflow = 1;
  1135. temp.mask_precision = 1;
  1136. AK::set_cw_x87(temp);
  1137. address.set_offset(address.offset() + 4);
  1138. m_fpu_sw = m_cpu.read_memory16(address).value();
  1139. address.set_offset(address.offset() + 4);
  1140. m_fpu_tw = m_cpu.read_memory16(address).value();
  1141. address.set_offset(address.offset() + 4);
  1142. m_fpu_ip = m_cpu.read_memory32(address).value();
  1143. address.set_offset(address.offset() + 4);
  1144. m_fpu_cs = m_cpu.read_memory16(address).value();
  1145. address.set_offset(address.offset() + 2);
  1146. m_fpu_iop = m_cpu.read_memory16(address).value();
  1147. address.set_offset(address.offset() + 2);
  1148. m_fpu_dp = m_cpu.read_memory32(address).value();
  1149. address.set_offset(address.offset() + 4);
  1150. m_fpu_ds = m_cpu.read_memory16(address).value();
  1151. }
  1152. void SoftFPU::FNSAVE(const X86::Instruction& insn)
  1153. {
  1154. FNSTENV(insn);
  1155. auto address = insn.modrm().resolve(m_cpu, insn);
  1156. address.set_offset(address.offset() + 28); // size of the ENV
  1157. // write fpu-stack to memory
  1158. u8 raw_data[80];
  1159. for (int i = 0; i < 8; ++i) {
  1160. memcpy(raw_data + 10 * i, &m_storage[i], 10);
  1161. }
  1162. for (int i = 0; i < 5; ++i) {
  1163. // FIXME: Shadow Value
  1164. m_cpu.write_memory128(address, shadow_wrap_as_initialized(((u128*)raw_data)[i]));
  1165. address.set_offset(address.offset() + 16);
  1166. }
  1167. FNINIT(insn);
  1168. }
  1169. void SoftFPU::FRSTOR(const X86::Instruction& insn)
  1170. {
  1171. FLDENV(insn);
  1172. auto address = insn.modrm().resolve(m_cpu, insn);
  1173. address.set_offset(address.offset() + 28); // size of the ENV
  1174. // read fpu-stack from memory
  1175. u8 raw_data[80];
  1176. for (int i = 0; i < 5; ++i) {
  1177. // FIXME: Shadow Value
  1178. ((u128*)raw_data)[i] = m_cpu.read_memory128(address).value();
  1179. address.set_offset(address.offset() + 16);
  1180. }
  1181. for (int i = 0; i < 8; ++i) {
  1182. memcpy(&m_storage[i], raw_data + 10 * i, 10);
  1183. }
  1184. memset(m_reg_is_mmx, 0, sizeof(m_reg_is_mmx));
  1185. }
  1186. void SoftFPU::FNSTSW(const X86::Instruction& insn)
  1187. {
  1188. insn.modrm().write16(m_cpu, insn, shadow_wrap_as_initialized(m_fpu_sw));
  1189. }
  1190. void SoftFPU::FNSTSW_AX(const X86::Instruction&)
  1191. {
  1192. m_cpu.set_ax(shadow_wrap_as_initialized(m_fpu_sw));
  1193. }
  1194. // FIXME: FWAIT
  1195. void SoftFPU::FNOP(const X86::Instruction&) { }
  1196. // DO NOTHING?
  1197. void SoftFPU::FNENI(const X86::Instruction&) { TODO_INSN(); }
  1198. void SoftFPU::FNDISI(const X86::Instruction&) { TODO_INSN(); }
  1199. void SoftFPU::FNSETPM(const X86::Instruction&) { TODO_INSN(); }
  1200. // MMX
  1201. // helpers
  1202. #define LOAD_MM_MM64M() \
  1203. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */ \
  1204. MMX mm; \
  1205. MMX mm64m; \
  1206. if (insn.modrm().mod() == 0b11) { /* 0b11 signals a register */ \
  1207. mm64m = mmx_get(insn.modrm().rm()); \
  1208. } else { \
  1209. auto temp = insn.modrm().read64(m_cpu, insn); \
  1210. warn_if_uninitialized(temp, "Read of uninitialized Memory as Packed integer"); \
  1211. mm64m.raw = temp.value(); \
  1212. } \
  1213. mm = mmx_get(insn.modrm().reg())
  1214. #define MMX_intrinsic(intrinsic, res_type, actor_type) \
  1215. LOAD_MM_MM64M(); \
  1216. mm.res_type = __builtin_ia32_##intrinsic(mm.actor_type, mm64m.actor_type); \
  1217. mmx_set(insn.modrm().reg(), mm); \
  1218. mmx_common();
  1219. // ARITHMETIC
  1220. void SoftFPU::PADDB_mm1_mm2m64(const X86::Instruction& insn)
  1221. {
  1222. LOAD_MM_MM64M();
  1223. mm.v8 += mm64m.v8;
  1224. mmx_set(insn.modrm().reg(), mm);
  1225. mmx_common();
  1226. }
  1227. void SoftFPU::PADDW_mm1_mm2m64(const X86::Instruction& insn)
  1228. {
  1229. LOAD_MM_MM64M();
  1230. mm.v16 += mm64m.v16;
  1231. mmx_set(insn.modrm().reg(), mm);
  1232. mmx_common();
  1233. }
  1234. void SoftFPU::PADDD_mm1_mm2m64(const X86::Instruction& insn)
  1235. {
  1236. LOAD_MM_MM64M();
  1237. mm.v32 += mm64m.v32;
  1238. mmx_set(insn.modrm().reg(), mm);
  1239. mmx_common();
  1240. }
  1241. void SoftFPU::PADDSB_mm1_mm2m64(const X86::Instruction& insn)
  1242. {
  1243. MMX_intrinsic(paddsb, v8, v8);
  1244. }
  1245. void SoftFPU::PADDSW_mm1_mm2m64(const X86::Instruction& insn)
  1246. {
  1247. MMX_intrinsic(paddsw, v16, v16);
  1248. }
  1249. void SoftFPU::PADDUSB_mm1_mm2m64(const X86::Instruction& insn)
  1250. {
  1251. MMX_intrinsic(paddusb, v8, v8);
  1252. }
  1253. void SoftFPU::PADDUSW_mm1_mm2m64(const X86::Instruction& insn)
  1254. {
  1255. MMX_intrinsic(paddusw, v16, v16);
  1256. }
  1257. void SoftFPU::PSUBB_mm1_mm2m64(const X86::Instruction& insn)
  1258. {
  1259. LOAD_MM_MM64M();
  1260. mm.v8 -= mm64m.v8;
  1261. mmx_set(insn.modrm().reg(), mm);
  1262. mmx_common();
  1263. }
  1264. void SoftFPU::PSUBW_mm1_mm2m64(const X86::Instruction& insn)
  1265. {
  1266. LOAD_MM_MM64M();
  1267. mm.v16 -= mm64m.v16;
  1268. mmx_set(insn.modrm().reg(), mm);
  1269. mmx_common();
  1270. }
  1271. void SoftFPU::PSUBD_mm1_mm2m64(const X86::Instruction& insn)
  1272. {
  1273. LOAD_MM_MM64M();
  1274. mm.v32 -= mm64m.v32;
  1275. mmx_set(insn.modrm().reg(), mm);
  1276. mmx_common();
  1277. }
  1278. void SoftFPU::PSUBSB_mm1_mm2m64(const X86::Instruction& insn)
  1279. {
  1280. MMX_intrinsic(psubsb, v8, v8);
  1281. }
  1282. void SoftFPU::PSUBSW_mm1_mm2m64(const X86::Instruction& insn)
  1283. {
  1284. MMX_intrinsic(psubsw, v16, v16);
  1285. }
  1286. void SoftFPU::PSUBUSB_mm1_mm2m64(const X86::Instruction& insn)
  1287. {
  1288. MMX_intrinsic(psubusb, v8, v8);
  1289. }
  1290. void SoftFPU::PSUBUSW_mm1_mm2m64(const X86::Instruction& insn)
  1291. {
  1292. MMX_intrinsic(psubusw, v16, v16);
  1293. }
  1294. void SoftFPU::PMULHW_mm1_mm2m64(const X86::Instruction& insn)
  1295. {
  1296. MMX_intrinsic(pmulhw, v16, v16);
  1297. }
  1298. void SoftFPU::PMULLW_mm1_mm2m64(const X86::Instruction& insn)
  1299. {
  1300. MMX_intrinsic(pmullw, v16, v16);
  1301. }
  1302. void SoftFPU::PMADDWD_mm1_mm2m64(const X86::Instruction& insn)
  1303. {
  1304. MMX_intrinsic(pmaddwd, v32, v16);
  1305. }
  1306. // COMPARISON
  1307. void SoftFPU::PCMPEQB_mm1_mm2m64(const X86::Instruction& insn)
  1308. {
  1309. LOAD_MM_MM64M();
  1310. mm.v8 = mm.v8 == mm64m.v8;
  1311. mmx_set(insn.modrm().reg(), mm);
  1312. mmx_common();
  1313. }
  1314. void SoftFPU::PCMPEQW_mm1_mm2m64(const X86::Instruction& insn)
  1315. {
  1316. LOAD_MM_MM64M();
  1317. mm.v16 = mm.v16 == mm64m.v16;
  1318. mmx_set(insn.modrm().reg(), mm);
  1319. mmx_common();
  1320. }
  1321. void SoftFPU::PCMPEQD_mm1_mm2m64(const X86::Instruction& insn)
  1322. {
  1323. LOAD_MM_MM64M();
  1324. mm.v32 = mm.v32 == mm64m.v32;
  1325. mmx_set(insn.modrm().reg(), mm);
  1326. mmx_common();
  1327. }
  1328. void SoftFPU::PCMPGTB_mm1_mm2m64(const X86::Instruction& insn)
  1329. {
  1330. LOAD_MM_MM64M();
  1331. mm.v8 = mm.v8 > mm64m.v8;
  1332. mmx_set(insn.modrm().reg(), mm);
  1333. mmx_common();
  1334. }
  1335. void SoftFPU::PCMPGTW_mm1_mm2m64(const X86::Instruction& insn)
  1336. {
  1337. LOAD_MM_MM64M();
  1338. mm.v16 = mm.v16 > mm64m.v16;
  1339. mmx_set(insn.modrm().reg(), mm);
  1340. mmx_common();
  1341. }
  1342. void SoftFPU::PCMPGTD_mm1_mm2m64(const X86::Instruction& insn)
  1343. {
  1344. LOAD_MM_MM64M();
  1345. mm.v32 = mm.v32 > mm64m.v32;
  1346. mmx_set(insn.modrm().reg(), mm);
  1347. mmx_common();
  1348. }
  1349. // CONVERSION
  1350. void SoftFPU::PACKSSDW_mm1_mm2m64(const X86::Instruction& insn)
  1351. {
  1352. MMX_intrinsic(packssdw, v16, v32);
  1353. }
  1354. void SoftFPU::PACKSSWB_mm1_mm2m64(const X86::Instruction& insn)
  1355. {
  1356. MMX_intrinsic(packsswb, v8, v16);
  1357. }
  1358. void SoftFPU::PACKUSWB_mm1_mm2m64(const X86::Instruction& insn)
  1359. {
  1360. MMX_intrinsic(packuswb, v8, v16);
  1361. }
  1362. // UNPACK
  1363. void SoftFPU::PUNPCKHBW_mm1_mm2m64(const X86::Instruction& insn)
  1364. {
  1365. MMX_intrinsic(punpckhbw, v8, v8);
  1366. }
  1367. void SoftFPU::PUNPCKHWD_mm1_mm2m64(const X86::Instruction& insn)
  1368. {
  1369. MMX_intrinsic(punpckhwd, v16, v16);
  1370. }
  1371. void SoftFPU::PUNPCKHDQ_mm1_mm2m64(const X86::Instruction& insn)
  1372. {
  1373. MMX_intrinsic(punpckhdq, v32, v32);
  1374. }
  1375. void SoftFPU::PUNPCKLBW_mm1_mm2m32(const X86::Instruction& insn)
  1376. {
  1377. MMX_intrinsic(punpcklbw, v8, v8);
  1378. }
  1379. void SoftFPU::PUNPCKLWD_mm1_mm2m32(const X86::Instruction& insn)
  1380. {
  1381. MMX_intrinsic(punpcklwd, v16, v16);
  1382. }
  1383. void SoftFPU::PUNPCKLDQ_mm1_mm2m32(const X86::Instruction& insn)
  1384. {
  1385. MMX_intrinsic(punpckldq, v32, v32);
  1386. }
  1387. // LOGICAL
  1388. void SoftFPU::PAND_mm1_mm2m64(const X86::Instruction& insn)
  1389. {
  1390. LOAD_MM_MM64M();
  1391. mm.raw &= mm64m.raw;
  1392. mmx_set(insn.modrm().reg(), mm);
  1393. mmx_common();
  1394. }
  1395. void SoftFPU::PANDN_mm1_mm2m64(const X86::Instruction& insn)
  1396. {
  1397. LOAD_MM_MM64M();
  1398. mm.raw &= ~mm64m.raw;
  1399. mmx_set(insn.modrm().reg(), mm);
  1400. mmx_common();
  1401. }
  1402. void SoftFPU::POR_mm1_mm2m64(const X86::Instruction& insn)
  1403. {
  1404. LOAD_MM_MM64M();
  1405. mm.raw |= mm64m.raw;
  1406. mmx_set(insn.modrm().reg(), mm);
  1407. mmx_common();
  1408. }
  1409. void SoftFPU::PXOR_mm1_mm2m64(const X86::Instruction& insn)
  1410. {
  1411. LOAD_MM_MM64M();
  1412. mm.raw ^= mm64m.raw;
  1413. mmx_set(insn.modrm().reg(), mm);
  1414. mmx_common();
  1415. }
  1416. // SHIFT
  1417. void SoftFPU::PSLLW_mm1_mm2m64(const X86::Instruction& insn)
  1418. {
  1419. LOAD_MM_MM64M();
  1420. mm.v16 <<= mm64m.v16;
  1421. mmx_set(insn.modrm().reg(), mm);
  1422. mmx_common();
  1423. }
  1424. void SoftFPU::PSLLW_mm1_imm8(const X86::Instruction& insn)
  1425. {
  1426. VERIFY(!insn.has_operand_size_override_prefix()); // SSE2
  1427. u8 imm = insn.imm8();
  1428. MMX mm = mmx_get(insn.modrm().reg());
  1429. mm.v16 <<= imm;
  1430. mmx_set(insn.modrm().reg(), mm);
  1431. mmx_common();
  1432. }
  1433. void SoftFPU::PSLLD_mm1_mm2m64(const X86::Instruction& insn)
  1434. {
  1435. LOAD_MM_MM64M();
  1436. mm.v32 <<= mm64m.v32;
  1437. mmx_set(insn.modrm().reg(), mm);
  1438. mmx_common();
  1439. }
  1440. void SoftFPU::PSLLD_mm1_imm8(const X86::Instruction& insn)
  1441. {
  1442. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1443. u8 imm = insn.imm8();
  1444. MMX mm = mmx_get(insn.modrm().reg());
  1445. mm.v32 <<= imm;
  1446. mmx_set(insn.modrm().reg(), mm);
  1447. mmx_common();
  1448. }
  1449. void SoftFPU::PSLLQ_mm1_mm2m64(const X86::Instruction& insn)
  1450. {
  1451. LOAD_MM_MM64M();
  1452. mm.raw <<= mm64m.raw;
  1453. mmx_set(insn.modrm().reg(), mm);
  1454. mmx_common();
  1455. }
  1456. void SoftFPU::PSLLQ_mm1_imm8(const X86::Instruction& insn)
  1457. {
  1458. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1459. u8 imm = insn.imm8();
  1460. MMX mm = mmx_get(insn.modrm().reg());
  1461. mm.raw <<= imm;
  1462. mmx_set(insn.modrm().reg(), mm);
  1463. mmx_common();
  1464. }
  1465. void SoftFPU::PSRAW_mm1_mm2m64(const X86::Instruction& insn)
  1466. {
  1467. LOAD_MM_MM64M();
  1468. mm.v16 >>= mm64m.v16;
  1469. mmx_set(insn.modrm().reg(), mm);
  1470. mmx_common();
  1471. }
  1472. void SoftFPU::PSRAW_mm1_imm8(const X86::Instruction& insn)
  1473. {
  1474. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1475. u8 imm = insn.imm8();
  1476. MMX mm = mmx_get(insn.modrm().reg());
  1477. mm.v16 >>= imm;
  1478. mmx_set(insn.modrm().reg(), mm);
  1479. mmx_common();
  1480. }
  1481. void SoftFPU::PSRAD_mm1_mm2m64(const X86::Instruction& insn)
  1482. {
  1483. LOAD_MM_MM64M();
  1484. mm.v32 >>= mm64m.v32;
  1485. mmx_set(insn.modrm().reg(), mm);
  1486. mmx_common();
  1487. }
  1488. void SoftFPU::PSRAD_mm1_imm8(const X86::Instruction& insn)
  1489. {
  1490. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1491. u8 imm = insn.imm8();
  1492. MMX mm = mmx_get(insn.modrm().reg());
  1493. mm.v32 >>= imm;
  1494. mmx_set(insn.modrm().reg(), mm);
  1495. mmx_common();
  1496. }
  1497. void SoftFPU::PSRLW_mm1_mm2m64(const X86::Instruction& insn)
  1498. {
  1499. LOAD_MM_MM64M();
  1500. mm.v16u >>= mm64m.v16u;
  1501. mmx_set(insn.modrm().reg(), mm);
  1502. mmx_common();
  1503. }
  1504. void SoftFPU::PSRLW_mm1_imm8(const X86::Instruction& insn)
  1505. {
  1506. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1507. u8 imm = insn.imm8();
  1508. MMX mm = mmx_get(insn.modrm().reg());
  1509. mm.v16u >>= imm;
  1510. mmx_set(insn.modrm().reg(), mm);
  1511. mmx_common();
  1512. }
  1513. void SoftFPU::PSRLD_mm1_mm2m64(const X86::Instruction& insn)
  1514. {
  1515. LOAD_MM_MM64M();
  1516. mm.v32u >>= mm64m.v32u;
  1517. mmx_set(insn.modrm().reg(), mm);
  1518. mmx_common();
  1519. }
  1520. void SoftFPU::PSRLD_mm1_imm8(const X86::Instruction& insn)
  1521. {
  1522. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1523. u8 imm = insn.imm8();
  1524. MMX mm = mmx_get(insn.modrm().reg());
  1525. mm.v32u >>= imm;
  1526. mmx_set(insn.modrm().reg(), mm);
  1527. mmx_common();
  1528. }
  1529. void SoftFPU::PSRLQ_mm1_mm2m64(const X86::Instruction& insn)
  1530. {
  1531. LOAD_MM_MM64M();
  1532. mm.raw >>= mm64m.raw;
  1533. mmx_set(insn.modrm().reg(), mm);
  1534. mmx_common();
  1535. }
  1536. void SoftFPU::PSRLQ_mm1_imm8(const X86::Instruction& insn)
  1537. {
  1538. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1539. u8 imm = insn.imm8();
  1540. MMX mm = mmx_get(insn.modrm().reg());
  1541. mm.raw >>= imm;
  1542. mmx_set(insn.modrm().reg(), mm);
  1543. mmx_common();
  1544. }
  1545. // DATA TRANSFER
  1546. void SoftFPU::MOVD_mm1_rm32(const X86::Instruction& insn)
  1547. {
  1548. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1549. u8 mmx_index = insn.modrm().reg();
  1550. // FIXME:: Shadow Value
  1551. // upper half is zeroed out
  1552. mmx_set(mmx_index, { .raw = insn.modrm().read32(m_cpu, insn).value() });
  1553. mmx_common();
  1554. }
  1555. void SoftFPU::MOVD_rm32_mm2(const X86::Instruction& insn)
  1556. {
  1557. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1558. u8 mmx_index = insn.modrm().reg();
  1559. // FIXME:: Shadow Value
  1560. insn.modrm().write32(m_cpu, insn,
  1561. shadow_wrap_as_initialized(static_cast<u32>(mmx_get(mmx_index).raw)));
  1562. mmx_common();
  1563. }
  1564. void SoftFPU::MOVQ_mm1_mm2m64(const X86::Instruction& insn)
  1565. {
  1566. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1567. // FIXME: Shadow Value
  1568. if (insn.modrm().mod() == 0b11) {
  1569. // instruction
  1570. mmx_set(insn.modrm().reg(),
  1571. mmx_get(insn.modrm().rm()));
  1572. } else {
  1573. mmx_set(insn.modrm().reg(),
  1574. { .raw = insn.modrm().read64(m_cpu, insn).value() });
  1575. }
  1576. mmx_common();
  1577. }
  1578. void SoftFPU::MOVQ_mm1m64_mm2(const X86::Instruction& insn)
  1579. {
  1580. VERIFY(!insn.has_operand_size_override_prefix()); /* SSE2 */
  1581. if (insn.modrm().mod() == 0b11) {
  1582. // instruction
  1583. mmx_set(insn.modrm().rm(),
  1584. mmx_get(insn.modrm().reg()));
  1585. } else {
  1586. // FIXME: Shadow Value
  1587. insn.modrm().write64(m_cpu, insn,
  1588. shadow_wrap_as_initialized(mmx_get(insn.modrm().reg()).raw));
  1589. }
  1590. mmx_common();
  1591. }
  1592. void SoftFPU::MOVQ_mm1_rm64(const X86::Instruction&) { TODO_INSN(); } // long mode
  1593. void SoftFPU::MOVQ_rm64_mm2(const X86::Instruction&) { TODO_INSN(); } // long mode
  1594. // EMPTY MMX STATE
  1595. void SoftFPU::EMMS(const X86::Instruction&)
  1596. {
  1597. // clear tagword
  1598. m_fpu_tw = 0xFFFF;
  1599. }
  1600. }