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