arithmetic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * @file arithmetic.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Arithmetic functions for unsigned integers.
  32. *
  33. * Synopsis:
  34. * num_lesser(string n1, string n2)
  35. * num_greater(string n1, string n2)
  36. * num_lesser_equal(string n1, string n2)
  37. * num_greater_equal(string n1, string n2)
  38. * num_equal(string n1, string n2)
  39. * num_different(string n1, string n2)
  40. *
  41. * Variables:
  42. * (empty) - "true" or "false", reflecting the value of the relation in question
  43. *
  44. * Description:
  45. * These statements perform arithmetic comparisons. The operands passed must be
  46. * non-negative decimal integers representable in a uintmax_t. Otherwise, an error
  47. * is triggered.
  48. *
  49. * Synopsis:
  50. * num_add(string n1, string n2)
  51. * num_subtract(string n1, string n2)
  52. * num_multiply(string n1, string n2)
  53. * num_divide(string n1, string n2)
  54. * num_modulo(string n1, string n2)
  55. *
  56. * Description:
  57. * These statements perform arithmetic operations. The operands passed must be
  58. * non-negative decimal integers representable in a uintmax_t, and the result must
  59. * also be representable and non-negative. For divide and modulo, n2 must be non-zero.
  60. * If any of these restrictions is violated, an error is triggered.
  61. *
  62. * Variables:
  63. * is_error - whether there was an arithmetic error with the operation (true/false).
  64. * (empty) - the result of the operation as a string representing a decimal number.
  65. * If an attempt is made to access this variable after an arithmetic error,
  66. * the variable resolution will fail, and an error will be logged including
  67. * information about the particular arithemtic error.
  68. */
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include <inttypes.h>
  73. #include <limits.h>
  74. #include <misc/parse_number.h>
  75. #include <ncd/module_common.h>
  76. #include <generated/blog_channel_ncd_arithmetic.h>
  77. struct boolean_instance {
  78. NCDModuleInst *i;
  79. int value;
  80. };
  81. typedef int (*boolean_compute_func) (uintmax_t n1, uintmax_t n2);
  82. struct number_instance {
  83. NCDModuleInst *i;
  84. char const *error;
  85. uintmax_t value;
  86. };
  87. typedef char const * (*number_compute_func) (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out);
  88. static int compute_lesser (uintmax_t n1, uintmax_t n2)
  89. {
  90. return n1 < n2;
  91. }
  92. static int compute_greater (uintmax_t n1, uintmax_t n2)
  93. {
  94. return n1 > n2;
  95. }
  96. static int compute_lesser_equal (uintmax_t n1, uintmax_t n2)
  97. {
  98. return n1 <= n2;
  99. }
  100. static int compute_greater_equal (uintmax_t n1, uintmax_t n2)
  101. {
  102. return n1 >= n2;
  103. }
  104. static int compute_equal (uintmax_t n1, uintmax_t n2)
  105. {
  106. return n1 == n2;
  107. }
  108. static int compute_different (uintmax_t n1, uintmax_t n2)
  109. {
  110. return n1 != n2;
  111. }
  112. static char const * compute_add (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out)
  113. {
  114. if (n1 > UINTMAX_MAX - n2) {
  115. return "addition overflow";
  116. }
  117. *out = n1 + n2;
  118. return NULL;
  119. }
  120. static char const * compute_subtract (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out)
  121. {
  122. if (n1 < n2) {
  123. return "subtraction underflow";
  124. }
  125. *out = n1 - n2;
  126. return NULL;
  127. }
  128. static char const * compute_multiply (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out)
  129. {
  130. if (n2 != 0 && n1 > UINTMAX_MAX / n2) {
  131. return "multiplication overflow";
  132. }
  133. *out = n1 * n2;
  134. return NULL;
  135. }
  136. static char const * compute_divide (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out)
  137. {
  138. if (n2 == 0) {
  139. return "division quotient is zero";
  140. }
  141. *out = n1 / n2;
  142. return NULL;
  143. }
  144. static char const * compute_modulo (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out)
  145. {
  146. if (n2 == 0) {
  147. return "modulo modulus is zero";
  148. }
  149. *out = n1 % n2;
  150. return NULL;
  151. }
  152. static void new_boolean_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, boolean_compute_func cfunc)
  153. {
  154. struct boolean_instance *o = vo;
  155. o->i = i;
  156. NCDValRef n1_arg;
  157. NCDValRef n2_arg;
  158. if (!NCDVal_ListRead(params->args, 2, &n1_arg, &n2_arg)) {
  159. ModuleLog(i, BLOG_ERROR, "wrong arity");
  160. goto fail0;
  161. }
  162. uintmax_t n1;
  163. if (!ncd_read_uintmax(n1_arg, &n1)) {
  164. ModuleLog(o->i, BLOG_ERROR, "wrong first argument");
  165. goto fail0;
  166. }
  167. uintmax_t n2;
  168. if (!ncd_read_uintmax(n2_arg, &n2)) {
  169. ModuleLog(o->i, BLOG_ERROR, "wrong second argument");
  170. goto fail0;
  171. }
  172. o->value = cfunc(n1, n2);
  173. NCDModuleInst_Backend_Up(i);
  174. return;
  175. fail0:
  176. NCDModuleInst_Backend_DeadError(i);
  177. }
  178. static int boolean_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  179. {
  180. struct boolean_instance *o = vo;
  181. if (name == NCD_STRING_EMPTY) {
  182. *out = ncd_make_boolean(mem, o->value, o->i->params->iparams->string_index);
  183. return 1;
  184. }
  185. return 0;
  186. }
  187. static void new_number_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, number_compute_func cfunc)
  188. {
  189. struct number_instance *o = vo;
  190. o->i = i;
  191. NCDValRef n1_arg;
  192. NCDValRef n2_arg;
  193. if (!NCDVal_ListRead(params->args, 2, &n1_arg, &n2_arg)) {
  194. ModuleLog(i, BLOG_ERROR, "wrong arity");
  195. goto fail0;
  196. }
  197. uintmax_t n1;
  198. if (!ncd_read_uintmax(n1_arg, &n1)) {
  199. ModuleLog(o->i, BLOG_ERROR, "wrong first argument");
  200. goto fail0;
  201. }
  202. uintmax_t n2;
  203. if (!ncd_read_uintmax(n2_arg, &n2)) {
  204. ModuleLog(o->i, BLOG_ERROR, "wrong second argument");
  205. goto fail0;
  206. }
  207. o->error = cfunc(i, n1, n2, &o->value);
  208. NCDModuleInst_Backend_Up(i);
  209. return;
  210. fail0:
  211. NCDModuleInst_Backend_DeadError(i);
  212. }
  213. static int number_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  214. {
  215. struct number_instance *o = vo;
  216. if (name == NCD_STRING_IS_ERROR) {
  217. *out = ncd_make_boolean(mem, !!o->error, o->i->params->iparams->string_index);
  218. return 1;
  219. }
  220. if (name == NCD_STRING_EMPTY) {
  221. if (o->error) {
  222. ModuleLog(o->i, BLOG_ERROR, "%s", o->error);
  223. return 0;
  224. }
  225. *out = ncd_make_uintmax(mem, o->value);
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. static void func_new_lesser (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  231. {
  232. new_boolean_templ(vo, i, params, compute_lesser);
  233. }
  234. static void func_new_greater (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  235. {
  236. new_boolean_templ(vo, i, params, compute_greater);
  237. }
  238. static void func_new_lesser_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  239. {
  240. new_boolean_templ(vo, i, params, compute_lesser_equal);
  241. }
  242. static void func_new_greater_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  243. {
  244. new_boolean_templ(vo, i, params, compute_greater_equal);
  245. }
  246. static void func_new_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  247. {
  248. new_boolean_templ(vo, i, params, compute_equal);
  249. }
  250. static void func_new_different (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  251. {
  252. new_boolean_templ(vo, i, params, compute_different);
  253. }
  254. static void func_new_add (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  255. {
  256. new_number_templ(vo, i, params, compute_add);
  257. }
  258. static void func_new_subtract (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  259. {
  260. new_number_templ(vo, i, params, compute_subtract);
  261. }
  262. static void func_new_multiply (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  263. {
  264. new_number_templ(vo, i, params, compute_multiply);
  265. }
  266. static void func_new_divide (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  267. {
  268. new_number_templ(vo, i, params, compute_divide);
  269. }
  270. static void func_new_modulo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  271. {
  272. new_number_templ(vo, i, params, compute_modulo);
  273. }
  274. static struct NCDModule modules[] = {
  275. {
  276. .type = "num_lesser",
  277. .func_new2 = func_new_lesser,
  278. .func_getvar2 = boolean_func_getvar2,
  279. .alloc_size = sizeof(struct boolean_instance)
  280. }, {
  281. .type = "num_greater",
  282. .func_new2 = func_new_greater,
  283. .func_getvar2 = boolean_func_getvar2,
  284. .alloc_size = sizeof(struct boolean_instance)
  285. }, {
  286. .type = "num_lesser_equal",
  287. .func_new2 = func_new_lesser_equal,
  288. .func_getvar2 = boolean_func_getvar2,
  289. .alloc_size = sizeof(struct boolean_instance)
  290. }, {
  291. .type = "num_greater_equal",
  292. .func_new2 = func_new_greater_equal,
  293. .func_getvar2 = boolean_func_getvar2,
  294. .alloc_size = sizeof(struct boolean_instance)
  295. }, {
  296. .type = "num_equal",
  297. .func_new2 = func_new_equal,
  298. .func_getvar2 = boolean_func_getvar2,
  299. .alloc_size = sizeof(struct boolean_instance)
  300. }, {
  301. .type = "num_different",
  302. .func_new2 = func_new_different,
  303. .func_getvar2 = boolean_func_getvar2,
  304. .alloc_size = sizeof(struct boolean_instance)
  305. }, {
  306. .type = "num_add",
  307. .func_new2 = func_new_add,
  308. .func_getvar2 = number_func_getvar2,
  309. .alloc_size = sizeof(struct number_instance)
  310. }, {
  311. .type = "num_subtract",
  312. .func_new2 = func_new_subtract,
  313. .func_getvar2 = number_func_getvar2,
  314. .alloc_size = sizeof(struct number_instance)
  315. }, {
  316. .type = "num_multiply",
  317. .func_new2 = func_new_multiply,
  318. .func_getvar2 = number_func_getvar2,
  319. .alloc_size = sizeof(struct number_instance)
  320. }, {
  321. .type = "num_divide",
  322. .func_new2 = func_new_divide,
  323. .func_getvar2 = number_func_getvar2,
  324. .alloc_size = sizeof(struct number_instance)
  325. }, {
  326. .type = "num_modulo",
  327. .func_new2 = func_new_modulo,
  328. .func_getvar2 = number_func_getvar2,
  329. .alloc_size = sizeof(struct number_instance)
  330. }, {
  331. .type = NULL
  332. }
  333. };
  334. const struct NCDModuleGroup ncdmodule_arithmetic = {
  335. .modules = modules
  336. };