| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- /**
- * @file arithmetic.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @section DESCRIPTION
- *
- * Arithmetic functions for unsigned integers.
- *
- * Synopsis:
- * num_lesser(string n1, string n2)
- * num_greater(string n1, string n2)
- * num_lesser_equal(string n1, string n2)
- * num_greater_equal(string n1, string n2)
- * num_equal(string n1, string n2)
- * num_different(string n1, string n2)
- *
- * Variables:
- * (empty) - "true" or "false", reflecting the value of the relation in question
- *
- * Description:
- * These statements perform arithmetic comparisons. The operands passed must be
- * non-negative decimal integers representable in a uintmax_t. Otherwise, an error
- * is triggered.
- *
- * Synopsis:
- * num_add(string n1, string n2)
- * num_subtract(string n1, string n2)
- * num_multiply(string n1, string n2)
- * num_divide(string n1, string n2)
- * num_modulo(string n1, string n2)
- *
- * Description:
- * These statements perform arithmetic operations. The operands passed must be
- * non-negative decimal integers representable in a uintmax_t, and the result must
- * also be representable and non-negative. For divide and modulo, n2 must be non-zero.
- * If any of these restrictions is violated, an error is triggered.
- *
- * Variables:
- * (empty) - the result of the operation as a string representing a decimal number
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <inttypes.h>
- #include <limits.h>
- #include <misc/parse_number.h>
- #include <ncd/NCDModule.h>
- #include <ncd/static_strings.h>
- #include <ncd/static_strings_utils.h>
- #include <generated/blog_channel_ncd_arithmetic.h>
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- struct boolean_instance {
- NCDModuleInst *i;
- int value;
- };
- typedef int (*boolean_compute_func) (uintmax_t n1, uintmax_t n2);
- struct number_instance {
- NCDModuleInst *i;
- char value[25];
- };
- typedef int (*number_compute_func) (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str);
- static int compute_lesser (uintmax_t n1, uintmax_t n2)
- {
- return n1 < n2;
- }
- static int compute_greater (uintmax_t n1, uintmax_t n2)
- {
- return n1 > n2;
- }
- static int compute_lesser_equal (uintmax_t n1, uintmax_t n2)
- {
- return n1 <= n2;
- }
- static int compute_greater_equal (uintmax_t n1, uintmax_t n2)
- {
- return n1 >= n2;
- }
- static int compute_equal (uintmax_t n1, uintmax_t n2)
- {
- return n1 == n2;
- }
- static int compute_different (uintmax_t n1, uintmax_t n2)
- {
- return n1 != n2;
- }
- static int compute_add (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
- {
- if (n1 > UINTMAX_MAX - n2) {
- ModuleLog(i, BLOG_ERROR, "addition overflow");
- return 0;
- }
- uintmax_t r = n1 + n2;
- sprintf(out_str, "%"PRIuMAX, r);
- return 1;
- }
- static int compute_subtract (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
- {
- if (n1 < n2) {
- ModuleLog(i, BLOG_ERROR, "subtraction underflow");
- return 0;
- }
- uintmax_t r = n1 - n2;
- sprintf(out_str, "%"PRIuMAX, r);
- return 1;
- }
- static int compute_multiply (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
- {
- if (n1 > UINTMAX_MAX / n2) {
- ModuleLog(i, BLOG_ERROR, "multiplication overflow");
- return 0;
- }
- uintmax_t r = n1 * n2;
- sprintf(out_str, "%"PRIuMAX, r);
- return 1;
- }
- static int compute_divide (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
- {
- if (n2 == 0) {
- ModuleLog(i, BLOG_ERROR, "division quotient is zero");
- return 0;
- }
- uintmax_t r = n1 / n2;
- sprintf(out_str, "%"PRIuMAX, r);
- return 1;
- }
- static int compute_modulo (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
- {
- if (n2 == 0) {
- ModuleLog(i, BLOG_ERROR, "modulo modulus is zero");
- return 0;
- }
- uintmax_t r = n1 % n2;
- sprintf(out_str, "%"PRIuMAX, r);
- return 1;
- }
- static void new_boolean_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, boolean_compute_func cfunc)
- {
- struct boolean_instance *o = vo;
- o->i = i;
-
- NCDValRef n1_arg;
- NCDValRef n2_arg;
- if (!NCDVal_ListRead(params->args, 2, &n1_arg, &n2_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsStringNoNulls(n1_arg) || !NCDVal_IsStringNoNulls(n2_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
-
- uintmax_t n1;
- uintmax_t n2;
- if (!parse_unsigned_integer(NCDVal_StringValue(n1_arg), &n1) || !parse_unsigned_integer(NCDVal_StringValue(n2_arg), &n2)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong value");
- goto fail0;
- }
-
- o->value = cfunc(n1, n2);
-
- NCDModuleInst_Backend_Up(i);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static int boolean_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
- {
- struct boolean_instance *o = vo;
-
- if (name == NCD_STRING_EMPTY) {
- *out = ncd_make_boolean(mem, o->value, o->i->params->iparams->string_index);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
- }
- return 1;
- }
-
- return 0;
- }
- static void new_number_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, number_compute_func cfunc)
- {
- struct number_instance *o = vo;
- o->i = i;
-
- NCDValRef n1_arg;
- NCDValRef n2_arg;
- if (!NCDVal_ListRead(params->args, 2, &n1_arg, &n2_arg)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsStringNoNulls(n1_arg) || !NCDVal_IsStringNoNulls(n2_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
-
- uintmax_t n1;
- uintmax_t n2;
- if (!parse_unsigned_integer(NCDVal_StringValue(n1_arg), &n1) || !parse_unsigned_integer(NCDVal_StringValue(n2_arg), &n2)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong value");
- goto fail0;
- }
-
- if (!cfunc(i, n1, n2, o->value)) {
- goto fail0;
- }
-
- NCDModuleInst_Backend_Up(i);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static int number_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
- {
- struct number_instance *o = vo;
-
- if (name == NCD_STRING_EMPTY) {
- *out = NCDVal_NewString(mem, o->value);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
- }
- return 1;
- }
-
- return 0;
- }
- static void func_new_lesser (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_boolean_templ(vo, i, params, compute_lesser);
- }
- static void func_new_greater (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_boolean_templ(vo, i, params, compute_greater);
- }
- static void func_new_lesser_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_boolean_templ(vo, i, params, compute_lesser_equal);
- }
- static void func_new_greater_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_boolean_templ(vo, i, params, compute_greater_equal);
- }
- static void func_new_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_boolean_templ(vo, i, params, compute_equal);
- }
- static void func_new_different (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_boolean_templ(vo, i, params, compute_different);
- }
- static void func_new_add (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_number_templ(vo, i, params, compute_add);
- }
- static void func_new_subtract (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_number_templ(vo, i, params, compute_subtract);
- }
- static void func_new_multiply (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_number_templ(vo, i, params, compute_multiply);
- }
- static void func_new_divide (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_number_templ(vo, i, params, compute_divide);
- }
- static void func_new_modulo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
- {
- new_number_templ(vo, i, params, compute_modulo);
- }
- static struct NCDModule modules[] = {
- {
- .type = "num_lesser",
- .func_new2 = func_new_lesser,
- .func_getvar2 = boolean_func_getvar2,
- .alloc_size = sizeof(struct boolean_instance)
- }, {
- .type = "num_greater",
- .func_new2 = func_new_greater,
- .func_getvar2 = boolean_func_getvar2,
- .alloc_size = sizeof(struct boolean_instance)
- }, {
- .type = "num_lesser_equal",
- .func_new2 = func_new_lesser_equal,
- .func_getvar2 = boolean_func_getvar2,
- .alloc_size = sizeof(struct boolean_instance)
- }, {
- .type = "num_greater_equal",
- .func_new2 = func_new_greater_equal,
- .func_getvar2 = boolean_func_getvar2,
- .alloc_size = sizeof(struct boolean_instance)
- }, {
- .type = "num_equal",
- .func_new2 = func_new_equal,
- .func_getvar2 = boolean_func_getvar2,
- .alloc_size = sizeof(struct boolean_instance)
- }, {
- .type = "num_different",
- .func_new2 = func_new_different,
- .func_getvar2 = boolean_func_getvar2,
- .alloc_size = sizeof(struct boolean_instance)
- }, {
- .type = "num_add",
- .func_new2 = func_new_add,
- .func_getvar2 = number_func_getvar2,
- .alloc_size = sizeof(struct number_instance)
- }, {
- .type = "num_subtract",
- .func_new2 = func_new_subtract,
- .func_getvar2 = number_func_getvar2,
- .alloc_size = sizeof(struct number_instance)
- }, {
- .type = "num_multiply",
- .func_new2 = func_new_multiply,
- .func_getvar2 = number_func_getvar2,
- .alloc_size = sizeof(struct number_instance)
- }, {
- .type = "num_divide",
- .func_new2 = func_new_divide,
- .func_getvar2 = number_func_getvar2,
- .alloc_size = sizeof(struct number_instance)
- }, {
- .type = "num_modulo",
- .func_new2 = func_new_modulo,
- .func_getvar2 = number_func_getvar2,
- .alloc_size = sizeof(struct number_instance)
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_arithmetic = {
- .modules = modules
- };
|