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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
23 #define _WIN32_IE 0x501
25 #define _WIN32_IE 0x400
49 #ifdef HAVE_SSE_INTRINSICS
50 #include <xmmintrin.h>
52 #ifdef HAVE_SYS_SYSCONF_H
53 #include <sys/sysconf.h>
56 #ifdef HAVE_PROC_PIDPATH
61 #include <sys/types.h>
62 #include <sys/sysctl.h>
67 #elif defined(_WIN32_IE)
76 #include "fpu_modes.h"
82 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
83 defined(_M_IX86) || defined(_M_X64))
84 using reg_type
= unsigned int;
85 static inline void get_cpuid(unsigned int f
, reg_type
*regs
)
86 { __get_cpuid(f
, ®s
[0], ®s
[1], ®s
[2], ®s
[3]); }
88 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
89 defined(_M_IX86) || defined(_M_X64))
91 static inline void get_cpuid(unsigned int f
, reg_type
*regs
)
92 { (__cpuid
)(regs
, f
); }
98 void FillCPUCaps(int capfilter
)
102 /* FIXME: We really should get this for all available CPUs in case different
103 * CPUs have different caps (is that possible on one machine?). */
107 char str
[sizeof(reg_type
[4])];
110 get_cpuid(0, cpuinf
[0].regs
);
111 if(cpuinf
[0].regs
[0] == 0)
112 ERR("Failed to get CPUID\n");
115 unsigned int maxfunc
= cpuinf
[0].regs
[0];
116 unsigned int maxextfunc
;
118 get_cpuid(0x80000000, cpuinf
[0].regs
);
119 maxextfunc
= cpuinf
[0].regs
[0];
121 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc
, maxextfunc
);
123 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf
[0].str
+4, cpuinf
[0].str
+12, cpuinf
[0].str
+8);
124 if(maxextfunc
>= 0x80000004)
126 get_cpuid(0x80000002, cpuinf
[0].regs
);
127 get_cpuid(0x80000003, cpuinf
[1].regs
);
128 get_cpuid(0x80000004, cpuinf
[2].regs
);
129 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf
[0].str
, cpuinf
[1].str
, cpuinf
[2].str
);
134 get_cpuid(1, cpuinf
[0].regs
);
135 if((cpuinf
[0].regs
[3]&(1<<25)))
137 if((caps
&CPU_CAP_SSE
) && (cpuinf
[0].regs
[3]&(1<<26)))
138 caps
|= CPU_CAP_SSE2
;
139 if((caps
&CPU_CAP_SSE2
) && (cpuinf
[0].regs
[2]&(1<<0)))
140 caps
|= CPU_CAP_SSE3
;
141 if((caps
&CPU_CAP_SSE3
) && (cpuinf
[0].regs
[2]&(1<<19)))
142 caps
|= CPU_CAP_SSE4_1
;
146 /* Assume support for whatever's supported if we can't check for it */
147 #if defined(HAVE_SSE4_1)
148 #warning "Assuming SSE 4.1 run-time support!"
149 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
| CPU_CAP_SSE3
| CPU_CAP_SSE4_1
;
150 #elif defined(HAVE_SSE3)
151 #warning "Assuming SSE 3 run-time support!"
152 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
| CPU_CAP_SSE3
;
153 #elif defined(HAVE_SSE2)
154 #warning "Assuming SSE 2 run-time support!"
155 caps
|= CPU_CAP_SSE
| CPU_CAP_SSE2
;
156 #elif defined(HAVE_SSE)
157 #warning "Assuming SSE run-time support!"
162 al::ifstream file
{"/proc/cpuinfo"};
164 ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
167 std::string features
;
169 auto getline
= [](std::istream
&f
, std::string
&output
) -> bool
171 while(f
.good() && f
.peek() == '\n')
173 return std::getline(f
, output
) && !output
.empty();
176 while(getline(file
, features
))
178 if(features
.compare(0, 10, "Features\t:", 10) == 0)
184 while((extpos
=features
.find("neon", extpos
+1)) != std::string::npos
)
186 if((extpos
== 0 || std::isspace(features
[extpos
-1])) &&
187 (extpos
+4 == features
.length() || std::isspace(features
[extpos
+4])))
189 caps
|= CPU_CAP_NEON
;
196 TRACE("Extensions:%s%s%s%s%s%s\n",
197 ((capfilter
&CPU_CAP_SSE
) ? ((caps
&CPU_CAP_SSE
) ? " +SSE" : " -SSE") : ""),
198 ((capfilter
&CPU_CAP_SSE2
) ? ((caps
&CPU_CAP_SSE2
) ? " +SSE2" : " -SSE2") : ""),
199 ((capfilter
&CPU_CAP_SSE3
) ? ((caps
&CPU_CAP_SSE3
) ? " +SSE3" : " -SSE3") : ""),
200 ((capfilter
&CPU_CAP_SSE4_1
) ? ((caps
&CPU_CAP_SSE4_1
) ? " +SSE4.1" : " -SSE4.1") : ""),
201 ((capfilter
&CPU_CAP_NEON
) ? ((caps
&CPU_CAP_NEON
) ? " +NEON" : " -NEON") : ""),
202 ((!capfilter
) ? " -none-" : "")
204 CPUCapFlags
= caps
& capfilter
;
210 #if defined(HAVE_SSE_INTRINSICS)
211 this->sse_state
= _mm_getcsr();
212 unsigned int sseState
= this->sse_state
;
213 sseState
|= 0x8000; /* set flush-to-zero */
214 sseState
|= 0x0040; /* set denormals-are-zero */
215 _mm_setcsr(sseState
);
217 #elif defined(__GNUC__) && defined(HAVE_SSE)
219 if((CPUCapFlags
&CPU_CAP_SSE
))
221 __asm__
__volatile__("stmxcsr %0" : "=m" (*&this->sse_state
));
222 unsigned int sseState
= this->sse_state
;
223 sseState
|= 0x8000; /* set flush-to-zero */
224 if((CPUCapFlags
&CPU_CAP_SSE2
))
225 sseState
|= 0x0040; /* set denormals-are-zero */
226 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&sseState
));
230 this->in_mode
= true;
235 if(!this->in_mode
) return;
237 #if defined(HAVE_SSE_INTRINSICS)
238 _mm_setcsr(this->sse_state
);
240 #elif defined(__GNUC__) && defined(HAVE_SSE)
242 if((CPUCapFlags
&CPU_CAP_SSE
))
243 __asm__
__volatile__("ldmxcsr %0" : : "m" (*&this->sse_state
));
245 this->in_mode
= false;
251 const PathNamePair
&GetProcBinary()
253 static PathNamePair ret
;
254 if(!ret
.fname
.empty() || !ret
.path
.empty())
257 al::vector
<WCHAR
> fullpath(256);
259 while((len
=GetModuleFileNameW(nullptr, fullpath
.data(), static_cast<DWORD
>(fullpath
.size()))) == fullpath
.size())
260 fullpath
.resize(fullpath
.size() << 1);
263 ERR("Failed to get process name: error %lu\n", GetLastError());
267 fullpath
.resize(len
);
268 if(fullpath
.back() != 0)
269 fullpath
.push_back(0);
271 auto sep
= std::find(fullpath
.rbegin()+1, fullpath
.rend(), '\\');
272 sep
= std::find(fullpath
.rbegin()+1, sep
, '/');
273 if(sep
!= fullpath
.rend())
276 ret
.fname
= wstr_to_utf8(&*sep
+ 1);
277 ret
.path
= wstr_to_utf8(fullpath
.data());
280 ret
.fname
= wstr_to_utf8(fullpath
.data());
282 TRACE("Got binary: %s, %s\n", ret
.path
.c_str(), ret
.fname
.c_str());
287 void al_print(FILE *logfile
, const char *fmt
, ...)
289 al::vector
<char> dynmsg
;
295 va_copy(args2
, args
);
296 int msglen
{std::vsnprintf(str
, sizeof(stcmsg
), fmt
, args
)};
297 if UNLIKELY(msglen
>= 0 && static_cast<size_t>(msglen
) >= sizeof(stcmsg
))
299 dynmsg
.resize(static_cast<size_t>(msglen
) + 1u);
301 msglen
= std::vsnprintf(str
, dynmsg
.size(), fmt
, args2
);
306 std::wstring wstr
{utf8_to_wstr(str
)};
307 fprintf(logfile
, "%ls", wstr
.c_str());
312 static inline int is_slash(int c
)
313 { return (c
== '\\' || c
== '/'); }
315 static void DirectorySearch(const char *path
, const char *ext
, al::vector
<std::string
> *const results
)
317 std::string pathstr
{path
};
320 TRACE("Searching %s\n", pathstr
.c_str());
322 std::wstring wpath
{utf8_to_wstr(pathstr
.c_str())};
323 WIN32_FIND_DATAW fdata
;
324 HANDLE hdl
{FindFirstFileW(wpath
.c_str(), &fdata
)};
325 if(hdl
!= INVALID_HANDLE_VALUE
)
327 size_t base
= results
->size();
329 results
->emplace_back();
330 std::string
&str
= results
->back();
333 str
+= wstr_to_utf8(fdata
.cFileName
);
334 TRACE(" got %s\n", str
.c_str());
335 } while(FindNextFileW(hdl
, &fdata
));
338 std::sort(results
->begin()+base
, results
->end());
342 al::vector
<std::string
> SearchDataFiles(const char *ext
, const char *subdir
)
344 static std::mutex search_lock
;
345 std::lock_guard
<std::mutex
> _
{search_lock
};
347 /* If the path is absolute, use it directly. */
348 al::vector
<std::string
> results
;
349 if(isalpha(subdir
[0]) && subdir
[1] == ':' && is_slash(subdir
[2]))
351 std::string path
{subdir
};
352 std::replace(path
.begin(), path
.end(), '/', '\\');
353 DirectorySearch(path
.c_str(), ext
, &results
);
356 if(subdir
[0] == '\\' && subdir
[1] == '\\' && subdir
[2] == '?' && subdir
[3] == '\\')
358 DirectorySearch(subdir
, ext
, &results
);
364 /* Search the app-local directory. */
365 if(auto localpath
= al::getenv(L
"ALSOFT_LOCAL_PATH"))
367 path
= wstr_to_utf8(localpath
->c_str());
368 if(is_slash(path
.back()))
371 else if(WCHAR
*cwdbuf
{_wgetcwd(nullptr, 0)})
373 path
= wstr_to_utf8(cwdbuf
);
374 if(is_slash(path
.back()))
380 std::replace(path
.begin(), path
.end(), '/', '\\');
381 DirectorySearch(path
.c_str(), ext
, &results
);
383 /* Search the local and global data dirs. */
384 static constexpr int ids
[2]{ CSIDL_APPDATA
, CSIDL_COMMON_APPDATA
};
387 WCHAR buffer
[MAX_PATH
];
388 if(SHGetSpecialFolderPathW(nullptr, buffer
, id
, FALSE
) == FALSE
)
391 path
= wstr_to_utf8(buffer
);
392 if(!is_slash(path
.back()))
395 std::replace(path
.begin(), path
.end(), '/', '\\');
397 DirectorySearch(path
.c_str(), ext
, &results
);
403 void SetRTPriority(void)
407 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
408 if(failed
) ERR("Failed to set priority level for thread\n");
413 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
418 const PathNamePair
&GetProcBinary()
420 static PathNamePair ret
;
421 if(!ret
.fname
.empty() || !ret
.path
.empty())
424 al::vector
<char> pathname
;
427 int mib
[4] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
428 if(sysctl(mib
, 4, nullptr, &pathlen
, nullptr, 0) == -1)
429 WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno
));
432 pathname
.resize(pathlen
+ 1);
433 sysctl(mib
, 4, pathname
.data(), &pathlen
, nullptr, 0);
434 pathname
.resize(pathlen
);
437 #ifdef HAVE_PROC_PIDPATH
440 char procpath
[PROC_PIDPATHINFO_MAXSIZE
]{};
441 const pid_t pid
{getpid()};
442 if(proc_pidpath(pid
, procpath
, sizeof(procpath
)) < 1)
443 ERR("proc_pidpath(%d, ...) failed: %s\n", pid
, strerror(errno
));
445 pathname
.insert(pathname
.end(), procpath
, procpath
+strlen(procpath
));
450 pathname
.resize(256);
452 const char *selfname
{"/proc/self/exe"};
453 ssize_t len
{readlink(selfname
, pathname
.data(), pathname
.size())};
454 if(len
== -1 && errno
== ENOENT
)
456 selfname
= "/proc/self/file";
457 len
= readlink(selfname
, pathname
.data(), pathname
.size());
459 if(len
== -1 && errno
== ENOENT
)
461 selfname
= "/proc/curproc/exe";
462 len
= readlink(selfname
, pathname
.data(), pathname
.size());
464 if(len
== -1 && errno
== ENOENT
)
466 selfname
= "/proc/curproc/file";
467 len
= readlink(selfname
, pathname
.data(), pathname
.size());
470 while(len
> 0 && static_cast<size_t>(len
) == pathname
.size())
472 pathname
.resize(pathname
.size() << 1);
473 len
= readlink(selfname
, pathname
.data(), pathname
.size());
477 WARN("Failed to readlink %s: %s\n", selfname
, strerror(errno
));
481 pathname
.resize(static_cast<size_t>(len
));
483 while(!pathname
.empty() && pathname
.back() == 0)
486 auto sep
= std::find(pathname
.crbegin(), pathname
.crend(), '/');
487 if(sep
!= pathname
.crend())
489 ret
.path
= std::string(pathname
.cbegin(), sep
.base()-1);
490 ret
.fname
= std::string(sep
.base(), pathname
.cend());
493 ret
.fname
= std::string(pathname
.cbegin(), pathname
.cend());
495 TRACE("Got binary: %s, %s\n", ret
.path
.c_str(), ret
.fname
.c_str());
500 void al_print(FILE *logfile
, const char *fmt
, ...)
505 vfprintf(logfile
, fmt
, ap
);
512 static void DirectorySearch(const char *path
, const char *ext
, al::vector
<std::string
> *const results
)
514 TRACE("Searching %s for *%s\n", path
, ext
);
515 DIR *dir
{opendir(path
)};
518 auto base
= results
->cend() - results
->cbegin();
519 const size_t extlen
{strlen(ext
)};
521 struct dirent
*dirent
;
522 while((dirent
=readdir(dir
)) != nullptr)
524 if(strcmp(dirent
->d_name
, ".") == 0 || strcmp(dirent
->d_name
, "..") == 0)
527 size_t len
{strlen(dirent
->d_name
)};
528 if(len
<= extlen
) continue;
529 if(al::strcasecmp(dirent
->d_name
+len
-extlen
, ext
) != 0)
532 results
->emplace_back();
533 std::string
&str
= results
->back();
535 if(str
.back() != '/')
537 str
+= dirent
->d_name
;
538 TRACE(" got %s\n", str
.c_str());
542 std::sort(results
->begin()+base
, results
->end());
546 al::vector
<std::string
> SearchDataFiles(const char *ext
, const char *subdir
)
548 static std::mutex search_lock
;
549 std::lock_guard
<std::mutex
> _
{search_lock
};
551 al::vector
<std::string
> results
;
554 DirectorySearch(subdir
, ext
, &results
);
558 /* Search the app-local directory. */
559 if(auto localpath
= al::getenv("ALSOFT_LOCAL_PATH"))
560 DirectorySearch(localpath
->c_str(), ext
, &results
);
563 al::vector
<char> cwdbuf(256);
564 while(!getcwd(cwdbuf
.data(), cwdbuf
.size()))
571 cwdbuf
.resize(cwdbuf
.size() << 1);
574 DirectorySearch(".", ext
, &results
);
577 DirectorySearch(cwdbuf
.data(), ext
, &results
);
582 // Search local data dir
583 if(auto datapath
= al::getenv("XDG_DATA_HOME"))
585 std::string
&path
= *datapath
;
586 if(path
.back() != '/')
589 DirectorySearch(path
.c_str(), ext
, &results
);
591 else if(auto homepath
= al::getenv("HOME"))
593 std::string
&path
= *homepath
;
594 if(path
.back() == '/')
596 path
+= "/.local/share/";
598 DirectorySearch(path
.c_str(), ext
, &results
);
601 // Search global data dirs
602 std::string datadirs
{al::getenv("XDG_DATA_DIRS").value_or("/usr/local/share/:/usr/share/")};
605 while(curpos
< datadirs
.size())
607 size_t nextpos
{datadirs
.find(':', curpos
)};
609 std::string path
{(nextpos
!= std::string::npos
) ?
610 datadirs
.substr(curpos
, nextpos
++ - curpos
) : datadirs
.substr(curpos
)};
613 if(path
.empty()) continue;
614 if(path
.back() != '/')
618 DirectorySearch(path
.c_str(), ext
, &results
);
627 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
630 struct sched_param param
;
631 /* Use the minimum real-time priority possible for now (on Linux this
632 * should be 1 for SCHED_RR) */
633 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
634 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
637 /* Real-time priority not available */
638 failed
= (RTPrioLevel
>0);
641 ERR("Failed to set priority level for thread\n");