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