Skip to content

Commit

Permalink
try Detect CRC Availability By SIGILL
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Sep 10, 2020
1 parent 6945a37 commit 1f1fba8
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion Core/MMKV_OSX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ extern MMKVPath_t g_rootDir;
enum { UnKnown = 0, PowerMac = 1, Mac, iPhone, iPod, iPad, AppleTV, AppleWatch };
static void GetAppleMachineInfo(int &device, int &version);

# ifdef MMKV_USE_ARMV8_CRC32
# include <setjmp.h>
# include <signal.h>

static sigjmp_buf g_jmpBuff;

static void TrivialSignalHandler(int signo) {
siglongjmp(g_jmpBuff, 1);
}

__attribute__((target("crc"))) static bool DetectCRCAvailabilityBySIGILL() {
volatile auto result = true;

volatile auto oldHandler = signal(SIGILL, TrivialSignalHandler);
if (oldHandler == SIG_ERR) {
return false;
}

if (sigsetjmp(g_jmpBuff, 1)) {
result = false;
} else {
auto tmp = __builtin_arm_crc32d(0, 1);
tmp = __builtin_arm_crc32w(tmp, 2);
tmp = __builtin_arm_crc32h(tmp, 3);
tmp = __builtin_arm_crc32b(tmp, 4);

result = !!tmp;
}

signal(SIGILL, oldHandler);
return result;
}
# endif

MMKV_NAMESPACE_BEGIN

# ifdef MMKV_IOS
Expand Down Expand Up @@ -99,7 +133,12 @@ void MMKV::minimalInit(MMKVPath_t defaultRootDir) {
GetAppleMachineInfo(device, version);
# ifdef MMKV_USE_ARMV8_CRC32
if ((device == iPhone && version >= 9) || (device == iPad && version >= 7)) {
CRC32 = mmkv::armv8_crc32;
// it's reported that some 64-bit iPhone 7 and above doesn't support CRC32: https://github.com/Tencent/MMKV/issues/525
// try to detect by signal handler
if (DetectCRCAvailabilityBySIGILL()) {
CRC32 = mmkv::armv8_crc32;
MMKVInfo("Detect CRC Availability By SIGILL: Looks like armv8 CRC32 instructions is supported");
}
}
# endif
MMKVInfo("Apple Device:%d, version:%d", device, version);
Expand Down

0 comments on commit 1f1fba8

Please sign in to comment.