diff options
Diffstat (limited to 'support/dktools/dk3maui.ctr')
-rw-r--r-- | support/dktools/dk3maui.ctr | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/support/dktools/dk3maui.ctr b/support/dktools/dk3maui.ctr new file mode 100644 index 0000000000..da637fd464 --- /dev/null +++ b/support/dktools/dk3maui.ctr @@ -0,0 +1,169 @@ +%% options + +copyright owner = Dirk Krause +copyright year = 2014-xxxx +license = bsd + + + +%% header + +/** @file dk3maui.h Mathematical operations on unsigned int. +*/ + +#include <dk3conf.h> + +#include <stdio.h> +#if DK3_HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#if DK3_HAVE_STDINT +#include <stdint.h> +#endif +#if DK3_HAVE_INTTYPES_H +#include <inttypes.h> +#endif +#if DK3_HAVE_LIMITS_H +#include <limits.h> +#endif +#if DK3_HAVE_MATH_H +#include <math.h> +#endif +#if DK3_HAVE_FLOAT_H +#include <float.h> +#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; +} + |