Add an SSE-enhanced HRTF mixer using intrinsics
[openal-soft.git] / Alc / helpers.c
blobd963f3a800beb95be3665153c3689b8d7eb4d852
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #include <time.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #ifdef HAVE_DLFCN_H
28 #include <dlfcn.h>
29 #endif
30 #ifdef HAVE_CPUID_H
31 #include <cpuid.h>
32 #endif
34 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
35 #define INITGUID
36 #include <windows.h>
37 #ifdef HAVE_GUIDDEF_H
38 #include <guiddef.h>
39 #else
40 #include <initguid.h>
41 #endif
43 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
44 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
46 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
48 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
49 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
50 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
51 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
53 #ifdef HAVE_MMDEVAPI
54 #include <devpropdef.h>
56 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
57 #endif
59 #endif
61 #include "alMain.h"
63 ALuint CPUCapFlags = 0;
66 void FillCPUCaps(ALuint capfilter)
68 ALuint caps = 0;
70 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
71 /* FIXME: We really should get this for all available CPUs in case different
72 * CPUs have different caps (is that possible on one machine?). */
73 #ifdef HAVE_CPUID_H
74 union {
75 unsigned int regs[4];
76 char str[sizeof(unsigned int[4])];
77 } cpuinf[3];
79 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
80 ERR("Failed to get CPUID\n");
81 else
83 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
84 if(__get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
85 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
86 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
87 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
89 if(!__get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
90 ERR("Failed to get CPU features\n");
91 else
93 #ifdef bit_MMX
94 if((cpuinf[0].regs[3]&bit_MMX))
95 caps |= CPU_CAP_MMX;
96 #endif
97 #ifdef bit_SSE
98 if((cpuinf[0].regs[3]&bit_SSE))
99 caps |= CPU_CAP_SSE;
100 #endif
103 #endif
104 #endif
105 #ifdef HAVE_ARM_NEON_H
106 /* Assume Neon support if compiled with it */
107 caps |= CPU_CAP_NEON;
108 #endif
110 TRACE("Got caps:%s%s%s%s\n", ((caps&CPU_CAP_MMX)?((capfilter&CPU_CAP_MMX)?" MMX":" (MMX)"):""),
111 ((caps&CPU_CAP_SSE)?((capfilter&CPU_CAP_SSE)?" SSE":" (SSE)"):""),
112 ((caps&CPU_CAP_NEON)?((capfilter&CPU_CAP_NEON)?" Neon":" (Neon)"):""),
113 ((!caps)?" (none)":""));
114 CPUCapFlags = caps & capfilter;
118 #ifdef _WIN32
119 void pthread_once(pthread_once_t *once, void (*callback)(void))
121 LONG ret;
122 while((ret=InterlockedExchange(once, 1)) == 1)
123 sched_yield();
124 if(ret == 0)
125 callback();
126 InterlockedExchange(once, 2);
130 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
132 *key = TlsAlloc();
133 if(callback)
134 InsertUIntMapEntry(&TlsDestructor, *key, callback);
135 return 0;
138 int pthread_key_delete(pthread_key_t key)
140 InsertUIntMapEntry(&TlsDestructor, key, NULL);
141 TlsFree(key);
142 return 0;
145 void *pthread_getspecific(pthread_key_t key)
146 { return TlsGetValue(key); }
148 int pthread_setspecific(pthread_key_t key, void *val)
150 TlsSetValue(key, val);
151 return 0;
155 void *LoadLib(const char *name)
156 { return LoadLibraryA(name); }
157 void CloseLib(void *handle)
158 { FreeLibrary((HANDLE)handle); }
159 void *GetSymbol(void *handle, const char *name)
161 void *ret;
163 ret = (void*)GetProcAddress((HANDLE)handle, name);
164 if(ret == NULL)
165 ERR("Failed to load %s\n", name);
166 return ret;
169 WCHAR *strdupW(const WCHAR *str)
171 const WCHAR *n;
172 WCHAR *ret;
173 size_t len;
175 n = str;
176 while(*n) n++;
177 len = n - str;
179 ret = calloc(sizeof(WCHAR), len+1);
180 if(ret != NULL)
181 memcpy(ret, str, sizeof(WCHAR)*len);
182 return ret;
185 #else
187 #include <pthread.h>
188 #ifdef HAVE_PTHREAD_NP_H
189 #include <pthread_np.h>
190 #endif
191 #include <sched.h>
193 void InitializeCriticalSection(CRITICAL_SECTION *cs)
195 pthread_mutexattr_t attrib;
196 int ret;
198 ret = pthread_mutexattr_init(&attrib);
199 assert(ret == 0);
201 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
202 #ifdef HAVE_PTHREAD_NP_H
203 if(ret != 0)
204 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
205 #endif
206 assert(ret == 0);
207 ret = pthread_mutex_init(cs, &attrib);
208 assert(ret == 0);
210 pthread_mutexattr_destroy(&attrib);
212 void DeleteCriticalSection(CRITICAL_SECTION *cs)
214 int ret;
215 ret = pthread_mutex_destroy(cs);
216 assert(ret == 0);
218 void EnterCriticalSection(CRITICAL_SECTION *cs)
220 int ret;
221 ret = pthread_mutex_lock(cs);
222 assert(ret == 0);
224 void LeaveCriticalSection(CRITICAL_SECTION *cs)
226 int ret;
227 ret = pthread_mutex_unlock(cs);
228 assert(ret == 0);
231 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
232 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
233 * Additionally, Win32 is supposed to measure the time since Windows started,
234 * as opposed to the actual time. */
235 ALuint timeGetTime(void)
237 #if _POSIX_TIMERS > 0
238 struct timespec ts;
239 int ret = -1;
241 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
242 #if _POSIX_MONOTONIC_CLOCK == 0
243 static int hasmono = 0;
244 if(hasmono > 0 || (hasmono == 0 &&
245 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
246 #endif
247 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
248 #endif
249 if(ret != 0)
250 ret = clock_gettime(CLOCK_REALTIME, &ts);
251 assert(ret == 0);
253 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
254 #else
255 struct timeval tv;
256 int ret;
258 ret = gettimeofday(&tv, NULL);
259 assert(ret == 0);
261 return tv.tv_usec/1000 + tv.tv_sec*1000;
262 #endif
265 void Sleep(ALuint t)
267 struct timespec tv, rem;
268 tv.tv_nsec = (t*1000000)%1000000000;
269 tv.tv_sec = t/1000;
271 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
272 tv = rem;
275 #ifdef HAVE_DLFCN_H
277 void *LoadLib(const char *name)
279 const char *err;
280 void *handle;
282 dlerror();
283 handle = dlopen(name, RTLD_NOW);
284 if((err=dlerror()) != NULL)
285 handle = NULL;
286 return handle;
288 void CloseLib(void *handle)
289 { dlclose(handle); }
290 void *GetSymbol(void *handle, const char *name)
292 const char *err;
293 void *sym;
295 dlerror();
296 sym = dlsym(handle, name);
297 if((err=dlerror()) != NULL)
299 WARN("Failed to load %s: %s\n", name, err);
300 sym = NULL;
302 return sym;
305 #endif
306 #endif
309 void al_print(const char *func, const char *fmt, ...)
311 char str[256];
312 int i;
314 i = snprintf(str, sizeof(str), "AL lib: %s: ", func);
315 if(i < (int)sizeof(str) && i > 0)
317 va_list ap;
318 va_start(ap, fmt);
319 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
320 va_end(ap);
322 str[sizeof(str)-1] = 0;
324 fprintf(LogFile, "%s", str);
325 fflush(LogFile);
329 void SetRTPriority(void)
331 ALboolean failed = AL_FALSE;
333 #ifdef _WIN32
334 if(RTPrioLevel > 0)
335 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
336 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
337 if(RTPrioLevel > 0)
339 struct sched_param param;
340 /* Use the minimum real-time priority possible for now (on Linux this
341 * should be 1 for SCHED_RR) */
342 param.sched_priority = sched_get_priority_min(SCHED_RR);
343 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
345 #else
346 /* Real-time priority not available */
347 failed = (RTPrioLevel>0);
348 #endif
349 if(failed)
350 ERR("Failed to set priority level for thread\n");
354 static void Lock(volatile ALenum *l)
356 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
357 sched_yield();
360 static void Unlock(volatile ALenum *l)
362 ExchangeInt(l, AL_FALSE);
365 void RWLockInit(RWLock *lock)
367 lock->read_count = 0;
368 lock->write_count = 0;
369 lock->read_lock = AL_FALSE;
370 lock->read_entry_lock = AL_FALSE;
371 lock->write_lock = AL_FALSE;
374 void ReadLock(RWLock *lock)
376 Lock(&lock->read_entry_lock);
377 Lock(&lock->read_lock);
378 if(IncrementRef(&lock->read_count) == 1)
379 Lock(&lock->write_lock);
380 Unlock(&lock->read_lock);
381 Unlock(&lock->read_entry_lock);
384 void ReadUnlock(RWLock *lock)
386 if(DecrementRef(&lock->read_count) == 0)
387 Unlock(&lock->write_lock);
390 void WriteLock(RWLock *lock)
392 if(IncrementRef(&lock->write_count) == 1)
393 Lock(&lock->read_lock);
394 Lock(&lock->write_lock);
397 void WriteUnlock(RWLock *lock)
399 Unlock(&lock->write_lock);
400 if(DecrementRef(&lock->write_count) == 0)
401 Unlock(&lock->read_lock);
405 void InitUIntMap(UIntMap *map, ALsizei limit)
407 map->array = NULL;
408 map->size = 0;
409 map->maxsize = 0;
410 map->limit = limit;
411 RWLockInit(&map->lock);
414 void ResetUIntMap(UIntMap *map)
416 WriteLock(&map->lock);
417 free(map->array);
418 map->array = NULL;
419 map->size = 0;
420 map->maxsize = 0;
421 WriteUnlock(&map->lock);
424 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
426 ALsizei pos = 0;
428 WriteLock(&map->lock);
429 if(map->size > 0)
431 ALsizei low = 0;
432 ALsizei high = map->size - 1;
433 while(low < high)
435 ALsizei mid = low + (high-low)/2;
436 if(map->array[mid].key < key)
437 low = mid + 1;
438 else
439 high = mid;
441 if(map->array[low].key < key)
442 low++;
443 pos = low;
446 if(pos == map->size || map->array[pos].key != key)
448 if(map->size == map->limit)
450 WriteUnlock(&map->lock);
451 return AL_OUT_OF_MEMORY;
454 if(map->size == map->maxsize)
456 ALvoid *temp = NULL;
457 ALsizei newsize;
459 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
460 if(newsize >= map->maxsize)
461 temp = realloc(map->array, newsize*sizeof(map->array[0]));
462 if(!temp)
464 WriteUnlock(&map->lock);
465 return AL_OUT_OF_MEMORY;
467 map->array = temp;
468 map->maxsize = newsize;
471 if(pos < map->size)
472 memmove(&map->array[pos+1], &map->array[pos],
473 (map->size-pos)*sizeof(map->array[0]));
474 map->size++;
476 map->array[pos].key = key;
477 map->array[pos].value = value;
478 WriteUnlock(&map->lock);
480 return AL_NO_ERROR;
483 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
485 ALvoid *ptr = NULL;
486 WriteLock(&map->lock);
487 if(map->size > 0)
489 ALsizei low = 0;
490 ALsizei high = map->size - 1;
491 while(low < high)
493 ALsizei mid = low + (high-low)/2;
494 if(map->array[mid].key < key)
495 low = mid + 1;
496 else
497 high = mid;
499 if(map->array[low].key == key)
501 ptr = map->array[low].value;
502 if(low < map->size-1)
503 memmove(&map->array[low], &map->array[low+1],
504 (map->size-1-low)*sizeof(map->array[0]));
505 map->size--;
508 WriteUnlock(&map->lock);
509 return ptr;
512 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
514 ALvoid *ptr = NULL;
515 ReadLock(&map->lock);
516 if(map->size > 0)
518 ALsizei low = 0;
519 ALsizei high = map->size - 1;
520 while(low < high)
522 ALsizei mid = low + (high-low)/2;
523 if(map->array[mid].key < key)
524 low = mid + 1;
525 else
526 high = mid;
528 if(map->array[low].key == key)
529 ptr = map->array[low].value;
531 ReadUnlock(&map->lock);
532 return ptr;