From f5e1656d15325b37c91ee110215e9a032e6cc1ce Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sun, 4 Oct 2020 11:52:28 -0700 Subject: 6948 - stress-testing prints of floats Looks good. --- 408print-float.mu | 6 +-- apps/raytracing/3.mu | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ apps/raytracing/ray.mu | 13 +++++ apps/raytracing/vec.mu | 11 +++++ 4 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 apps/raytracing/3.mu create mode 100644 apps/raytracing/ray.mu diff --git a/408print-float.mu b/408print-float.mu index d1782044..67e46f03 100644 --- a/408print-float.mu +++ b/408print-float.mu @@ -8,7 +8,7 @@ # mantissa exponent # = 0 | 000000000000000000000000 | 01111110 # zero-pad mantissa exponent -# = +1.000000 p -01 +# = +1.000000 P -01 fn test-print-float-normal { var screen-on-stack: screen var screen/esi: (addr screen) <- address screen-on-stack @@ -21,7 +21,7 @@ fn test-print-float-normal { half <- divide two-f print-float screen, half # - check-screen-row screen, 1, "1.000000p-01 ", "F - test-print-float-normal" + check-screen-row screen, 1, "1.000000P-01 ", "F - test-print-float-normal" } fn test-print-float-zero { @@ -150,7 +150,7 @@ $print-float:body: { mantissa <- and 0x7fffff print-int32-hex-bits screen, mantissa, 0x18 # print exponent - print-string screen, "p" + print-string screen, "P" exponent <- subtract 0x7f compare exponent, 0 { diff --git a/apps/raytracing/3.mu b/apps/raytracing/3.mu new file mode 100644 index 00000000..6be820f6 --- /dev/null +++ b/apps/raytracing/3.mu @@ -0,0 +1,127 @@ +# Listing 7 of https://raytracing.github.io/books/RayTracingInOneWeekend.html +# +# To run (on Linux): +# $ git clone https://github.com/akkartik/mu +# $ cd mu +# $ ./translate_mu apps/raytracing/3.mu +# $ ./a.elf > 3.ppm + +fn main -> exit-status/ebx: int { + print-string 0, "P3\n256 256\n255\n" + var _four/edx: int <- copy 4 + var four/xmm1: float <- convert _four + var one-fourth/xmm1: float <- reciprocal four + var max/edx: int <- copy 0xff + var image-size/xmm2: float <- convert max + var j/ecx: int <- copy 0xff + { + compare j, 0 + break-if-< + var i/eax: int <- copy 0 + { + compare i, 0xff + break-if-> +#? var c: rgb +#? # compute r +#? var tmp/xmm0: float <- convert i +#? tmp <- divide image-size +#? var r-addr/edx: (addr float) <- get c, r +#? copy-to *r-addr, tmp +#? #? var tmp2/ebx: int <- reinterpret *r-addr +#? #? print-int32-hex 0, tmp2 +#? #? print-string 0, "\n" +#? # compute g +#? tmp <- convert j +#? tmp <- divide image-size +#? var g-addr/edx: (addr float) <- get c, g +#? copy-to *g-addr, tmp +#? # compute b +#? var b-addr/edx: (addr float) <- get c, b +#? copy-to *b-addr, one-fourth +#? # emit +#? var c-addr/edx: (addr rgb) <- address c +#? print-rgb 0, c-addr + var p: vec3 + # compute r + var tmp/xmm0: float <- convert i + tmp <- divide image-size + var r-addr/edx: (addr float) <- get p, x + copy-to *r-addr, tmp +#? var tmp2/ebx: int <- reinterpret *r-addr +#? print-int32-hex 0, tmp2 +#? print-string 0, "\n" + # compute g + tmp <- convert j + tmp <- divide image-size + var g-addr/edx: (addr float) <- get p, y + copy-to *g-addr, tmp + # compute b + var b-addr/edx: (addr float) <- get p, z + copy-to *b-addr, one-fourth + # emit + var p-addr/edx: (addr vec3) <- address p + print-vec3 0, p-addr + print-string 0, "\n" + i <- increment + loop + } + j <- decrement + loop + } + exit-status <- copy 1 +} + +type rgb { + # components normalized to within [0.0, 1.0] + r: float + g: float + b: float +} + +# 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 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 + print-int32-decimal screen, result-int + print-string screen, " " + # print 255 * c->g + src-addr <- get c, g + result <- copy xn + result <- multiply *src-addr + result-int <- convert result + print-int32-decimal screen, result-int + print-string screen, " " + # print 255 * c->b + src-addr <- get c, b + result <- copy xn + result <- multiply *src-addr + result-int <- convert result + print-int32-decimal screen, result-int + print-string screen, "\n" +} + +type vec3 { + x: float + y: float + z: float +} + +fn print-vec3 screen: (addr screen), _a: (addr vec3) { + var a/esi: (addr vec3) <- copy _a + print-string screen, "(" + var tmp/eax: (addr float) <- get a, x + print-float screen, *tmp + print-string screen, ", " + tmp <- get a, y + print-float screen, *tmp + print-string screen, ", " + tmp <- get a, z + print-float screen, *tmp + print-string screen, ")" +} diff --git a/apps/raytracing/ray.mu b/apps/raytracing/ray.mu new file mode 100644 index 00000000..ca2cb475 --- /dev/null +++ b/apps/raytracing/ray.mu @@ -0,0 +1,13 @@ +type ray { + orig: vec3 # point + dir: vec3 +} + +fn ray-at _self: (addr ray), t: float, out: (addr vec3) { + var self/esi: (addr ray) <- copy _self + var src/eax: (addr vec3) <- get self, dir + copy-object src, out + vec3-mul-by out, t + src <- get self, orig + vec3-add-to out, src +} diff --git a/apps/raytracing/vec.mu b/apps/raytracing/vec.mu index 29516fb1..5539a0dd 100644 --- a/apps/raytracing/vec.mu +++ b/apps/raytracing/vec.mu @@ -35,4 +35,15 @@ fn vec3-unit in: (addr vec3), out: (addr vec3) { } fn print-vec3 screen: (addr screen), _a: (addr vec3) { + var a/esi: (addr vec3) <- copy _a + print-string screen, "(" + var tmp/eax: (addr float) <- get a, x + print-float screen, *tmp + print-string screen, ", " + tmp <- get a, y + print-float screen, *tmp + print-string screen, ", " + tmp <- get a, z + print-float screen, *tmp + print-string screen, ")" } -- cgit 1.4.1-2-gfad0 vc@akkartik.com> 2016-06-13 16:39:57 -0700 3056' href='/akkartik/mu/commit/068hash.cc?h=hlt&id=3df1842ccf623d6635456653d3bb7b9cef0e5481'>3df1842c ^
37421f8a ^







3df1842c ^
37421f8a ^
78c50205 ^
3a9ff084 ^
37421f8a ^



















c442a5ad ^
067c8ed6 ^
b0bf5321 ^
37421f8a ^


3a9ff084 ^
23d3a022 ^
6c96a437 ^
37421f8a ^






b24eb476 ^
3473c63a ^
f6c7b415 ^
3377364a ^
6c96a437 ^
3473c63a ^
067c8ed6 ^
37421f8a ^





37421f8a ^
b55b15a9 ^
b24eb476 ^

6c96a437 ^
3473c63a ^
37e71785 ^
37421f8a ^


3adc9e08 ^
37421f8a ^



37421f8a ^
b55b15a9 ^
af023b32 ^
b24eb476 ^
3473c63a ^
05331766 ^
37e71785 ^
af023b32 ^
37421f8a ^















4a943d4e ^




















37421f8a ^
4a943d4e ^
























37421f8a ^
4a943d4e ^




















37e71785 ^
37421f8a ^


4a943d4e ^










37421f8a ^


4a943d4e ^











37421f8a ^
4a943d4e ^


















37421f8a ^
4a943d4e ^
















37421f8a ^
4a943d4e ^
























37421f8a ^
4a943d4e ^

























37421f8a ^
4a943d4e ^

























37421f8a ^
4a943d4e ^
























37421f8a ^




4a943d4e ^












37421f8a ^







2b250717 ^
37421f8a ^

78c50205 ^
0f2781f8 ^
d5fe534d ^




37421f8a ^
23d3a022 ^
d5fe534d ^

6c96a437 ^
d5fe534d ^












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422