Skip to content

Commit

Permalink
Add cpuid.c.
Browse files Browse the repository at this point in the history
  • Loading branch information
dot-asm committed Jun 12, 2023
1 parent 0e0ebaf commit f8af94a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions bindings/go/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
#include "consts.c"
#include "vect.c"
#include "exports.c"
#include "cpuid.c"
82 changes: 82 additions & 0 deletions src/cpuid.c
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
1 change: 1 addition & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
#ifdef BLST_FR_PENTAROOT
# include "pentaroot.c"
#endif
#include "cpuid.c"

0 comments on commit f8af94a

Please sign in to comment.