about summary refs log tree commit diff stats
path: root/023float.cc
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-10-05 10:16:53 -0700
committerKartik Agaram <vc@akkartik.com>2020-10-05 10:25:25 -0700
commitf13576b5d273ef9175e938b15f55bb1ead22fb1d (patch)
treea54c4667e384a7eec44d0e5e7c2da687b670b7e9 /023float.cc
parentbb3ce6cdea12ff00b998c5a1c6dbf2c83dba77c2 (diff)
downloadmu-f13576b5d273ef9175e938b15f55bb1ead22fb1d.tar.gz
6957
The final fix to the raytracing program involves rounding modes. It turns
out x86 processors round floats by default, unlike C which has trained
me to expect truncation. Rather than mess with the MXCSR register, I added
another instruction for truncation. Now milestone 3 emits perfectly correct
results.
Diffstat (limited to '023float.cc')
-rw-r--r--023float.cc32
1 files changed, 30 insertions, 2 deletions
diff --git a/023float.cc b/023float.cc
index e3524c98..178e86b3 100644
--- a/023float.cc
+++ b/023float.cc
@@ -111,10 +111,11 @@ case 0x2a: {  // convert integer to float
 
 :(before "End Initialize Op Names")
 put_new(Name_f3_0f, "2d", "convert floating-point to int (cvtss2si)");
+put_new(Name_f3_0f, "2c", "truncate floating-point to int (cvttss2si)");
 
 :(code)
 void test_cvtss2si() {
-  Xmm[0] = 10.0;
+  Xmm[0] = 9.8;
   run(
       "== code 0x1\n"
       // op     ModR/M  SIB   displacement  immediate
@@ -134,7 +135,34 @@ case 0x2d: {  // convert float to integer
   const uint8_t dest = (modrm>>3)&0x7;
   trace(Callstack_depth+1, "run") << "convert x/m32 to " << rname(dest) << end();
   const float* src = effective_address_float(modrm);
-  Reg[dest].i = *src;
+  Reg[dest].i = round(*src);
+  trace(Callstack_depth+1, "run") << rname(dest) << " is now 0x" << HEXWORD << Reg[dest].i << end();
+  break;
+}
+
+:(code)
+void test_cvttss2si() {
+  Xmm[0] = 9.8;
+  run(
+      "== code 0x1\n"
+      // op     ModR/M  SIB   displacement  immediate
+      "f3 0f 2c c0                                    \n"
+      // ModR/M in binary: 11 (direct mode) 000 (EAX) 000 (XMM0)
+  );
+  CHECK_TRACE_CONTENTS(
+      "run: truncate x/m32 to EAX\n"
+      "run: x/m32 is XMM0\n"
+      "run: EAX is now 0x00000009\n"
+  );
+}
+
+:(before "End Three-Byte Opcodes Starting With f3 0f")
+case 0x2c: {  // truncate float to integer
+  const uint8_t modrm = next();
+  const uint8_t dest = (modrm>>3)&0x7;
+  trace(Callstack_depth+1, "run") << "truncate x/m32 to " << rname(dest) << end();
+  const float* src = effective_address_float(modrm);
+  Reg[dest].i = trunc(*src);
   trace(Callstack_depth+1, "run") << rname(dest) << " is now 0x" << HEXWORD << Reg[dest].i << end();
   break;
 }