1 /*
2 Copyright (C) 2015-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5
6 /*
7 WARNING: This file was generated by the dkct program (see
8 http://dktools.sourceforge.net/ for details).
9 Changes you make here will be lost if dkct is run again!
10 You should modify the original source and run dkct on it.
11 Original source: dk4maamu.ctr
12 */
13
14 /** @file dk4maamu.c The dk4maamu module.
15 */
16
17
18 #include "dk4conf.h"
19 #include <libdk4base/dk4types.h>
20 #include <libdk4base/dk4error.h>
21 #include <libdk4base/dk4numco.h>
22 #include <libdk4ma/dk4maamu.h>
23
24
25
26 #if DK4_HAVE_INTMAX_T
27
28 uintmax_t
dk4ma_uintmax_t_add(uintmax_t a,uintmax_t b,dk4_er_t * erp)29 dk4ma_uintmax_t_add(uintmax_t a, uintmax_t b, dk4_er_t *erp)
30 {
31 if ((UINTMAX_MAX - a) < b) {
32 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
33 }
34 return (uintmax_t)(a + b);
35 }
36
37
38
39 uintmax_t
dk4ma_uintmax_t_sub(uintmax_t a,uintmax_t b,dk4_er_t * erp)40 dk4ma_uintmax_t_sub(uintmax_t a, uintmax_t b, dk4_er_t *erp)
41 {
42 if (b > a) {
43 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
44 }
45 return (uintmax_t)(a - b);
46 }
47
48
49
50 uintmax_t
dk4ma_uintmax_t_mul(uintmax_t a,uintmax_t b,dk4_er_t * erp)51 dk4ma_uintmax_t_mul(uintmax_t a, uintmax_t b, dk4_er_t *erp)
52 {
53 if ((uintmax_t)0UL < a) {
54 if ((UINTMAX_MAX / a) < b) {
55 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
56 }
57 }
58 return (uintmax_t)(a * b);
59 }
60
61
62
63 uintmax_t
dk4ma_uintmax_t_div(uintmax_t a,uintmax_t b,dk4_er_t * erp)64 dk4ma_uintmax_t_div(uintmax_t a, uintmax_t b, dk4_er_t *erp)
65 {
66 if ((uintmax_t)0UL < b) {
67 return (uintmax_t)(a / b);
68 } else {
69 dk4error_set_simple_error_code(erp, DK4_E_MATH_DIVZERO);
70 return (uintmax_t)(UINTMAX_MAX);
71 }
72 }
73
74 #endif
75
76