about summary refs log tree commit diff stats
path: root/apps/raytracing/3.mu
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 /apps/raytracing/3.mu
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 'apps/raytracing/3.mu')
-rw-r--r--apps/raytracing/3.mu19
1 files changed, 11 insertions, 8 deletions
diff --git a/apps/raytracing/3.mu b/apps/raytracing/3.mu
index 359b5c9b..28640c7b 100644
--- a/apps/raytracing/3.mu
+++ b/apps/raytracing/3.mu
@@ -212,6 +212,8 @@ fn main -> exit-status/ebx: int {
         ray-color r, c
         # write color
         print-rgb 0, c
+#?         print-rgb-raw 0, c
+#?         print-string 0, "\n"
       }
       i <- increment
       loop
@@ -257,27 +259,28 @@ type rgb {
 # print translating to [0, 256)
 fn print-rgb screen: (addr screen), _c: (addr rgb) {
   var c/esi: (addr rgb) <- copy _c
-  var n/ecx: int <- copy 0xff  # turns out 255 works just as well as 255.999, which is lucky because we don't have floating-point literals
-  var xn/xmm1: float <- convert n
-  # print 255 * c->r
+  var xn: float
+  var xn-addr/ecx: (addr float) <- address xn
+  fill-in-rational xn-addr, 0x3e7ff, 0x3e8  # 255999 / 1000
+  # print 255.999 * c->r
   var result/xmm0: float <- copy xn
   var src-addr/eax: (addr float) <- get c, r
   result <- multiply *src-addr
-  var result-int/edx: int <- convert result
+  var result-int/edx: int <- truncate result
   print-int32-decimal screen, result-int
   print-string screen, " "
-  # print 255 * c->g
+  # print 255.999 * c->g
   src-addr <- get c, g
   result <- copy xn
   result <- multiply *src-addr
-  result-int <- convert result
+  result-int <- truncate result
   print-int32-decimal screen, result-int
   print-string screen, " "
-  # print 255 * c->b
+  # print 255.999 * c->b
   src-addr <- get c, b
   result <- copy xn
   result <- multiply *src-addr
-  result-int <- convert result
+  result-int <- truncate result
   print-int32-decimal screen, result-int
   print-string screen, "\n"
 }