SoftFPU.cpp 49 KB

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