summaryrefslogtreecommitdiff
path: root/support/dktools/dk4maami.ctr
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/dktools/dk4maami.ctr
Initial commit
Diffstat (limited to 'support/dktools/dk4maami.ctr')
-rw-r--r--support/dktools/dk4maami.ctr236
1 files changed, 236 insertions, 0 deletions
diff --git a/support/dktools/dk4maami.ctr b/support/dktools/dk4maami.ctr
new file mode 100644
index 0000000000..b3bd550267
--- /dev/null
+++ b/support/dktools/dk4maami.ctr
@@ -0,0 +1,236 @@
+%% options
+
+copyright owner = Dirk Krause
+copyright year = 2015-xxxx
+license = bsd
+
+
+%% header
+
+/** @file
+ Arithmetic operations on intmax_t.
+
+ 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
+
+#if DK4_HAVE_INTMAX_T
+
+/** Absolute value.
+ @param a Operand.
+ @param erp Error report, may be NULL.
+ @return Result on success, INTMAX_MAX if a is INTMAX_MIN
+ (overflow is reported in erp).
+
+ Error codes: DK4_E_OVERFLOW.
+*/
+intmax_t
+dk4ma_intmax_t_abs(intmax_t a, dk4_er_t *erp);
+
+/** 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.
+*/
+intmax_t
+dk4ma_intmax_t_add(intmax_t a, intmax_t 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.
+*/
+intmax_t
+dk4ma_intmax_t_sub(intmax_t a, intmax_t 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.
+*/
+intmax_t
+dk4ma_intmax_t_mul(intmax_t a, intmax_t 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,
+ INTMAX_MIN, INTMAX_MAX, or 0 on division by zero or overflow
+ (see the ec component of erp).
+
+ Error codes: DK4_E_OVERFLOW, DK4_E_DIVZERO.
+*/
+intmax_t
+dk4ma_intmax_t_div(intmax_t a, intmax_t b, dk4_er_t *erp);
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+
+%% module
+
+#include "dk4conf.h"
+#include "dk4types.h"
+#include "dk4error.h"
+#include "dk4numco.h"
+#include "dk4maami.h"
+
+
+
+#if DK4_HAVE_INTMAX_T
+
+intmax_t
+dk4ma_intmax_t_abs(intmax_t a, dk4_er_t *erp)
+{
+ if (INTMAX_MIN == a) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ return (((intmax_t)0L <= a) ? a : ((INTMAX_MIN == a) ? INTMAX_MAX : ((intmax_t)0L - a)));
+}
+
+
+
+intmax_t
+dk4ma_intmax_t_add(intmax_t a, intmax_t b, dk4_er_t *erp)
+{
+ if (((intmax_t)0L < a) && ((intmax_t)0L < b)) {
+ if ((INTMAX_MAX - a) < b) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ } else {
+ if (((intmax_t)0L > a) && ((intmax_t)0L > b)) {
+ if (INTMAX_MIN == a) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ } else {
+ if ((INTMAX_MIN - a) > b) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ }
+ }
+ }
+ return (a + b);
+}
+
+
+
+intmax_t
+dk4ma_intmax_t_sub(intmax_t a, intmax_t b, dk4_er_t *erp)
+{
+ if (((intmax_t)0L < a) && ((intmax_t)0L > b)) {
+ if ((INTMAX_MAX + b) < a) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ } else {
+ if (((intmax_t)0L > a) && ((intmax_t)0L < b)) {
+ if ((INTMAX_MIN + b) > a) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ }
+ }
+ return (a - b);
+}
+
+
+
+intmax_t
+dk4ma_intmax_t_mul(intmax_t a, intmax_t b, dk4_er_t *erp)
+{
+ if (((intmax_t)0L != a) && ((intmax_t)0L != b)) {
+ if (INTMAX_MIN == a) {
+ if ((intmax_t)1L != b) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ } else {
+ if (INTMAX_MIN == b) {
+ if ((intmax_t)1L != a) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ } else {
+ if ((intmax_t)0L < a) {
+ if ((intmax_t)0L < b) {
+ if ((INTMAX_MAX / a) < b) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ } else {
+ if ((INTMAX_MIN / a) > b) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ }
+ } else {
+ if ((intmax_t)0L < b) {
+ if ((INTMAX_MIN / b) > a) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ } else {
+ if ((INTMAX_MAX / ((intmax_t)0L - a)) < ((intmax_t)0L - b)) {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ }
+ }
+ }
+ }
+ }
+ }
+ return (a * b);
+}
+
+
+
+intmax_t
+dk4ma_intmax_t_div(intmax_t a, intmax_t b, dk4_er_t *erp)
+{
+ intmax_t back = 0;
+ if ((intmax_t)0L != b) {
+ if ((INTMAX_MIN == a) && ((intmax_t)(-1L) == b)) {
+ back = INTMAX_MAX;
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ } else {
+ back = a / b;
+ }
+ } else {
+ if ((intmax_t)0L < a) {
+ back = INTMAX_MAX;
+ } else {
+ if ((intmax_t)0L > a) {
+ back = INTMAX_MIN;
+ }
+ }
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_DIVZERO);
+ }
+ return back;
+}
+
+#endif
+