1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-11-15 05:45:55 +05:30

opt_int_div.h: extract sign explanation

This commit is contained in:
Intel A80486DX2-66 2024-03-09 17:48:05 +03:00
parent e8d0b5a091
commit fef612cec2
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -20,7 +20,9 @@
// helper functions // helper functions
#define INT_BIN_DIV(a, b) ((a) >> (uintmax_t) log2l((b))) #define INT_BIN_DIV(a, b) ((a) >> (uintmax_t) log2l((b)))
#define INT_DIV_NEG_RESULT_SIGN(a, b) (((a) < 0) ^ ((b) < 0)) #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 INT_ABS(x) ((x) < 0 ? -(x) : (x))
// the main macro // the main macro
@ -28,8 +30,7 @@
( /* beginning */ \ ( /* beginning */ \
/* check for equality of a and b */ \ /* check for equality of a and b */ \
INT_ABS((a)) == INT_ABS((b)) ? \ INT_ABS((a)) == INT_ABS((b)) ? \
/* if only one of the numbers is negative, the result is -1, */ \ /* the result is +/-1 */ \
/* otherwise it is 1. */ \
(INT_DIV_NEG_RESULT_SIGN(a, b) ? -1 : 1) : ( \ (INT_DIV_NEG_RESULT_SIGN(a, b) ? -1 : 1) : ( \
\ \
/* check if b is a power of 2 */ \ /* check if b is a power of 2 */ \
@ -38,8 +39,6 @@
/* after point) == 0 */ \ /* after point) == 0 */ \
(roundl(fmodl(log2l(INT_ABS((b))), 1.l) * OPT_INT_DIV_ROUNDING) \ (roundl(fmodl(log2l(INT_ABS((b))), 1.l) * OPT_INT_DIV_ROUNDING) \
== 0.l) ? ( \ == 0.l) ? ( \
/* if only one of the numbers is negative, the result is */ \
/* negative */ \
INT_DIV_NEG_RESULT_SIGN(a, b) ? \ INT_DIV_NEG_RESULT_SIGN(a, b) ? \
-INT_BIN_DIV(INT_ABS(a), INT_ABS(b)) \ -INT_BIN_DIV(INT_ABS(a), INT_ABS(b)) \
: \ : \