From f13576b5d273ef9175e938b15f55bb1ead22fb1d Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 5 Oct 2020 10:16:53 -0700 Subject: 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. --- apps/raytracing/3.mu | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'apps/raytracing/3.mu') 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" } -- cgit 1.4.1-2-gfad0