a6663d22b4fe211f535e5198b9c1f44c261a35e5
[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 <intrin.h>
27 #include <assert.h>
28
29 #include "pbd/fpu.h"
30 #include "pbd/error.h"
31
32 #include "i18n.h"
33
34 using namespace PBD;
35 using namespace std;
36
37 FPU::FPU ()
38 {
39         unsigned long cpuflags = 0;
40
41         _flags = Flags (0);
42
43 #if !( (defined __x86_64__) || (defined __i386__) ) // !ARCH_X86
44         return;
45 #else
46
47 #ifdef PLATFORM_WINDOWS
48
49 #ifndef USE_X86_64_ASM
50         /* no 32 bit version of assembler for windows */
51         return;
52 #else
53         // Get CPU flags using Microsoft function
54         // It works for both 64 and 32 bit systems
55         // no need to use assembler for getting info from register, this function does this for us
56         int cpuInfo[4];
57         __cpuid (cpuInfo, 1);
58         cpuflags = cpuInfo[3];
59 #endif
60
61 #else   
62
63 #ifndef _LP64 /* *nix; 32 bit version. This odd macro constant is required because we need something that identifies this as a 32 bit
64                  build on Linux and on OS X. Anything that serves this purpose will do, but this is the best thing we've identified
65                  so far.
66               */
67         
68         asm volatile (
69                 "mov $1, %%eax\n"
70                 "pushl %%ebx\n"
71                 "cpuid\n"
72                 "movl %%edx, %0\n"
73                 "popl %%ebx\n"
74                 : "=r" (cpuflags)
75                 : 
76                 : "%eax", "%ecx", "%edx"
77                 );
78         
79 #else /* *nix; 64 bit version */
80         
81         /* asm notes: although we explicitly save&restore rbx, we must tell
82            gcc that ebx,rbx is clobbered so that it doesn't try to use it as an intermediate
83            register when storing rbx. gcc 4.3 didn't make this "mistake", but gcc 4.4
84            does, at least on x86_64.
85         */
86
87         asm volatile (
88                 "pushq %%rbx\n"
89                 "movq $1, %%rax\n"
90                 "cpuid\n"
91                 "movq %%rdx, %0\n"
92                 "popq %%rbx\n"
93                 : "=r" (cpuflags)
94                 : 
95                 : "%rax", "%rbx", "%rcx", "%rdx"
96                 );
97
98 #endif /* USE_X86_64_ASM */
99 #endif /* PLATFORM_WINDOWS */
100
101         if (cpuflags & (1<<25)) {
102                 _flags = Flags (_flags | (HasSSE|HasFlushToZero));
103         }
104
105         if (cpuflags & (1<<26)) {
106                 _flags = Flags (_flags | HasSSE2);
107         }
108
109         if (cpuflags & (1 << 24)) {
110                 
111                 char** fxbuf = 0;
112                 
113                 /* DAZ wasn't available in the first version of SSE. Since
114                    setting a reserved bit in MXCSR causes a general protection
115                    fault, we need to be able to check the availability of this
116                    feature without causing problems. To do this, one needs to
117                    set up a 512-byte area of memory to save the SSE state to,
118                    using fxsave, and then one needs to inspect bytes 28 through
119                    31 for the MXCSR_MASK value. If bit 6 is set, DAZ is
120                    supported, otherwise, it isn't.
121                 */
122                 
123 #ifndef HAVE_POSIX_MEMALIGN
124                 fxbuf = (char **) malloc (sizeof (char *));
125                 assert (fxbuf);
126                 *fxbuf = (char *) malloc (512);
127                 assert (*fxbuf);
128 #else
129                 (void) posix_memalign ((void **) &fxbuf, 16, sizeof (char *));
130                 assert (fxbuf);
131                 (void) posix_memalign ((void **) fxbuf, 16, 512);
132                 assert (*fxbuf);
133 #endif                  
134                 
135                 memset (*fxbuf, 0, 512);
136                 
137 #ifdef COMPILER_MSVC
138                 __asm {
139                         mov eax, fxbuf
140                         fxsave   [eax]
141                };
142 #else
143                 asm volatile (
144                         "fxsave (%0)"
145                         :
146                         : "r" (*fxbuf)
147                         : "memory"
148                         );
149 #endif
150                 
151                 uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
152                 
153                 /* if the mask is zero, set its default value (from intel specs) */
154                 
155                 if (mxcsr_mask == 0) {
156                         mxcsr_mask = 0xffbf;
157                 }
158                 
159                 if (mxcsr_mask & (1<<6)) {
160                         _flags = Flags (_flags | HasDenormalsAreZero);
161                 } 
162                 
163                 free (*fxbuf);
164                 free (fxbuf);
165         }
166 #endif
167 }                       
168
169 FPU::~FPU ()
170 {
171 }