arithmetic.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * (empty) - the result of the operation as a string representing a decimal number
  64. */
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. #include <string.h>
  68. #include <inttypes.h>
  69. #include <limits.h>
  70. #include <misc/parse_number.h>
  71. #include <ncd/NCDModule.h>
  72. #include <generated/blog_channel_ncd_arithmetic.h>
  73. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  74. struct instance {
  75. NCDModuleInst *i;
  76. char value[25];
  77. };
  78. typedef int (*compute_func) (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str);
  79. static int compute_lesser (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  80. {
  81. strcpy(out_str, (n1 < n2) ? "true" : "false");
  82. return 1;
  83. }
  84. static int compute_greater (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  85. {
  86. strcpy(out_str, (n1 > n2) ? "true" : "false");
  87. return 1;
  88. }
  89. static int compute_lesser_equal (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  90. {
  91. strcpy(out_str, (n1 <= n2) ? "true" : "false");
  92. return 1;
  93. }
  94. static int compute_greater_equal (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  95. {
  96. strcpy(out_str, (n1 >= n2) ? "true" : "false");
  97. return 1;
  98. }
  99. static int compute_equal (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  100. {
  101. strcpy(out_str, (n1 == n2) ? "true" : "false");
  102. return 1;
  103. }
  104. static int compute_different (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  105. {
  106. strcpy(out_str, (n1 != n2) ? "true" : "false");
  107. return 1;
  108. }
  109. static int compute_add (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  110. {
  111. if (n1 > UINTMAX_MAX - n2) {
  112. ModuleLog(i, BLOG_ERROR, "addition overflow");
  113. return 0;
  114. }
  115. uintmax_t r = n1 + n2;
  116. sprintf(out_str, "%"PRIuMAX, r);
  117. return 1;
  118. }
  119. static int compute_subtract (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  120. {
  121. if (n1 < n2) {
  122. ModuleLog(i, BLOG_ERROR, "subtraction underflow");
  123. return 0;
  124. }
  125. uintmax_t r = n1 - n2;
  126. sprintf(out_str, "%"PRIuMAX, r);
  127. return 1;
  128. }
  129. static int compute_multiply (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  130. {
  131. if (n1 > UINTMAX_MAX / n2) {
  132. ModuleLog(i, BLOG_ERROR, "multiplication overflow");
  133. return 0;
  134. }
  135. uintmax_t r = n1 * n2;
  136. sprintf(out_str, "%"PRIuMAX, r);
  137. return 1;
  138. }
  139. static int compute_divide (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  140. {
  141. if (n2 == 0) {
  142. ModuleLog(i, BLOG_ERROR, "division quotient is zero");
  143. return 0;
  144. }
  145. uintmax_t r = n1 / n2;
  146. sprintf(out_str, "%"PRIuMAX, r);
  147. return 1;
  148. }
  149. static int compute_modulo (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str)
  150. {
  151. if (n2 == 0) {
  152. ModuleLog(i, BLOG_ERROR, "modulo modulus is zero");
  153. return 0;
  154. }
  155. uintmax_t r = n1 % n2;
  156. sprintf(out_str, "%"PRIuMAX, r);
  157. return 1;
  158. }
  159. static void new_templ (NCDModuleInst *i, compute_func cfunc)
  160. {
  161. struct instance *o = malloc(sizeof(*o));
  162. if (!o) {
  163. ModuleLog(i, BLOG_ERROR, "malloc failed");
  164. goto fail0;
  165. }
  166. o->i = i;
  167. NCDModuleInst_Backend_SetUser(i, o);
  168. NCDValue *n1_arg;
  169. NCDValue *n2_arg;
  170. if (!NCDValue_ListRead(i->args, 2, &n1_arg, &n2_arg)) {
  171. ModuleLog(i, BLOG_ERROR, "wrong arity");
  172. goto fail1;
  173. }
  174. if (!NCDValue_IsStringNoNulls(n1_arg) || !NCDValue_IsStringNoNulls(n2_arg)) {
  175. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  176. goto fail1;
  177. }
  178. uintmax_t n1;
  179. uintmax_t n2;
  180. if (!parse_unsigned_integer(NCDValue_StringValue(n1_arg), &n1) || !parse_unsigned_integer(NCDValue_StringValue(n2_arg), &n2)) {
  181. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  182. goto fail1;
  183. }
  184. if (!cfunc(i, n1, n2, o->value)) {
  185. goto fail1;
  186. }
  187. NCDModuleInst_Backend_Up(i);
  188. return;
  189. fail1:
  190. free(o);
  191. fail0:
  192. NCDModuleInst_Backend_SetError(i);
  193. NCDModuleInst_Backend_Dead(i);
  194. }
  195. static void func_die (void *vo)
  196. {
  197. struct instance *o = vo;
  198. NCDModuleInst *i = o->i;
  199. // free instance
  200. free(o);
  201. NCDModuleInst_Backend_Dead(i);
  202. }
  203. static int func_getvar (void *vo, const char *name, NCDValue *out_value)
  204. {
  205. struct instance *o = vo;
  206. if (!strcmp(name, "")) {
  207. if (!NCDValue_InitString(out_value, o->value)) {
  208. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  209. return 0;
  210. }
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. static void func_new_lesser (NCDModuleInst *i)
  216. {
  217. new_templ(i, compute_lesser);
  218. }
  219. static void func_new_greater (NCDModuleInst *i)
  220. {
  221. new_templ(i, compute_greater);
  222. }
  223. static void func_new_lesser_equal (NCDModuleInst *i)
  224. {
  225. new_templ(i, compute_lesser_equal);
  226. }
  227. static void func_new_greater_equal (NCDModuleInst *i)
  228. {
  229. new_templ(i, compute_greater_equal);
  230. }
  231. static void func_new_equal (NCDModuleInst *i)
  232. {
  233. new_templ(i, compute_equal);
  234. }
  235. static void func_new_different (NCDModuleInst *i)
  236. {
  237. new_templ(i, compute_different);
  238. }
  239. static void func_new_add (NCDModuleInst *i)
  240. {
  241. new_templ(i, compute_add);
  242. }
  243. static void func_new_subtract (NCDModuleInst *i)
  244. {
  245. new_templ(i, compute_subtract);
  246. }
  247. static void func_new_multiply (NCDModuleInst *i)
  248. {
  249. new_templ(i, compute_multiply);
  250. }
  251. static void func_new_divide (NCDModuleInst *i)
  252. {
  253. new_templ(i, compute_divide);
  254. }
  255. static void func_new_modulo (NCDModuleInst *i)
  256. {
  257. new_templ(i, compute_modulo);
  258. }
  259. static const struct NCDModule modules[] = {
  260. {
  261. .type = "num_lesser",
  262. .func_new = func_new_lesser,
  263. .func_die = func_die,
  264. .func_getvar = func_getvar
  265. }, {
  266. .type = "num_greater",
  267. .func_new = func_new_greater,
  268. .func_die = func_die,
  269. .func_getvar = func_getvar
  270. }, {
  271. .type = "num_lesser_equal",
  272. .func_new = func_new_lesser_equal,
  273. .func_die = func_die,
  274. .func_getvar = func_getvar
  275. }, {
  276. .type = "num_greater_equal",
  277. .func_new = func_new_greater_equal,
  278. .func_die = func_die,
  279. .func_getvar = func_getvar
  280. }, {
  281. .type = "num_equal",
  282. .func_new = func_new_equal,
  283. .func_die = func_die,
  284. .func_getvar = func_getvar
  285. }, {
  286. .type = "num_different",
  287. .func_new = func_new_different,
  288. .func_die = func_die,
  289. .func_getvar = func_getvar
  290. }, {
  291. .type = "num_add",
  292. .func_new = func_new_add,
  293. .func_die = func_die,
  294. .func_getvar = func_getvar
  295. }, {
  296. .type = "num_subtract",
  297. .func_new = func_new_subtract,
  298. .func_die = func_die,
  299. .func_getvar = func_getvar
  300. }, {
  301. .type = "num_multiply",
  302. .func_new = func_new_multiply,
  303. .func_die = func_die,
  304. .func_getvar = func_getvar
  305. }, {
  306. .type = "num_divide",
  307. .func_new = func_new_divide,
  308. .func_die = func_die,
  309. .func_getvar = func_getvar
  310. }, {
  311. .type = "num_modulo",
  312. .func_new = func_new_modulo,
  313. .func_die = func_die,
  314. .func_getvar = func_getvar
  315. }, {
  316. .type = NULL
  317. }
  318. };
  319. const struct NCDModuleGroup ncdmodule_arithmetic = {
  320. .modules = modules
  321. };