source: trunk/libdjvu/MMX.cpp @ 426

Last change on this file since 426 was 280, checked in by rbri, 12 years ago

DJVU plugin: djvulibre updated to version 3.5.22

File size: 7.0 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, either Version 2 of the license,
9//C- or (at your option) any later version. The license should have
10//C- accompanied the software or you may obtain a copy of the license
11//C- from the Free Software Foundation at http://www.fsf.org .
12//C-
13//C- This program is distributed in the hope that it will be useful,
14//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//C- GNU General Public License for more details.
17//C-
18//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19//C- Lizardtech Software.  Lizardtech Software has authorized us to
20//C- replace the original DjVu(r) Reference Library notice by the following
21//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22//C-
23//C-  ------------------------------------------------------------------
24//C- | DjVu (r) Reference Library (v. 3.5)
25//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26//C- | The DjVu Reference Library is protected by U.S. Pat. No.
27//C- | 6,058,214 and patents pending.
28//C- |
29//C- | This software is subject to, and may be distributed under, the
30//C- | GNU General Public License, either Version 2 of the license,
31//C- | or (at your option) any later version. The license should have
32//C- | accompanied the software or you may obtain a copy of the license
33//C- | from the Free Software Foundation at http://www.fsf.org .
34//C- |
35//C- | The computer code originally released by LizardTech under this
36//C- | license and unmodified by other parties is deemed "the LIZARDTECH
37//C- | ORIGINAL CODE."  Subject to any third party intellectual property
38//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39//C- | non-exclusive license to make, use, sell, or otherwise dispose of
40//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42//C- | General Public License.   This grant only confers the right to
43//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44//C- | the extent such infringement is reasonably necessary to enable
45//C- | recipient to make, have made, practice, sell, or otherwise dispose
46//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47//C- | any greater extent that may be necessary to utilize further
48//C- | modifications or combinations.
49//C- |
50//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54//C- +------------------------------------------------------------------
55//
56// $Id: MMX.cpp,v 1.12 2007/03/25 20:48:33 leonb Exp $
57// $Name: release_3_5_22 $
58
59#ifdef HAVE_CONFIG_H
60# include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma implementation
64#endif
65
66#include "MMX.h"
67#include <stdio.h>
68#include <stdlib.h>
69
70
71#ifdef HAVE_NAMESPACES
72namespace DJVU {
73# ifdef NOT_DEFINED // Just to fool emacs c++ mode
74}
75#endif
76#endif
77
78
79// ----------------------------------------
80// PRINTING MMX REGISTERS (Debug)
81
82
83#if defined(MMX) && defined(DEBUG)
84extern "C" void
85mmx_show()
86{
87  /* This function can be called from a debugger
88     in order to visualize the contents of the MMX registers. */
89  int mmregs[16];
90  MMXra( movq,  mm0, &mmregs[0]);
91  MMXra( movq,  mm1, &mmregs[2]);
92  MMXra( movq,  mm2, &mmregs[4]);
93  MMXra( movq,  mm3, &mmregs[6]);
94  MMXra( movq,  mm4, &mmregs[8]);
95  MMXra( movq,  mm5, &mmregs[10]);
96  MMXra( movq,  mm6, &mmregs[12]);
97  MMXra( movq,  mm7, &mmregs[14]);
98  MMXemms;
99  for (int i=0; i<8; i++)
100    DjVuPrintMessageUTF8("mm%d: %08x%08x\n", i, 
101           mmregs[i+i+1], mmregs[i+i]);
102  MMXar( movq,  &mmregs[0], mm0);
103  MMXar( movq,  &mmregs[2], mm1);
104  MMXar( movq,  &mmregs[4], mm2);
105  MMXar( movq,  &mmregs[6], mm3);
106  MMXar( movq,  &mmregs[8], mm4);
107  MMXar( movq,  &mmregs[10], mm5);
108  MMXar( movq,  &mmregs[12], mm6);
109  MMXar( movq,  &mmregs[14], mm7);
110}
111#endif
112
113
114
115// ----------------------------------------
116// MMX ENABLE/DISABLE
117
118// Default settings autodetect MMX.
119// Use macro DISABLE_MMX to disable MMX by default.
120
121#if defined(MMX) && !defined(DISABLE_MMX)
122int MMXControl::mmxflag = -1;
123#else
124int MMXControl::mmxflag = 0;
125#endif
126
127int 
128MMXControl::disable_mmx()
129{
130  mmxflag = 0;
131  return mmxflag;
132}
133
134int 
135MMXControl::enable_mmx()
136{
137  int cpuflags = 0;
138  const char *envvar = getenv("LIBDJVU_DISABLE_MMX");
139  if (envvar && envvar[0] && envvar[0]!='0')
140    return ((mmxflag = 0));
141 
142#if defined(MMX) && defined(__GNUC__) && defined(__i386__)
143  // Detection of MMX for GCC
144  __asm__ volatile ("pushl %%ebx\n\t"
145                    "pushfl\n\t"   
146                    "popl %%ecx\n\t"
147                    "xorl %%edx,%%edx\n\t"
148                    // Check that CPUID exists
149                    "movl %%ecx,%%eax\n\t"
150                    "xorl $0x200000,%%eax\n\t"
151                    "pushl %%eax\n\t"
152                    "popfl\n\t"
153                    "pushfl\n\t"
154                    "popl %%eax\n\t"
155                    "xorl %%ecx,%%eax\n\t"
156                    "jz 1f\n\t"
157                    "pushl %%ecx\n\t"
158                    "popfl\n\t"
159                    // Check that CR0:EM is clear
160                    "smsw %%ax\n\t"
161                    "andl $4,%%eax\n\t"
162                    "jnz 1f\n\t"
163                    // Execute CPUID
164                    "movl $1,%%eax\n\t"
165                    "cpuid\n"
166                    // EBX contains magic when -fPIC is on.
167                    "1:\tpopl %%ebx\n\t"
168                    "movl %%edx, %0"
169                    : "=m" (cpuflags) :
170                    : "eax","ecx","edx");
171#endif
172#if defined(MMX) && defined(_MSC_VER) && defined(_M_IX86)
173  // Detection of MMX for MSVC
174  __asm {  pushfd
175           pop     ecx
176           xor     edx,edx
177             ;// Check that CPUID exists
178           mov     eax,ecx       
179           xor     eax,0x200000
180           push    eax
181           popfd
182           pushfd
183           pop     eax
184           xor     eax,ecx
185           jz      fini
186           push    ecx
187           popfd
188             ;// Check that CR0:EM is zero
189           smsw    ax
190           and     eax,4
191           jnz     fini
192             ;// Execute CPUID
193           mov     eax,1
194           _emit   0xf
195           _emit   0xa2
196         fini:
197           mov     cpuflags,edx
198             ;// MSVC determines clobbered registers by scanning the assembly code.
199             ;// Since it does not know CPUID, it would not know that EBX is clobbered
200             ;// without the dummy instruction below...
201           xor     ebx,ebx
202         }
203#endif
204  mmxflag = !!(cpuflags & 0x800000);
205  return mmxflag;
206}
207
208
209
210#ifdef HAVE_NAMESPACES
211}
212# ifndef NOT_USING_DJVU_NAMESPACE
213using namespace DJVU;
214# endif
215#endif
Note: See TracBrowser for help on using the repository browser.