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/dk4maaui.ctr |
Initial commit
Diffstat (limited to 'support/dktools/dk4maaui.ctr')
-rw-r--r-- | support/dktools/dk4maaui.ctr | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/support/dktools/dk4maaui.ctr b/support/dktools/dk4maaui.ctr new file mode 100644 index 0000000000..35a57ce74b --- /dev/null +++ b/support/dktools/dk4maaui.ctr @@ -0,0 +1,143 @@ +%% options + +copyright owner = Dirk Krause +copyright year = 2015-xxxx +license = bsd + + + +%% header + +/** @file + Arithmetic operations on unsigned int. + + CRT on Windows: Optional. +*/ + +#ifndef DK4CONF_H_INCLUDED +#include "dk4conf.h" +#endif + +#ifndef DK4TYPES_H_INCLUDED +#include "dk4types.h" +#endif + +#ifndef DK4ERROR_H_INCLUDED +#include "dk4error.h" +#endif + + + +#ifdef __cplusplus +extern "C" { +#endif + +/** Addition. + @param a Left operand. + @param b Right operand. + @param erp Error report, may be NULL. + @return Result on success, least significant result bits + on overflow (see ec component of erp). + + Error codes: DK4_E_OVERFLOW. +*/ +unsigned +dk4ma_uint_add(unsigned a, unsigned b, dk4_er_t *erp); + +/** Subtraction. + @param a Left operand. + @param b Right operand. + @param erp Error report, may be NULL. + @return Result on success, least significant result bits + on overflow (see ec component of erp). + + Error codes: DK4_E_OVERFLOW. +*/ +unsigned +dk4ma_uint_sub(unsigned a, unsigned b, dk4_er_t *erp); + +/** Multiplication. + @param a Left factor. + @param b right factor. + @param erp Error report, may be NULL. + @return Result on success, least significant + bits on overflow (see ec component of erp). + + Error codes: DK4_E_OVERFLOW. +*/ +unsigned +dk4ma_uint_mul(unsigned a, unsigned b, dk4_er_t *erp); + +/** Division. + @param a Counter (left operand). + @param b Denominator (right operand). + @param erp Error report, may be NULL. + @return Division result on success, + UINT_MAX on division by zero + (see the ec component of erp). + + Error codes: DK4_E_DIVZERO. +*/ +unsigned +dk4ma_uint_div(unsigned a, unsigned b, dk4_er_t *erp); + +#ifdef __cplusplus +} +#endif + + +%% module + +#include "dk4conf.h" +#include "dk4types.h" +#include "dk4error.h" +#include "dk4numco.h" +#include "dk4maaui.h" + + +unsigned +dk4ma_uint_add(unsigned a, unsigned b, dk4_er_t *erp) +{ + if ((UINT_MAX - a) < b) { + dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW); + } + return (unsigned)(a + b); +} + + + +unsigned +dk4ma_uint_sub(unsigned a, unsigned b, dk4_er_t *erp) +{ + if (b > a) { + dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW); + } + return (unsigned)(a - b); +} + + + +unsigned +dk4ma_uint_mul(unsigned a, unsigned b, dk4_er_t *erp) +{ + if (0U < a) { + if ((UINT_MAX / a) < b) { + dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW); + } + } + return (unsigned)(a * b); +} + + + +unsigned +dk4ma_uint_div(unsigned a, unsigned b, dk4_er_t *erp) +{ + if (0U < b) { + return (unsigned)(a / b); + } else { + dk4error_set_simple_error_code(erp, DK4_E_MATH_DIVZERO); + return (unsigned)(UINT_MAX); + } +} + |