SoftFPU.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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 { // NOLINT(readability-implicit-bool-conversion) 0/1 to follow spec closer
  33. ALWAYS_INLINE void SoftFPU::warn_if_mmx_absolute(u8 index) const
  34. {
  35. if (m_reg_is_mmx[index]) [[unlikely]] {
  36. reportln("\033[31;1mWarning! Use of an MMX register as an FPU value ({} abs)\033[0m\n", 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. set_c1(0);
  1013. fpu_push(1.0l);
  1014. }
  1015. void SoftFPU::FLDZ(const X86::Instruction&)
  1016. {
  1017. set_c1(0);
  1018. fpu_push(0.0l);
  1019. }
  1020. void SoftFPU::FLDPI(const X86::Instruction&)
  1021. {
  1022. set_c1(0);
  1023. fpu_push(M_PIl);
  1024. }
  1025. void SoftFPU::FLDL2E(const X86::Instruction&)
  1026. {
  1027. set_c1(0);
  1028. fpu_push(M_LOG2El);
  1029. }
  1030. void SoftFPU::FLDLN2(const X86::Instruction&)
  1031. {
  1032. set_c1(0);
  1033. fpu_push(M_LN2l);
  1034. }
  1035. void SoftFPU::FLDL2T(const X86::Instruction&)
  1036. {
  1037. set_c1(0);
  1038. fpu_push(log2l(10.0l));
  1039. }
  1040. void SoftFPU::FLDLG2(const X86::Instruction&)
  1041. {
  1042. set_c1(0);
  1043. fpu_push(log10l(2.0l));
  1044. }
  1045. // CONTROL
  1046. void SoftFPU::FINCSTP(const X86::Instruction&)
  1047. {
  1048. m_fpu_stack_top = (m_fpu_stack_top + 1u) % 8u;
  1049. set_c1(0);
  1050. }
  1051. void SoftFPU::FDECSTP(const X86::Instruction&)
  1052. {
  1053. m_fpu_stack_top = (m_fpu_stack_top - 1u) % 8u;
  1054. set_c1(0);
  1055. }
  1056. void SoftFPU::FFREE(const X86::Instruction& insn)
  1057. {
  1058. fpu_set_tag(insn.modrm().reg_fpu(), FPU_Tag::Empty);
  1059. }
  1060. void SoftFPU::FFREEP(const X86::Instruction& insn)
  1061. {
  1062. FFREE(insn);
  1063. fpu_pop();
  1064. }
  1065. void SoftFPU::FNINIT(const X86::Instruction&)
  1066. {
  1067. m_fpu_cw = 0x037F;
  1068. m_fpu_sw = 0;
  1069. m_fpu_tw = 0xFFFF;
  1070. m_fpu_ip = 0;
  1071. m_fpu_cs = 0;
  1072. m_fpu_dp = 0;
  1073. m_fpu_ds = 0;
  1074. m_fpu_iop = 0;
  1075. };
  1076. void SoftFPU::FNCLEX(const X86::Instruction&)
  1077. {
  1078. m_fpu_error_invalid = 0;
  1079. m_fpu_error_denorm = 0;
  1080. m_fpu_error_zero_div = 0;
  1081. m_fpu_error_overflow = 0;
  1082. m_fpu_error_underflow = 0;
  1083. m_fpu_error_precision = 0;
  1084. m_fpu_error_stackfault = 0;
  1085. m_fpu_busy = 0;
  1086. }
  1087. void SoftFPU::FNSTCW(const X86::Instruction& insn)
  1088. {
  1089. insn.modrm().write16(m_cpu, insn, shadow_wrap_as_initialized(m_fpu_cw));
  1090. }
  1091. void SoftFPU::FLDCW(const X86::Instruction& insn)
  1092. {
  1093. m_fpu_cw = insn.modrm().read16(m_cpu, insn).value();
  1094. }
  1095. void SoftFPU::FNSTENV(const X86::Instruction& insn)
  1096. {
  1097. // Assuming we are always in Protected mode
  1098. // FIXME: 16-bit Format
  1099. // 32-bit Format
  1100. /* 31--------------16---------------0
  1101. * | | CW | 0
  1102. * +----------------+---------------+
  1103. * | | SW | 4
  1104. * +----------------+---------------+
  1105. * | | TW | 8
  1106. * +----------------+---------------+
  1107. * | FIP | 12
  1108. * +----+-----------+---------------+
  1109. * |0000|fpuOp[10:0]| FIP_sel | 16
  1110. * +----+-----------+---------------+
  1111. * | FDP | 20
  1112. * +----------------+---------------+
  1113. * | | FDP_ds | 24
  1114. * +----------------|---------------+
  1115. * */
  1116. auto address = insn.modrm().resolve(m_cpu, insn);
  1117. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_cw));
  1118. address.set_offset(address.offset() + 4);
  1119. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_sw));
  1120. address.set_offset(address.offset() + 4);
  1121. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_tw));
  1122. address.set_offset(address.offset() + 4);
  1123. m_cpu.write_memory32(address, shadow_wrap_as_initialized(m_fpu_ip));
  1124. address.set_offset(address.offset() + 4);
  1125. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_cs));
  1126. address.set_offset(address.offset() + 2);
  1127. m_cpu.write_memory16(address, shadow_wrap_as_initialized<u16>(m_fpu_iop & 0x3FFU));
  1128. address.set_offset(address.offset() + 2);
  1129. m_cpu.write_memory32(address, shadow_wrap_as_initialized(m_fpu_dp));
  1130. address.set_offset(address.offset() + 4);
  1131. m_cpu.write_memory16(address, shadow_wrap_as_initialized(m_fpu_ds));
  1132. }
  1133. void SoftFPU::FLDENV(const X86::Instruction& insn)
  1134. {
  1135. // Assuming we are always in Protected mode
  1136. // FIXME: 16-bit Format
  1137. auto address = insn.modrm().resolve(m_cpu, insn);
  1138. // FIXME: Shadow Values
  1139. m_fpu_cw = m_cpu.read_memory16(address).value();
  1140. address.set_offset(address.offset() + 4);
  1141. m_fpu_sw = m_cpu.read_memory16(address).value();
  1142. address.set_offset(address.offset() + 4);
  1143. m_fpu_tw = m_cpu.read_memory16(address).value();
  1144. address.set_offset(address.offset() + 4);
  1145. m_fpu_ip = m_cpu.read_memory32(address).value();
  1146. address.set_offset(address.offset() + 4);
  1147. m_fpu_cs = m_cpu.read_memory16(address).value();
  1148. address.set_offset(address.offset() + 2);
  1149. m_fpu_iop = m_cpu.read_memory16(address).value();
  1150. address.set_offset(address.offset() + 2);
  1151. m_fpu_dp = m_cpu.read_memory32(address).value();
  1152. address.set_offset(address.offset() + 4);
  1153. m_fpu_ds = m_cpu.read_memory16(address).value();
  1154. }
  1155. void SoftFPU::FNSAVE(const X86::Instruction& insn)
  1156. {
  1157. FNSTENV(insn);
  1158. auto address = insn.modrm().resolve(m_cpu, insn);
  1159. address.set_offset(address.offset() + 28); // size of the ENV
  1160. // write fpu-stack to memory
  1161. u8 raw_data[80];
  1162. for (int i = 0; i < 8; ++i) {
  1163. memcpy(raw_data + 10 * i, &m_storage[i], 10);
  1164. }
  1165. for (int i = 0; i < 5; ++i) {
  1166. // FIXME: Shadow Value
  1167. m_cpu.write_memory128(address, shadow_wrap_as_initialized(((u128*)raw_data)[i]));
  1168. address.set_offset(address.offset() + 16);
  1169. }
  1170. FNINIT(insn);
  1171. }
  1172. void SoftFPU::FRSTOR(const X86::Instruction& insn)
  1173. {
  1174. FLDENV(insn);
  1175. auto address = insn.modrm().resolve(m_cpu, insn);
  1176. address.set_offset(address.offset() + 28); // size of the ENV
  1177. // read fpu-stack from memory
  1178. u8 raw_data[80];
  1179. for (int i = 0; i < 5; ++i) {
  1180. // FIXME: Shadow Value
  1181. ((u128*)raw_data)[i] = m_cpu.read_memory128(address).value();
  1182. address.set_offset(address.offset() + 16);
  1183. }
  1184. for (int i = 0; i < 8; ++i) {
  1185. memcpy(&m_storage[i], raw_data + 10 * i, 10);
  1186. }
  1187. memset(m_reg_is_mmx, 0, sizeof(m_reg_is_mmx));
  1188. }
  1189. void SoftFPU::FNSTSW(const X86::Instruction& insn)
  1190. {
  1191. insn.modrm().write16(m_cpu, insn, shadow_wrap_as_initialized(m_fpu_sw));
  1192. }
  1193. void SoftFPU::FNSTSW_AX(const X86::Instruction&)
  1194. {
  1195. m_cpu.set_ax(shadow_wrap_as_initialized(m_fpu_sw));
  1196. }
  1197. // FIXME: FWAIT
  1198. void SoftFPU::FNOP(const X86::Instruction&) { }
  1199. // DO NOTHING?
  1200. void SoftFPU::FNENI(const X86::Instruction&) { TODO_INSN(); }
  1201. void SoftFPU::FNDISI(const X86::Instruction&) { TODO_INSN(); }
  1202. void SoftFPU::FNSETPM(const X86::Instruction&) { TODO_INSN(); }
  1203. // MMX
  1204. // helpers
  1205. #define LOAD_MM_MM64M() \
  1206. MMX mm; \
  1207. MMX mm64m; \
  1208. if (insn.modrm().mod() == 0b11) { /* 0b11 signals a register */ \
  1209. mm64m = mmx_get(insn.modrm().rm()); \
  1210. } else { \
  1211. auto temp = insn.modrm().read64(m_cpu, insn); \
  1212. warn_if_uninitialized(temp, "Read of uninitialized Memory as Packed integer"); \
  1213. mm64m.raw = temp.value(); \
  1214. } \
  1215. mm = mmx_get(insn.modrm().reg())
  1216. #define MMX_intrinsic(intrinsic, res_type, actor_type) \
  1217. LOAD_MM_MM64M(); \
  1218. mm.res_type = __builtin_ia32_##intrinsic(mm.actor_type, mm64m.actor_type); \
  1219. mmx_set(insn.modrm().reg(), mm); \
  1220. mmx_common();
  1221. // ARITHMETIC
  1222. void SoftFPU::PADDB_mm1_mm2m64(const X86::Instruction& insn)
  1223. {
  1224. LOAD_MM_MM64M();
  1225. mm.v8 += mm64m.v8;
  1226. mmx_set(insn.modrm().reg(), mm);
  1227. mmx_common();
  1228. }
  1229. void SoftFPU::PADDW_mm1_mm2m64(const X86::Instruction& insn)
  1230. {
  1231. LOAD_MM_MM64M();
  1232. mm.v16 += mm64m.v16;
  1233. mmx_set(insn.modrm().reg(), mm);
  1234. mmx_common();
  1235. }
  1236. void SoftFPU::PADDD_mm1_mm2m64(const X86::Instruction& insn)
  1237. {
  1238. LOAD_MM_MM64M();
  1239. mm.v32 += mm64m.v32;
  1240. mmx_set(insn.modrm().reg(), mm);
  1241. mmx_common();
  1242. }
  1243. void SoftFPU::PADDSB_mm1_mm2m64(const X86::Instruction& insn)
  1244. {
  1245. MMX_intrinsic(paddsb, v8, v8);
  1246. }
  1247. void SoftFPU::PADDSW_mm1_mm2m64(const X86::Instruction& insn)
  1248. {
  1249. MMX_intrinsic(paddsw, v16, v16);
  1250. }
  1251. void SoftFPU::PADDUSB_mm1_mm2m64(const X86::Instruction& insn)
  1252. {
  1253. MMX_intrinsic(paddusb, v8, v8);
  1254. }
  1255. void SoftFPU::PADDUSW_mm1_mm2m64(const X86::Instruction& insn)
  1256. {
  1257. MMX_intrinsic(paddusw, v16, v16);
  1258. }
  1259. void SoftFPU::PSUBB_mm1_mm2m64(const X86::Instruction& insn)
  1260. {
  1261. LOAD_MM_MM64M();
  1262. mm.v8 -= mm64m.v8;
  1263. mmx_set(insn.modrm().reg(), mm);
  1264. mmx_common();
  1265. }
  1266. void SoftFPU::PSUBW_mm1_mm2m64(const X86::Instruction& insn)
  1267. {
  1268. LOAD_MM_MM64M();
  1269. mm.v16 -= mm64m.v16;
  1270. mmx_set(insn.modrm().reg(), mm);
  1271. mmx_common();
  1272. }
  1273. void SoftFPU::PSUBD_mm1_mm2m64(const X86::Instruction& insn)
  1274. {
  1275. LOAD_MM_MM64M();
  1276. mm.v32 -= mm64m.v32;
  1277. mmx_set(insn.modrm().reg(), mm);
  1278. mmx_common();
  1279. }
  1280. void SoftFPU::PSUBSB_mm1_mm2m64(const X86::Instruction& insn)
  1281. {
  1282. MMX_intrinsic(psubsb, v8, v8);
  1283. }
  1284. void SoftFPU::PSUBSW_mm1_mm2m64(const X86::Instruction& insn)
  1285. {
  1286. MMX_intrinsic(psubsw, v16, v16);
  1287. }
  1288. void SoftFPU::PSUBUSB_mm1_mm2m64(const X86::Instruction& insn)
  1289. {
  1290. MMX_intrinsic(psubusb, v8, v8);
  1291. }
  1292. void SoftFPU::PSUBUSW_mm1_mm2m64(const X86::Instruction& insn)
  1293. {
  1294. MMX_intrinsic(psubusw, v16, v16);
  1295. }
  1296. void SoftFPU::PMULHW_mm1_mm2m64(const X86::Instruction& insn)
  1297. {
  1298. MMX_intrinsic(pmulhw, v16, v16);
  1299. }
  1300. void SoftFPU::PMULLW_mm1_mm2m64(const X86::Instruction& insn)
  1301. {
  1302. MMX_intrinsic(pmullw, v16, v16);
  1303. }
  1304. void SoftFPU::PMADDWD_mm1_mm2m64(const X86::Instruction& insn)
  1305. {
  1306. MMX_intrinsic(pmaddwd, v32, v16);
  1307. }
  1308. // COMPARISON
  1309. void SoftFPU::PCMPEQB_mm1_mm2m64(const X86::Instruction& insn)
  1310. {
  1311. LOAD_MM_MM64M();
  1312. mm.v8 = mm.v8 == mm64m.v8;
  1313. mmx_set(insn.modrm().reg(), mm);
  1314. mmx_common();
  1315. }
  1316. void SoftFPU::PCMPEQW_mm1_mm2m64(const X86::Instruction& insn)
  1317. {
  1318. LOAD_MM_MM64M();
  1319. mm.v16 = mm.v16 == mm64m.v16;
  1320. mmx_set(insn.modrm().reg(), mm);
  1321. mmx_common();
  1322. }
  1323. void SoftFPU::PCMPEQD_mm1_mm2m64(const X86::Instruction& insn)
  1324. {
  1325. LOAD_MM_MM64M();
  1326. mm.v32 = mm.v32 == mm64m.v32;
  1327. mmx_set(insn.modrm().reg(), mm);
  1328. mmx_common();
  1329. }
  1330. void SoftFPU::PCMPGTB_mm1_mm2m64(const X86::Instruction& insn)
  1331. {
  1332. LOAD_MM_MM64M();
  1333. mm.v8 = mm.v8 > mm64m.v8;
  1334. mmx_set(insn.modrm().reg(), mm);
  1335. mmx_common();
  1336. }
  1337. void SoftFPU::PCMPGTW_mm1_mm2m64(const X86::Instruction& insn)
  1338. {
  1339. LOAD_MM_MM64M();
  1340. mm.v16 = mm.v16 > mm64m.v16;
  1341. mmx_set(insn.modrm().reg(), mm);
  1342. mmx_common();
  1343. }
  1344. void SoftFPU::PCMPGTD_mm1_mm2m64(const X86::Instruction& insn)
  1345. {
  1346. LOAD_MM_MM64M();
  1347. mm.v32 = mm.v32 > mm64m.v32;
  1348. mmx_set(insn.modrm().reg(), mm);
  1349. mmx_common();
  1350. }
  1351. // CONVERSION
  1352. void SoftFPU::PACKSSDW_mm1_mm2m64(const X86::Instruction& insn)
  1353. {
  1354. MMX_intrinsic(packssdw, v16, v32);
  1355. }
  1356. void SoftFPU::PACKSSWB_mm1_mm2m64(const X86::Instruction& insn)
  1357. {
  1358. MMX_intrinsic(packsswb, v8, v16);
  1359. }
  1360. void SoftFPU::PACKUSWB_mm1_mm2m64(const X86::Instruction& insn)
  1361. {
  1362. MMX_intrinsic(packuswb, v8, v16);
  1363. }
  1364. // UNPACK
  1365. void SoftFPU::PUNPCKHBW_mm1_mm2m64(const X86::Instruction& insn)
  1366. {
  1367. MMX_intrinsic(punpckhbw, v8, v8);
  1368. }
  1369. void SoftFPU::PUNPCKHWD_mm1_mm2m64(const X86::Instruction& insn)
  1370. {
  1371. MMX_intrinsic(punpckhwd, v16, v16);
  1372. }
  1373. void SoftFPU::PUNPCKHDQ_mm1_mm2m64(const X86::Instruction& insn)
  1374. {
  1375. MMX_intrinsic(punpckhdq, v32, v32);
  1376. }
  1377. void SoftFPU::PUNPCKLBW_mm1_mm2m32(const X86::Instruction& insn)
  1378. {
  1379. MMX_intrinsic(punpcklbw, v8, v8);
  1380. }
  1381. void SoftFPU::PUNPCKLWD_mm1_mm2m32(const X86::Instruction& insn)
  1382. {
  1383. MMX_intrinsic(punpcklwd, v16, v16);
  1384. }
  1385. void SoftFPU::PUNPCKLDQ_mm1_mm2m32(const X86::Instruction& insn)
  1386. {
  1387. MMX_intrinsic(punpckldq, v32, v32);
  1388. }
  1389. // LOGICAL
  1390. void SoftFPU::PAND_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::PANDN_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::POR_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. void SoftFPU::PXOR_mm1_mm2m64(const X86::Instruction& insn)
  1412. {
  1413. LOAD_MM_MM64M();
  1414. mm.raw ^= mm64m.raw;
  1415. mmx_set(insn.modrm().reg(), mm);
  1416. mmx_common();
  1417. }
  1418. // SHIFT
  1419. void SoftFPU::PSLLW_mm1_mm2m64(const X86::Instruction& insn)
  1420. {
  1421. LOAD_MM_MM64M();
  1422. mm.v16 <<= mm64m.v16;
  1423. mmx_set(insn.modrm().reg(), mm);
  1424. mmx_common();
  1425. }
  1426. void SoftFPU::PSLLW_mm1_imm8(const X86::Instruction& insn)
  1427. {
  1428. u8 imm = insn.imm8();
  1429. MMX mm = mmx_get(insn.modrm().reg());
  1430. mm.v16 <<= imm;
  1431. mmx_set(insn.modrm().reg(), mm);
  1432. mmx_common();
  1433. }
  1434. void SoftFPU::PSLLD_mm1_mm2m64(const X86::Instruction& insn)
  1435. {
  1436. LOAD_MM_MM64M();
  1437. mm.v32 <<= mm64m.v32;
  1438. mmx_set(insn.modrm().reg(), mm);
  1439. mmx_common();
  1440. }
  1441. void SoftFPU::PSLLD_mm1_imm8(const X86::Instruction& insn)
  1442. {
  1443. u8 imm = insn.imm8();
  1444. MMX mm = mmx_get(insn.modrm().reg());
  1445. mm.v32 <<= imm;
  1446. mmx_set(insn.modrm().reg(), mm);
  1447. mmx_common();
  1448. }
  1449. void SoftFPU::PSLLQ_mm1_mm2m64(const X86::Instruction& insn)
  1450. {
  1451. LOAD_MM_MM64M();
  1452. mm.raw <<= mm64m.raw;
  1453. mmx_set(insn.modrm().reg(), mm);
  1454. mmx_common();
  1455. }
  1456. void SoftFPU::PSLLQ_mm1_imm8(const X86::Instruction& insn)
  1457. {
  1458. u8 imm = insn.imm8();
  1459. MMX mm = mmx_get(insn.modrm().reg());
  1460. mm.raw <<= imm;
  1461. mmx_set(insn.modrm().reg(), mm);
  1462. mmx_common();
  1463. }
  1464. void SoftFPU::PSRAW_mm1_mm2m64(const X86::Instruction& insn)
  1465. {
  1466. LOAD_MM_MM64M();
  1467. mm.v16 >>= mm64m.v16;
  1468. mmx_set(insn.modrm().reg(), mm);
  1469. mmx_common();
  1470. }
  1471. void SoftFPU::PSRAW_mm1_imm8(const X86::Instruction& insn)
  1472. {
  1473. u8 imm = insn.imm8();
  1474. MMX mm = mmx_get(insn.modrm().reg());
  1475. mm.v16 >>= imm;
  1476. mmx_set(insn.modrm().reg(), mm);
  1477. mmx_common();
  1478. }
  1479. void SoftFPU::PSRAD_mm1_mm2m64(const X86::Instruction& insn)
  1480. {
  1481. LOAD_MM_MM64M();
  1482. mm.v32 >>= mm64m.v32;
  1483. mmx_set(insn.modrm().reg(), mm);
  1484. mmx_common();
  1485. }
  1486. void SoftFPU::PSRAD_mm1_imm8(const X86::Instruction& insn)
  1487. {
  1488. u8 imm = insn.imm8();
  1489. MMX mm = mmx_get(insn.modrm().reg());
  1490. mm.v32 >>= imm;
  1491. mmx_set(insn.modrm().reg(), mm);
  1492. mmx_common();
  1493. }
  1494. void SoftFPU::PSRLW_mm1_mm2m64(const X86::Instruction& insn)
  1495. {
  1496. LOAD_MM_MM64M();
  1497. mm.v16u >>= mm64m.v16u;
  1498. mmx_set(insn.modrm().reg(), mm);
  1499. mmx_common();
  1500. }
  1501. void SoftFPU::PSRLW_mm1_imm8(const X86::Instruction& insn)
  1502. {
  1503. u8 imm = insn.imm8();
  1504. MMX mm = mmx_get(insn.modrm().reg());
  1505. mm.v16u >>= imm;
  1506. mmx_set(insn.modrm().reg(), mm);
  1507. mmx_common();
  1508. }
  1509. void SoftFPU::PSRLD_mm1_mm2m64(const X86::Instruction& insn)
  1510. {
  1511. LOAD_MM_MM64M();
  1512. mm.v32u >>= mm64m.v32u;
  1513. mmx_set(insn.modrm().reg(), mm);
  1514. mmx_common();
  1515. }
  1516. void SoftFPU::PSRLD_mm1_imm8(const X86::Instruction& insn)
  1517. {
  1518. u8 imm = insn.imm8();
  1519. MMX mm = mmx_get(insn.modrm().reg());
  1520. mm.v32u >>= imm;
  1521. mmx_set(insn.modrm().reg(), mm);
  1522. mmx_common();
  1523. }
  1524. void SoftFPU::PSRLQ_mm1_mm2m64(const X86::Instruction& insn)
  1525. {
  1526. LOAD_MM_MM64M();
  1527. mm.raw >>= mm64m.raw;
  1528. mmx_set(insn.modrm().reg(), mm);
  1529. mmx_common();
  1530. }
  1531. void SoftFPU::PSRLQ_mm1_imm8(const X86::Instruction& insn)
  1532. {
  1533. u8 imm = insn.imm8();
  1534. MMX mm = mmx_get(insn.modrm().reg());
  1535. mm.raw >>= imm;
  1536. mmx_set(insn.modrm().reg(), mm);
  1537. mmx_common();
  1538. }
  1539. // DATA TRANSFER
  1540. void SoftFPU::MOVD_mm1_rm32(const X86::Instruction& insn)
  1541. {
  1542. u8 mmx_index = insn.modrm().reg();
  1543. // FIXME:: Shadow Value
  1544. // upper half is zeroed out
  1545. mmx_set(mmx_index, { .raw = insn.modrm().read32(m_cpu, insn).value() });
  1546. mmx_common();
  1547. };
  1548. void SoftFPU::MOVD_rm32_mm2(const X86::Instruction& insn)
  1549. {
  1550. u8 mmx_index = insn.modrm().reg();
  1551. // FIXME:: Shadow Value
  1552. insn.modrm().write32(m_cpu, insn,
  1553. shadow_wrap_as_initialized(static_cast<u32>(mmx_get(mmx_index).raw)));
  1554. mmx_common();
  1555. };
  1556. void SoftFPU::MOVQ_mm1_mm2m64(const X86::Instruction& insn)
  1557. {
  1558. // FIXME: Shadow Value
  1559. if (insn.modrm().mod() == 0b11) {
  1560. // instruction
  1561. mmx_set(insn.modrm().reg(),
  1562. mmx_get(insn.modrm().rm()));
  1563. } else {
  1564. mmx_set(insn.modrm().reg(),
  1565. { .raw = insn.modrm().read64(m_cpu, insn).value() });
  1566. }
  1567. mmx_common();
  1568. }
  1569. void SoftFPU::MOVQ_mm1m64_mm2(const X86::Instruction& insn)
  1570. {
  1571. if (insn.modrm().mod() == 0b11) {
  1572. // instruction
  1573. mmx_set(insn.modrm().rm(),
  1574. mmx_get(insn.modrm().reg()));
  1575. } else {
  1576. // FIXME: Shadow Value
  1577. insn.modrm().write64(m_cpu, insn,
  1578. shadow_wrap_as_initialized(mmx_get(insn.modrm().reg()).raw));
  1579. }
  1580. mmx_common();
  1581. }
  1582. void SoftFPU::MOVQ_mm1_rm64(const X86::Instruction&) { TODO_INSN(); }; // long mode
  1583. void SoftFPU::MOVQ_rm64_mm2(const X86::Instruction&) { TODO_INSN(); }; // long mode
  1584. // EMPTY MMX STATE
  1585. void SoftFPU::EMMS(const X86::Instruction&)
  1586. {
  1587. // clear tagword
  1588. m_fpu_tw = 0xFFFF;
  1589. }
  1590. }