0998f43bdcd58f20097be260f7d90cce4130594b
[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 #if !(defined (COMPILER_MSVC) || defined (COMPILER_MINGW))
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 #include "pbd/fpu.h"
29 #include "pbd/error.h"
30
31 #include "i18n.h"
32
33 using namespace PBD;
34 using namespace std;
35
36 FPU::FPU ()
37 {
38         unsigned long cpuflags = 0;
39
40         _flags = Flags (0);
41
42 #if !( (defined __x86_64__) || (defined __i386__) ) // !ARCH_X86
43         return;
44 #else
45
46 #ifndef _LP64 //USE_X86_64_ASM
47         asm volatile (
48                 "mov $1, %%eax\n"
49                 "pushl %%ebx\n"
50                 "cpuid\n"
51                 "movl %%edx, %0\n"
52                 "popl %%ebx\n"
53                 : "=r" (cpuflags)
54                 : 
55                 : "%eax", "%ecx", "%edx"
56                 );
57         
58 #else
59         
60         /* asm notes: although we explicitly save&restore rbx, we must tell
61            gcc that ebx,rbx is clobbered so that it doesn't try to use it as an intermediate
62            register when storing rbx. gcc 4.3 didn't make this "mistake", but gcc 4.4
63            does, at least on x86_64.
64         */
65
66         asm volatile (
67                 "pushq %%rbx\n"
68                 "movq $1, %%rax\n"
69                 "cpuid\n"
70                 "movq %%rdx, %0\n"
71                 "popq %%rbx\n"
72                 : "=r" (cpuflags)
73                 : 
74                 : "%rax", "%rbx", "%rcx", "%rdx"
75                 );
76
77 #endif /* USE_X86_64_ASM */
78
79         if (cpuflags & (1<<25)) {
80                 _flags = Flags (_flags | (HasSSE|HasFlushToZero));
81         }
82
83         if (cpuflags & (1<<26)) {
84                 _flags = Flags (_flags | HasSSE2);
85         }
86
87         if (cpuflags & (1 << 24)) {
88                 
89                 char** fxbuf = 0;
90                 
91                 /* DAZ wasn't available in the first version of SSE. Since
92                    setting a reserved bit in MXCSR causes a general protection
93                    fault, we need to be able to check the availability of this
94                    feature without causing problems. To do this, one needs to
95                    set up a 512-byte area of memory to save the SSE state to,
96                    using fxsave, and then one needs to inspect bytes 28 through
97                    31 for the MXCSR_MASK value. If bit 6 is set, DAZ is
98                    supported, otherwise, it isn't.
99                 */
100                 
101 #ifndef HAVE_POSIX_MEMALIGN
102                 fxbuf = (char **) malloc (sizeof (char *));
103                 assert (fxbuf);
104                 *fxbuf = (char *) malloc (512);
105                 assert (*fxbuf);
106 #else
107                 (void) posix_memalign ((void **) &fxbuf, 16, sizeof (char *));
108                 assert (fxbuf);
109                 (void) posix_memalign ((void **) fxbuf, 16, 512);
110                 assert (*fxbuf);
111 #endif                  
112                 
113                 memset (*fxbuf, 0, 512);
114                 
115                 asm volatile (
116                         "fxsave (%0)"
117                         :
118                         : "r" (*fxbuf)
119                         : "memory"
120                         );
121                 
122                 uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
123                 
124                 /* if the mask is zero, set its default value (from intel specs) */
125                 
126                 if (mxcsr_mask == 0) {
127                         mxcsr_mask = 0xffbf;
128                 }
129                 
130                 if (mxcsr_mask & (1<<6)) {
131                         _flags = Flags (_flags | HasDenormalsAreZero);
132                 } 
133                 
134                 free (*fxbuf);
135                 free (fxbuf);
136         }
137 #endif
138 }                       
139
140 FPU::~FPU ()
141 {
142 }
143
144 #else  // COMPILER_MSVC
145         const char* pbd_fpu = "pbd/msvc/fpu.cc takes precedence over this file";
146 #endif // COMPILER_MSVC