SoftFPU.cpp 50 KB

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