1 /*****************************************************************************
2 * osdep.c: platform-specific code
3 *****************************************************************************
4 * Copyright (C) 2003-2013 x264 project
6 * Authors: Steven Walters <kemuri9@gmail.com>
7 * Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
30 #include <sys/types.h>
31 #include <sys/timeb.h>
38 #define WIN32_LEAN_AND_MEAN
40 /* this is a global in pthread-win32 to indicate if it has been initialized or not */
41 extern int ptw32_processInitialized;
44 int64_t x264_mdate( void )
49 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
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;
57 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
58 /* state of the threading library being initialized */
59 static volatile LONG x264_threading_is_init = 0;
61 static void x264_threading_destroy( void )
64 pthread_win32_thread_detach_np();
65 pthread_win32_process_detach_np();
67 x264_win32_threading_destroy();
71 int x264_threading_init( void )
73 /* if already init, then do nothing */
74 if( InterlockedCompareExchange( &x264_threading_is_init, 1, 0 ) )
77 /* if static pthread-win32 is already initialized, then do nothing */
78 if( ptw32_processInitialized )
80 if( !pthread_win32_process_attach_np() )
83 if( x264_win32_threading_init() )
86 /* register cleanup to run at process termination */
87 atexit( x264_threading_destroy );
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. */
99 // Global variable indicating cpu
100 int __intel_cpu_indicator = 0;
101 // CPU dispatcher function
102 void x264_intel_cpu_indicator_init( void )
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;
122 __intel_cpu_indicator = 1;
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 )
130 x264_safe_intel_cpu_indicator_init();
133 void x264_intel_cpu_indicator_init( void )