%% options copyright owner = Dirk Krause copyright year = 2014 license = bsd %% header /** @file dk3maus.h Mathematical operations on unsigned short. */ #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 short dk3ma_us_add_ok(unsigned short a, unsigned short 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 short dk3ma_us_sub_ok(unsigned short a, unsigned short 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 short dk3ma_us_mul_ok(unsigned short a, unsigned short 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 short dk3ma_us_div_ok(unsigned short a, unsigned short 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 short dk3ma_us_gcd(unsigned short a, unsigned short b); #ifdef __cplusplus } #endif %% module #include "dk3ma.h" $!trace-include unsigned short dk3ma_us_add_ok(unsigned short a, unsigned short b, int *ec) { if (NULL != ec) { if ((DK3_US_MAX - a) < b) { *ec = DK3_ERROR_MATH_OVERFLOW; } } return (a + b); } unsigned short dk3ma_us_sub_ok(unsigned short a, unsigned short b, int *ec) { if (NULL != ec) { if (b > a) { *ec = DK3_ERROR_MATH_OVERFLOW; } } return (a - b); } unsigned short dk3ma_us_mul_ok(unsigned short a, unsigned short b, int *ec) { if (NULL != ec) { if (0 != a) { if ((DK3_US_MAX / a) < b) { *ec = DK3_ERROR_MATH_OVERFLOW; } } } return (a * b); } unsigned short dk3ma_us_div_ok(unsigned short a, unsigned short b, int *ec) { unsigned short back = (unsigned short)0; if (0 != b) { back = a / b; } else { if (NULL != ec) { *ec = DK3_ERROR_MATH_DIVZERO; } back = DK3_US_MAX; } return back; } unsigned short dk3ma_us_gcd(unsigned short a, unsigned short b) { unsigned short h; while (0 < b) { h = a % b; a = b; b = h; } if (0 == a) { a = (unsigned short)1; } return a; }