Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime neon detection #383

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions c/blake3_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ enum cpu_feature {
AVX2 = 1 << 4,
AVX512F = 1 << 5,
AVX512VL = 1 << 6,
ARM_NEON = 1 << 7,
/* ... */
UNDEFINED = 1 << 30
};
Expand Down Expand Up @@ -155,8 +156,19 @@ static
}
ATOMIC_STORE(g_cpu_features, features);
return features;
#elif defined(__aarch64__)
uint64_t id_aa64pfr0_el1;
__asm__ ("mrs %0, ID_AA64PFR0_EL1" : "=r" (id_aa64pfr0_el1));
if(((id_aa64pfr0_el1 >> 20) & (1<<0 | 1<<1 | 1<<2 | 1 << 3)) != 15) {
// https://developer.arm.com/documentation/ddi0595/2021-12/AArch64-Registers/ID-AA64PFR0-EL1--AArch64-Processor-Feature-Register-0?lang=en
// 15 means not implemented, 0 means neon is present but float16 is missing, 1 means neon with float16 is present ?
features = ARM_NEON;
} else {
features = 0;
}
ATOMIC_STORE(g_cpu_features, features);
return features;
#else
/* How to detect NEON? */
return 0;
#endif
}
Expand Down Expand Up @@ -260,11 +272,19 @@ void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs,
}
#endif
#endif

#if BLAKE3_USE_NEON == 1
blake3_hash_many_neon(inputs, num_inputs, blocks, key, counter,
increment_counter, flags, flags_start, flags_end, out);
return;
#elif __aarch64__
{
const enum cpu_feature features = get_cpu_features();
if(features & ARM_NEON) {
blake3_hash_many_neon(inputs, num_inputs, blocks, key, counter,
increment_counter, flags, flags_start, flags_end, out);
return;
}
}
#endif

blake3_hash_many_portable(inputs, num_inputs, blocks, key, counter,
Expand Down Expand Up @@ -300,6 +320,11 @@ size_t blake3_simd_degree(void) {
#endif
#if BLAKE3_USE_NEON == 1
return 4;
#elif defined(__aarch64__)
const enum cpu_feature features = get_cpu_features();
if(features & ARM_NEON) {
return 4;
}
#endif
return 1;
}
1 change: 1 addition & 0 deletions c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum cpu_feature {
AVX2 = 1 << 4,
AVX512F = 1 << 5,
AVX512VL = 1 << 6,
ARM_NEON = 1 << 7,
/* ... */
UNDEFINED = 1 << 30
};
Expand Down