2013-01-16 13:15:38 -05:00
|
|
|
/*
|
2019-08-02 23:10:55 -04:00
|
|
|
* Copyright (C) 2007-2016 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
|
|
|
|
* Copyright (C) 2013-2015 John Emmas <john@creativepost.co.uk>
|
|
|
|
* Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
|
|
|
|
2020-08-23 14:27:45 -04:00
|
|
|
#ifdef ARM_NEON_SUPPORT
|
|
|
|
/* Needed for ARM NEON detection */
|
|
|
|
#include <sys/auxv.h>
|
|
|
|
#include <asm/hwcap.h>
|
|
|
|
#endif
|
|
|
|
|
2023-02-09 14:54:46 -05:00
|
|
|
/* x86 CPU - need __cpuid to query flags */
|
|
|
|
#if (defined __x86_64__) || (defined __i386__) || (defined _M_X64) || (defined _M_IX86)
|
|
|
|
# ifdef PLATFORM_WINDOWS
|
|
|
|
# include <intrin.h>
|
|
|
|
# elif defined(__GNUC__) || defined(__clang__)
|
|
|
|
# include <cpuid.h>
|
|
|
|
# else
|
|
|
|
# error "Unsupported compiler: need __cpuid and __cpuidex for CPU detection"
|
|
|
|
# endif
|
2023-02-08 14:27:24 -05:00
|
|
|
#endif
|
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
#include "pbd/compose.h"
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/fpu.h"
|
|
|
|
#include "pbd/error.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2016-07-14 14:44:52 -04:00
|
|
|
#include "pbd/i18n.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
using namespace PBD;
|
|
|
|
using namespace std;
|
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
FPU* FPU::_instance (0);
|
|
|
|
|
2023-02-09 14:54:46 -05:00
|
|
|
#if (defined __x86_64__) || (defined __i386__) || (defined _M_X64) || (defined _M_IX86)
|
2023-02-08 14:27:24 -05:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
/* Use MSVC/mingw intrinsic to get CPUID */
|
|
|
|
static void cpuid(int reg[4], int cpuid_leaf)
|
2015-08-11 22:56:55 -04:00
|
|
|
{
|
2023-02-08 14:27:24 -05:00
|
|
|
__cpuid(reg, cpuid_leaf);
|
2015-08-11 22:56:55 -04:00
|
|
|
}
|
2023-02-08 14:27:24 -05:00
|
|
|
/* Get extended features of CPUID */
|
|
|
|
static void cpuidex(int reg[4], int cpuid_leaf, int cpuid_level)
|
|
|
|
{
|
|
|
|
__cpuidex(reg, cpuid_leaf, cpuid_level);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* use __cpuid() with the same API to match the MSVC/mingw intrinsic */
|
|
|
|
static void cpuid(int reg[4], int cpuid_leaf)
|
|
|
|
{
|
|
|
|
__cpuid_count(cpuid_leaf, 0, reg[0], reg[1], reg[2], reg[3]);
|
|
|
|
}
|
|
|
|
/* Get extended features of CPUID */
|
|
|
|
static void cpuidex(int reg[4], int cpuid_leaf, int cpuid_level)
|
|
|
|
{
|
|
|
|
__cpuid_count(cpuid_leaf, cpuid_level, reg[0], reg[1], reg[2], reg[3]);
|
|
|
|
}
|
|
|
|
#endif // PLATFORM_WINDOWS
|
2015-08-11 23:25:29 -04:00
|
|
|
|
2016-10-13 22:35:05 -04:00
|
|
|
#ifndef HAVE_XGETBV // Allow definition by build system
|
|
|
|
#if defined(__MINGW32__) && defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR >= 5
|
|
|
|
#define HAVE_XGETBV
|
|
|
|
#elif defined(_MSC_VER) && _MSC_VER >= 1600
|
|
|
|
// '_xgetbv()' was only available from VC10 onwards
|
|
|
|
#define HAVE_XGETBV
|
|
|
|
#endif
|
2015-08-11 23:06:47 -04:00
|
|
|
#endif
|
2015-08-11 22:56:55 -04:00
|
|
|
|
2016-10-13 22:35:05 -04:00
|
|
|
#ifndef HAVE_XGETBV
|
|
|
|
|
|
|
|
#ifdef COMPILER_MSVC
|
2015-08-16 08:14:54 -04:00
|
|
|
|
|
|
|
// '_xgetbv()' was only available from VC10 onwards
|
|
|
|
__declspec(noinline) static uint64_t
|
|
|
|
_xgetbv (uint32_t xcr)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// N.B. The following would probably work for a pre-VC10 build,
|
|
|
|
// although it might suffer from optimization issues. We'd need
|
|
|
|
// to place this function into its own (unoptimized) source file.
|
2015-10-04 14:51:05 -04:00
|
|
|
__asm {
|
2015-08-16 08:14:54 -04:00
|
|
|
mov ecx, [xcr]
|
2016-10-13 22:35:05 -04:00
|
|
|
__asm _emit 0x0f __asm _emit 0x01 __asm _emit 0xd0 /*xgetbv*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
_xgetbv (uint32_t xcr)
|
|
|
|
{
|
|
|
|
#ifdef __APPLE__
|
|
|
|
/* it would be nice to make this work on OS X but as long we use veclib,
|
|
|
|
we don't really need to know about SSE/AVX on that platform.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
uint32_t eax, edx;
|
|
|
|
__asm__ volatile ("xgetbv" : "=a" (eax), "=d" (edx) : "c" (xcr));
|
|
|
|
return (static_cast<uint64_t>(edx) << 32) | eax;
|
|
|
|
#endif
|
2015-08-16 08:14:54 -04:00
|
|
|
}
|
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
#endif /* !COMPILER_MSVC */
|
2016-10-13 22:35:05 -04:00
|
|
|
#endif /* !HAVE_XGETBV */
|
2015-08-12 13:16:01 -04:00
|
|
|
#endif /* ARCH_X86 */
|
2015-08-11 22:56:55 -04:00
|
|
|
|
2015-08-11 23:25:29 -04:00
|
|
|
#ifndef _XCR_XFEATURE_ENABLED_MASK
|
|
|
|
#define _XCR_XFEATURE_ENABLED_MASK 0
|
|
|
|
#endif
|
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
FPU*
|
|
|
|
FPU::instance()
|
|
|
|
{
|
|
|
|
if (!_instance) {
|
|
|
|
_instance = new FPU;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:05:25 -04:00
|
|
|
void
|
|
|
|
FPU::destroy ()
|
|
|
|
{
|
|
|
|
delete _instance;
|
|
|
|
_instance = 0;
|
|
|
|
}
|
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
FPU::FPU ()
|
|
|
|
: _flags ((Flags) 0)
|
|
|
|
{
|
|
|
|
if (_instance) {
|
|
|
|
error << _("FPU object instantiated more than once") << endmsg;
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2016-03-04 12:05:47 -05:00
|
|
|
if (getenv("ARDOUR_FPU_FLAGS")) {
|
|
|
|
_flags = Flags (atoi (getenv("ARDOUR_FPU_FLAGS")));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-23 14:27:45 -04:00
|
|
|
#ifdef ARM_NEON_SUPPORT
|
2020-08-24 16:03:28 -04:00
|
|
|
# ifdef __aarch64__
|
|
|
|
/* all armv8+ features NEON used in arm_neon_functions.cc */
|
|
|
|
_flags = Flags(_flags | HasNEON);
|
|
|
|
# elif defined __arm__
|
|
|
|
if (getauxval(AT_HWCAP) & HWCAP_NEON) {
|
|
|
|
_flags = Flags(_flags | HasNEON);
|
2020-08-23 14:27:45 -04:00
|
|
|
}
|
2020-08-24 16:03:28 -04:00
|
|
|
# endif
|
2020-08-23 14:27:45 -04:00
|
|
|
#endif
|
|
|
|
|
2015-04-30 12:44:28 -04:00
|
|
|
#if !( (defined __x86_64__) || (defined __i386__) || (defined _M_X64) || (defined _M_IX86) ) // !ARCH_X86
|
2015-08-11 22:56:55 -04:00
|
|
|
/* Non-Intel architecture, nothing to do here */
|
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
|
|
|
|
2019-04-13 13:19:29 -04:00
|
|
|
/* Get the CPU vendor just for kicks
|
|
|
|
*
|
|
|
|
* __cpuid with an InfoType argument of 0 returns the number of
|
|
|
|
* valid Ids in CPUInfo[0] and the CPU identification string in
|
|
|
|
* the other three array elements. The CPU identification string is
|
|
|
|
* not in linear order. The code below arranges the information
|
|
|
|
* in a human readable form. The human readable order is CPUInfo[1] |
|
|
|
|
* CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
|
|
|
|
* before using memcpy to copy these three array elements to cpu_string.
|
|
|
|
*/
|
2015-04-21 11:39:19 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
int cpu_info[4];
|
|
|
|
char cpu_string[48];
|
|
|
|
string cpu_vendor;
|
2015-04-21 11:39:19 -04:00
|
|
|
|
2023-02-08 14:27:24 -05:00
|
|
|
cpuid(cpu_info, 0);
|
2015-08-11 22:56:55 -04:00
|
|
|
|
|
|
|
int num_ids = cpu_info[0];
|
2019-04-13 13:19:29 -04:00
|
|
|
std::swap(cpu_info[2], cpu_info[3]);
|
2015-08-11 22:56:55 -04:00
|
|
|
memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
|
2019-04-13 13:19:29 -04:00
|
|
|
cpu_vendor.assign(cpu_string, 3 * sizeof(cpu_info[1]));
|
2015-08-11 22:56:55 -04:00
|
|
|
|
|
|
|
info << string_compose (_("CPU vendor: %1"), cpu_vendor) << endmsg;
|
|
|
|
|
|
|
|
if (num_ids > 0) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2023-02-08 14:27:24 -05:00
|
|
|
int cpu_info_ex[4];
|
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
/* Now get CPU/FPU flags */
|
2023-02-08 14:27:24 -05:00
|
|
|
cpuid(cpu_info, 1);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2023-02-08 14:27:24 -05:00
|
|
|
/* Get CPU extended features: Like AVX512F */
|
|
|
|
cpuidex(cpu_info_ex, 7, 0);
|
2015-08-11 22:56:55 -04:00
|
|
|
|
2015-08-14 07:29:40 -04:00
|
|
|
if ((cpu_info[2] & (1<<27)) /* OSXSAVE */ &&
|
|
|
|
(cpu_info[2] & (1<<28) /* AVX */) &&
|
|
|
|
((_xgetbv (_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6)) { /* OS really supports XSAVE */
|
2023-02-02 12:13:12 -05:00
|
|
|
info << _("AVX capable processor") << endmsg;
|
2021-01-01 14:01:37 -05:00
|
|
|
_flags = Flags (_flags | (HasAVX));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpu_info[2] & (1<<12) /* FMA */) {
|
|
|
|
info << _("AVX with FMA capable processor") << endmsg;
|
|
|
|
_flags = Flags (_flags | (HasFMA));
|
2015-08-11 17:23:11 -04:00
|
|
|
}
|
2015-08-11 16:36:39 -04:00
|
|
|
|
2023-02-08 14:27:24 -05:00
|
|
|
if (cpu_info_ex[1] & (1<<16) /* AVX512F */) {
|
2023-02-02 12:13:12 -05:00
|
|
|
info << _("AVX512F capable processor") << endmsg;
|
|
|
|
_flags = Flags (_flags | (HasAVX512F));
|
|
|
|
}
|
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
if (cpu_info[3] & (1<<25)) {
|
|
|
|
_flags = Flags (_flags | (HasSSE|HasFlushToZero));
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
if (cpu_info[3] & (1<<26)) {
|
|
|
|
_flags = Flags (_flags | HasSSE2);
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
/* Figure out CPU/FPU denormal handling capabilities */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
if (cpu_info[3] & (1 << 24)) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
char** fxbuf = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04: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
|
2015-08-11 22:56:55 -04:00
|
|
|
fxbuf = (char **) _aligned_malloc (sizeof (char *), 16);
|
|
|
|
assert (fxbuf);
|
|
|
|
*fxbuf = (char *) _aligned_malloc (512, 16);
|
|
|
|
assert (*fxbuf);
|
2015-04-23 20:43:52 -04:00
|
|
|
# else
|
|
|
|
# warning using default malloc for aligned memory
|
2015-08-11 22:56:55 -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
|
2015-08-11 22:56:55 -04:00
|
|
|
(void) posix_memalign ((void **) &fxbuf, 16, sizeof (char *));
|
|
|
|
assert (fxbuf);
|
|
|
|
(void) posix_memalign ((void **) fxbuf, 16, 512);
|
|
|
|
assert (*fxbuf);
|
2015-10-05 10:17:49 -04:00
|
|
|
#endif
|
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
memset (*fxbuf, 0, 512);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-04-21 11:39:19 -04:00
|
|
|
#ifdef COMPILER_MSVC
|
2021-01-07 04:22:43 -05:00
|
|
|
char* buf = *fxbuf;
|
|
|
|
#ifdef _WIN64
|
|
|
|
/* For 64-bit compilation, MSVC doesn't support inline assembly !!
|
|
|
|
( https://docs.microsoft.com/en-us/cpp/assembler/inline/inline-assembler?view=msvc-160 ) */
|
|
|
|
|
|
|
|
/* but instead, it uses something called 'x64 intrinsics'
|
|
|
|
1: ( https://docs.microsoft.com/en-us/cpp/intrinsics/x64-amd64-intrinsics-list?view=msvc-160 )
|
|
|
|
2: ( https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_fxsave ) */
|
|
|
|
_fxsave (buf);
|
|
|
|
#else
|
2015-08-11 22:56:55 -04:00
|
|
|
__asm {
|
|
|
|
mov eax, buf
|
|
|
|
fxsave [eax]
|
|
|
|
};
|
2021-01-07 04:22:43 -05:00
|
|
|
#endif
|
2015-04-21 11:39:19 -04:00
|
|
|
#else
|
2015-08-11 22:56:55 -04:00
|
|
|
asm volatile (
|
|
|
|
"fxsave (%0)"
|
|
|
|
:
|
|
|
|
: "r" (*fxbuf)
|
|
|
|
: "memory"
|
|
|
|
);
|
2015-04-21 11:39:19 -04:00
|
|
|
#endif
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
/* if the mask is zero, set its default value (from intel specs) */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
if (mxcsr_mask == 0) {
|
|
|
|
mxcsr_mask = 0xffbf;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
if (mxcsr_mask & (1<<6)) {
|
|
|
|
_flags = Flags (_flags | HasDenormalsAreZero);
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-04-23 20:43:52 -04:00
|
|
|
#if !defined HAVE_POSIX_MEMALIGN && defined PLATFORM_WINDOWS
|
2015-08-11 22:56:55 -04:00
|
|
|
_aligned_free (*fxbuf);
|
|
|
|
_aligned_free (fxbuf);
|
2015-04-23 20:43:52 -04:00
|
|
|
#else
|
2015-08-11 22:56:55 -04:00
|
|
|
free (*fxbuf);
|
|
|
|
free (fxbuf);
|
2015-04-23 20:43:52 -04:00
|
|
|
#endif
|
2015-08-11 22:56:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* finally get the CPU brand */
|
2023-02-08 14:27:24 -05:00
|
|
|
cpuid(cpu_info, 0x80000000);
|
2015-08-11 22:56:55 -04:00
|
|
|
|
|
|
|
const int parameter_end = 0x80000004;
|
|
|
|
string cpu_brand;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
if (cpu_info[0] >= parameter_end) {
|
|
|
|
char* cpu_string_ptr = cpu_string;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-08-11 22:56:55 -04:00
|
|
|
for (int parameter = 0x80000002; parameter <= parameter_end &&
|
|
|
|
cpu_string_ptr < &cpu_string[sizeof(cpu_string)]; parameter++) {
|
2023-02-08 14:27:24 -05:00
|
|
|
cpuid(cpu_info, parameter);
|
2015-08-11 22:56:55 -04:00
|
|
|
memcpy(cpu_string_ptr, cpu_info, sizeof(cpu_info));
|
|
|
|
cpu_string_ptr += sizeof(cpu_info);
|
|
|
|
}
|
|
|
|
cpu_brand.assign(cpu_string, cpu_string_ptr - cpu_string);
|
|
|
|
info << string_compose (_("CPU brand: %1"), cpu_brand) << endmsg;
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2015-08-11 22:56:55 -04:00
|
|
|
}
|
2015-08-12 13:16:01 -04:00
|
|
|
#endif /* !ARCH_X86 */
|
2015-10-05 10:17:49 -04:00
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
FPU::~FPU ()
|
|
|
|
{
|
|
|
|
}
|