Use wrappers for float-typed math functions
[openal-soft.git] / OpenAL32 / Include / alu.h
blobcd8f15549b04a9b9bdbaf28ee7262c5020032619
1 #ifndef _ALU_H_
2 #define _ALU_H_
4 #include "AL/al.h"
5 #include "AL/alc.h"
6 #include "AL/alext.h"
8 #include <limits.h>
9 #include <math.h>
10 #ifdef HAVE_FLOAT_H
11 #include <float.h>
12 /* HACK: Seems cross-compiling with MinGW includes the wrong float.h, which
13 * doesn't define Windows' _controlfp and related macros */
14 #if defined(__MINGW32__) && !defined(_RC_CHOP)
15 /* Control word masks for unMask */
16 #define _MCW_EM 0x0008001F /* Error masks */
17 #define _MCW_IC 0x00040000 /* Infinity */
18 #define _MCW_RC 0x00000300 /* Rounding */
19 #define _MCW_PC 0x00030000 /* Precision */
20 /* Control word values for unNew (use with related unMask above) */
21 #define _EM_INVALID 0x00000010
22 #define _EM_DENORMAL 0x00080000
23 #define _EM_ZERODIVIDE 0x00000008
24 #define _EM_OVERFLOW 0x00000004
25 #define _EM_UNDERFLOW 0x00000002
26 #define _EM_INEXACT 0x00000001
27 #define _IC_AFFINE 0x00040000
28 #define _IC_PROJECTIVE 0x00000000
29 #define _RC_CHOP 0x00000300
30 #define _RC_UP 0x00000200
31 #define _RC_DOWN 0x00000100
32 #define _RC_NEAR 0x00000000
33 #define _PC_24 0x00020000
34 #define _PC_53 0x00010000
35 #define _PC_64 0x00000000
36 _CRTIMP unsigned int __cdecl __MINGW_NOTHROW _controlfp (unsigned int unNew, unsigned int unMask);
37 #endif
38 #endif
39 #ifdef HAVE_IEEEFP_H
40 #include <ieeefp.h>
41 #endif
44 #define F_PI (3.14159265358979323846f) /* pi */
45 #define F_PI_2 (1.57079632679489661923f) /* pi/2 */
47 #ifndef HAVE_POWF
48 static __inline float powf(float x, float y)
49 { return (float)pow(x, y); }
50 #endif
52 #ifndef HAVE_SQRTF
53 static __inline float sqrtf(float x)
54 { (float)sqrt(x); }
55 #endif
57 #ifndef HAVE_COSF
58 static __inline float cosf(float x)
59 { (float)cos(x); }
60 #endif
62 #ifndef HAVE_SINF
63 static __inline float sinf(float x)
64 { (float)sin(x); }
65 #endif
67 #ifndef HAVE_ACOSF
68 static __inline float acosf(float x)
69 { (float)acos(x); }
70 #endif
72 #ifndef HAVE_ASINF
73 static __inline float asinf(float x)
74 { (float)asin(x); }
75 #endif
77 #ifndef HAVE_ATANF
78 static __inline float atanf(float x)
79 { (float)atan(x); }
80 #endif
82 #ifndef HAVE_ATAN2F
83 static __inline float atan2f(float x, float y)
84 { (float)atan2(x, y); }
85 #endif
87 #ifndef HAVE_FABSF
88 static __inline float fabsf(float x)
89 { (float)fabs(x); }
90 #endif
92 #ifndef HAVE_LOG10F
93 static __inline float log10f(float x)
94 { (float)log10(x); }
95 #endif
97 #ifndef HAVE_FLOORF
98 static __inline float floorf(float x)
99 { (float)floor(x); }
100 #endif
102 #ifdef __cplusplus
103 extern "C" {
104 #endif
106 struct ALsource;
107 struct ALbuffer;
108 struct DirectParams;
109 struct SendParams;
111 typedef ALvoid (*DryMixerFunc)(struct ALsource *self, ALCdevice *Device,
112 struct DirectParams *params,
113 const ALfloat *RESTRICT data, ALuint srcfrac,
114 ALuint OutPos, ALuint SamplesToDo,
115 ALuint BufferSize);
116 typedef ALvoid (*WetMixerFunc)(struct ALsource *self, ALuint sendidx,
117 struct SendParams *params,
118 const ALfloat *RESTRICT data, ALuint srcfrac,
119 ALuint OutPos, ALuint SamplesToDo,
120 ALuint BufferSize);
122 enum Resampler {
123 PointResampler,
124 LinearResampler,
125 CubicResampler,
127 ResamplerMax,
130 enum Channel {
131 FrontLeft = 0,
132 FrontRight,
133 FrontCenter,
134 LFE,
135 BackLeft,
136 BackRight,
137 BackCenter,
138 SideLeft,
139 SideRight,
141 MaxChannels
144 enum DistanceModel {
145 InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
146 LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
147 ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
148 InverseDistance = AL_INVERSE_DISTANCE,
149 LinearDistance = AL_LINEAR_DISTANCE,
150 ExponentDistance = AL_EXPONENT_DISTANCE,
151 DisableDistance = AL_NONE,
153 DefaultDistanceModel = InverseDistanceClamped
156 #define BUFFERSIZE 4096
158 #define FRACTIONBITS (14)
159 #define FRACTIONONE (1<<FRACTIONBITS)
160 #define FRACTIONMASK (FRACTIONONE-1)
162 /* Size for temporary stack storage of buffer data. Must be a multiple of the
163 * size of ALfloat, ie, 4. Larger values need more stack, while smaller values
164 * may need more iterations. The value needs to be a sensible size, however, as
165 * it constrains the max stepping value used for mixing.
166 * The mixer requires being able to do two samplings per mixing loop. A 16KB
167 * buffer can hold 512 sample frames for a 7.1 float buffer. With the cubic
168 * resampler (which requires 3 padding sample frames), this limits the maximum
169 * step to about 508. This means that buffer_freq*source_pitch cannot exceed
170 * device_freq*508 for an 8-channel 32-bit buffer. */
171 #ifndef STACK_DATA_SIZE
172 #define STACK_DATA_SIZE 16384
173 #endif
176 static __inline ALfloat minf(ALfloat a, ALfloat b)
177 { return ((a > b) ? b : a); }
178 static __inline ALfloat maxf(ALfloat a, ALfloat b)
179 { return ((a > b) ? a : b); }
180 static __inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
181 { return minf(max, maxf(min, val)); }
183 static __inline ALuint minu(ALuint a, ALuint b)
184 { return ((a > b) ? b : a); }
185 static __inline ALuint maxu(ALuint a, ALuint b)
186 { return ((a > b) ? a : b); }
187 static __inline ALuint clampu(ALuint val, ALuint min, ALuint max)
188 { return minu(max, maxu(min, val)); }
190 static __inline ALint mini(ALint a, ALint b)
191 { return ((a > b) ? b : a); }
192 static __inline ALint maxi(ALint a, ALint b)
193 { return ((a > b) ? a : b); }
194 static __inline ALint clampi(ALint val, ALint min, ALint max)
195 { return mini(max, maxi(min, val)); }
197 static __inline ALint64 mini64(ALint64 a, ALint64 b)
198 { return ((a > b) ? b : a); }
199 static __inline ALint64 maxi64(ALint64 a, ALint64 b)
200 { return ((a > b) ? a : b); }
201 static __inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
202 { return mini64(max, maxi64(min, val)); }
205 static __inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
207 return val1 + (val2-val1)*mu;
209 static __inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
211 ALfloat mu2 = mu*mu;
212 ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
213 ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
214 ALfloat a2 = -0.5f*val0 + 0.5f*val2;
215 ALfloat a3 = val1;
217 return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
221 static __inline int SetMixerFPUMode(void)
223 #if defined(_FPU_GETCW) && defined(_FPU_SETCW) && (defined(__i386__) || defined(__x86_64__))
224 fpu_control_t fpuState, newState;
225 _FPU_GETCW(fpuState);
226 newState = fpuState&~(_FPU_EXTENDED|_FPU_DOUBLE|_FPU_SINGLE |
227 _FPU_RC_NEAREST|_FPU_RC_DOWN|_FPU_RC_UP|_FPU_RC_ZERO);
228 newState |= _FPU_SINGLE | _FPU_RC_ZERO;
229 _FPU_SETCW(newState);
230 #else
231 int fpuState;
232 #if defined(HAVE__CONTROLFP)
233 fpuState = _controlfp(0, 0);
234 (void)_controlfp(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC);
235 #elif defined(HAVE_FESETROUND)
236 fpuState = fegetround();
237 #ifdef FE_TOWARDZERO
238 fesetround(FE_TOWARDZERO);
239 #endif
240 #endif
241 #endif
242 return fpuState;
245 static __inline void RestoreFPUMode(int state)
247 #if defined(_FPU_GETCW) && defined(_FPU_SETCW) && (defined(__i386__) || defined(__x86_64__))
248 fpu_control_t fpuState = state;
249 _FPU_SETCW(fpuState);
250 #elif defined(HAVE__CONTROLFP)
251 _controlfp(state, _MCW_RC|_MCW_PC);
252 #elif defined(HAVE_FESETROUND)
253 fesetround(state);
254 #endif
258 static __inline void aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
260 outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
261 outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
262 outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
265 static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
267 return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
268 inVector1[2]*inVector2[2];
271 static __inline void aluNormalize(ALfloat *inVector)
273 ALfloat lengthsqr = aluDotproduct(inVector, inVector);
274 if(lengthsqr > 0.0f)
276 ALfloat inv_length = 1.0f/sqrtf(lengthsqr);
277 inVector[0] *= inv_length;
278 inVector[1] *= inv_length;
279 inVector[2] *= inv_length;
284 ALvoid aluInitPanning(ALCdevice *Device);
286 ALvoid ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat *gains);
288 ALvoid CalcSourceParams(struct ALsource *ALSource, const ALCcontext *ALContext);
289 ALvoid CalcNonAttnSourceParams(struct ALsource *ALSource, const ALCcontext *ALContext);
291 DryMixerFunc SelectDirectMixer(enum Resampler Resampler);
292 DryMixerFunc SelectHrtfMixer(enum Resampler Resampler);
293 WetMixerFunc SelectSendMixer(enum Resampler Resampler);
295 ALvoid MixSource(struct ALsource *Source, ALCdevice *Device, ALuint SamplesToDo);
297 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
298 ALvoid aluHandleDisconnect(ALCdevice *device);
300 extern ALfloat ConeScale;
301 extern ALfloat ZScale;
303 #ifdef __cplusplus
305 #endif
307 #endif