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