From 4c6e9e43e5fa13d7f1fc4c1f6a45b37a02fb2d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=BCtzel?= Date: Thu, 26 Oct 2023 11:14:05 +0200 Subject: [PATCH] LAGraph: Use compile-time checks for size of uintptr_t Instead of checking the size of `uintptr_t` on run-time (inside a loop), move that check to the preprocessor. That way code that will never be executed isn't compiled. Modern compilers might be able to optimize the checks and code paths away that will never be executed, there is no guarantees they will do that. I hope it is ok to check at compile time if the pointer size is neither 32-bit nor 64-bit. (Compilation will likely fail at other points anyway if that should be the case.) --- LAGraph/src/algorithm/LG_CC_Boruvka.c | 35 ++++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/LAGraph/src/algorithm/LG_CC_Boruvka.c b/LAGraph/src/algorithm/LG_CC_Boruvka.c index 24261658a..cf7dbf566 100644 --- a/LAGraph/src/algorithm/LG_CC_Boruvka.c +++ b/LAGraph/src/algorithm/LG_CC_Boruvka.c @@ -25,6 +25,8 @@ // slower in general than LG_CC_FastSV6, which uses GxB pack/unpack methods // for faster access to the contents of the matrices and vectors. +#include + #include "LG_internal.h" //------------------------------------------------------------------------------ @@ -132,11 +134,13 @@ int LG_CC_Boruvka "G->A must be known to be symmetric") ; // determine the pointer size - GrB_Type UintPtr_type = - ((sizeof (uintptr_t) == sizeof (uint32_t)) ? GrB_UINT32 : - ((sizeof (uintptr_t) == sizeof (uint64_t)) ? GrB_UINT64 : NULL)) ; - LG_ASSERT_MSG (UintPtr_type != NULL, - GrB_NOT_IMPLEMENTED, "system has an unsupported sizeof (uintptr_t)") ; + #if defined (UINT64_MAX) && UINT64_MAX == UINTPTR_MAX + GrB_Type UintPtr_type = GrB_UINT64; + #elif defined (UINT32_MAX) && UINT32_MAX == UINTPTR_MAX + GrB_Type UintPtr_type = GrB_UINT32; + #else + # error "system has an unsupported sizeof (uintptr_t)" + #endif //-------------------------------------------------------------------------- // initializations @@ -256,18 +260,15 @@ int LG_CC_Boruvka // This step is the costliest part of this algorithm when dealing with // large matrices. - if (sizeof (void *) == sizeof (uint32_t)) - { - // 32-bit platforms - GRB_TRY (GrB_Matrix_select_UINT32 (S, NULL, NULL, select_op, S, - (uintptr_t) Px, NULL)) ; - } - else - { - // 64-bit platforms - GRB_TRY (GrB_Matrix_select_UINT64 (S, NULL, NULL, select_op, S, - (uintptr_t) Px, NULL)) ; - } + #if defined (UINT32_MAX) && UINT32_MAX == UINTPTR_MAX + // 32-bit platforms + GRB_TRY (GrB_Matrix_select_UINT32 (S, NULL, NULL, select_op, S, + (uintptr_t) Px, NULL)) ; + #else + // 64-bit platforms + GRB_TRY (GrB_Matrix_select_UINT64 (S, NULL, NULL, select_op, S, + (uintptr_t) Px, NULL)) ; + #endif GRB_TRY (GrB_Matrix_nvals (&nvals, S)) ; }