2013-01-16 13:15:38 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2012 Paul Davis
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
2015-04-21 11:39:19 -04:00
|
|
|
|
2010-11-10 18:13:29 -05:00
|
|
|
#include "libpbd-config.h"
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
#define _XOPEN_SOURCE 600
|
2009-12-31 12:51:27 -05:00
|
|
|
#include <cstring> // for memset
|
2008-09-10 11:03:30 -04:00
|
|
|
#include <cstdlib>
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <stdint.h>
|
2012-05-16 18:43:23 -04:00
|
|
|
#include <assert.h>
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2015-04-21 12:31:02 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/fpu.h"
|
|
|
|
#include "pbd/error.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
using namespace PBD;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
FPU::FPU ()
|
|
|
|
{
|
|
|
|
unsigned long cpuflags = 0;
|
|
|
|
|
|
|
|
_flags = Flags (0);
|
|
|
|
|
2011-12-02 15:38:31 -05:00
|
|
|
#if !( (defined __x86_64__) || (defined __i386__) ) // !ARCH_X86
|
2008-06-02 17:41:35 -04:00
|
|
|
return;
|
2011-12-02 15:38:31 -05:00
|
|
|
#else
|
2009-12-31 12:51:27 -05:00
|
|
|
|
2015-04-21 11:39:19 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
|
|
|
|
// Get CPU flags using Microsoft function
|
|
|
|
// It works for both 64 and 32 bit systems
|
|
|
|
// no need to use assembler for getting info from register, this function does this for us
|
|
|
|
int cpuInfo[4];
|
|
|
|
__cpuid (cpuInfo, 1);
|
|
|
|
cpuflags = cpuInfo[3];
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2015-04-21 12:13:00 -04:00
|
|
|
#ifndef _LP64 /* *nix; 32 bit version. This odd macro constant is required because we need something that identifies this as a 32 bit
|
|
|
|
build on Linux and on OS X. Anything that serves this purpose will do, but this is the best thing we've identified
|
|
|
|
so far.
|
|
|
|
*/
|
2015-04-21 11:39:19 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
asm volatile (
|
|
|
|
"mov $1, %%eax\n"
|
|
|
|
"pushl %%ebx\n"
|
|
|
|
"cpuid\n"
|
|
|
|
"movl %%edx, %0\n"
|
|
|
|
"popl %%ebx\n"
|
|
|
|
: "=r" (cpuflags)
|
|
|
|
:
|
2009-12-31 13:16:03 -05:00
|
|
|
: "%eax", "%ecx", "%edx"
|
2008-06-02 17:41:35 -04:00
|
|
|
);
|
|
|
|
|
2015-04-21 11:39:19 -04:00
|
|
|
#else /* *nix; 64 bit version */
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-12-31 13:19:46 -05:00
|
|
|
/* asm notes: although we explicitly save&restore rbx, we must tell
|
|
|
|
gcc that ebx,rbx is clobbered so that it doesn't try to use it as an intermediate
|
|
|
|
register when storing rbx. gcc 4.3 didn't make this "mistake", but gcc 4.4
|
|
|
|
does, at least on x86_64.
|
|
|
|
*/
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
asm volatile (
|
|
|
|
"pushq %%rbx\n"
|
|
|
|
"movq $1, %%rax\n"
|
|
|
|
"cpuid\n"
|
|
|
|
"movq %%rdx, %0\n"
|
|
|
|
"popq %%rbx\n"
|
|
|
|
: "=r" (cpuflags)
|
|
|
|
:
|
2009-12-31 12:51:27 -05:00
|
|
|
: "%rax", "%rbx", "%rcx", "%rdx"
|
2008-06-02 17:41:35 -04:00
|
|
|
);
|
|
|
|
|
2015-04-23 20:43:52 -04:00
|
|
|
#endif /* _LP64 */
|
2015-04-21 11:39:19 -04:00
|
|
|
#endif /* PLATFORM_WINDOWS */
|
2009-12-31 12:51:27 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (cpuflags & (1<<25)) {
|
|
|
|
_flags = Flags (_flags | (HasSSE|HasFlushToZero));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpuflags & (1<<26)) {
|
|
|
|
_flags = Flags (_flags | HasSSE2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpuflags & (1 << 24)) {
|
|
|
|
|
2012-05-16 18:43:23 -04:00
|
|
|
char** fxbuf = 0;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2011-12-26 17:32:21 -05:00
|
|
|
/* DAZ wasn't available in the first version of SSE. Since
|
|
|
|
setting a reserved bit in MXCSR causes a general protection
|
|
|
|
fault, we need to be able to check the availability of this
|
|
|
|
feature without causing problems. To do this, one needs to
|
|
|
|
set up a 512-byte area of memory to save the SSE state to,
|
|
|
|
using fxsave, and then one needs to inspect bytes 28 through
|
|
|
|
31 for the MXCSR_MASK value. If bit 6 is set, DAZ is
|
|
|
|
supported, otherwise, it isn't.
|
|
|
|
*/
|
2015-04-23 20:43:52 -04:00
|
|
|
|
2012-05-16 20:54:43 -04:00
|
|
|
#ifndef HAVE_POSIX_MEMALIGN
|
2015-04-23 20:43:52 -04:00
|
|
|
# ifdef PLATFORM_WINDOWS
|
|
|
|
fxbuf = (char **) _aligned_malloc (sizeof (char *), 16);
|
|
|
|
assert (fxbuf);
|
|
|
|
*fxbuf = (char *) _aligned_malloc (512, 16);
|
|
|
|
assert (*fxbuf);
|
|
|
|
# else
|
|
|
|
# warning using default malloc for aligned memory
|
2012-05-16 18:43:23 -04:00
|
|
|
fxbuf = (char **) malloc (sizeof (char *));
|
|
|
|
assert (fxbuf);
|
|
|
|
*fxbuf = (char *) malloc (512);
|
|
|
|
assert (*fxbuf);
|
2015-04-23 20:43:52 -04:00
|
|
|
# endif
|
2008-06-02 17:41:35 -04:00
|
|
|
#else
|
2013-11-04 21:32:41 -05:00
|
|
|
(void) posix_memalign ((void **) &fxbuf, 16, sizeof (char *));
|
2012-05-16 18:43:23 -04:00
|
|
|
assert (fxbuf);
|
2013-11-04 21:32:41 -05:00
|
|
|
(void) posix_memalign ((void **) fxbuf, 16, 512);
|
2012-05-16 18:43:23 -04:00
|
|
|
assert (*fxbuf);
|
2008-06-02 17:41:35 -04:00
|
|
|
#endif
|
2012-05-16 18:43:23 -04:00
|
|
|
|
|
|
|
memset (*fxbuf, 0, 512);
|
|
|
|
|
2015-04-21 11:39:19 -04:00
|
|
|
#ifdef COMPILER_MSVC
|
|
|
|
__asm {
|
|
|
|
mov eax, fxbuf
|
|
|
|
fxsave [eax]
|
|
|
|
};
|
|
|
|
#else
|
2012-05-16 18:43:23 -04:00
|
|
|
asm volatile (
|
|
|
|
"fxsave (%0)"
|
|
|
|
:
|
|
|
|
: "r" (*fxbuf)
|
|
|
|
: "memory"
|
|
|
|
);
|
2015-04-21 11:39:19 -04:00
|
|
|
#endif
|
2012-05-16 18:43:23 -04:00
|
|
|
|
|
|
|
uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
|
|
|
|
|
|
|
|
/* if the mask is zero, set its default value (from intel specs) */
|
|
|
|
|
|
|
|
if (mxcsr_mask == 0) {
|
|
|
|
mxcsr_mask = 0xffbf;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2012-05-16 18:43:23 -04:00
|
|
|
|
|
|
|
if (mxcsr_mask & (1<<6)) {
|
|
|
|
_flags = Flags (_flags | HasDenormalsAreZero);
|
|
|
|
}
|
|
|
|
|
2015-04-23 20:43:52 -04:00
|
|
|
#if !defined HAVE_POSIX_MEMALIGN && defined PLATFORM_WINDOWS
|
|
|
|
_aligned_free (*fxbuf);
|
|
|
|
_aligned_free (fxbuf);
|
|
|
|
#else
|
2012-05-16 18:43:23 -04:00
|
|
|
free (*fxbuf);
|
|
|
|
free (fxbuf);
|
2015-04-23 20:43:52 -04:00
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
2011-12-02 15:38:31 -05:00
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
FPU::~FPU ()
|
|
|
|
{
|
|
|
|
}
|