SoftFPU.cpp 50 KB

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