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
30 #include "pbd/error.h"
39 unsigned long cpuflags = 0;
43 #if !( (defined __x86_64__) || (defined __i386__) ) // !ARCH_X86
47 #ifdef PLATFORM_WINDOWS
49 #ifndef USE_X86_64_ASM
50 /* no 32 bit version of assembler for windows */
53 // Get CPU flags using Microsoft function
54 // It works for both 64 and 32 bit systems
55 // no need to use assembler for getting info from register, this function does this for us
58 cpuflags = cpuInfo[3];
63 #ifndef _LP64 /* *nix; 32 bit version. This odd macro constant is required because we need something that identifies this as a 32 bit
64 build on Linux and on OS X. Anything that serves this purpose will do, but this is the best thing we've identified
76 : "%eax", "%ecx", "%edx"
79 #else /* *nix; 64 bit version */
81 /* asm notes: although we explicitly save&restore rbx, we must tell
82 gcc that ebx,rbx is clobbered so that it doesn't try to use it as an intermediate
83 register when storing rbx. gcc 4.3 didn't make this "mistake", but gcc 4.4
84 does, at least on x86_64.
95 : "%rax", "%rbx", "%rcx", "%rdx"
98 #endif /* USE_X86_64_ASM */
99 #endif /* PLATFORM_WINDOWS */
101 if (cpuflags & (1<<25)) {
102 _flags = Flags (_flags | (HasSSE|HasFlushToZero));
105 if (cpuflags & (1<<26)) {
106 _flags = Flags (_flags | HasSSE2);
109 if (cpuflags & (1 << 24)) {
113 /* DAZ wasn't available in the first version of SSE. Since
114 setting a reserved bit in MXCSR causes a general protection
115 fault, we need to be able to check the availability of this
116 feature without causing problems. To do this, one needs to
117 set up a 512-byte area of memory to save the SSE state to,
118 using fxsave, and then one needs to inspect bytes 28 through
119 31 for the MXCSR_MASK value. If bit 6 is set, DAZ is
120 supported, otherwise, it isn't.
123 #ifndef HAVE_POSIX_MEMALIGN
124 fxbuf = (char **) malloc (sizeof (char *));
126 *fxbuf = (char *) malloc (512);
129 (void) posix_memalign ((void **) &fxbuf, 16, sizeof (char *));
131 (void) posix_memalign ((void **) fxbuf, 16, 512);
135 memset (*fxbuf, 0, 512);
151 uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
153 /* if the mask is zero, set its default value (from intel specs) */
155 if (mxcsr_mask == 0) {
159 if (mxcsr_mask & (1<<6)) {
160 _flags = Flags (_flags | HasDenormalsAreZero);