flac_cpu.c 15 KB

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