-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ | |
#include "consts.c" | ||
#include "vect.c" | ||
#include "exports.c" | ||
#include "cpuid.c" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright Supranational LLC | ||
* Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
int __blst_platform_cap = 0; | ||
|
||
#if defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) | ||
|
||
# if defined(__GNUC__) || defined(__clang__) || defined(__SUNPRO_C) | ||
static void __cpuidex(int info[4], int func, int sub) | ||
{ | ||
int eax, ebx, ecx, edx; | ||
|
||
__asm__("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) | ||
: "a"(func), "c"(sub)); | ||
|
||
info[0] = eax; | ||
info[1] = ebx; | ||
info[2] = ecx; | ||
info[3] = edx; | ||
} | ||
# else | ||
# include <intrin.h> | ||
# endif | ||
|
||
# if defined(__GNUC__) || defined(__clang__) | ||
__attribute__((constructor)) | ||
# endif | ||
int __blst_cpuid(void) | ||
{ | ||
int info[4], cap = 0; | ||
|
||
__cpuidex(info, 0, 0); | ||
if (info[0] > 6) { | ||
__cpuidex(info, 7, 0); | ||
cap |= (info[1]>>19) & 1; /* ADX */ | ||
cap |= (info[1]>>28) & 2; /* SHA */ | ||
} | ||
|
||
__blst_platform_cap = cap; | ||
|
||
return 0; | ||
} | ||
|
||
# if defined(_MSC_VER) && !defined(__clang__) | ||
# pragma section(".CRT$XCU",read) | ||
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = __blst_cpuid; | ||
# elif defined(__SUNPRO_C) | ||
# pragma init(__blst_cpuid) | ||
# endif | ||
|
||
#elif defined(__aarch64__) || defined(__aarch64) | ||
|
||
# if defined(__linux__) && (defined(__GNUC__) || defined(__clang__)) | ||
extern unsigned long getauxval(unsigned long type) __attribute__ ((weak)); | ||
|
||
__attribute__((constructor)) | ||
int __blst_cpuid(void) | ||
{ | ||
int cap = 0; | ||
|
||
if (getauxval) { | ||
unsigned long hwcap_ce = getauxval(16); | ||
cap = (hwcap_ce>>6) & 1; /* SHA256 */ | ||
} | ||
|
||
__blst_platform_cap = cap; | ||
|
||
return 0; | ||
} | ||
# elif defined(__APPLE__) && (defined(__GNUC__) || defined(__clang__)) | ||
__attribute__((constructor)) | ||
int __blst_cpuid() | ||
{ | ||
__blst_platform_cap = 1; /* SHA256 */ | ||
return 0; | ||
} | ||
# endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ | |
#ifdef BLST_FR_PENTAROOT | ||
# include "pentaroot.c" | ||
#endif | ||
#include "cpuid.c" |