]> git.sesse.net Git - x264/blob - common/osdep.c
Fix crash when using libx264.dll compiled with ICL for X86_64
[x264] / common / osdep.c
1 /*****************************************************************************
2  * osdep.c: platform-specific code
3  *****************************************************************************
4  * Copyright (C) 2003-2012 x264 project
5  *
6  * Authors: Steven Walters <kemuri9@gmail.com>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 #include "common.h"
28
29 #if SYS_WINDOWS
30 #include <sys/types.h>
31 #include <sys/timeb.h>
32 #else
33 #include <sys/time.h>
34 #endif
35 #include <time.h>
36
37 #if PTW32_STATIC_LIB
38 #define WIN32_LEAN_AND_MEAN
39 #include <windows.h>
40 /* this is a global in pthread-win32 to indicate if it has been initialized or not */
41 extern int ptw32_processInitialized;
42 #endif
43
44 int64_t x264_mdate( void )
45 {
46 #if SYS_WINDOWS
47     struct timeb tb;
48     ftime( &tb );
49     return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
50 #else
51     struct timeval tv_date;
52     gettimeofday( &tv_date, NULL );
53     return (int64_t)tv_date.tv_sec * 1000000 + (int64_t)tv_date.tv_usec;
54 #endif
55 }
56
57 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
58 /* state of the threading library being initialized */
59 static volatile LONG x264_threading_is_init = 0;
60
61 static void x264_threading_destroy( void )
62 {
63 #if PTW32_STATIC_LIB
64     pthread_win32_thread_detach_np();
65     pthread_win32_process_detach_np();
66 #else
67     x264_win32_threading_destroy();
68 #endif
69 }
70
71 int x264_threading_init( void )
72 {
73     /* if already init, then do nothing */
74     if( InterlockedCompareExchange( &x264_threading_is_init, 1, 0 ) )
75         return 0;
76 #if PTW32_STATIC_LIB
77     /* if static pthread-win32 is already initialized, then do nothing */
78     if( ptw32_processInitialized )
79         return 0;
80     if( !pthread_win32_process_attach_np() )
81         return -1;
82 #else
83     if( x264_win32_threading_init() )
84         return -1;
85 #endif
86     /* register cleanup to run at process termination */
87     atexit( x264_threading_destroy );
88
89     return 0;
90 }
91 #endif
92
93 #if HAVE_MMX
94 #ifdef __INTEL_COMPILER
95 /* Agner's patch to Intel's CPU dispatcher from pages 131-132 of
96  * http://agner.org/optimize/optimizing_cpp.pdf (2011-01-30)
97  * adapted to x264's cpu schema. */
98
99 // Global variable indicating cpu
100 int __intel_cpu_indicator = 0;
101 // CPU dispatcher function
102 void x264_intel_cpu_indicator_init( void )
103 {
104     unsigned int cpu = x264_cpu_detect();
105     if( cpu&X264_CPU_AVX )
106         __intel_cpu_indicator = 0x20000;
107     else if( cpu&X264_CPU_SSE42 )
108         __intel_cpu_indicator = 0x8000;
109     else if( cpu&X264_CPU_SSE4 )
110         __intel_cpu_indicator = 0x2000;
111     else if( cpu&X264_CPU_SSSE3 )
112         __intel_cpu_indicator = 0x1000;
113     else if( cpu&X264_CPU_SSE3 )
114         __intel_cpu_indicator = 0x800;
115     else if( cpu&X264_CPU_SSE2 && !(cpu&X264_CPU_SSE2_IS_SLOW) )
116         __intel_cpu_indicator = 0x200;
117     else if( cpu&X264_CPU_SSE )
118         __intel_cpu_indicator = 0x80;
119     else if( cpu&X264_CPU_MMX2 )
120         __intel_cpu_indicator = 8;
121     else
122         __intel_cpu_indicator = 1;
123 }
124
125 /* __intel_cpu_indicator_init appears to have a non-standard calling convention that
126  * assumes certain registers aren't preserved, so we'll route it through a function
127  * that backs up all the registers. */
128 void __intel_cpu_indicator_init( void )
129 {
130     x264_safe_intel_cpu_indicator_init();
131 }
132 #else
133 void x264_intel_cpu_indicator_init( void )
134 {}
135 #endif
136 #endif