KallistiOS  ##version##
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
fmath.h
Go to the documentation of this file.
1 /* KallistiOS ##version##
2 
3  dc/fmath.h
4  Copyright (C) 2001 Andrew Kieschnick
5  Copyright (C) 2013, 2014 Lawrence Sebald
6 
7 */
8 
9 #ifndef __DC_FMATH_H
10 #define __DC_FMATH_H
11 
12 #include <sys/cdefs.h>
13 __BEGIN_DECLS
14 
15 #include <arch/types.h>
16 #include <dc/fmath_base.h>
17 
18 /**
19  \file dc/fmath.h
20  \brief Inline functions for the DC's special math instructions
21  \author Andrew Kieschnick
22  \author Lawrence Sebald
23 */
24 
25 /* Sigh... C99 treats inline stuff a lot differently than traditional GCC did,
26  so we need to take care of that... */
27 #if __STDC_VERSION__ >= 199901L
28 #define __FMINLINE static inline
29 #elif __GNUC__
30 #define __FMINLINE extern inline
31 #else
32 /* Uhm... I guess this is the best we can do? */
33 #define __FMINLINE static
34 #endif
35 
36 /**
37  \brief Floating point inner product.
38  \return v1 dot v2 (inner product)
39 */
40 __FMINLINE float fipr(float x, float y, float z, float w,
41  float a, float b, float c, float d) {
42  return __fipr(x, y, z, w, a, b, c, d);
43 }
44 
45 /**
46  \brief Floating point inner product w/self (square of vector magnitude)
47  \return v1 dot v1 (square of magnitude)
48 */
49 __FMINLINE float fipr_magnitude_sqr(float x, float y, float z, float w) {
50  return __fipr_magnitude_sqr(x, y, z, w);
51 }
52 
53 /**
54  \brief Floating point sine
55  \param r a floating point number between 0 and 2*PI
56  \return sin(r), where r is [0..2*PI]
57 */
58 __FMINLINE float fsin(float r) {
59  return __fsin(r);
60 }
61 
62 /**
63  \brief Floating point cosine
64  \param r a floating point number between 0 and 2*PI
65  \return cos(r), where r is [0..2*PI]
66 */
67 __FMINLINE float fcos(float r) {
68  return __fcos(r);
69 }
70 
71 /**
72  \brief Floating point tangent
73  \param r a floating point number between 0 and 2*PI
74  \return tan(r), where r is [0..2*PI]
75 */
76 __FMINLINE float ftan(float r) {
77  return __ftan(r);
78 }
79 
80 /**
81  \brief Integer sine
82  \param d an integer between 0 and 65535
83  \return sin(d), where d is [0..65535]
84 */
85 __FMINLINE float fisin(int d) {
86  return __fisin(d);
87 }
88 
89 /**
90  \brief Integer cosine
91  \param d an integer between 0 and 65535
92  \return cos(d), where d is [0..65535]
93 */
94 __FMINLINE float ficos(int d) {
95  return __ficos(d);
96 }
97 
98 /**
99  \brief Integer tangent
100  \param d an integer between 0 and 65535
101  \return tan(d), where d is [0..65535]
102 */
103 __FMINLINE float fitan(int d) {
104  return __fitan(d);
105 }
106 
107 /**
108  \brief Floating point square root
109  \return sqrt(f)
110 */
111 __FMINLINE float fsqrt(float f) {
112  return __fsqrt(f);
113 }
114 
115 /**
116  \return 1.0f / sqrt(f)
117 */
118 __FMINLINE float frsqrt(float f) {
119  return __frsqrt(f);
120 }
121 
122 /** \brief Calculate the sine and cosine of a value in degrees.
123 
124  This function uses the fsca instruction to calculate an approximation of the
125  sine and cosine of the input value.
126 
127  \param f The value to calculate the sine and cosine of.
128  \param s Storage for the returned sine value.
129  \param c Storage for the returned cosine value.
130 */
131 __FMINLINE void fsincos(float f, float *s, float *c) {
132  __fsincos(f, *s, *c);
133 }
134 
135 /** \brief Calculate the sine and cosine of a value in radians.
136 
137  This function uses the fsca instruction to calculate an approximation of the
138  sine and cosine of the input value.
139 
140  \param f The value to calculate the sine and cosine of.
141  \param s Storage for the returned sine value.
142  \param c Storage for the returned cosine value.
143 */
144 __FMINLINE void fsincosr(float f, float *s, float *c) {
145  __fsincosr(f, *s, *c);
146 }
147 
148 /** \brief Calculate the offset color value for a set of bumpmap parameters.
149 
150  This function calculates the value to be placed into the oargb value for the
151  use of bumpmapping on a polygon. The angles specified should be expressed in
152  radians and within the limits specified for the individual parameter.
153 
154  \param h Weighting value in the range [0, 1] for how defined
155  the bumpiness of the surface should be.
156  \param t Spherical elevation angle in the range [0, pi/2]
157  between the surface and the lighting source. A value
158  of pi/2 implies that the light is directly overhead.
159  \param q Spherical rotation angle in the range [0, 2*pi]
160  between the surface and the lighting source.
161  \return 32-bit packed value to be used as an offset color on
162  the surface to be bump mapped.
163 
164  \note For more information about how bumpmapping on the PVR works, refer
165  to <a href="https://google.com/patents/US6819319">US Patent
166  6,819,319</a>, which describes the algorithm implemented in the
167  hardware (specifically look at Figures 2 and 3, along with the
168  description in the Detailed Description section).
169  \note Thanks to Fredrik Ehnbom for figuring this stuff out and posting it
170  to the mailing list back in 2005!
171 */
172 __FMINLINE uint32 pvr_pack_bump(float h, float t, float q) {
173  uint8 hp = (uint8)(h * 255.0f);
174  uint8 k1 = ~hp;
175  uint8 k2 = (uint8)(hp * __fsin(t));
176  uint8 k3 = (uint8)(hp * __fcos(t));
177  uint8 qp = (uint8)((q / (2 * F_PI)) * 255.0f);
178 
179 
180  return (k1 << 24) | (k2 << 16) | (k3 << 8) | qp;
181 }
182 
183 /* Make sure we declare the non-inline versions for C99 and non-gcc. Why they'd
184  ever be needed, since they're inlined above, who knows? I guess in case
185  someone tries to take the address of one of them? */
186 /** \cond */
187 #if __STDC_VERSION__ >= 199901L || !defined(__GNUC__)
188 extern float fipr(float x, float y, float z, float w, float a, float b, float c,
189  float d);
190 extern float fipr_magnitude_sqr(float x, float y, float z, float w);
191 extern float fsin(float r);
192 extern float fcos(float r);
193 extern float ftan(float r);
194 extern float fisin(int d);
195 extern float ficos(int d);
196 extern float fitan(int d);
197 extern float fsqrt(float f);
198 extern float frsqrt(float f);
199 extern void fsincos(float f, float *s, float *c);
200 extern void fsincosr(float f, float *s, float *c);
201 #endif /* __STDC_VERSION__ >= 199901L || !defined(__GNUC__) */
202 /** \endcond */
203 
204 __END_DECLS
205 
206 #endif /* __DC_FMATH_H */
Common integer types.
__FMINLINE void fsincosr(float f, float *s, float *c)
Calculate the sine and cosine of a value in radians.
Definition: fmath.h:144
__FMINLINE float fipr(float x, float y, float z, float w, float a, float b, float c, float d)
Floating point inner product.
Definition: fmath.h:40
__FMINLINE float frsqrt(float f)
Definition: fmath.h:118
__FMINLINE float ftan(float r)
Floating point tangent.
Definition: fmath.h:76
__FMINLINE float fisin(int d)
Integer sine.
Definition: fmath.h:85
__FMINLINE float fipr_magnitude_sqr(float x, float y, float z, float w)
Floating point inner product w/self (square of vector magnitude)
Definition: fmath.h:49
#define __FMINLINE
Definition: fmath.h:33
unsigned long uint32
32-bit unsigned integer
Definition: types.h:28
__FMINLINE float ficos(int d)
Integer cosine.
Definition: fmath.h:94
__FMINLINE uint32 pvr_pack_bump(float h, float t, float q)
Calculate the offset color value for a set of bumpmap parameters.
Definition: fmath.h:172
#define F_PI
Definition: fmath_base.h:23
unsigned char uint8
8-bit unsigned integer
Definition: types.h:30
__FMINLINE float fcos(float r)
Floating point cosine.
Definition: fmath.h:67
__FMINLINE float fsqrt(float f)
Floating point square root.
Definition: fmath.h:111
__FMINLINE void fsincos(float f, float *s, float *c)
Calculate the sine and cosine of a value in degrees.
Definition: fmath.h:131
Base definitions for the DC's special math instructions.
__FMINLINE float fitan(int d)
Integer tangent.
Definition: fmath.h:103
__FMINLINE float fsin(float r)
Floating point sine.
Definition: fmath.h:58