can't use xgetbv instruction with apple gcc under OS X Lion
[ardour.git] / libs / pbd / fpu.cc
1 /*
2     Copyright (C) 2012 Paul Davis 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "libpbd-config.h"
21
22 #define _XOPEN_SOURCE 600
23 #include <cstring> // for memset
24 #include <cstdlib>
25 #include <stdint.h>
26 #include <assert.h>
27
28 #ifdef PLATFORM_WINDOWS
29 #include <intrin.h>
30 #endif
31
32 #include "pbd/compose.h"
33 #include "pbd/fpu.h"
34 #include "pbd/error.h"
35
36 #include "i18n.h"
37
38 using namespace PBD;
39 using namespace std;
40
41 FPU* FPU::_instance (0);
42
43 #ifndef COMPILER_MSVC
44
45 /* use __cpuid() as the name to match the MSVC intrinsic */
46
47 static void
48 __cpuid(int regs[4], int cpuid_leaf)
49 {
50         int eax, ebx, ecx, edx;
51         asm volatile (
52 #if defined(__i386__)
53                 "pushl %%ebx;\n\t"
54 #endif
55                 "movl %4, %%eax;\n\t"
56                 "cpuid;\n\t"
57                 "movl %%eax, %0;\n\t"
58                 "movl %%ebx, %1;\n\t"
59                 "movl %%ecx, %2;\n\t"
60                 "movl %%edx, %3;\n\t"
61 #if defined(__i386__)
62                 "popl %%ebx;\n\t"
63 #endif
64                 :"=m" (eax), "=m" (ebx), "=m" (ecx), "=m" (edx)
65                 :"r" (cpuid_leaf)
66                 :"%eax",
67 #if !defined(__i386__)
68                  "%ebx",
69 #endif
70                  "%ecx", "%edx");
71         
72         regs[0] = eax;
73         regs[1] = ebx;
74         regs[2] = ecx;
75         regs[3] = edx;
76 }
77
78 static uint64_t
79 _xgetbv (uint32_t xcr)
80 {
81 #ifdef __APPLE__
82         /* it would be nice to make this work on OS X but as long we use veclib,
83            we don't really need to know about SSE/AVX on that platform.
84         */
85         return 0;
86 #else
87         uint32_t eax, edx;
88         __asm__ volatile ("xgetbv" : "=a" (eax), "=d" (edx) : "c" (xcr));
89         return (static_cast<uint64_t>(edx) << 32) | eax;
90 #endif
91 }
92
93 #define _XCR_XFEATURE_ENABLED_MASK 0
94
95 #endif /* !COMPILER_MSVC */
96
97 FPU*
98 FPU::instance()
99 {
100         if (!_instance) {
101                 _instance = new FPU;
102         }
103
104         return _instance;
105 }
106
107 FPU::FPU ()
108         : _flags ((Flags) 0)
109 {
110         if (_instance) {
111                 error << _("FPU object instantiated more than once") << endmsg;
112         }
113
114 #if !( (defined __x86_64__) || (defined __i386__) || (defined _M_X64) || (defined _M_IX86) ) // !ARCH_X86
115         /* Non-Intel architecture, nothing to do here */
116         return;
117 #else
118
119         /* Get the CPU vendor just for kicks */
120
121         // __cpuid with an InfoType argument of 0 returns the number of
122         // valid Ids in CPUInfo[0] and the CPU identification string in
123         // the other three array elements. The CPU identification string is
124         // not in linear order. The code below arranges the information
125         // in a human readable form. The human readable order is CPUInfo[1] |
126         // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
127         // before using memcpy to copy these three array elements to cpu_string.
128
129         int cpu_info[4];
130         char cpu_string[48];
131         string cpu_vendor;
132
133         __cpuid (cpu_info, 0);
134
135         int num_ids = cpu_info[0];
136         std::swap(cpu_info[2], cpu_info[3]);
137         memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
138         cpu_vendor.assign(cpu_string, 3 * sizeof(cpu_info[1]));
139
140         info << string_compose (_("CPU vendor: %1"), cpu_vendor) << endmsg;
141
142         if (num_ids > 0) {
143         
144                 /* Now get CPU/FPU flags */
145         
146                 __cpuid (cpu_info, 1);
147
148                 if ((cpu_info[2] & (1<<27)) /* AVX */ &&
149                     (cpu_info[2] & (1<<28) /* (OS)XSAVE */) &&
150                     (_xgetbv (_XCR_XFEATURE_ENABLED_MASK) & 0x6)) { /* OS really supports XSAVE */
151                         info << _("AVX-capable processor") << endmsg;
152                         _flags = Flags (_flags | (HasAVX) );
153                 }
154
155                 if (cpu_info[3] & (1<<25)) {
156                         _flags = Flags (_flags | (HasSSE|HasFlushToZero));
157                 }
158
159                 if (cpu_info[3] & (1<<26)) {
160                         _flags = Flags (_flags | HasSSE2);
161                 }
162
163                 /* Figure out CPU/FPU denormal handling capabilities */
164         
165                 if (cpu_info[3] & (1 << 24)) {
166                 
167                         char** fxbuf = 0;
168                 
169                         /* DAZ wasn't available in the first version of SSE. Since
170                            setting a reserved bit in MXCSR causes a general protection
171                            fault, we need to be able to check the availability of this
172                            feature without causing problems. To do this, one needs to
173                            set up a 512-byte area of memory to save the SSE state to,
174                            using fxsave, and then one needs to inspect bytes 28 through
175                            31 for the MXCSR_MASK value. If bit 6 is set, DAZ is
176                            supported, otherwise, it isn't.
177                         */
178
179 #ifndef HAVE_POSIX_MEMALIGN
180 #  ifdef PLATFORM_WINDOWS
181                         fxbuf = (char **) _aligned_malloc (sizeof (char *), 16);
182                         assert (fxbuf);
183                         *fxbuf = (char *) _aligned_malloc (512, 16);
184                         assert (*fxbuf);
185 #  else
186 #  warning using default malloc for aligned memory
187                         fxbuf = (char **) malloc (sizeof (char *));
188                         assert (fxbuf);
189                         *fxbuf = (char *) malloc (512);
190                         assert (*fxbuf);
191 #  endif
192 #else
193                         (void) posix_memalign ((void **) &fxbuf, 16, sizeof (char *));
194                         assert (fxbuf);
195                         (void) posix_memalign ((void **) fxbuf, 16, 512);
196                         assert (*fxbuf);
197 #endif                  
198                 
199                         memset (*fxbuf, 0, 512);
200                 
201 #ifdef COMPILER_MSVC
202                         char *buf = *fxbuf;
203                         __asm {
204                                 mov eax, buf
205                                         fxsave   [eax]
206                                         };
207 #else
208                         asm volatile (
209                                 "fxsave (%0)"
210                                 :
211                                 : "r" (*fxbuf)
212                                 : "memory"
213                                 );
214 #endif
215                 
216                         uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
217                 
218                         /* if the mask is zero, set its default value (from intel specs) */
219                 
220                         if (mxcsr_mask == 0) {
221                                 mxcsr_mask = 0xffbf;
222                         }
223                 
224                         if (mxcsr_mask & (1<<6)) {
225                                 _flags = Flags (_flags | HasDenormalsAreZero);
226                         } 
227                 
228 #if !defined HAVE_POSIX_MEMALIGN && defined PLATFORM_WINDOWS
229                         _aligned_free (*fxbuf);
230                         _aligned_free (fxbuf);
231 #else
232                         free (*fxbuf);
233                         free (fxbuf);
234 #endif
235                 }
236 #endif
237
238                 /* finally get the CPU brand */
239
240                 __cpuid (cpu_info, 0x80000000);
241
242                 const int parameter_end = 0x80000004;
243                 string cpu_brand;
244         
245                 if (cpu_info[0] >= parameter_end) {
246                         char* cpu_string_ptr = cpu_string;
247                 
248                         for (int parameter = 0x80000002; parameter <= parameter_end &&
249                                      cpu_string_ptr < &cpu_string[sizeof(cpu_string)]; parameter++) {
250                                 __cpuid(cpu_info, parameter);
251                                 memcpy(cpu_string_ptr, cpu_info, sizeof(cpu_info));
252                                 cpu_string_ptr += sizeof(cpu_info);
253                         }
254                         cpu_brand.assign(cpu_string, cpu_string_ptr - cpu_string);
255                         info << string_compose (_("CPU brand: %1"), cpu_brand) << endmsg;
256                 } 
257         }
258 }                       
259
260 FPU::~FPU ()
261 {
262 }