diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/dktools/dk3maus.ctr |
Initial commit
Diffstat (limited to 'support/dktools/dk3maus.ctr')
-rw-r--r-- | support/dktools/dk3maus.ctr | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/support/dktools/dk3maus.ctr b/support/dktools/dk3maus.ctr new file mode 100644 index 0000000000..cfbb8cff15 --- /dev/null +++ b/support/dktools/dk3maus.ctr @@ -0,0 +1,169 @@ +%% options + +copyright owner = Dirk Krause +copyright year = 2014-xxxx +license = bsd + + + +%% header + +/** @file dk3maus.h Mathematical operations on unsigned short. +*/ + +#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 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; +} + |