From 5049a3977e346806beb586500ca9ff19d5441b07 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Mon, 22 Apr 2024 00:21:44 +0300 Subject: [PATCH] opt_int_div.h: use a faster algorithm of checking --- c-programming/math/opt_int_div.h | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/c-programming/math/opt_int_div.h b/c-programming/math/opt_int_div.h index 15b48e8..a45f1fd 100644 --- a/c-programming/math/opt_int_div.h +++ b/c-programming/math/opt_int_div.h @@ -12,31 +12,18 @@ #include #include -// parameters -#define OPT_INT_DIV_IS_TEST_PRECISION_LIMITED 0 /* true (1) or false (0) */ -#define OPT_INT_DIV_TEST_PRECISION 8 /* only if the above parameter is 1 */ - -// constants -#define OPT_INT_DIV_ROUNDING \ - /* formula: 10^OPT_INT_DIV_TEST_PRECISION */ \ - powl(10.l, (long double) OPT_INT_DIV_TEST_PRECISION) - // helper functions -#define INT_BIN_DIV(a, b) ((a) >> (uintmax_t) log2l((b))) +#define INT_BIN_DIV(a, b) ((a) >> (uintmax_t) log2l((b))) /* NOTE: log2l may + slow things down */ #define INT_DIV_NEG_RESULT_SIGN(a, b) \ /* the sign is negative only if one of the numbers is negative */ \ (((a) < 0) != ((b) < 0)) /* 1 if sign is negative else 0 */ #define INT_ABS(x) ((x) < 0 ? -(x) : (x)) -#define LOG2_DEC_PORTION(b) fmodl(log2l(INT_ABS((b))), 1.l) #define OPT_INT_DIV_TEST(b) \ /* check if b is a power of 2 */ \ /* */ \ - /* formula: round(log_2(b) % 1, OPT_INT_DIV_TEST_PRECISION digits */ \ - /* after point) == 0 */ \ - OPT_INT_DIV_IS_TEST_PRECISION_LIMITED ? \ - roundl(LOG2_DEC_PORTION((b)) * OPT_INT_DIV_ROUNDING) == 0.l \ - : \ - LOG2_DEC_PORTION((b)) == 0.l + /* formula: ( X & (X - 1) ) == 0 */ \ + ((b) & ((b) - 1)) == 0 // the main macro #define OPT_INT_DIV(a, b) \