%% options copyright owner = Dirk Krause copyright year = 2014 license = bsd %% header /** @file dk3maui.h Mathematical operations on unsigned int. */ #include #include #if DK3_HAVE_SYS_TYPES_H #include #endif #if DK3_HAVE_STDINT #include #endif #if DK3_HAVE_INTTYPES_H #include #endif #if DK3_HAVE_LIMITS_H #include #endif #if DK3_HAVE_MATH_H #include #endif #if DK3_HAVE_FLOAT_H #include #endif #ifdef __cplusplus extern "C" { #endif /** Addition. @param a Left operand. @param b Right operand. @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW when returning. @return Summary of a and b. */ unsigned dk3ma_ui_add_ok(unsigned a, unsigned b, int *ec); /** Substraction. @param a Left operand. @param b Right operand. @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW when returning. @return Difference of a and b. */ unsigned dk3ma_ui_sub_ok(unsigned a, unsigned b, int *ec); /** Multiplication. @param a Left operand. @param b Right operand. @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW when returning. @return Product of a and b. */ unsigned dk3ma_ui_mul_ok(unsigned a, unsigned b, int *ec); /** Division. @param a Left operand (nominator). @param b Right operand (denominator). @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW or DK3_ERROR_MATH_DIVZERO when returning. @return Fraction of a and b. */ unsigned dk3ma_ui_div_ok(unsigned a, unsigned b, int *ec); /** Greatest common divisor. @param a Left operand. @param b Right operand. The variable may be set to DK3_ERROR_MATH_OVERFLOW when returning. @return Greatest common divisor of a and b. */ unsigned dk3ma_ui_gcd(unsigned a, unsigned b); #ifdef __cplusplus } #endif %% module #include "dk3ma.h" $!trace-include unsigned dk3ma_ui_add_ok(unsigned a, unsigned b, int *ec) { if (NULL != ec) { if ((DK3_U_MAX - a) < b) { *ec = DK3_ERROR_MATH_OVERFLOW; } } return (a + b); } unsigned dk3ma_ui_sub_ok(unsigned a, unsigned b, int *ec) { if (NULL != ec) { if (b > a) { *ec = DK3_ERROR_MATH_OVERFLOW; } } return (a - b); } unsigned dk3ma_ui_mul_ok(unsigned a, unsigned b, int *ec) { if (NULL != ec) { if (0U != a) { if ((DK3_U_MAX / a) < b) { *ec = DK3_ERROR_MATH_OVERFLOW; } } } return (a * b); } unsigned dk3ma_ui_div_ok(unsigned a, unsigned b, int *ec) { unsigned back = (unsigned)0; if (0U != b) { back = a / b; } else { if (NULL != ec) { *ec = DK3_ERROR_MATH_DIVZERO; } back = DK3_U_MAX; } return back; } unsigned dk3ma_ui_gcd(unsigned a, unsigned b) { unsigned h; while (0U < b) { h = a % b; a = b; b = h; } if (0U == a) { a = 1U; } return a; }