First THREADED backport attempt, focusing on adding locks and making sure the API...
[platform/upstream/gstreamer.git] / gst / gstcpu.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2003 Colin Walters <walters@verbum.org>
5  *
6  * gstcpu.c: CPU detection and architecture-specific routines
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "gst_private.h"
25 #include <glib.h>
26
27
28 #include "gstcpu.h"
29 #include "gstinfo.h"
30
31 static GStaticMutex _cpu_mutex = G_STATIC_MUTEX_INIT;
32 static guint32 _gst_cpu_flags = 0;
33
34 #if defined(HAVE_CPU_I386) && defined(__GNUC__)
35 #define _gst_cpu_initialize_arch _gst_cpu_initialize_i386
36 gboolean _gst_cpu_initialize_i386 (gulong * flags, GString * featurelist);
37 #else
38 #define _gst_cpu_initialize_arch _gst_cpu_initialize_none
39 gboolean _gst_cpu_initialize_none (gulong * flags, GString * featurelist);
40 #endif
41
42
43 void
44 _gst_cpu_initialize (gboolean opt)
45 {
46   GString *featurelist = g_string_new ("");
47   gulong flags = 0;
48
49   if (opt) {
50     if (!_gst_cpu_initialize_arch (&flags, featurelist))
51       g_string_append (featurelist, "NONE");
52   } else
53     g_string_append (featurelist, "(DISABLED)");
54
55   GST_CAT_INFO (GST_CAT_GST_INIT, "CPU features: (%08lx) %s", flags,
56       featurelist->str);
57   g_string_free (featurelist, TRUE);
58 }
59
60 gboolean
61 _gst_cpu_initialize_none (gulong * flags, GString * featurelist)
62 {
63   return FALSE;
64 }
65
66 #if defined(HAVE_CPU_I386) && defined(__GNUC__)
67 static void
68 gst_cpuid_i386 (int x, unsigned long *eax, unsigned long *ebx,
69     unsigned long *ecx, unsigned long *edx)
70 {
71   unsigned long regs[4];
72
73   asm (
74       /* GCC-3.2 (and possibly others) don't clobber ebx properly,
75        * so we save/restore it directly. */
76 "  movl %%ebx, %%esi\n" "  cpuid\n" "  movl %%eax, %0\n" "  movl %%ebx, %1\n" "  movl %%ecx, %2\n" "  movl %%edx, %3\n" "  movl %%esi, %%ebx\n":"=o" (regs[0]), "=o" (regs[1]), "=o" (regs[2]),
77       "=o" (regs
78           [3])
79 :    "a" (x)
80 :    "ecx", "edx", "esi");
81
82   *eax = regs[0];
83   *ebx = regs[1];
84   *ecx = regs[2];
85   *edx = regs[3];
86 }
87
88 gboolean
89 _gst_cpu_initialize_i386 (gulong * flags, GString * featurelist)
90 {
91   gboolean AMD;
92   gulong eax = 0, ebx = 0, ecx = 0, edx = 0;
93   gboolean res = FALSE;
94
95   g_static_mutex_lock (&_cpu_mutex);
96   gst_cpuid_i386 (0, &eax, &ebx, &ecx, &edx);
97
98   AMD = (ebx == 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
99
100   gst_cpuid_i386 (1, &eax, &ebx, &ecx, &edx);
101
102   if (edx & (1 << 23)) {
103     _gst_cpu_flags |= GST_CPU_FLAG_MMX;
104     g_string_append (featurelist, "MMX ");
105
106     if (edx & (1 << 25)) {
107       _gst_cpu_flags |= GST_CPU_FLAG_SSE;
108       _gst_cpu_flags |= GST_CPU_FLAG_MMXEXT;
109       g_string_append (featurelist, "SSE ");
110     }
111
112     gst_cpuid_i386 (0x80000000, &eax, &ebx, &ecx, &edx);
113
114     if (eax >= 0x80000001) {
115
116       gst_cpuid_i386 (0x80000001, &eax, &ebx, &ecx, &edx);
117
118       if (edx & (1 << 31)) {
119         _gst_cpu_flags |= GST_CPU_FLAG_3DNOW;
120         g_string_append (featurelist, "3DNOW ");
121       }
122       if (AMD && (edx & (1 << 22))) {
123         _gst_cpu_flags |= GST_CPU_FLAG_MMXEXT;
124         g_string_append (featurelist, "MMXEXT ");
125       }
126     }
127   }
128   *flags = eax;
129   if (_gst_cpu_flags)
130     res = TRUE;
131   g_static_mutex_unlock (&_cpu_mutex);
132
133   return res;
134 }
135 #endif
136
137 GstCPUFlags
138 gst_cpu_get_flags (void)
139 {
140   GstCPUFlags res;
141
142   g_static_mutex_lock (&_cpu_mutex);
143   res = _gst_cpu_flags;
144   g_static_mutex_unlock (&_cpu_mutex);
145
146   return res;
147 }