Use __cpuid from intrin.h in Windows when available
[openal-soft.git] / Alc / helpers.c
blob6eff516e1402213025575c20aec1a3597ac0bab5
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 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
24 #define INITGUID
25 #include <windows.h>
26 #ifdef HAVE_GUIDDEF_H
27 #include <guiddef.h>
28 #else
29 #include <initguid.h>
30 #endif
32 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
33 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
35 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
37 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
38 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
39 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
40 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
42 #ifdef HAVE_MMDEVAPI
43 #include <devpropdef.h>
45 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
46 #endif
48 #endif
50 #include <stdlib.h>
51 #include <time.h>
52 #include <errno.h>
53 #include <stdarg.h>
54 #ifdef HAVE_DLFCN_H
55 #include <dlfcn.h>
56 #endif
57 #ifdef HAVE_INTRIN_H
58 #include <intrin.h>
59 #endif
60 #ifdef HAVE_CPUID_H
61 #include <cpuid.h>
62 #endif
63 #ifdef HAVE_FLOAT_H
64 #include <float.h>
65 #endif
66 #ifdef HAVE_IEEEFP_H
67 #include <ieeefp.h>
68 #endif
70 #include "alMain.h"
72 ALuint CPUCapFlags = 0;
75 void FillCPUCaps(ALuint capfilter)
77 ALuint caps = 0;
79 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
80 /* FIXME: We really should get this for all available CPUs in case different
81 * CPUs have different caps (is that possible on one machine?). */
82 #ifdef HAVE_CPUID_H
83 union {
84 unsigned int regs[4];
85 char str[sizeof(unsigned int[4])];
86 } cpuinf[3];
88 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
89 ERR("Failed to get CPUID\n");
90 else
92 unsigned int maxfunc = cpuinf[0].regs[0];
93 unsigned int maxextfunc = 0;
95 if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
96 maxextfunc = cpuinf[0].regs[0];
97 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
99 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
100 if(maxextfunc >= 0x80000004 &&
101 __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
102 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
103 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
104 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
106 if(maxfunc >= 1 &&
107 __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
109 #ifdef bit_SSE
110 if((cpuinf[0].regs[3]&bit_SSE))
111 caps |= CPU_CAP_SSE;
112 #endif
115 #elif defined(HAVE___CPUID)
116 union {
117 int regs[4];
118 char str[sizeof(int[4])];
119 } cpuinf[3];
120 unsigned int maxfunc = 0;
121 unsigned int maxextfunc = 0;
123 (__cpuid)(cpuinf[0].regs, 0);
124 maxfunc = cpuinf[0].regs[0];
126 (__cpuid)(cpuinf[0].regs, 0x80000000);
127 maxextfunc = cpuinf[0].regs[0];
128 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
130 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
131 if(maxextfunc >= 0x80000004)
133 (__cpuid)(cpuinf[0].regs, 0x80000002);
134 (__cpuid)(cpuinf[1].regs, 0x80000003);
135 (__cpuid)(cpuinf[2].regs, 0x80000004);
136 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
139 if(maxfunc >= 1)
141 (__cpuid)(cpuinf[0].regs, 1);
142 if((cpuinf[0].regs[3]&(1<<25)))
143 caps |= CPU_CAP_SSE;
145 #endif
146 #endif
147 #ifdef HAVE_NEON
148 /* Assume Neon support if compiled with it */
149 caps |= CPU_CAP_NEON;
150 #endif
152 TRACE("Got caps:%s%s%s\n", ((caps&CPU_CAP_SSE)?((capfilter&CPU_CAP_SSE)?" SSE":" (SSE)"):""),
153 ((caps&CPU_CAP_NEON)?((capfilter&CPU_CAP_NEON)?" Neon":" (Neon)"):""),
154 ((!caps)?" -none-":""));
155 CPUCapFlags = caps & capfilter;
159 void *al_malloc(size_t alignment, size_t size)
161 #if defined(HAVE_ALIGNED_ALLOC)
162 size = (size+(alignment-1))&~(alignment-1);
163 return aligned_alloc(alignment, size);
164 #elif defined(HAVE_POSIX_MEMALIGN)
165 void *ret;
166 if(posix_memalign(&ret, alignment, size) == 0)
167 return ret;
168 return NULL;
169 #elif defined(HAVE__ALIGNED_MALLOC)
170 return _aligned_malloc(size, alignment);
171 #else
172 char *ret = malloc(size+alignment);
173 if(ret != NULL)
175 *(ret++) = 0x00;
176 while(((ALintptrEXT)ret&(alignment-1)) != 0)
177 *(ret++) = 0x55;
179 return ret;
180 #endif
183 void *al_calloc(size_t alignment, size_t size)
185 void *ret = al_malloc(alignment, size);
186 if(ret) memset(ret, 0, size);
187 return ret;
190 void al_free(void *ptr)
192 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
193 free(ptr);
194 #elif defined(HAVE__ALIGNED_MALLOC)
195 _aligned_free(ptr);
196 #else
197 if(ptr != NULL)
199 char *finder = ptr;
200 do {
201 --finder;
202 } while(*finder == 0x55);
203 free(finder);
205 #endif
209 #if (defined(HAVE___CONTROL87_2) || defined(HAVE__CONTROLFP)) && (defined(__x86_64__) || defined(_M_X64))
210 /* Win64 doesn't allow us to set the precision control. */
211 #undef _MCW_PC
212 #define _MCW_PC 0
213 #endif
215 void SetMixerFPUMode(FPUCtl *ctl)
217 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
218 unsigned short fpuState;
219 __asm__ __volatile__("fnstcw %0" : "=m" (*&fpuState));
220 ctl->state = fpuState;
221 fpuState &= ~0x300; /* clear precision to single */
222 fpuState |= 0xC00; /* set round-to-zero */
223 __asm__ __volatile__("fldcw %0" : : "m" (*&fpuState));
224 #ifdef HAVE_SSE
225 if((CPUCapFlags&CPU_CAP_SSE))
227 int sseState;
228 __asm__ __volatile__("stmxcsr %0" : "=m" (*&sseState));
229 ctl->sse_state = sseState;
230 sseState |= 0x0C00; /* set round-to-zero */
231 sseState |= 0x8000; /* set flush-to-zero */
232 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
234 #endif
235 #elif defined(HAVE___CONTROL87_2)
236 int mode;
237 __control87_2(0, 0, &ctl->state, &ctl->sse_state);
238 __control87_2(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC, &mode, NULL);
239 #ifdef HAVE_SSE
240 if((CPUCapFlags&CPU_CAP_SSE))
241 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
242 #endif
243 #elif defined(HAVE__CONTROLFP)
244 ctl->state = _controlfp(0, 0);
245 (void)_controlfp(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC);
246 #elif defined(HAVE_FESETROUND)
247 ctl->state = fegetround();
248 #ifdef FE_TOWARDZERO
249 fesetround(FE_TOWARDZERO);
250 #endif
251 #endif
254 void RestoreFPUMode(const FPUCtl *ctl)
256 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
257 unsigned short fpuState = ctl->state;
258 __asm__ __volatile__("fldcw %0" : : "m" (*&fpuState));
259 #ifdef HAVE_SSE
260 if((CPUCapFlags&CPU_CAP_SSE))
261 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
262 #endif
263 #elif defined(HAVE___CONTROL87_2)
264 int mode;
265 __control87_2(ctl->state, _MCW_RC|_MCW_PC, &mode, NULL);
266 #ifdef HAVE_SSE
267 if((CPUCapFlags&CPU_CAP_SSE))
268 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
269 #endif
270 #elif defined(HAVE__CONTROLFP)
271 _controlfp(ctl->state, _MCW_RC|_MCW_PC);
272 #elif defined(HAVE_FESETROUND)
273 fesetround(ctl->state);
274 #endif
278 #ifdef _WIN32
279 void pthread_once(pthread_once_t *once, void (*callback)(void))
281 LONG ret;
282 while((ret=InterlockedExchange(once, 1)) == 1)
283 sched_yield();
284 if(ret == 0)
285 callback();
286 InterlockedExchange(once, 2);
290 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
292 *key = TlsAlloc();
293 if(callback)
294 InsertUIntMapEntry(&TlsDestructor, *key, callback);
295 return 0;
298 int pthread_key_delete(pthread_key_t key)
300 InsertUIntMapEntry(&TlsDestructor, key, NULL);
301 TlsFree(key);
302 return 0;
305 void *pthread_getspecific(pthread_key_t key)
306 { return TlsGetValue(key); }
308 int pthread_setspecific(pthread_key_t key, void *val)
310 TlsSetValue(key, val);
311 return 0;
315 void *LoadLib(const char *name)
316 { return LoadLibraryA(name); }
317 void CloseLib(void *handle)
318 { FreeLibrary((HANDLE)handle); }
319 void *GetSymbol(void *handle, const char *name)
321 void *ret;
323 ret = (void*)GetProcAddress((HANDLE)handle, name);
324 if(ret == NULL)
325 ERR("Failed to load %s\n", name);
326 return ret;
329 WCHAR *strdupW(const WCHAR *str)
331 const WCHAR *n;
332 WCHAR *ret;
333 size_t len;
335 n = str;
336 while(*n) n++;
337 len = n - str;
339 ret = calloc(sizeof(WCHAR), len+1);
340 if(ret != NULL)
341 memcpy(ret, str, sizeof(WCHAR)*len);
342 return ret;
345 #else
347 #include <pthread.h>
348 #ifdef HAVE_PTHREAD_NP_H
349 #include <pthread_np.h>
350 #endif
351 #include <sched.h>
353 void InitializeCriticalSection(CRITICAL_SECTION *cs)
355 pthread_mutexattr_t attrib;
356 int ret;
358 ret = pthread_mutexattr_init(&attrib);
359 assert(ret == 0);
361 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
362 #ifdef HAVE_PTHREAD_NP_H
363 if(ret != 0)
364 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
365 #endif
366 assert(ret == 0);
367 ret = pthread_mutex_init(cs, &attrib);
368 assert(ret == 0);
370 pthread_mutexattr_destroy(&attrib);
372 void DeleteCriticalSection(CRITICAL_SECTION *cs)
374 int ret;
375 ret = pthread_mutex_destroy(cs);
376 assert(ret == 0);
378 void EnterCriticalSection(CRITICAL_SECTION *cs)
380 int ret;
381 ret = pthread_mutex_lock(cs);
382 assert(ret == 0);
384 void LeaveCriticalSection(CRITICAL_SECTION *cs)
386 int ret;
387 ret = pthread_mutex_unlock(cs);
388 assert(ret == 0);
391 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
392 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
393 * Additionally, Win32 is supposed to measure the time since Windows started,
394 * as opposed to the actual time. */
395 ALuint timeGetTime(void)
397 #if _POSIX_TIMERS > 0
398 struct timespec ts;
399 int ret = -1;
401 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
402 #if _POSIX_MONOTONIC_CLOCK == 0
403 static int hasmono = 0;
404 if(hasmono > 0 || (hasmono == 0 &&
405 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
406 #endif
407 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
408 #endif
409 if(ret != 0)
410 ret = clock_gettime(CLOCK_REALTIME, &ts);
411 assert(ret == 0);
413 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
414 #else
415 struct timeval tv;
416 int ret;
418 ret = gettimeofday(&tv, NULL);
419 assert(ret == 0);
421 return tv.tv_usec/1000 + tv.tv_sec*1000;
422 #endif
425 void Sleep(ALuint t)
427 struct timespec tv, rem;
428 tv.tv_nsec = (t*1000000)%1000000000;
429 tv.tv_sec = t/1000;
431 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
432 tv = rem;
435 #ifdef HAVE_DLFCN_H
437 void *LoadLib(const char *name)
439 const char *err;
440 void *handle;
442 dlerror();
443 handle = dlopen(name, RTLD_NOW);
444 if((err=dlerror()) != NULL)
445 handle = NULL;
446 return handle;
448 void CloseLib(void *handle)
449 { dlclose(handle); }
450 void *GetSymbol(void *handle, const char *name)
452 const char *err;
453 void *sym;
455 dlerror();
456 sym = dlsym(handle, name);
457 if((err=dlerror()) != NULL)
459 WARN("Failed to load %s: %s\n", name, err);
460 sym = NULL;
462 return sym;
465 #endif
466 #endif
469 void al_print(const char *type, const char *func, const char *fmt, ...)
471 char str[256];
472 int i;
474 i = snprintf(str, sizeof(str), "AL lib: %s %s: ", type, func);
475 if(i > 0 && (unsigned int)i < sizeof(str))
477 va_list ap;
478 va_start(ap, fmt);
479 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
480 va_end(ap);
482 str[sizeof(str)-1] = 0;
484 fprintf(LogFile, "%s", str);
485 fflush(LogFile);
489 void SetRTPriority(void)
491 ALboolean failed = AL_FALSE;
493 #ifdef _WIN32
494 if(RTPrioLevel > 0)
495 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
496 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
497 if(RTPrioLevel > 0)
499 struct sched_param param;
500 /* Use the minimum real-time priority possible for now (on Linux this
501 * should be 1 for SCHED_RR) */
502 param.sched_priority = sched_get_priority_min(SCHED_RR);
503 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
505 #else
506 /* Real-time priority not available */
507 failed = (RTPrioLevel>0);
508 #endif
509 if(failed)
510 ERR("Failed to set priority level for thread\n");
514 static void Lock(volatile ALenum *l)
516 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
517 sched_yield();
520 static void Unlock(volatile ALenum *l)
522 ExchangeInt(l, AL_FALSE);
525 void RWLockInit(RWLock *lock)
527 lock->read_count = 0;
528 lock->write_count = 0;
529 lock->read_lock = AL_FALSE;
530 lock->read_entry_lock = AL_FALSE;
531 lock->write_lock = AL_FALSE;
534 void ReadLock(RWLock *lock)
536 Lock(&lock->read_entry_lock);
537 Lock(&lock->read_lock);
538 if(IncrementRef(&lock->read_count) == 1)
539 Lock(&lock->write_lock);
540 Unlock(&lock->read_lock);
541 Unlock(&lock->read_entry_lock);
544 void ReadUnlock(RWLock *lock)
546 if(DecrementRef(&lock->read_count) == 0)
547 Unlock(&lock->write_lock);
550 void WriteLock(RWLock *lock)
552 if(IncrementRef(&lock->write_count) == 1)
553 Lock(&lock->read_lock);
554 Lock(&lock->write_lock);
557 void WriteUnlock(RWLock *lock)
559 Unlock(&lock->write_lock);
560 if(DecrementRef(&lock->write_count) == 0)
561 Unlock(&lock->read_lock);
565 void InitUIntMap(UIntMap *map, ALsizei limit)
567 map->array = NULL;
568 map->size = 0;
569 map->maxsize = 0;
570 map->limit = limit;
571 RWLockInit(&map->lock);
574 void ResetUIntMap(UIntMap *map)
576 WriteLock(&map->lock);
577 free(map->array);
578 map->array = NULL;
579 map->size = 0;
580 map->maxsize = 0;
581 WriteUnlock(&map->lock);
584 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
586 ALsizei pos = 0;
588 WriteLock(&map->lock);
589 if(map->size > 0)
591 ALsizei low = 0;
592 ALsizei high = map->size - 1;
593 while(low < high)
595 ALsizei mid = low + (high-low)/2;
596 if(map->array[mid].key < key)
597 low = mid + 1;
598 else
599 high = mid;
601 if(map->array[low].key < key)
602 low++;
603 pos = low;
606 if(pos == map->size || map->array[pos].key != key)
608 if(map->size == map->limit)
610 WriteUnlock(&map->lock);
611 return AL_OUT_OF_MEMORY;
614 if(map->size == map->maxsize)
616 ALvoid *temp = NULL;
617 ALsizei newsize;
619 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
620 if(newsize >= map->maxsize)
621 temp = realloc(map->array, newsize*sizeof(map->array[0]));
622 if(!temp)
624 WriteUnlock(&map->lock);
625 return AL_OUT_OF_MEMORY;
627 map->array = temp;
628 map->maxsize = newsize;
631 if(pos < map->size)
632 memmove(&map->array[pos+1], &map->array[pos],
633 (map->size-pos)*sizeof(map->array[0]));
634 map->size++;
636 map->array[pos].key = key;
637 map->array[pos].value = value;
638 WriteUnlock(&map->lock);
640 return AL_NO_ERROR;
643 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
645 ALvoid *ptr = NULL;
646 WriteLock(&map->lock);
647 if(map->size > 0)
649 ALsizei low = 0;
650 ALsizei high = map->size - 1;
651 while(low < high)
653 ALsizei mid = low + (high-low)/2;
654 if(map->array[mid].key < key)
655 low = mid + 1;
656 else
657 high = mid;
659 if(map->array[low].key == key)
661 ptr = map->array[low].value;
662 if(low < map->size-1)
663 memmove(&map->array[low], &map->array[low+1],
664 (map->size-1-low)*sizeof(map->array[0]));
665 map->size--;
668 WriteUnlock(&map->lock);
669 return ptr;
672 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
674 ALvoid *ptr = NULL;
675 ReadLock(&map->lock);
676 if(map->size > 0)
678 ALsizei low = 0;
679 ALsizei high = map->size - 1;
680 while(low < high)
682 ALsizei mid = low + (high-low)/2;
683 if(map->array[mid].key < key)
684 low = mid + 1;
685 else
686 high = mid;
688 if(map->array[low].key == key)
689 ptr = map->array[low].value;
691 ReadUnlock(&map->lock);
692 return ptr;