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