~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to app/base/cpu-accel.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* The GIMP -- an image manipulation program
2
 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
 
 */
18
 
 
19
 
/*
20
 
 * x86 bits Copyright (C) Manish Singh <yosh@gimp.org>
21
 
 */
22
 
 
23
 
/*
24
 
 * PPC CPU acceleration detection was taken from DirectFB but seems to be
25
 
 * originating from mpeg2dec with the following copyright:
26
 
 *
27
 
 * Copyright (C) 1999-2001 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
28
 
 */
29
 
 
30
 
#include "config.h"
31
 
 
32
 
#include <string.h>
33
 
#include <signal.h>
34
 
#include <setjmp.h>
35
 
 
36
 
#include <glib.h>
37
 
 
38
 
#include "cpu-accel.h"
39
 
 
40
 
#if defined(ARCH_X86) && defined(USE_MMX) && defined(__GNUC__)
41
 
 
42
 
#define HAVE_ACCEL 1
43
 
 
44
 
typedef enum
45
 
{
46
 
  ARCH_X86_VENDOR_NONE,
47
 
  ARCH_X86_VENDOR_INTEL,
48
 
  ARCH_X86_VENDOR_AMD,
49
 
  ARCH_X86_VENDOR_CENTAUR,
50
 
  ARCH_X86_VENDOR_CYRIX,
51
 
  ARCH_X86_VENDOR_NSC,
52
 
  ARCH_X86_VENDOR_TRANSMETA,
53
 
  ARCH_X86_VENDOR_NEXGEN,
54
 
  ARCH_X86_VENDOR_RISE,
55
 
  ARCH_X86_VENDOR_UMC,
56
 
  ARCH_X86_VENDOR_SIS,
57
 
  ARCH_X86_VENDOR_UNKNOWN    = 0xff
58
 
} X86Vendor;
59
 
 
60
 
enum
61
 
{
62
 
  ARCH_X86_INTEL_FEATURE_MMX      = 1 << 23,
63
 
  ARCH_X86_INTEL_FEATURE_XMM      = 1 << 25,
64
 
  ARCH_X86_INTEL_FEATURE_XMM2     = 1 << 26,
65
 
 
66
 
  ARCH_X86_AMD_FEATURE_MMXEXT     = 1 << 22,
67
 
  ARCH_X86_AMD_FEATURE_3DNOW      = 1 << 31,
68
 
 
69
 
  ARCH_X86_CENTAUR_FEATURE_MMX    = 1 << 23,
70
 
  ARCH_X86_CENTAUR_FEATURE_MMXEXT = 1 << 24,
71
 
  ARCH_X86_CENTAUR_FEATURE_3DNOW  = 1 << 31,
72
 
 
73
 
  ARCH_X86_CYRIX_FEATURE_MMX      = 1 << 23,
74
 
  ARCH_X86_CYRIX_FEATURE_MMXEXT   = 1 << 24
75
 
};
76
 
 
77
 
#if !defined(ARCH_X86_64) && (defined(PIC) || defined(__PIC__))
78
 
#define cpuid(op,eax,ebx,ecx,edx)  \
79
 
  __asm__ ("movl %%ebx, %%esi\n\t" \
80
 
           "cpuid\n\t"             \
81
 
           "xchgl %%ebx,%%esi"     \
82
 
           : "=a" (eax),           \
83
 
             "=S" (ebx),           \
84
 
             "=c" (ecx),           \
85
 
             "=d" (edx)            \
86
 
           : "0" (op))
87
 
#else
88
 
#define cpuid(op,eax,ebx,ecx,edx)  \
89
 
  __asm__ ("cpuid"                 \
90
 
           : "=a" (eax),           \
91
 
             "=b" (ebx),           \
92
 
             "=c" (ecx),           \
93
 
             "=d" (edx)            \
94
 
           : "0" (op))
95
 
#endif
96
 
 
97
 
 
98
 
static X86Vendor
99
 
arch_get_vendor (void)
100
 
{
101
 
  guint32 eax, ebx, ecx, edx;
102
 
  gchar   id[16];
103
 
 
104
 
#ifndef ARCH_X86_64
105
 
  /* Only need to check this on ia32 */
106
 
  __asm__ ("pushfl\n\t"
107
 
           "pushfl\n\t"
108
 
           "popl %0\n\t"
109
 
           "movl %0,%1\n\t"
110
 
           "xorl $0x200000,%0\n\t"
111
 
           "pushl %0\n\t"
112
 
           "popfl\n\t"
113
 
           "pushfl\n\t"
114
 
           "popl %0\n\t"
115
 
           "popfl"
116
 
           : "=a" (eax),
117
 
             "=c" (ecx)
118
 
           :
119
 
           : "cc");
120
 
 
121
 
  if (eax == ecx)
122
 
    return ARCH_X86_VENDOR_NONE;
123
 
#endif
124
 
 
125
 
  cpuid (0, eax, ebx, ecx, edx);
126
 
 
127
 
  if (eax == 0)
128
 
    return ARCH_X86_VENDOR_NONE;
129
 
 
130
 
  *(int *)&id[0] = ebx;
131
 
  *(int *)&id[4] = edx;
132
 
  *(int *)&id[8] = ecx;
133
 
  
134
 
  id[12] = '\0';
135
 
 
136
 
#ifdef ARCH_X86_64
137
 
  if (strcmp (id, "AuthenticAMD") == 0)
138
 
    return ARCH_X86_VENDOR_AMD;
139
 
  else if (strcmp (id, "GenuineIntel") == 0)
140
 
    return ARCH_X86_VENDOR_INTEL;
141
 
#else
142
 
  if (strcmp (id, "GenuineIntel") == 0)
143
 
    return ARCH_X86_VENDOR_INTEL;
144
 
  else if (strcmp (id, "AuthenticAMD") == 0)
145
 
    return ARCH_X86_VENDOR_AMD;
146
 
  else if (strcmp (id, "CentaurHauls") == 0)
147
 
    return ARCH_X86_VENDOR_CENTAUR;
148
 
  else if (strcmp (id, "CyrixInstead") == 0)
149
 
    return ARCH_X86_VENDOR_CYRIX;
150
 
  else if (strcmp (id, "Geode by NSC") == 0)
151
 
    return ARCH_X86_VENDOR_NSC;
152
 
  else if (strcmp (id, "GenuineTMx86") == 0 ||
153
 
           strcmp (id, "TransmetaCPU") == 0)
154
 
    return ARCH_X86_VENDOR_TRANSMETA;
155
 
  else if (strcmp (id, "NexGenDriven") == 0)
156
 
    return ARCH_X86_VENDOR_NEXGEN;
157
 
  else if (strcmp (id, "RiseRiseRise") == 0)
158
 
    return ARCH_X86_VENDOR_RISE;
159
 
  else if (strcmp (id, "UMC UMC UMC ") == 0)
160
 
    return ARCH_X86_VENDOR_UMC;
161
 
  else if (strcmp (id, "SiS SiS SiS ") == 0)
162
 
    return ARCH_X86_VENDOR_SIS;
163
 
#endif
164
 
 
165
 
  return ARCH_X86_VENDOR_UNKNOWN;
166
 
}
167
 
 
168
 
static guint32
169
 
arch_accel_intel (void)
170
 
{
171
 
  guint32 caps = 0;
172
 
 
173
 
#ifdef USE_MMX
174
 
  {
175
 
    guint32 eax, ebx, ecx, edx;
176
 
 
177
 
    cpuid (1, eax, ebx, ecx, edx);
178
 
 
179
 
    if ((edx & ARCH_X86_INTEL_FEATURE_MMX) == 0)
180
 
      return 0;
181
 
 
182
 
    caps = CPU_ACCEL_X86_MMX;
183
 
 
184
 
#ifdef USE_SSE
185
 
    if (edx & ARCH_X86_INTEL_FEATURE_XMM)
186
 
      caps |= CPU_ACCEL_X86_SSE | CPU_ACCEL_X86_MMXEXT;
187
 
 
188
 
    if (edx & ARCH_X86_INTEL_FEATURE_XMM2)
189
 
      caps |= CPU_ACCEL_X86_SSE2;
190
 
#endif /* USE_SSE */
191
 
  }
192
 
#endif /* USE_MMX */
193
 
 
194
 
  return caps;
195
 
}
196
 
 
197
 
static guint32
198
 
arch_accel_amd (void)
199
 
{
200
 
  guint32 caps;
201
 
 
202
 
  caps = arch_accel_intel ();
203
 
 
204
 
#ifdef USE_MMX
205
 
  {
206
 
    guint32 eax, ebx, ecx, edx;
207
 
 
208
 
    cpuid (0x80000000, eax, ebx, ecx, edx);
209
 
 
210
 
    if (eax < 0x80000001)
211
 
      return caps;
212
 
 
213
 
#ifdef USE_SSE
214
 
    cpuid (0x80000001, eax, ebx, ecx, edx);
215
 
 
216
 
    if (edx & ARCH_X86_AMD_FEATURE_3DNOW)
217
 
      caps |= CPU_ACCEL_X86_3DNOW;
218
 
 
219
 
    if (edx & ARCH_X86_AMD_FEATURE_MMXEXT)
220
 
      caps |= CPU_ACCEL_X86_MMXEXT;
221
 
#endif /* USE_SSE */
222
 
  }
223
 
#endif /* USE_MMX */
224
 
 
225
 
  return caps;
226
 
}
227
 
 
228
 
static guint32
229
 
arch_accel_centaur (void)
230
 
{
231
 
  guint32 caps;
232
 
 
233
 
  caps = arch_accel_intel ();
234
 
 
235
 
#ifdef USE_MMX
236
 
  {
237
 
    guint32 eax, ebx, ecx, edx;
238
 
 
239
 
    cpuid (0x80000000, eax, ebx, ecx, edx);
240
 
 
241
 
    if (eax < 0x80000001)
242
 
      return caps;
243
 
 
244
 
    cpuid (0x80000001, eax, ebx, ecx, edx);
245
 
 
246
 
    if (edx & ARCH_X86_CENTAUR_FEATURE_MMX)
247
 
      caps |= CPU_ACCEL_X86_MMX;
248
 
 
249
 
#ifdef USE_SSE
250
 
    if (edx & ARCH_X86_CENTAUR_FEATURE_3DNOW)
251
 
      caps |= CPU_ACCEL_X86_3DNOW;
252
 
 
253
 
    if (edx & ARCH_X86_CENTAUR_FEATURE_MMXEXT)
254
 
      caps |= CPU_ACCEL_X86_MMXEXT;
255
 
#endif /* USE_SSE */
256
 
  }
257
 
#endif /* USE_MMX */
258
 
 
259
 
  return caps;
260
 
}
261
 
 
262
 
static guint32
263
 
arch_accel_cyrix (void)
264
 
{
265
 
  guint32 caps;
266
 
 
267
 
  caps = arch_accel_intel ();
268
 
 
269
 
#ifdef USE_MMX
270
 
  {
271
 
    guint32 eax, ebx, ecx, edx;
272
 
 
273
 
    cpuid (0, eax, ebx, ecx, edx);
274
 
 
275
 
    if (eax != 2)
276
 
      return caps;
277
 
 
278
 
    cpuid (0x80000001, eax, ebx, ecx, edx);
279
 
 
280
 
    if (edx & ARCH_X86_CYRIX_FEATURE_MMX)
281
 
      caps |= CPU_ACCEL_X86_MMX;
282
 
 
283
 
#ifdef USE_SSE
284
 
    if (edx & ARCH_X86_CYRIX_FEATURE_MMXEXT)
285
 
      caps |= CPU_ACCEL_X86_MMXEXT;
286
 
#endif /* USE_SSE */
287
 
  }
288
 
#endif /* USE_MMX */
289
 
 
290
 
  return caps;
291
 
}
292
 
 
293
 
#ifdef USE_SSE
294
 
static jmp_buf sigill_return;
295
 
 
296
 
static void
297
 
sigill_handler (gint n)
298
 
{
299
 
  longjmp (sigill_return, 1);
300
 
}
301
 
 
302
 
static gboolean
303
 
arch_accel_sse_os_support (void)
304
 
{
305
 
  if (setjmp (sigill_return))
306
 
    {
307
 
      return FALSE;
308
 
    }
309
 
  else
310
 
    {
311
 
      signal (SIGILL, sigill_handler);
312
 
      __asm__ __volatile__ ("xorps %xmm0, %xmm0");
313
 
      signal (SIGILL, SIG_DFL);
314
 
    }
315
 
 
316
 
  return TRUE;
317
 
}
318
 
#endif /* USE_SSE */
319
 
 
320
 
static guint32
321
 
arch_accel (void)
322
 
{
323
 
  guint32 caps;
324
 
  X86Vendor vendor;
325
 
 
326
 
  vendor = arch_get_vendor ();
327
 
 
328
 
  switch (vendor)
329
 
    {
330
 
    case ARCH_X86_VENDOR_NONE:
331
 
      caps = 0;
332
 
      break;
333
 
 
334
 
    case ARCH_X86_VENDOR_AMD:
335
 
      caps = arch_accel_amd ();
336
 
      break;
337
 
 
338
 
    case ARCH_X86_VENDOR_CENTAUR:
339
 
      caps = arch_accel_centaur ();
340
 
      break;
341
 
 
342
 
    case ARCH_X86_VENDOR_CYRIX:
343
 
    case ARCH_X86_VENDOR_NSC:
344
 
      caps = arch_accel_cyrix ();
345
 
      break;
346
 
 
347
 
    /* check for what Intel speced, even if UNKNOWN */
348
 
    default:
349
 
      caps = arch_accel_intel ();
350
 
      break;
351
 
    }
352
 
 
353
 
#ifdef USE_SSE
354
 
  if ((caps & CPU_ACCEL_X86_SSE) && !arch_accel_sse_os_support ())
355
 
    caps &= ~(CPU_ACCEL_X86_SSE | CPU_ACCEL_X86_SSE2);
356
 
#endif
357
 
 
358
 
  return caps;
359
 
}
360
 
 
361
 
#endif /* ARCH_X86 && USE_MMX && __GNUC__ */
362
 
 
363
 
 
364
 
#if defined (ARCH_PPC) && defined (USE_ALTIVEC) && defined(__GNUC__)
365
 
 
366
 
#define HAVE_ACCEL 1
367
 
 
368
 
static          sigjmp_buf   jmpbuf;
369
 
static volatile sig_atomic_t canjump = 0;
370
 
 
371
 
static void
372
 
sigill_handler (gint sig)
373
 
{
374
 
  if (!canjump)
375
 
    {
376
 
      signal (sig, SIG_DFL);
377
 
      raise (sig);
378
 
    }
379
 
 
380
 
  canjump = 0;
381
 
  siglongjmp (jmpbuf, 1);
382
 
}
383
 
 
384
 
static guint32
385
 
arch_accel (void)
386
 
{
387
 
  signal (SIGILL, sigill_handler);
388
 
  
389
 
  if (sigsetjmp (jmpbuf, 1))
390
 
    {
391
 
      signal (SIGILL, SIG_DFL);
392
 
      return 0;
393
 
    }
394
 
 
395
 
  canjump = 1;
396
 
 
397
 
  asm volatile ("mtspr 256, %0\n\t"
398
 
                "vand %%v0, %%v0, %%v0"
399
 
                :
400
 
                : "r" (-1));
401
 
 
402
 
  signal (SIGILL, SIG_DFL);
403
 
 
404
 
  return CPU_ACCEL_PPC_ALTIVEC;
405
 
}
406
 
 
407
 
#endif /* ARCH_PPC && USE_ALTIVEC && __GNUC__ */
408
 
 
409
 
 
410
 
guint32
411
 
cpu_accel (void)
412
 
{
413
 
#ifdef HAVE_ACCEL
414
 
  static guint32 accel = ~0U;
415
 
 
416
 
  if (accel != ~0U)
417
 
    return accel;
418
 
 
419
 
  accel = arch_accel ();
420
 
 
421
 
  return accel;
422
 
 
423
 
#else /* !HAVE_ACCEL */
424
 
  return 0;
425
 
#endif
426
 
}
427
 
 
428
 
void
429
 
cpu_accel_print_results (void)
430
 
{
431
 
  g_printerr ("Testing CPU features...\n");
432
 
 
433
 
#ifdef ARCH_X86
434
 
  g_printerr ("  mmx     : %s\n",
435
 
              (cpu_accel() & CPU_ACCEL_X86_MMX)     ? "yes" : "no");
436
 
  g_printerr ("  3dnow   : %s\n",
437
 
              (cpu_accel() & CPU_ACCEL_X86_3DNOW)   ? "yes" : "no");
438
 
  g_printerr ("  mmxext  : %s\n",
439
 
              (cpu_accel() & CPU_ACCEL_X86_MMXEXT)  ? "yes" : "no");
440
 
  g_printerr ("  sse     : %s\n",
441
 
              (cpu_accel() & CPU_ACCEL_X86_SSE)     ? "yes" : "no");
442
 
  g_printerr ("  sse2    : %s\n",
443
 
              (cpu_accel() & CPU_ACCEL_X86_SSE2)    ? "yes" : "no");
444
 
#endif
445
 
#ifdef ARCH_PPC
446
 
  g_printerr ("  altivec : %s\n",
447
 
              (cpu_accel() & CPU_ACCEL_PPC_ALTIVEC) ? "yes" : "no");
448
 
#endif
449
 
  g_printerr ("\n");
450
 
}