Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_function_integer.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * afw_function_execute_* functions for Integer
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
18 /*
19  * Adaptive function: abs<integer>
20  *
21  * afw_function_execute_abs_integer
22  *
23  * See afw_function_bindings.h for more information.
24  *
25  * Compute the absolute value of the integer value and return the integer
26  * result.
27  *
28  * This function is pure, so it will always return the same result
29  * given exactly the same parameters and has no side effects.
30  *
31  * Declaration:
32  *
33  * ```
34  * function abs<integer>(
35  * value: integer
36  * ): integer;
37  * ```
38  *
39  * Parameters:
40  *
41  * value - (integer)
42  *
43  * Returns:
44  *
45  * (integer)
46  */
47 const afw_value_t *
50 {
51  const afw_value_integer_t *arg;
52 
54 
55  return (arg->internal >= 0)
56  ? (const afw_value_t *)arg
57  : afw_value_create_integer(-(arg->internal), x->p, x->xctx);
58 }
59 
60 
61 
62 /*
63  * Adaptive function: add<integer>
64  *
65  * afw_function_execute_add_integer
66  *
67  * See afw_function_bindings.h for more information.
68  *
69  * Add 2 or more integer values and return the integer result.
70  *
71  * This function is pure, so it will always return the same result
72  * given exactly the same parameters and has no side effects.
73  *
74  * Declaration:
75  *
76  * ```
77  * function add<integer>(
78  * values_1: integer,
79  * values_2: integer,
80  * ...values_rest: (list of integer)
81  * ): integer;
82  * ```
83  *
84  * Parameters:
85  *
86  * values - (2 or more integer)
87  *
88  * Returns:
89  *
90  * (integer)
91  */
92 const afw_value_t *
95 {
96  const afw_value_integer_t *arg;
97  afw_integer_t sum;
98  afw_size_t n;
99 
100  for (sum = 0, n = 1; n <= x->argc; n++) {
102  if ((arg->internal > 0 && (sum > AFW_INTEGER_MAX - arg->internal)) ||
103  (arg->internal < 0 && (sum < AFW_INTEGER_MIN - arg->internal)))
104  {
105  AFW_THROW_ERROR_Z(arg_error, "Integer add overflow", x->xctx);
106  }
107  sum += arg->internal;
108  }
109 
110  return afw_value_create_integer(sum, x->p, x->xctx);
111 }
112 
113 
114 
115 /*
116  * Adaptive function: divide<integer>
117  *
118  * afw_function_execute_divide_integer
119  *
120  * See afw_function_bindings.h for more information.
121  *
122  * Divide integer dividend by integer divisor and return the integer quotient.
123  *
124  * This function is pure, so it will always return the same result
125  * given exactly the same parameters and has no side effects.
126  *
127  * Declaration:
128  *
129  * ```
130  * function divide<integer>(
131  * dividend: integer,
132  * divisor: integer
133  * ): integer;
134  * ```
135  *
136  * Parameters:
137  *
138  * dividend - (integer)
139  *
140  * divisor - (integer)
141  *
142  * Returns:
143  *
144  * (integer)
145  */
146 const afw_value_t *
149 {
150  const afw_value_integer_t *arg1;
151  const afw_value_integer_t *arg2;
152 
155 
156  if (arg2->internal == 0) {
157  AFW_THROW_ERROR_Z(arg_error, "Integer divide by zero error", x->xctx);
158  }
159 
161  arg1->internal / arg2->internal,
162  x->p, x->xctx);
163 }
164 
165 
166 
167 /*
168  * Adaptive function: mod<integer>
169  *
170  * afw_function_execute_mod_integer
171  *
172  * See afw_function_bindings.h for more information.
173  *
174  * Divide integer dividend by integer divisor and return the integer remainder.
175  *
176  * This function is pure, so it will always return the same result
177  * given exactly the same parameters and has no side effects.
178  *
179  * Declaration:
180  *
181  * ```
182  * function mod<integer>(
183  * dividend: integer,
184  * divisor: integer
185  * ): integer;
186  * ```
187  *
188  * Parameters:
189  *
190  * dividend - (integer)
191  *
192  * divisor - (integer)
193  *
194  * Returns:
195  *
196  * (integer)
197  */
198 const afw_value_t *
201 {
202  const afw_value_integer_t *arg1;
203  const afw_value_integer_t *arg2;
204 
207 
208  if (arg2->internal == 0) {
209  AFW_THROW_ERROR_Z(arg_error, "Integer divide by zero error", x->xctx);
210  }
211 
213  arg1->internal % arg2->internal,
214  x->p, x->xctx);
215 }
216 
217 
218 
219 /*
220  * Adaptive function: multiply<integer>
221  *
222  * afw_function_execute_multiply_integer
223  *
224  * See afw_function_bindings.h for more information.
225  *
226  * Multiply 2 or more integer values and return the integer result.
227  *
228  * This function is pure, so it will always return the same result
229  * given exactly the same parameters and has no side effects.
230  *
231  * Declaration:
232  *
233  * ```
234  * function multiply<integer>(
235  * values_1: integer,
236  * values_2: integer,
237  * ...values_rest: (list of integer)
238  * ): integer;
239  * ```
240  *
241  * Parameters:
242  *
243  * values - (2 or more integer)
244  *
245  * Returns:
246  *
247  * (integer)
248  */
249 const afw_value_t *
252 {
253  const afw_value_integer_t *arg;
254  afw_integer_t result, next;
255  afw_size_t n;
256 
257  for (result = 1, next = 1, n = 1; n <= x->argc; n++) {
259  next *= arg->internal;
260  if (result != 0 && next / result != arg->internal) {
261  AFW_THROW_ERROR_Z(arg_error, "Integer multiply overflow", x->xctx);
262  }
263  result = next;
264  }
265 
266  return afw_value_create_integer(result, x->p, x->xctx);
267 }
268 
269 
270 
271 /*
272  * Adaptive function: subtract<integer>
273  *
274  * afw_function_execute_subtract_integer
275  *
276  * See afw_function_bindings.h for more information.
277  *
278  * Subtract integer arg2 from integer arg1 and return the integer result.
279  *
280  * This function is pure, so it will always return the same result
281  * given exactly the same parameters and has no side effects.
282  *
283  * Declaration:
284  *
285  * ```
286  * function subtract<integer>(
287  * arg1: integer,
288  * arg2: integer
289  * ): integer;
290  * ```
291  *
292  * Parameters:
293  *
294  * arg1 - (integer)
295  *
296  * arg2 - (integer)
297  *
298  * Returns:
299  *
300  * (integer)
301  */
302 const afw_value_t *
305 {
306  const afw_value_integer_t *arg1;
307  const afw_value_integer_t *arg2;
308 
311 
312 
313  if ((-arg2->internal < 0 && (arg1->internal < AFW_INTEGER_MIN - -arg2->internal)) ||
314  (-arg2->internal > 0 && (arg1->internal > AFW_INTEGER_MAX - -arg2->internal)))
315  {
316  AFW_THROW_ERROR_Z(arg_error, "Integer subtract overflow", x->xctx);
317  }
318 
320  arg1->internal - arg2->internal,
321  x->p, x->xctx);
322 }
323 
324 
325 
326 /*
327  * Adaptive function: to_double<integer>
328  *
329  * afw_function_execute_to_double_integer
330  *
331  * See afw_function_bindings.h for more information.
332  *
333  * Converts integer arg to double and returns double result.
334  *
335  * This function is pure, so it will always return the same result
336  * given exactly the same parameters.
337  *
338  * Parameters:
339  * arg - (integer)
340  *
341  * Returns:
342  * (double)
343  */
344 const afw_value_t *
347 {
348  const afw_value_integer_t *arg;
349 
351 
353  (afw_double_t)arg->internal,
354  x->p, x->xctx);
355 }
356 
357 
358 
359 /*
360  * Adaptive function: negative<integer>
361  *
362  * afw_function_execute_negative_integer
363  *
364  * See afw_function_bindings.h for more information.
365  *
366  * Return negative of integer value.
367  *
368  * This function is pure, so it will always return the same result
369  * given exactly the same parameters and has no side effects.
370  *
371  * Declaration:
372  *
373  * ```
374  * function negative<integer>(
375  * value: integer
376  * ): integer;
377  * ```
378  *
379  * Parameters:
380  *
381  * value - (integer)
382  *
383  * Returns:
384  *
385  * (integer)
386  */
387 const afw_value_t *
390 {
391  const afw_value_integer_t *arg;
392 
394 
395  return afw_value_create_integer(-arg->internal, x->p, x->xctx);
396 }
Adaptive Framework Core Internal.
afw_value_create_double(double internal, const afw_pool_t *p, afw_xctx_t *xctx)
Create function for unmanaged data type double value.
afw_value_create_integer(afw_integer_t internal, const afw_pool_t *p, afw_xctx_t *xctx)
Create function for unmanaged data type integer value.
double afw_double_t
Normal AFW number is double.
Definition: afw_common.h:202
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
#define AFW_INTEGER_MAX
largest afw_integer_t
Definition: afw_common.h:281
#define AFW_INTEGER_MIN
smallest afw_integer_t
Definition: afw_common.h:291
apr_int64_t afw_integer_t
typedef for big signed int.
Definition: afw_common.h:321
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
#define AFW_FUNCTION_EVALUATE_REQUIRED_DATA_TYPE_PARAMETER(A_RESULT, A_N, A_TYPE)
Evaluate an arg for a particular data type.
Definition: afw_function.h:328
const afw_value_t * afw_function_execute_multiply_integer(afw_function_execute_t *x)
Adaptive Function multiply<integer>
const afw_value_t * afw_function_execute_divide_integer(afw_function_execute_t *x)
Adaptive Function divide<integer>
const afw_value_t * afw_function_execute_subtract_integer(afw_function_execute_t *x)
Adaptive Function subtract<integer>
const afw_value_t * afw_function_execute_negative_integer(afw_function_execute_t *x)
Adaptive Function negative<integer>
const afw_value_t * afw_function_execute_mod_integer(afw_function_execute_t *x)
Adaptive Function mod<integer>
const afw_value_t * afw_function_execute_abs_integer(afw_function_execute_t *x)
Adaptive Function abs<integer>
const afw_value_t * afw_function_execute_add_integer(afw_function_execute_t *x)
Adaptive Function add<integer>
const afw_value_t * afw_function_execute_to_double_integer(afw_function_execute_t *x)
Function implementation function afw_function_execute_to_double_integer.
Function execute parameter.
Definition: afw_function.h:53
afw_xctx_t * xctx
The execution context (xctx) of caller.
Definition: afw_function.h:62
const afw_pool_t * p
Pool for result.
Definition: afw_function.h:59
afw_size_t argc
This is the argv count not counting argv[0].
Definition: afw_function.h:89
struct for data type integer values.
Interface afw_value public struct.