From dc38515f79696033be87183a1c9da60deacdc4bf Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Wed, 18 Aug 2021 15:01:56 +0300 Subject: [PATCH] Add CPUID functions for runtime dispatch --- common/rfb/CMakeLists.txt | 1 + common/rfb/EncodeManager.cxx | 1 + common/rfb/VNCServerST.cxx | 4 +++ common/rfb/cpuid.cxx | 70 ++++++++++++++++++++++++++++++++++++ common/rfb/cpuid.h | 28 +++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 common/rfb/cpuid.cxx create mode 100644 common/rfb/cpuid.h diff --git a/common/rfb/CMakeLists.txt b/common/rfb/CMakeLists.txt index 38d3472..5e1944f 100644 --- a/common/rfb/CMakeLists.txt +++ b/common/rfb/CMakeLists.txt @@ -63,6 +63,7 @@ set(RFB_SOURCES VNCServerST.cxx ZRLEEncoder.cxx ZRLEDecoder.cxx + cpuid.cxx encodings.cxx util.cxx xxhash.c) diff --git a/common/rfb/EncodeManager.cxx b/common/rfb/EncodeManager.cxx index db3add5..8785190 100644 --- a/common/rfb/EncodeManager.cxx +++ b/common/rfb/EncodeManager.cxx @@ -22,6 +22,7 @@ #include #include +#include #include #include #include diff --git a/common/rfb/VNCServerST.cxx b/common/rfb/VNCServerST.cxx index fd13cf3..3dbe9a5 100644 --- a/common/rfb/VNCServerST.cxx +++ b/common/rfb/VNCServerST.cxx @@ -53,6 +53,7 @@ #include +#include #include #include #include @@ -132,6 +133,9 @@ VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) { lastUserInputTime = lastDisconnectTime = time(0); slog.debug("creating single-threaded server %s", name.buf); + slog.info("CPU capability: SSE2 %s, AVX512f %s", + supportsSSE2() ? "yes" : "no", + supportsAVX512f() ? "yes" : "no"); DLPRegion.enabled = DLPRegion.percents = false; diff --git a/common/rfb/cpuid.cxx b/common/rfb/cpuid.cxx new file mode 100644 index 0000000..c89f950 --- /dev/null +++ b/common/rfb/cpuid.cxx @@ -0,0 +1,70 @@ +/* Copyright (C) 2021 Kasm Web + * + * This 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 software 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 software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#include + +static uint32_t cpuid[4] = { 0 }; +static uint32_t extcpuid[4] = { 0 }; + +static void getcpuid() { + if (cpuid[0]) + return; + +#if defined(__x86_64__) || defined(__i386__) + uint32_t eax, ecx = 0; + + eax = 1; // normal feature bits + + __asm__ __volatile__( + "cpuid\n\t" + : "=a"(cpuid[0]), "=b"(cpuid[1]), "=c"(cpuid[2]), "=d"(cpuid[3]) + : "0"(eax), "2"(ecx) + ); + + eax = 7; // ext feature bits + ecx = 0; + + __asm__ __volatile__( + "cpuid\n\t" + : "=a"(extcpuid[0]), "=b"(extcpuid[1]), "=c"(extcpuid[2]), "=d"(extcpuid[3]) + : "0"(eax), "2"(ecx) + ); +#endif +} + +namespace rfb { + +bool supportsSSE2() { + getcpuid(); +#if defined(__x86_64__) || defined(__i386__) + #define bit_SSE2 (1 << 26) + return cpuid[3] & bit_SSE2; +#endif + return false; +} + +bool supportsAVX512f() { + getcpuid(); +#if defined(__x86_64__) || defined(__i386__) + #define bit_AVX512f (1 << 16) + return extcpuid[1] & bit_AVX512f; +#endif + return false; +} + +}; // namespace rfb diff --git a/common/rfb/cpuid.h b/common/rfb/cpuid.h new file mode 100644 index 0000000..c84b4a2 --- /dev/null +++ b/common/rfb/cpuid.h @@ -0,0 +1,28 @@ +/* Copyright (C) 2021 Kasm Web + * + * This 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 software 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 software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#ifndef __RFB_CPUID_H__ +#define __RFB_CPUID_H__ + +namespace rfb { + + bool supportsSSE2(); + bool supportsAVX512f(); +}; + +#endif