https://github.com/akkartik/mu/blob/main/apps/raytracing/vec.mu
1 type vec3 {
2 x: float
3 y: float
4 z: float
5 }
6
7 fn print-vec3 screen: (addr screen), _v: (addr vec3) {
8 var v/esi: (addr vec3) <- copy _v
9 print-string screen, "("
10 var tmp/eax: (addr float) <- get v, x
11 print-float-hex screen, *tmp
12 print-string screen, ", "
13 tmp <- get v, y
14 print-float-hex screen, *tmp
15 print-string screen, ", "
16 tmp <- get v, z
17 print-float-hex screen, *tmp
18 print-string screen, ")"
19 }
20
21 fn vec3-add-to _v1: (addr vec3), _v2: (addr vec3) {
22 var v1/edi: (addr vec3) <- copy _v1
23 var v2/esi: (addr vec3) <- copy _v2
24
25 var arg1/eax: (addr float) <- get v1, x
26 var arg2/ecx: (addr float) <- get v2, x
27 var result/xmm0: float <- copy *arg1
28 result <- add *arg2
29 copy-to *arg1, result
30
31 arg1 <- get v1, y
32 arg2 <- get v2, y
33 result <- copy *arg1
34 result <- add *arg2
35 copy-to *arg1, result
36
37 arg1 <- get v1, z
38 arg2 <- get v2, z
39 result <- copy *arg1
40 result <- add *arg2
41 copy-to *arg1, result
42 }
43
44 fn vec3-subtract-from v1: (addr vec3), v2: (addr vec3) {
45 var tmp-storage: vec3
46 var tmp/eax: (addr vec3) <- address tmp-storage
47 copy-object v2, tmp
48 vec3-negate tmp
49 vec3-add-to v1, tmp
50 }
51
52 fn vec3-negate v: (addr vec3) {
53 var negative-one/eax: int <- copy -1
54 var negative-one-f/xmm0: float <- convert negative-one
55 vec3-scale-up v, negative-one-f
56 }
57
58 fn vec3-scale-up _v: (addr vec3), f: float {
59 var v/edi: (addr vec3) <- copy _v
60
61 var dest/eax: (addr float) <- get v, x
62 var result/xmm0: float <- copy *dest
63 result <- multiply f
64 copy-to *dest, result
65
66 dest <- get v, y
67 result <- copy *dest
68 result <- multiply f
69 copy-to *dest, result
70
71 dest <- get v, z
72 result <- copy *dest
73 result <- multiply f
74 copy-to *dest, result
75 }
76
77 fn vec3-scale-down _v: (addr vec3), f: float {
78 var v/edi: (addr vec3) <- copy _v
79
80 var dest/eax: (addr float) <- get v, x
81 var result/xmm0: float <- copy *dest
82 result <- divide f
83 copy-to *dest, result
84
85 dest <- get v, y
86 result <- copy *dest
87 result <- divide f
88 copy-to *dest, result
89
90 dest <- get v, z
91 result <- copy *dest
92 result <- divide f
93 copy-to *dest, result
94 }
95
96 fn vec3-unit in: (addr vec3), out: (addr vec3) {
97 var len/xmm0: float <- vec3-length in
98
99
100
101 copy-object in, out
102 vec3-scale-down out, len
103 }
104
105 fn vec3-length v: (addr vec3) -> _/xmm0: float {
106 var result/xmm0: float <- vec3-length-squared v
107 result <- square-root result
108 return result
109 }
110
111 fn vec3-length-squared _v: (addr vec3) -> _/xmm0: float {
112 var v/esi: (addr vec3) <- copy _v
113
114 var src/eax: (addr float) <- get v, x
115 var tmp/xmm1: float <- copy *src
116 tmp <- multiply tmp
117 var result/xmm0: float <- copy tmp
118
119 src <- get v, y
120 tmp <- copy *src
121 tmp <- multiply tmp
122 result <- add tmp
123
124 src <- get v, z
125 tmp <- copy *src
126 tmp <- multiply tmp
127 result <- add tmp
128 return result
129 }
130
131 fn vec3-dot _v1: (addr vec3), _v2: (addr vec3) -> _/xmm0: float {
132 }
133
134 fn vec3-cross _v1: (addr vec3), _v2: (addr vec3), out: (addr vec3) {
135 }