Skip to content

Commit a28e8d5

Browse files
committed
Add detection of ARM CRC32 and PMULL instructions for NetBSD.
1 parent 63694c8 commit a28e8d5

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

‎crc32.c‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,10 @@
232232
the hardware instructions.
233233
234234
Define ARMCRC32_ASK to ask the operating system if the feature is present, in
235-
which case define one of ARMCRC32_AUXV, ARMCRC32_ELFAUX, ARMCRC32_SYSCTL, or
236-
ARMCRC32_WIN to use, respectively, getauxval() on Linux, elf_aux_info() on
237-
FreeBSD, sysctlbyname() on Apple systems, or IsProcessorFeaturePresent() on
235+
which case define one of ARMCRC32_AUXV, ARMCRC32_ELFAUX, ARMCRC32_SYSCTL,
236+
ARMCRC32_NETBSD, or ARMCRC32_WIN to use, respectively, getauxval() on Linux,
237+
elf_aux_info() on FreeBSD and OpenBSD, sysctlbyname() on Apple systems, the
238+
ID registers from sysctlbyname() on NetBSD, or IsProcessorFeaturePresent() on
238239
Windows.
239240
240241
The build can define Z_NO_ARMCRC32_RT to disallow the run-time check.
@@ -285,6 +286,9 @@
285286
# define ARMCRC32_ELFAUX /* elf_aux_info() */
286287
# define ARMCRC32_OS
287288
# endif
289+
# elif defined(__NetBSD__) && ARMCRC32_WORD == 8
290+
# define ARMCRC32_NETBSD /* sysctlbyname() for ID registers */
291+
# define ARMCRC32_OS
288292
# elif defined(__APPLE__)
289293
# define ARMCRC32_SYSCTL /* sysctlbyname() */
290294
# define ARMCRC32_OS
@@ -824,6 +828,10 @@ const z_crc_t FAR * ZEXPORT get_crc_table(void) {
824828
#elif defined(ARMCRC32_SYSCTL)
825829
# include <sys/types.h>
826830
# include <sys/sysctl.h>
831+
#elif defined(ARMCRC32_NETBSD)
832+
# include <sys/types.h>
833+
# include <sys/sysctl.h>
834+
# include <aarch64/armreg.h> /* for struct aarch64_sysctl_cpu_id */
827835
#elif defined(ARMCRC32_WIN) && !defined(_WINBASE_)
828836
__declspec(dllimport) int __stdcall IsProcessorFeaturePresent(unsigned long);
829837
#endif
@@ -869,6 +877,19 @@ local int arm_probe(int want) {
869877
if ((want & ARM_HAVE_PMULL) && arm_sysctl("hw.optional.arm.FEAT_PMULL"))
870878
caps |= ARM_HAVE_PMULL;
871879
# endif
880+
#elif defined(ARMCRC32_NETBSD)
881+
/* ID_AA64ISAR0_EL1 of cpu0: CRC32 in bits 16..19 (1 if present), and AES
882+
in bits 4..7 (2 if PMULL is present) */
883+
struct aarch64_sysctl_cpu_id id;
884+
size_t size = sizeof(id);
885+
if (sysctlbyname("machdep.cpu0.cpu_id", &id, &size, NULL, 0) == 0 &&
886+
size == sizeof(id)) {
887+
if (((id.ac_aa64isar0 >> 16) & 15) >= 1)
888+
caps |= ARM_HAVE_CRC32;
889+
if (((id.ac_aa64isar0 >> 4) & 15) >= 2)
890+
caps |= ARM_HAVE_PMULL;
891+
}
892+
(void)want;
872893
#else
873894
if ((want & ARM_HAVE_CRC32) &&
874895
IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))

0 commit comments

Comments
 (0)