2 Copyright (C) 2012 Paul Davis
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.
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.
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.
20 #include "libpbd-config.h"
22 #define _XOPEN_SOURCE 600
23 #include <cstring> // for memset
28 #ifdef PLATFORM_WINDOWS
33 #include "pbd/error.h"
42 unsigned long cpuflags = 0;
46 #if !( (defined __x86_64__) || (defined __i386__) || (defined _M_X64) || (defined _M_IX86) ) // !ARCH_X86
50 #ifdef PLATFORM_WINDOWS
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
57 cpuflags = cpuInfo[3];
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
74 : "%eax", "%ecx", "%edx"
77 #else /* *nix; 64 bit version */
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.
93 : "%rax", "%rbx", "%rcx", "%rdx"
97 #endif /* PLATFORM_WINDOWS */
100 if (cpuflags & ((1<<27) /* AVX */ |(1<<28) /* XGETBV */)) {
102 std::cerr << "Looks like AVX\n";
104 /* now check if YMM resters state is saved: which means OS does
105 * know about new YMM registers and saves them during context
106 * switches it's true for most cases, but we must be sure
108 * giving 0 as the argument to _xgetbv() fetches the
109 * XCR_XFEATURE_ENABLED_MASK, which we need to check for
110 * the 2nd and 3rd bits, indicating correct register save/restore.
113 uint64_t xcrFeatureMask = 0;
115 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 4
116 unsigned int eax, edx, index = 0;
117 asm volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
118 xcrFeatureMask = ((unsigned long long)edx << 32) | eax;
119 #elif defined (COMPILER_MSVC)
120 xcrFeatureMask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
122 if (xcrFeatureMask & 0x6) {
123 std::cerr << "Definitely AVX\n";
124 _flags = Flags (_flags | (HasAVX) );
127 #endif /* !__APPLE__ */
129 if (cpuflags & (1<<25)) {
130 _flags = Flags (_flags | (HasSSE|HasFlushToZero));
133 if (cpuflags & (1<<26)) {
134 _flags = Flags (_flags | HasSSE2);
137 if (cpuflags & (1 << 24)) {
141 /* DAZ wasn't available in the first version of SSE. Since
142 setting a reserved bit in MXCSR causes a general protection
143 fault, we need to be able to check the availability of this
144 feature without causing problems. To do this, one needs to
145 set up a 512-byte area of memory to save the SSE state to,
146 using fxsave, and then one needs to inspect bytes 28 through
147 31 for the MXCSR_MASK value. If bit 6 is set, DAZ is
148 supported, otherwise, it isn't.
151 #ifndef HAVE_POSIX_MEMALIGN
152 # ifdef PLATFORM_WINDOWS
153 fxbuf = (char **) _aligned_malloc (sizeof (char *), 16);
155 *fxbuf = (char *) _aligned_malloc (512, 16);
158 # warning using default malloc for aligned memory
159 fxbuf = (char **) malloc (sizeof (char *));
161 *fxbuf = (char *) malloc (512);
165 (void) posix_memalign ((void **) &fxbuf, 16, sizeof (char *));
167 (void) posix_memalign ((void **) fxbuf, 16, 512);
171 memset (*fxbuf, 0, 512);
188 uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
190 /* if the mask is zero, set its default value (from intel specs) */
192 if (mxcsr_mask == 0) {
196 if (mxcsr_mask & (1<<6)) {
197 _flags = Flags (_flags | HasDenormalsAreZero);
200 #if !defined HAVE_POSIX_MEMALIGN && defined PLATFORM_WINDOWS
201 _aligned_free (*fxbuf);
202 _aligned_free (fxbuf);