The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
15KB

  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of the Xiph.org Foundation nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include "include/private/cpu.h"
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #if defined FLAC__CPU_IA32
  38. # include <signal.h>
  39. #elif defined FLAC__CPU_PPC
  40. # if !defined FLAC__NO_ASM
  41. # if defined FLAC__SYS_DARWIN
  42. # include <sys/sysctl.h>
  43. # include <mach/mach.h>
  44. # include <mach/mach_host.h>
  45. # include <mach/host_info.h>
  46. # include <mach/machine.h>
  47. # ifndef CPU_SUBTYPE_POWERPC_970
  48. # define CPU_SUBTYPE_POWERPC_970 ((cpu_subtype_t) 100)
  49. # endif
  50. # else /* FLAC__SYS_DARWIN */
  51. # include <signal.h>
  52. # include <setjmp.h>
  53. static sigjmp_buf jmpbuf;
  54. static volatile sig_atomic_t canjump = 0;
  55. static void sigill_handler (int sig)
  56. {
  57. if (!canjump) {
  58. signal (sig, SIG_DFL);
  59. raise (sig);
  60. }
  61. canjump = 0;
  62. siglongjmp (jmpbuf, 1);
  63. }
  64. # endif /* FLAC__SYS_DARWIN */
  65. # endif /* FLAC__NO_ASM */
  66. #endif /* FLAC__CPU_PPC */
  67. #if defined (__NetBSD__) || defined(__OpenBSD__)
  68. #include <sys/param.h>
  69. #include <sys/sysctl.h>
  70. #include <machine/cpu.h>
  71. #endif
  72. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  73. #include <sys/types.h>
  74. #include <sys/sysctl.h>
  75. #endif
  76. #if defined(__APPLE__)
  77. /* how to get sysctlbyname()? */
  78. #endif
  79. /* these are flags in EDX of CPUID AX=00000001 */
  80. static const unsigned FLAC__CPUINFO_IA32_CPUID_CMOV = 0x00008000;
  81. static const unsigned FLAC__CPUINFO_IA32_CPUID_MMX = 0x00800000;
  82. static const unsigned FLAC__CPUINFO_IA32_CPUID_FXSR = 0x01000000;
  83. static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE = 0x02000000;
  84. static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE2 = 0x04000000;
  85. /* these are flags in ECX of CPUID AX=00000001 */
  86. static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE3 = 0x00000001;
  87. static const unsigned FLAC__CPUINFO_IA32_CPUID_SSSE3 = 0x00000200;
  88. /* these are flags in EDX of CPUID AX=80000001 */
  89. static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_3DNOW = 0x80000000;
  90. static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXT3DNOW = 0x40000000;
  91. static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXTMMX = 0x00400000;
  92. /*
  93. * Extra stuff needed for detection of OS support for SSE on IA-32
  94. */
  95. #if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM && !defined FLAC__NO_SSE_OS && !defined FLAC__SSE_OS
  96. # if defined(__linux__)
  97. /*
  98. * If the OS doesn't support SSE, we will get here with a SIGILL. We
  99. * modify the return address to jump over the offending SSE instruction
  100. * and also the operation following it that indicates the instruction
  101. * executed successfully. In this way we use no global variables and
  102. * stay thread-safe.
  103. *
  104. * 3 + 3 + 6:
  105. * 3 bytes for "xorps xmm0,xmm0"
  106. * 3 bytes for estimate of how long the follwing "inc var" instruction is
  107. * 6 bytes extra in case our estimate is wrong
  108. * 12 bytes puts us in the NOP "landing zone"
  109. */
  110. # undef USE_OBSOLETE_SIGCONTEXT_FLAVOR /* #define this to use the older signal handler method */
  111. # ifdef USE_OBSOLETE_SIGCONTEXT_FLAVOR
  112. static void sigill_handler_sse_os(int signal, struct sigcontext sc)
  113. {
  114. (void)signal;
  115. sc.eip += 3 + 3 + 6;
  116. }
  117. # else
  118. # include <sys/ucontext.h>
  119. static void sigill_handler_sse_os(int signal, siginfo_t *si, void *uc)
  120. {
  121. (void)signal, (void)si;
  122. ((ucontext_t*)uc)->uc_mcontext.gregs[14/*REG_EIP*/] += 3 + 3 + 6;
  123. }
  124. # endif
  125. # elif defined(_MSC_VER)
  126. # include <windows.h>
  127. # undef USE_TRY_CATCH_FLAVOR /* #define this to use the try/catch method for catching illegal opcode exception */
  128. # ifdef USE_TRY_CATCH_FLAVOR
  129. # else
  130. LONG CALLBACK sigill_handler_sse_os(EXCEPTION_POINTERS *ep)
  131. {
  132. if(ep->ExceptionRecord->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION) {
  133. ep->ContextRecord->Eip += 3 + 3 + 6;
  134. return EXCEPTION_CONTINUE_EXECUTION;
  135. }
  136. return EXCEPTION_CONTINUE_SEARCH;
  137. }
  138. # endif
  139. # endif
  140. #endif
  141. void FLAC__cpu_info(FLAC__CPUInfo *info)
  142. {
  143. /*
  144. * IA32-specific
  145. */
  146. #ifdef FLAC__CPU_IA32
  147. info->type = FLAC__CPUINFO_TYPE_IA32;
  148. #if !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
  149. info->use_asm = true; /* we assume a minimum of 80386 with FLAC__CPU_IA32 */
  150. info->data.ia32.cpuid = FLAC__cpu_have_cpuid_asm_ia32()? true : false;
  151. info->data.ia32.bswap = info->data.ia32.cpuid; /* CPUID => BSWAP since it came after */
  152. info->data.ia32.cmov = false;
  153. info->data.ia32.mmx = false;
  154. info->data.ia32.fxsr = false;
  155. info->data.ia32.sse = false;
  156. info->data.ia32.sse2 = false;
  157. info->data.ia32.sse3 = false;
  158. info->data.ia32.ssse3 = false;
  159. info->data.ia32._3dnow = false;
  160. info->data.ia32.ext3dnow = false;
  161. info->data.ia32.extmmx = false;
  162. if(info->data.ia32.cpuid) {
  163. /* http://www.sandpile.org/ia32/cpuid.htm */
  164. FLAC__uint32 flags_edx, flags_ecx;
  165. FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx);
  166. info->data.ia32.cmov = (flags_edx & FLAC__CPUINFO_IA32_CPUID_CMOV )? true : false;
  167. info->data.ia32.mmx = (flags_edx & FLAC__CPUINFO_IA32_CPUID_MMX )? true : false;
  168. info->data.ia32.fxsr = (flags_edx & FLAC__CPUINFO_IA32_CPUID_FXSR )? true : false;
  169. info->data.ia32.sse = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE )? true : false;
  170. info->data.ia32.sse2 = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE2 )? true : false;
  171. info->data.ia32.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 )? true : false;
  172. info->data.ia32.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3)? true : false;
  173. #ifdef FLAC__USE_3DNOW
  174. flags_edx = FLAC__cpu_info_extended_amd_asm_ia32();
  175. info->data.ia32._3dnow = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_3DNOW )? true : false;
  176. info->data.ia32.ext3dnow = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXT3DNOW)? true : false;
  177. info->data.ia32.extmmx = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXTMMX )? true : false;
  178. #else
  179. info->data.ia32._3dnow = info->data.ia32.ext3dnow = info->data.ia32.extmmx = false;
  180. #endif
  181. #ifdef DEBUG
  182. fprintf(stderr, "CPU info (IA-32):\n");
  183. fprintf(stderr, " CPUID ...... %c\n", info->data.ia32.cpuid ? 'Y' : 'n');
  184. fprintf(stderr, " BSWAP ...... %c\n", info->data.ia32.bswap ? 'Y' : 'n');
  185. fprintf(stderr, " CMOV ....... %c\n", info->data.ia32.cmov ? 'Y' : 'n');
  186. fprintf(stderr, " MMX ........ %c\n", info->data.ia32.mmx ? 'Y' : 'n');
  187. fprintf(stderr, " FXSR ....... %c\n", info->data.ia32.fxsr ? 'Y' : 'n');
  188. fprintf(stderr, " SSE ........ %c\n", info->data.ia32.sse ? 'Y' : 'n');
  189. fprintf(stderr, " SSE2 ....... %c\n", info->data.ia32.sse2 ? 'Y' : 'n');
  190. fprintf(stderr, " SSE3 ....... %c\n", info->data.ia32.sse3 ? 'Y' : 'n');
  191. fprintf(stderr, " SSSE3 ...... %c\n", info->data.ia32.ssse3 ? 'Y' : 'n');
  192. fprintf(stderr, " 3DNow! ..... %c\n", info->data.ia32._3dnow ? 'Y' : 'n');
  193. fprintf(stderr, " 3DNow!-ext . %c\n", info->data.ia32.ext3dnow? 'Y' : 'n');
  194. fprintf(stderr, " 3DNow!-MMX . %c\n", info->data.ia32.extmmx ? 'Y' : 'n');
  195. #endif
  196. /*
  197. * now have to check for OS support of SSE/SSE2
  198. */
  199. if(info->data.ia32.fxsr || info->data.ia32.sse || info->data.ia32.sse2) {
  200. #if defined FLAC__NO_SSE_OS
  201. /* assume user knows better than us; turn it off */
  202. info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  203. #elif defined FLAC__SSE_OS
  204. /* assume user knows better than us; leave as detected above */
  205. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
  206. int sse = 0;
  207. size_t len;
  208. /* at least one of these must work: */
  209. len = sizeof(sse); sse = sse || (sysctlbyname("hw.instruction_sse", &sse, &len, NULL, 0) == 0 && sse);
  210. len = sizeof(sse); sse = sse || (sysctlbyname("hw.optional.sse" , &sse, &len, NULL, 0) == 0 && sse); /* __APPLE__ ? */
  211. if(!sse)
  212. info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  213. #elif defined(__NetBSD__) || defined (__OpenBSD__)
  214. # if __NetBSD_Version__ >= 105250000 || (defined __OpenBSD__)
  215. int val = 0, mib[2] = { CTL_MACHDEP, CPU_SSE };
  216. size_t len = sizeof(val);
  217. if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val)
  218. info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  219. else { /* double-check SSE2 */
  220. mib[1] = CPU_SSE2;
  221. len = sizeof(val);
  222. if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val)
  223. info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  224. }
  225. # else
  226. info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  227. # endif
  228. #elif defined(__linux__)
  229. int sse = 0;
  230. struct sigaction sigill_save;
  231. #ifdef USE_OBSOLETE_SIGCONTEXT_FLAVOR
  232. if(0 == sigaction(SIGILL, NULL, &sigill_save) && signal(SIGILL, (void (*)(int))sigill_handler_sse_os) != SIG_ERR)
  233. #else
  234. struct sigaction sigill_sse;
  235. sigill_sse.sa_sigaction = sigill_handler_sse_os;
  236. __sigemptyset(&sigill_sse.sa_mask);
  237. sigill_sse.sa_flags = SA_SIGINFO | SA_RESETHAND; /* SA_RESETHAND just in case our SIGILL return jump breaks, so we don't get stuck in a loop */
  238. if(0 == sigaction(SIGILL, &sigill_sse, &sigill_save))
  239. #endif
  240. {
  241. /* http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html */
  242. /* see sigill_handler_sse_os() for an explanation of the following: */
  243. asm volatile (
  244. "xorl %0,%0\n\t" /* for some reason, still need to do this to clear 'sse' var */
  245. "xorps %%xmm0,%%xmm0\n\t" /* will cause SIGILL if unsupported by OS */
  246. "incl %0\n\t" /* SIGILL handler will jump over this */
  247. /* landing zone */
  248. "nop\n\t" /* SIGILL jump lands here if "inc" is 9 bytes */
  249. "nop\n\t"
  250. "nop\n\t"
  251. "nop\n\t"
  252. "nop\n\t"
  253. "nop\n\t"
  254. "nop\n\t" /* SIGILL jump lands here if "inc" is 3 bytes (expected) */
  255. "nop\n\t"
  256. "nop" /* SIGILL jump lands here if "inc" is 1 byte */
  257. : "=r"(sse)
  258. : "r"(sse)
  259. );
  260. sigaction(SIGILL, &sigill_save, NULL);
  261. }
  262. if(!sse)
  263. info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  264. #elif defined(_MSC_VER)
  265. # ifdef USE_TRY_CATCH_FLAVOR
  266. _try {
  267. __asm {
  268. # if _MSC_VER <= 1200
  269. /* VC6 assembler doesn't know SSE, have to emit bytecode instead */
  270. _emit 0x0F
  271. _emit 0x57
  272. _emit 0xC0
  273. # else
  274. xorps xmm0,xmm0
  275. # endif
  276. }
  277. }
  278. _except(EXCEPTION_EXECUTE_HANDLER) {
  279. if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION)
  280. info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  281. }
  282. # else
  283. int sse = 0;
  284. LPTOP_LEVEL_EXCEPTION_FILTER save = SetUnhandledExceptionFilter(sigill_handler_sse_os);
  285. /* see GCC version above for explanation */
  286. /* http://msdn2.microsoft.com/en-us/library/4ks26t93.aspx */
  287. /* http://www.codeproject.com/cpp/gccasm.asp */
  288. /* http://www.hick.org/~mmiller/msvc_inline_asm.html */
  289. __asm {
  290. # if _MSC_VER <= 1200
  291. /* VC6 assembler doesn't know SSE, have to emit bytecode instead */
  292. _emit 0x0F
  293. _emit 0x57
  294. _emit 0xC0
  295. # else
  296. xorps xmm0,xmm0
  297. # endif
  298. inc sse
  299. nop
  300. nop
  301. nop
  302. nop
  303. nop
  304. nop
  305. nop
  306. nop
  307. nop
  308. }
  309. SetUnhandledExceptionFilter(save);
  310. if(!sse)
  311. info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  312. # endif
  313. #else
  314. /* no way to test, disable to be safe */
  315. info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
  316. #endif
  317. #ifdef DEBUG
  318. fprintf(stderr, " SSE OS sup . %c\n", info->data.ia32.sse ? 'Y' : 'n');
  319. #endif
  320. }
  321. }
  322. #else
  323. info->use_asm = false;
  324. #endif
  325. /*
  326. * PPC-specific
  327. */
  328. #elif defined FLAC__CPU_PPC
  329. info->type = FLAC__CPUINFO_TYPE_PPC;
  330. # if !defined FLAC__NO_ASM
  331. info->use_asm = true;
  332. # ifdef FLAC__USE_ALTIVEC
  333. # if defined FLAC__SYS_DARWIN
  334. {
  335. int val = 0, mib[2] = { CTL_HW, HW_VECTORUNIT };
  336. size_t len = sizeof(val);
  337. info->data.ppc.altivec = !(sysctl(mib, 2, &val, &len, NULL, 0) || !val);
  338. }
  339. {
  340. host_basic_info_data_t hostInfo;
  341. mach_msg_type_number_t infoCount;
  342. infoCount = HOST_BASIC_INFO_COUNT;
  343. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);
  344. info->data.ppc.ppc64 = (hostInfo.cpu_type == CPU_TYPE_POWERPC) && (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_970);
  345. }
  346. # else /* FLAC__USE_ALTIVEC && !FLAC__SYS_DARWIN */
  347. {
  348. /* no Darwin, do it the brute-force way */
  349. /* @@@@@@ this is not thread-safe; replace with SSE OS method above or remove */
  350. info->data.ppc.altivec = 0;
  351. info->data.ppc.ppc64 = 0;
  352. signal (SIGILL, sigill_handler);
  353. canjump = 0;
  354. if (!sigsetjmp (jmpbuf, 1)) {
  355. canjump = 1;
  356. asm volatile (
  357. "mtspr 256, %0\n\t"
  358. "vand %%v0, %%v0, %%v0"
  359. :
  360. : "r" (-1)
  361. );
  362. info->data.ppc.altivec = 1;
  363. }
  364. canjump = 0;
  365. if (!sigsetjmp (jmpbuf, 1)) {
  366. int x = 0;
  367. canjump = 1;
  368. /* PPC64 hardware implements the cntlzd instruction */
  369. asm volatile ("cntlzd %0, %1" : "=r" (x) : "r" (x) );
  370. info->data.ppc.ppc64 = 1;
  371. }
  372. signal (SIGILL, SIG_DFL); /*@@@@@@ should save and restore old signal */
  373. }
  374. # endif
  375. # else /* !FLAC__USE_ALTIVEC */
  376. info->data.ppc.altivec = 0;
  377. info->data.ppc.ppc64 = 0;
  378. # endif
  379. # else
  380. info->use_asm = false;
  381. # endif
  382. /*
  383. * unknown CPI
  384. */
  385. #else
  386. info->type = FLAC__CPUINFO_TYPE_UNKNOWN;
  387. info->use_asm = false;
  388. #endif
  389. }