Use the correct multiplier for int32 output with CoreAudio
[openal-soft.git] / Alc / helpers.c
blob6358f044916bb5e5d50791a1e8bbdf3a3c56156c
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_MALLOC_H
28 #include <malloc.h>
29 #endif
31 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
32 #define INITGUID
33 #include <windows.h>
34 #ifdef HAVE_GUIDDEF_H
35 #include <guiddef.h>
36 #else
37 #include <initguid.h>
38 #endif
40 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
41 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
43 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
45 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
46 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
47 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
48 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
50 #ifdef HAVE_MMDEVAPI
51 #include <devpropdef.h>
52 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
53 #endif
54 #endif
55 #ifdef HAVE_DLFCN_H
56 #include <dlfcn.h>
57 #endif
58 #ifdef HAVE_CPUID_H
59 #include <cpuid.h>
60 #endif
61 #ifdef HAVE_FLOAT_H
62 #include <float.h>
63 #endif
64 #ifdef HAVE_IEEEFP_H
65 #include <ieeefp.h>
66 #endif
68 #include "alMain.h"
70 ALuint CPUCapFlags = 0;
73 void FillCPUCaps(ALuint capfilter)
75 ALuint caps = 0;
77 /* FIXME: We really should get this for all available CPUs in case different
78 * CPUs have different caps (is that possible on one machine?). */
79 #if defined(HAVE_CPUID_H) && (defined(__i386__) || defined(__x86_64__) || \
80 defined(_M_IX86) || defined(_M_X64))
81 union {
82 unsigned int regs[4];
83 char str[sizeof(unsigned int[4])];
84 } cpuinf[3];
86 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
87 ERR("Failed to get CPUID\n");
88 else
90 unsigned int maxfunc = cpuinf[0].regs[0];
91 unsigned int maxextfunc = 0;
93 if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
94 maxextfunc = cpuinf[0].regs[0];
95 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
97 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
98 if(maxextfunc >= 0x80000004 &&
99 __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
100 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
101 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
102 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
104 if(maxfunc >= 1 &&
105 __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
107 #ifdef bit_SSE
108 if((cpuinf[0].regs[3]&bit_SSE))
109 caps |= CPU_CAP_SSE;
110 #endif
113 #elif defined(HAVE_WINDOWS_H)
114 HMODULE k32 = GetModuleHandleA("kernel32.dll");
115 BOOL (WINAPI*IsProcessorFeaturePresent)(DWORD ProcessorFeature);
116 IsProcessorFeaturePresent = (BOOL(WINAPI*)(DWORD))GetProcAddress(k32, "IsProcessorFeaturePresent");
117 if(!IsProcessorFeaturePresent)
118 ERR("IsProcessorFeaturePresent not available; CPU caps not detected\n");
119 else
121 if(IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE))
122 caps |= CPU_CAP_SSE;
124 #endif
125 #ifdef HAVE_NEON
126 /* Assume Neon support if compiled with it */
127 caps |= CPU_CAP_NEON;
128 #endif
130 TRACE("Got caps:%s%s%s\n", ((caps&CPU_CAP_SSE)?((capfilter&CPU_CAP_SSE)?" SSE":" (SSE)"):""),
131 ((caps&CPU_CAP_NEON)?((capfilter&CPU_CAP_NEON)?" Neon":" (Neon)"):""),
132 ((!caps)?" -none-":""));
133 CPUCapFlags = caps & capfilter;
137 void *al_malloc(size_t alignment, size_t size)
139 #if defined(HAVE_ALIGNED_ALLOC)
140 size = (size+(alignment-1))&~(alignment-1);
141 return aligned_alloc(alignment, size);
142 #elif defined(HAVE_POSIX_MEMALIGN)
143 void *ret;
144 if(posix_memalign(&ret, alignment, size) == 0)
145 return ret;
146 return NULL;
147 #elif defined(HAVE__ALIGNED_MALLOC)
148 return _aligned_malloc(size, alignment);
149 #else
150 char *ret = malloc(size+alignment);
151 if(ret != NULL)
153 *(ret++) = 0x00;
154 while(((ALintptrEXT)ret&(alignment-1)) != 0)
155 *(ret++) = 0x55;
157 return ret;
158 #endif
161 void *al_calloc(size_t alignment, size_t size)
163 void *ret = al_malloc(alignment, size);
164 if(ret) memset(ret, 0, size);
165 return ret;
168 void al_free(void *ptr)
170 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
171 free(ptr);
172 #elif defined(HAVE__ALIGNED_MALLOC)
173 _aligned_free(ptr);
174 #else
175 if(ptr != NULL)
177 char *finder = ptr;
178 do {
179 --finder;
180 } while(*finder == 0x55);
181 free(finder);
183 #endif
187 #if (defined(HAVE___CONTROL87_2) || defined(HAVE__CONTROLFP)) && (defined(__x86_64__) || defined(_M_X64))
188 /* Win64 doesn't allow us to set the precision control. */
189 #undef _MCW_PC
190 #define _MCW_PC 0
191 #endif
193 void SetMixerFPUMode(FPUCtl *ctl)
195 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
196 unsigned short fpuState;
197 __asm__ __volatile__("fnstcw %0" : "=m" (*&fpuState));
198 ctl->state = fpuState;
199 fpuState &= ~0x300; /* clear precision to single */
200 fpuState |= 0xC00; /* set round-to-zero */
201 __asm__ __volatile__("fldcw %0" : : "m" (*&fpuState));
202 #ifdef HAVE_SSE
203 if((CPUCapFlags&CPU_CAP_SSE))
205 int sseState;
206 __asm__ __volatile__("stmxcsr %0" : "=m" (*&sseState));
207 ctl->sse_state = sseState;
208 sseState |= 0x0C00; /* set round-to-zero */
209 sseState |= 0x8000; /* set flush-to-zero */
210 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
212 #endif
213 #elif defined(HAVE___CONTROL87_2)
214 int mode;
215 __control87_2(0, 0, &ctl->state, NULL);
216 __control87_2(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC, &mode, NULL);
217 #ifdef HAVE_SSE
218 if((CPUCapFlags&CPU_CAP_SSE))
220 __control87_2(0, 0, NULL, &ctl->sse_state);
221 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
223 #endif
224 #elif defined(HAVE__CONTROLFP)
225 ctl->state = _controlfp(0, 0);
226 (void)_controlfp(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC);
227 #elif defined(HAVE_FESETROUND)
228 ctl->state = fegetround();
229 #ifdef FE_TOWARDZERO
230 fesetround(FE_TOWARDZERO);
231 #endif
232 #endif
235 void RestoreFPUMode(const FPUCtl *ctl)
237 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
238 unsigned short fpuState = ctl->state;
239 __asm__ __volatile__("fldcw %0" : : "m" (*&fpuState));
240 #ifdef HAVE_SSE
241 if((CPUCapFlags&CPU_CAP_SSE))
242 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
243 #endif
244 #elif defined(HAVE___CONTROL87_2)
245 int mode;
246 __control87_2(ctl->state, _MCW_RC|_MCW_PC, &mode, NULL);
247 #ifdef HAVE_SSE
248 if((CPUCapFlags&CPU_CAP_SSE))
249 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
250 #endif
251 #elif defined(HAVE__CONTROLFP)
252 _controlfp(ctl->state, _MCW_RC|_MCW_PC);
253 #elif defined(HAVE_FESETROUND)
254 fesetround(ctl->state);
255 #endif
259 #ifdef _WIN32
260 void pthread_once(pthread_once_t *once, void (*callback)(void))
262 LONG ret;
263 while((ret=InterlockedExchange(once, 1)) == 1)
264 sched_yield();
265 if(ret == 0)
266 callback();
267 InterlockedExchange(once, 2);
271 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
273 *key = TlsAlloc();
274 if(callback)
275 InsertUIntMapEntry(&TlsDestructor, *key, callback);
276 return 0;
279 int pthread_key_delete(pthread_key_t key)
281 InsertUIntMapEntry(&TlsDestructor, key, NULL);
282 TlsFree(key);
283 return 0;
286 void *pthread_getspecific(pthread_key_t key)
287 { return TlsGetValue(key); }
289 int pthread_setspecific(pthread_key_t key, void *val)
291 TlsSetValue(key, val);
292 return 0;
296 void *LoadLib(const char *name)
297 { return LoadLibraryA(name); }
298 void CloseLib(void *handle)
299 { FreeLibrary((HANDLE)handle); }
300 void *GetSymbol(void *handle, const char *name)
302 void *ret;
304 ret = (void*)GetProcAddress((HANDLE)handle, name);
305 if(ret == NULL)
306 ERR("Failed to load %s\n", name);
307 return ret;
310 WCHAR *strdupW(const WCHAR *str)
312 const WCHAR *n;
313 WCHAR *ret;
314 size_t len;
316 n = str;
317 while(*n) n++;
318 len = n - str;
320 ret = calloc(sizeof(WCHAR), len+1);
321 if(ret != NULL)
322 memcpy(ret, str, sizeof(WCHAR)*len);
323 return ret;
326 #else
328 #include <pthread.h>
329 #ifdef HAVE_PTHREAD_NP_H
330 #include <pthread_np.h>
331 #endif
332 #include <sched.h>
334 void InitializeCriticalSection(CRITICAL_SECTION *cs)
336 pthread_mutexattr_t attrib;
337 int ret;
339 ret = pthread_mutexattr_init(&attrib);
340 assert(ret == 0);
342 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
343 #ifdef HAVE_PTHREAD_NP_H
344 if(ret != 0)
345 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
346 #endif
347 assert(ret == 0);
348 ret = pthread_mutex_init(cs, &attrib);
349 assert(ret == 0);
351 pthread_mutexattr_destroy(&attrib);
353 void DeleteCriticalSection(CRITICAL_SECTION *cs)
355 int ret;
356 ret = pthread_mutex_destroy(cs);
357 assert(ret == 0);
359 void EnterCriticalSection(CRITICAL_SECTION *cs)
361 int ret;
362 ret = pthread_mutex_lock(cs);
363 assert(ret == 0);
365 void LeaveCriticalSection(CRITICAL_SECTION *cs)
367 int ret;
368 ret = pthread_mutex_unlock(cs);
369 assert(ret == 0);
372 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
373 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
374 * Additionally, Win32 is supposed to measure the time since Windows started,
375 * as opposed to the actual time. */
376 ALuint timeGetTime(void)
378 #if _POSIX_TIMERS > 0
379 struct timespec ts;
380 int ret = -1;
382 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
383 #if _POSIX_MONOTONIC_CLOCK == 0
384 static int hasmono = 0;
385 if(hasmono > 0 || (hasmono == 0 &&
386 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
387 #endif
388 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
389 #endif
390 if(ret != 0)
391 ret = clock_gettime(CLOCK_REALTIME, &ts);
392 assert(ret == 0);
394 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
395 #else
396 struct timeval tv;
397 int ret;
399 ret = gettimeofday(&tv, NULL);
400 assert(ret == 0);
402 return tv.tv_usec/1000 + tv.tv_sec*1000;
403 #endif
406 void Sleep(ALuint t)
408 struct timespec tv, rem;
409 tv.tv_nsec = (t*1000000)%1000000000;
410 tv.tv_sec = t/1000;
412 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
413 tv = rem;
416 #ifdef HAVE_DLFCN_H
418 void *LoadLib(const char *name)
420 const char *err;
421 void *handle;
423 dlerror();
424 handle = dlopen(name, RTLD_NOW);
425 if((err=dlerror()) != NULL)
426 handle = NULL;
427 return handle;
429 void CloseLib(void *handle)
430 { dlclose(handle); }
431 void *GetSymbol(void *handle, const char *name)
433 const char *err;
434 void *sym;
436 dlerror();
437 sym = dlsym(handle, name);
438 if((err=dlerror()) != NULL)
440 WARN("Failed to load %s: %s\n", name, err);
441 sym = NULL;
443 return sym;
446 #endif
447 #endif
450 void al_print(const char *type, const char *func, const char *fmt, ...)
452 char str[256];
453 int i;
455 i = snprintf(str, sizeof(str), "AL lib: %s %s: ", type, func);
456 if(i > 0 && (unsigned int)i < sizeof(str))
458 va_list ap;
459 va_start(ap, fmt);
460 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
461 va_end(ap);
463 str[sizeof(str)-1] = 0;
465 fprintf(LogFile, "%s", str);
466 fflush(LogFile);
470 void SetRTPriority(void)
472 ALboolean failed = AL_FALSE;
474 #ifdef _WIN32
475 if(RTPrioLevel > 0)
476 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
477 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
478 if(RTPrioLevel > 0)
480 struct sched_param param;
481 /* Use the minimum real-time priority possible for now (on Linux this
482 * should be 1 for SCHED_RR) */
483 param.sched_priority = sched_get_priority_min(SCHED_RR);
484 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
486 #else
487 /* Real-time priority not available */
488 failed = (RTPrioLevel>0);
489 #endif
490 if(failed)
491 ERR("Failed to set priority level for thread\n");
495 static void Lock(volatile ALenum *l)
497 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
498 sched_yield();
501 static void Unlock(volatile ALenum *l)
503 ExchangeInt(l, AL_FALSE);
506 void RWLockInit(RWLock *lock)
508 lock->read_count = 0;
509 lock->write_count = 0;
510 lock->read_lock = AL_FALSE;
511 lock->read_entry_lock = AL_FALSE;
512 lock->write_lock = AL_FALSE;
515 void ReadLock(RWLock *lock)
517 Lock(&lock->read_entry_lock);
518 Lock(&lock->read_lock);
519 if(IncrementRef(&lock->read_count) == 1)
520 Lock(&lock->write_lock);
521 Unlock(&lock->read_lock);
522 Unlock(&lock->read_entry_lock);
525 void ReadUnlock(RWLock *lock)
527 if(DecrementRef(&lock->read_count) == 0)
528 Unlock(&lock->write_lock);
531 void WriteLock(RWLock *lock)
533 if(IncrementRef(&lock->write_count) == 1)
534 Lock(&lock->read_lock);
535 Lock(&lock->write_lock);
538 void WriteUnlock(RWLock *lock)
540 Unlock(&lock->write_lock);
541 if(DecrementRef(&lock->write_count) == 0)
542 Unlock(&lock->read_lock);
546 void InitUIntMap(UIntMap *map, ALsizei limit)
548 map->array = NULL;
549 map->size = 0;
550 map->maxsize = 0;
551 map->limit = limit;
552 RWLockInit(&map->lock);
555 void ResetUIntMap(UIntMap *map)
557 WriteLock(&map->lock);
558 free(map->array);
559 map->array = NULL;
560 map->size = 0;
561 map->maxsize = 0;
562 WriteUnlock(&map->lock);
565 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
567 ALsizei pos = 0;
569 WriteLock(&map->lock);
570 if(map->size > 0)
572 ALsizei low = 0;
573 ALsizei high = map->size - 1;
574 while(low < high)
576 ALsizei mid = low + (high-low)/2;
577 if(map->array[mid].key < key)
578 low = mid + 1;
579 else
580 high = mid;
582 if(map->array[low].key < key)
583 low++;
584 pos = low;
587 if(pos == map->size || map->array[pos].key != key)
589 if(map->size == map->limit)
591 WriteUnlock(&map->lock);
592 return AL_OUT_OF_MEMORY;
595 if(map->size == map->maxsize)
597 ALvoid *temp = NULL;
598 ALsizei newsize;
600 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
601 if(newsize >= map->maxsize)
602 temp = realloc(map->array, newsize*sizeof(map->array[0]));
603 if(!temp)
605 WriteUnlock(&map->lock);
606 return AL_OUT_OF_MEMORY;
608 map->array = temp;
609 map->maxsize = newsize;
612 if(pos < map->size)
613 memmove(&map->array[pos+1], &map->array[pos],
614 (map->size-pos)*sizeof(map->array[0]));
615 map->size++;
617 map->array[pos].key = key;
618 map->array[pos].value = value;
619 WriteUnlock(&map->lock);
621 return AL_NO_ERROR;
624 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
626 ALvoid *ptr = NULL;
627 WriteLock(&map->lock);
628 if(map->size > 0)
630 ALsizei low = 0;
631 ALsizei high = map->size - 1;
632 while(low < high)
634 ALsizei mid = low + (high-low)/2;
635 if(map->array[mid].key < key)
636 low = mid + 1;
637 else
638 high = mid;
640 if(map->array[low].key == key)
642 ptr = map->array[low].value;
643 if(low < map->size-1)
644 memmove(&map->array[low], &map->array[low+1],
645 (map->size-1-low)*sizeof(map->array[0]));
646 map->size--;
649 WriteUnlock(&map->lock);
650 return ptr;
653 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
655 ALvoid *ptr = NULL;
656 ReadLock(&map->lock);
657 if(map->size > 0)
659 ALsizei low = 0;
660 ALsizei high = map->size - 1;
661 while(low < high)
663 ALsizei mid = low + (high-low)/2;
664 if(map->array[mid].key < key)
665 low = mid + 1;
666 else
667 high = mid;
669 if(map->array[low].key == key)
670 ptr = map->array[low].value;
672 ReadUnlock(&map->lock);
673 return ptr;