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: dk4maal.ctr
12 */
13
14 /** @file dk4maal.c The dk4maal 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/dk4maal.h>
23
24
25
26 long
dk4ma_long_abs(long a,dk4_er_t * erp)27 dk4ma_long_abs(long a, dk4_er_t *erp)
28 {
29 if (LONG_MIN == a) {
30 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
31 }
32 return ((0L <= a) ? a : ((LONG_MIN == a) ? LONG_MAX : (0L - a)));
33 }
34
35
36
37 long
dk4ma_long_add(long a,long b,dk4_er_t * erp)38 dk4ma_long_add(long a, long b, dk4_er_t *erp)
39 {
40 if ((0L < a) && (0L < b)) {
41 if ((LONG_MAX - a) < b) {
42 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
43 }
44 } else {
45 if ((0L > a) && (0L > b)) {
46 if (LONG_MIN == a) {
47 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
48 } else {
49 if ((LONG_MIN - a) > b) {
50 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
51 }
52 }
53 }
54 }
55 return (a + b);
56 }
57
58
59
60 long
dk4ma_long_sub(long a,long b,dk4_er_t * erp)61 dk4ma_long_sub(long a, long b, dk4_er_t *erp)
62 {
63 if ((0L < a) && (0L > b)) {
64 if ((LONG_MAX + b) < a) {
65 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
66 }
67 } else {
68 if ((0L > a) && (0L < b)) {
69 if ((LONG_MIN + b) > a) {
70 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
71 }
72 }
73 }
74 return (a - b);
75 }
76
77
78
79 long
dk4ma_long_mul(long a,long b,dk4_er_t * erp)80 dk4ma_long_mul(long a, long b, dk4_er_t *erp)
81 {
82 if ((0L != a) && (0L != b)) {
83 if (LONG_MIN == a) {
84 if (1L != b) {
85 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
86 }
87 } else {
88 if (LONG_MIN == b) {
89 if (1L != a) {
90 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
91 }
92 } else {
93 if (0L < a) {
94 if (0L < b) {
95 if ((LONG_MAX / a) < b) {
96 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
97 }
98 } else {
99 if ((LONG_MIN / a) > b) {
100 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
101 }
102 }
103 } else {
104 if (0L < b) {
105 if ((LONG_MIN / b) > a) {
106 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
107 }
108 } else {
109 if ((LONG_MAX / (0L - a)) < (0L - b)) {
110 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
111 }
112 }
113 }
114 }
115 }
116 }
117 return (a * b);
118 }
119
120
121
122 long
dk4ma_long_div(long a,long b,dk4_er_t * erp)123 dk4ma_long_div(long a, long b, dk4_er_t *erp)
124 {
125 long back = 0;
126 if (0L != b) {
127 if ((LONG_MIN == a) && (-1L == b)) {
128 back = LONG_MAX;
129 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
130 } else {
131 back = a / b;
132 }
133 } else {
134 if (0L < a) {
135 back = LONG_MAX;
136 } else {
137 if (0L > a) {
138 back = LONG_MIN;
139 }
140 }
141 dk4error_set_simple_error_code(erp, DK4_E_MATH_DIVZERO);
142 }
143 return back;
144 }
145
146
147
148 long
dk4ma_long_from(dk4_im_t i,dk4_er_t * erp)149 dk4ma_long_from(dk4_im_t i, dk4_er_t *erp)
150 {
151 if (((dk4_im_t)(LONG_MAX) < i) || ((dk4_im_t)(LONG_MIN) > i)) {
152 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
153 }
154 return ((long)i);
155 }
156
157
158
159 long
dk4ma_long_from_double(double d,dk4_er_t * erp)160 dk4ma_long_from_double(double d, dk4_er_t *erp)
161 {
162 long back = 0L;
163 if ((double)(LONG_MIN) > d) {
164 dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
165 back = LONG_MIN;
166 }
167 else {
168 if ((double)(LONG_MAX) < d) {
169 dk4error_set_simple_error_code(erp,DK4_E_MATH_OVERFLOW);
170 back = LONG_MAX;
171 }
172 else {
173 back = (long)d;
174 }
175 }
176 return back;
177 }
178