/** * @file arithmetic.c * @author Ambroz Bizjak * * @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 #include #include #include #include #include #include #include #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__) struct instance { NCDModuleInst *i; char value[25]; }; typedef int (*compute_func) (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str); static int compute_lesser (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str) { strcpy(out_str, (n1 < n2) ? "true" : "false"); return 1; } static int compute_greater (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str) { strcpy(out_str, (n1 > n2) ? "true" : "false"); return 1; } static int compute_lesser_equal (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str) { strcpy(out_str, (n1 <= n2) ? "true" : "false"); return 1; } static int compute_greater_equal (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str) { strcpy(out_str, (n1 >= n2) ? "true" : "false"); return 1; } static int compute_equal (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str) { strcpy(out_str, (n1 == n2) ? "true" : "false"); return 1; } static int compute_different (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str) { strcpy(out_str, (n1 != n2) ? "true" : "false"); return 1; } 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_templ (void *vo, NCDModuleInst *i, compute_func cfunc) { struct instance *o = vo; o->i = i; NCDValRef n1_arg; NCDValRef n2_arg; if (!NCDVal_ListRead(i->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 func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out) { struct instance *o = vo; if (!strcmp(name, "")) { *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) { new_templ(vo, i, compute_lesser); } static void func_new_greater (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_greater); } static void func_new_lesser_equal (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_lesser_equal); } static void func_new_greater_equal (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_greater_equal); } static void func_new_equal (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_equal); } static void func_new_different (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_different); } static void func_new_add (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_add); } static void func_new_subtract (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_subtract); } static void func_new_multiply (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_multiply); } static void func_new_divide (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_divide); } static void func_new_modulo (void *vo, NCDModuleInst *i) { new_templ(vo, i, compute_modulo); } static const struct NCDModule modules[] = { { .type = "num_lesser", .func_new2 = func_new_lesser, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_greater", .func_new2 = func_new_greater, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_lesser_equal", .func_new2 = func_new_lesser_equal, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_greater_equal", .func_new2 = func_new_greater_equal, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_equal", .func_new2 = func_new_equal, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_different", .func_new2 = func_new_different, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_add", .func_new2 = func_new_add, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_subtract", .func_new2 = func_new_subtract, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_multiply", .func_new2 = func_new_multiply, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_divide", .func_new2 = func_new_divide, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = "num_modulo", .func_new2 = func_new_modulo, .func_getvar = func_getvar, .alloc_size = sizeof(struct instance) }, { .type = NULL } }; const struct NCDModuleGroup ncdmodule_arithmetic = { .modules = modules };