From 040341007cf4492fdc8aa2d3f198dd22c2288ced Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Tue, 10 Mar 2020 19:18:33 -0400 Subject: auto-detect cores --- modexp.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'modexp.c') diff --git a/modexp.c b/modexp.c index 8fea2ea..82c79b8 100644 --- a/modexp.c +++ b/modexp.c @@ -49,23 +49,41 @@ #include "hal_internal.h" /* - * Whether we want to use the new ModExpNG core. + * Enable use of the ModExpNG core, if present. */ -static int use_modexpng = 0; +static enum { + unknown = -1, + modexpa7_core = 0, + modexpng_core = 1 +} which_core = unknown; -hal_error_t hal_modexp_use_modexpng(const int onoff) +static inline hal_error_t init_modexp_core(void) { - if (onoff && (hal_core_find(MODEXPNG_NAME, NULL) == NULL)) + if (which_core != unknown) + return HAL_OK; + else if (hal_core_find(MODEXPNG_NAME, NULL) != NULL) + return (which_core = modexpng_core), HAL_OK; + else if (hal_core_find(MODEXPA7_NAME, NULL) != NULL) + return (which_core = modexpa7_core), HAL_OK; + else return HAL_ERROR_CORE_NOT_FOUND; +} - use_modexpng = onoff; - return HAL_OK; +hal_error_t hal_modexp_use_modexpng(const int onoff) +{ + if (onoff && hal_core_find(MODEXPNG_NAME, NULL) != NULL) + return (which_core = modexpng_core), HAL_OK; + else if (!onoff && hal_core_find(MODEXPA7_NAME, NULL) != NULL) + return (which_core = modexpa7_core), HAL_OK; + else + return HAL_ERROR_CORE_NOT_FOUND; } int hal_modexp_using_modexpng(void) { - return use_modexpng; + return (init_modexp_core() == HAL_OK && + which_core == modexpng_core); } /* -- cgit v1.2.3