KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
vec3f.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/vec3f.h
4 Copyright (C) 2013, 2014 Josh "PH3NOM" Pearson
5
6*/
7
8/** \file dc/vec3f.h
9 \brief Basic matrix operations.
10 \ingroup math_matrices
11
12 This file contains various basic vector math functionality for using the
13 SH4's vector instructions. Higher level functionality in KGL is built off
14 of these.
15
16 \author Josh "PH3NOM" Pearson
17 \see dc/matrix.h
18*/
19
20#ifndef __DC_VEC3F_H
21#define __DC_VEC3F_H
22
23#include <sys/cdefs.h>
24__BEGIN_DECLS
25
26/** \addtogroup math_matrices
27 @{
28*/
29
30/** \brief 3D floating-point vector */
31typedef struct vec3f {
32 float x, y, z;
33} vec3f_t;
34
35/** \cond */
36#define R_DEG 182.04444443623349541909523793743f
37#define R_RAD 10430.37835f
38/* \endcond */
39
40/** \brief Macro to return the scalar dot product of two 3d vectors.
41
42 This macro is an inline assembly operation using the SH4's fast
43 (approximate) math instructions, and returns a single-precision
44 floating-point value.
45
46 \param x1 The X coordinate of first vector.
47 \param y1 The Y coordinate of first vector.
48 \param z1 The Z coordinate of first vector.
49 \param x2 The X coordinate of second vector.
50 \param y2 The Y coordinate of second vector.
51 \param z2 The Z coordinate of second vector.
52 \param w The result of the calculation.
53*/
54#define vec3f_dot(x1, y1, z1, x2, y2, z2, w) { \
55 register float __x __asm__("fr0") = (x1); \
56 register float __y __asm__("fr1") = (y1); \
57 register float __z __asm__("fr2") = (z1); \
58 register float __w __asm__("fr3"); \
59 register float __a __asm__("fr4") = (x2); \
60 register float __b __asm__("fr5") = (y2); \
61 register float __c __asm__("fr6") = (z2); \
62 register float __d __asm__("fr7"); \
63 __asm__ __volatile__( \
64 "fldi0 fr3\n" \
65 "fldi0 fr7\n" \
66 "fipr fv4,fv0" \
67 : "+f" (__w) \
68 : "f" (__x), "f" (__y), "f" (__z), "f" (__w), \
69 "f" (__a), "f" (__b), "f" (__c), "f" (__d) \
70 ); \
71 w = __w; \
72 }
73
74/** \brief Macro to return scalar Euclidean length of a 3d vector.
75
76 This macro is an inline assembly operation using the SH4's fast
77 (approximate) math instructions, and returns a single-precision
78 floating-point value.
79
80 \param x The X coordinate of vector.
81 \param y The Y coordinate of vector.
82 \param z The Z coordinate of vector.
83 \param w The result of the calculation.
84*/
85#define vec3f_length(x, y, z, w) { \
86 register float __x __asm__("fr0") = (x); \
87 register float __y __asm__("fr1") = (y); \
88 register float __z __asm__("fr2") = (z); \
89 register float __w __asm__("fr3"); \
90 __asm__ __volatile__( \
91 "fldi0 fr3\n" \
92 "fipr fv0,fv0\n" \
93 "fsqrt fr3\n" \
94 : "+f" (__w) \
95 : "f" (__x), "f" (__y), "f" (__z), "f" (__w) \
96 ); \
97 w = __w; \
98 }
99
100/** \brief Macro to return the Euclidean distance between two 3d vectors.
101
102 This macro is an inline assembly operation using the SH4's fast
103 (approximate) math instructions, and returns a single-precision
104 floating-point value.
105
106 \param x1 The X coordinate of first vector.
107 \param y1 The Y coordinate of first vector.
108 \param z1 The Z coordinate of first vector.
109 \param x2 The X coordinate of second vector.
110 \param y2 The Y coordinate of second vector.
111 \param z2 The Z coordinate of second vector.
112 \param w The result of the calculation.
113*/
114#define vec3f_distance(x1, y1, z1, x2, y2, z2, w) { \
115 register float __x __asm__("fr0") = (x2-x1); \
116 register float __y __asm__("fr1") = (y2-y1); \
117 register float __z __asm__("fr2") = (z2-z1); \
118 register float __w __asm__("fr3"); \
119 __asm__ __volatile__( \
120 "fldi0 fr3\n" \
121 "fipr fv0,fv0\n" \
122 "fsqrt fr3\n" \
123 : "+f" (__w) \
124 : "f" (__x), "f" (__y), "f" (__z), "f" (__w) \
125 ); \
126 w = __w; \
127 }
128
129/** \brief Macro to return the normalized version of a vector.
130
131 This macro is an inline assembly operation using the SH4's fast
132 (approximate) math instructions to calculate a vector that is in the same
133 direction as the input vector but with a Euclidean length of one. The input
134 vector is modified by the operation as the resulting values.
135
136 \param x The X coordinate of vector.
137 \param y The Y coordinate of vector.
138 \param z The Z coordinate of vector.
139*/
140#define vec3f_normalize(x, y, z) { \
141 register float __x __asm__("fr0") = x; \
142 register float __y __asm__("fr1") = y; \
143 register float __z __asm__("fr2") = z; \
144 __asm__ __volatile__( \
145 "fldi0 fr3\n" \
146 "fipr fv0,fv0\n" \
147 "fsrra fr3\n" \
148 "fmul fr3, fr0\n" \
149 "fmul fr3, fr1\n" \
150 "fmul fr3, fr2\n" \
151 : "=f" (__x), "=f" (__y), "=f" (__z) \
152 : "0" (__x), "1" (__y), "2" (__z) \
153 : "fr3" ); \
154 x = __x; y = __y; z = __z; \
155 }
156
157/** \brief Macro to return the normalized version of a vector minus another
158 vector.
159
160 This macro is an inline assembly operation using the SH4's fast
161 (approximate) math instructions. The return vector is stored into the third
162 vertex parameter: x3, y3, and z3.
163
164 \param x1 The X coordinate of first vector.
165 \param y1 The Y coordinate of first vector.
166 \param z1 The Z coordinate of first vector.
167 \param x2 The X coordinate of second vector.
168 \param y2 The Y coordinate of second vector.
169 \param z2 The Z coordinate of second vector.
170 \param x3 The X coordinate of output vector.
171 \param y3 The Y coordinate of output vector.
172 \param z3 The Z coordinate of output vector.
173*/
174#define vec3f_sub_normalize(x1, y1, z1, x2, y2, z2, x3, y3, z3) { \
175 register float __x __asm__("fr0") = x1 - x2; \
176 register float __y __asm__("fr1") = y1 - y2; \
177 register float __z __asm__("fr2") = z1 - z2; \
178 __asm__ __volatile__( \
179 "fldi0 fr3\n" \
180 "fipr fv0,fv0\n" \
181 "fsrra fr3\n" \
182 "fmul fr3, fr0\n" \
183 "fmul fr3, fr1\n" \
184 "fmul fr3, fr2\n" \
185 : "=f" (__x), "=f" (__y), "=f" (__z) \
186 : "0" (__x), "1" (__y), "2" (__z) \
187 : "fr3" ); \
188 x3 = __x; y3 = __y; z3 = __z; \
189 }
190
191/** \brief Macro to rotate a vector about its origin on the x, y plane.
192
193 This macro is an inline assembly operation using the SH4's fast
194 (approximate) math instructions. The return vector is stored into the first
195 vertex parameter: x1, y1, and z1.
196
197 \param px The X coordinate of vector to rotate.
198 \param py The Y coordinate of vector to rotate.
199 \param pz The Z coordinate of vector to rotate.
200 \param cx The X coordinate of origin vector.
201 \param cy The Y coordinate of origin vector.
202 \param cz The Z coordinate of origin vector.
203 \param r The angle (in radians) of rotation.
204*/
205#define vec3f_rotr_xy(px, py, pz, cx, cy, cz, r) { \
206 register float __px __asm__("fr0") = px; \
207 register float __py __asm__("fr1") = py; \
208 register float __cx __asm__("fr4") = cx; \
209 register float __cy __asm__("fr5") = cy; \
210 register float __r __asm__("fr6") = r; \
211 register float __s __asm__("fr7") = R_RAD; \
212 __asm__ __volatile__( \
213 "fmul fr7, fr6\n" \
214 "ftrc fr6, fpul\n" \
215 "fsca fpul, dr6\n" \
216 "fsub fr4, fr0\n" \
217 "fsub fr5, fr1\n" \
218 "fmov fr0, fr2\n" \
219 "fmov fr1, fr3\n" \
220 "fmul fr7, fr0\n" \
221 "fmul fr6, fr1\n" \
222 "fmul fr6, fr2\n" \
223 "fmul fr7, fr3\n" \
224 "fadd fr0, fr4\n" \
225 "fsub fr1, fr4\n" \
226 "fadd fr2, fr5\n" \
227 "fadd fr3, fr5\n" \
228 : "+f" (__cx), "+f" (__cy) \
229 : "f" (__px), "f" (__py), "f" (__r), "f" (__s) ); \
230 px = __cx; py = __cy; \
231 }
232
233/** \brief Macro to rotate a vector about its origin on the x, z plane.
234
235 This macro is an inline assembly operation using the SH4's fast
236 (approximate) math instructions. The return vector is stored into the first
237 vertex parameter: x1, y1, and z1.
238
239 \param px The X coordinate of vector to rotate.
240 \param py The Y coordinate of vector to rotate.
241 \param pz The Z coordinate of vector to rotate.
242 \param cx The X coordinate of origin vector.
243 \param cy The Y coordinate of origin vector.
244 \param cz The Z coordinate of origin vector.
245 \param r The angle (in radians) of rotation.
246*/
247#define vec3f_rotr_xz(px, py, pz, cx, cy, cz, r) { \
248 register float __px __asm__("fr0") = px; \
249 register float __pz __asm__("fr1") = pz; \
250 register float __cx __asm__("fr4") = cx; \
251 register float __cz __asm__("fr5") = cz; \
252 register float __r __asm__("fr6") = r; \
253 register float __s __asm__("fr7") = R_RAD; \
254 __asm__ __volatile__( \
255 "fmul fr7, fr6\n" \
256 "ftrc fr6, fpul\n" \
257 "fsca fpul, dr6\n" \
258 "fsub fr4, fr0\n" \
259 "fsub fr5, fr1\n" \
260 "fmov fr0, fr2\n" \
261 "fmov fr1, fr3\n" \
262 "fmul fr7, fr0\n" \
263 "fmul fr6, fr1\n" \
264 "fmul fr6, fr2\n" \
265 "fmul fr7, fr3\n" \
266 "fadd fr0, fr4\n" \
267 "fsub fr1, fr4\n" \
268 "fadd fr2, fr5\n" \
269 "fadd fr3, fr5\n" \
270 : "+f" (__cx), "+f" (__cz) \
271 : "f" (__px), "f" (__pz), "f" (__r), "f" (__s) ); \
272 px = __cx; pz = __cz; \
273 }
274
275/** \brief Macro to rotate a vector about its origin on the y, z plane.
276
277 This macro is an inline assembly operation using the SH4's fast
278 (approximate) math instructions. The return vector is stored into the first
279 vertex parameter: x1, y1, and z1.
280
281 \param px The X coordinate of vector to rotate.
282 \param py The Y coordinate of vector to rotate.
283 \param pz The Z coordinate of vector to rotate.
284 \param cx The X coordinate of origin vector.
285 \param cy The Y coordinate of origin vector.
286 \param cz The Z coordinate of origin vector.
287 \param r The angle (in radians) of rotation.
288*/
289#define vec3f_rotr_yz(px, py, pz, cx, cy, cz, r) { \
290 register float __py __asm__("fr0") = py; \
291 register float __pz __asm__("fr1") = pz; \
292 register float __cy __asm__("fr4") = cy; \
293 register float __cz __asm__("fr5") = cz; \
294 register float __r __asm__("fr6") = r; \
295 register float __s __asm__("fr7") = R_RAD; \
296 __asm__ __volatile__( \
297 "fmul fr7, fr6\n" \
298 "ftrc fr6, fpul\n" \
299 "fsca fpul, dr6\n" \
300 "fsub fr4, fr0\n" \
301 "fsub fr5, fr1\n" \
302 "fmov fr0, fr2\n" \
303 "fmov fr1, fr3\n" \
304 "fmul fr7, fr0\n" \
305 "fmul fr6, fr1\n" \
306 "fmul fr6, fr2\n" \
307 "fmul fr7, fr3\n" \
308 "fadd fr0, fr4\n" \
309 "fsub fr1, fr4\n" \
310 "fadd fr2, fr5\n" \
311 "fadd fr3, fr5\n" \
312 : "+f" (__cy), "+f" (__cz) \
313 : "f" (__py), "f" (__pz), "f" (__r), "f" (__s) ); \
314 py = __cy; pz = __cz; \
315 }
316
317/** \brief Macro to rotate a vector about its origin on the x, y plane.
318
319 This macro is an inline assembly operation using the SH4's fast
320 (approximate) math instructions. The return vector is stored into the first
321 vertex parameter: x1, y1, and z1.
322
323 \param px The X coordinate of vector to rotate.
324 \param py The Y coordinate of vector to rotate.
325 \param pz The Z coordinate of vector to rotate.
326 \param cx The X coordinate of origin vector.
327 \param cy The Y coordinate of origin vector.
328 \param cz The Z coordinate of origin vector.
329 \param r The angle (in degrees) of rotation.
330*/
331#define vec3f_rotd_xy(px, py, pz, cx, cy, cz, r) { \
332 register float __px __asm__("fr0") = px; \
333 register float __pz __asm__("fr1") = pz; \
334 register float __cx __asm__("fr4") = cx; \
335 register float __cz __asm__("fr5") = cz; \
336 register float __r __asm__("fr6") = r; \
337 register float __s __asm__("fr7") = R_DEG; \
338 __asm__ __volatile__( \
339 "fmul fr7, fr6\n" \
340 "ftrc fr6, fpul\n" \
341 "fsca fpul, dr6\n" \
342 "fsub fr4, fr0\n" \
343 "fsub fr5, fr1\n" \
344 "fmov fr0, fr2\n" \
345 "fmov fr1, fr3\n" \
346 "fmul fr7, fr0\n" \
347 "fmul fr6, fr1\n" \
348 "fmul fr6, fr2\n" \
349 "fmul fr7, fr3\n" \
350 "fadd fr0, fr4\n" \
351 "fsub fr1, fr4\n" \
352 "fadd fr2, fr5\n" \
353 "fadd fr3, fr5\n" \
354 : "+f" (__cx), "+f" (__cz) \
355 : "f" (__px), "f" (__pz), "f" (__r), "f" (__s) ); \
356 px = __cx; pz = __cz; \
357 }
358
359/** \brief Macro to rotate a vector about its origin on the x, z plane.
360
361 This macro is an inline assembly operation using the SH4's fast
362 (approximate) math instructions. The return vector is stored into the first
363 vertex parameter: x1, y1, and z1.
364
365 \param px The X coordinate of vector to rotate.
366 \param py The Y coordinate of vector to rotate.
367 \param pz The Z coordinate of vector to rotate.
368 \param cx The X coordinate of origin vector.
369 \param cy The Y coordinate of origin vector.
370 \param cz The Z coordinate of origin vector.
371 \param r The angle (in degrees) of rotation.
372*/
373#define vec3f_rotd_xz(px, py, pz, cx, cy, cz, r) { \
374 register float __px __asm__("fr0") = px; \
375 register float __pz __asm__("fr1") = pz; \
376 register float __cx __asm__("fr4") = cx; \
377 register float __cz __asm__("fr5") = cz; \
378 register float __r __asm__("fr6") = r; \
379 register float __s __asm__("fr7") = R_DEG; \
380 __asm__ __volatile__( \
381 "fmul fr7, fr6\n" \
382 "ftrc fr6, fpul\n" \
383 "fsca fpul, dr6\n" \
384 "fsub fr4, fr0\n" \
385 "fsub fr5, fr1\n" \
386 "fmov fr0, fr2\n" \
387 "fmov fr1, fr3\n" \
388 "fmul fr7, fr0\n" \
389 "fmul fr6, fr1\n" \
390 "fmul fr6, fr2\n" \
391 "fmul fr7, fr3\n" \
392 "fadd fr0, fr4\n" \
393 "fsub fr1, fr4\n" \
394 "fadd fr2, fr5\n" \
395 "fadd fr3, fr5\n" \
396 : "+f" (__cx), "+f" (__cz) \
397 : "f" (__px), "f" (__pz), "f" (__r), "f" (__s) ); \
398 px = __cx; pz = __cz; \
399 }
400
401/** \brief Macro to rotate a vector about its origin on the y, z plane.
402
403 This macro is an inline assembly operation using the SH4's fast
404 (approximate) math instructions. The return vector is stored into the first
405 vertex parameter: x1, y1, and z1.
406
407 \param px The X coordinate of vector to rotate.
408 \param py The Y coordinate of vector to rotate.
409 \param pz The Z coordinate of vector to rotate.
410 \param cx The X coordinate of origin vector.
411 \param cy The Y coordinate of origin vector.
412 \param cz The Z coordinate of origin vector.
413 \param r The angle (in degrees) of rotation.
414*/
415#define vec3f_rotd_yz(px, py, pz, cx, cy, cz, r) { \
416 register float __py __asm__("fr0") = py; \
417 register float __pz __asm__("fr1") = pz; \
418 register float __cy __asm__("fr4") = cy; \
419 register float __cz __asm__("fr5") = cz; \
420 register float __r __asm__("fr6") = r; \
421 register float __s __asm__("fr7") = R_DEG; \
422 __asm__ __volatile__( \
423 "fmul fr7, fr6\n" \
424 "ftrc fr6, fpul\n" \
425 "fsca fpul, dr6\n" \
426 "fsub fr4, fr0\n" \
427 "fsub fr5, fr1\n" \
428 "fmov fr0, fr2\n" \
429 "fmov fr1, fr3\n" \
430 "fmul fr7, fr0\n" \
431 "fmul fr6, fr1\n" \
432 "fmul fr6, fr2\n" \
433 "fmul fr7, fr3\n" \
434 "fadd fr0, fr4\n" \
435 "fsub fr1, fr4\n" \
436 "fadd fr2, fr5\n" \
437 "fadd fr3, fr5\n" \
438 : "+f" (__cy), "+f" (__cz) \
439 : "f" (__py), "f" (__pz), "f" (__r), "f" (__s) ); \
440 py = __cy; pz = __cz; \
441 }
442
443/** @} */
444
445__END_DECLS
446
447#endif /* !__DC_VEC3F_H */
3D floating-point vector
Definition vec3f.h:31
float x
Definition vec3f.h:32