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