arithmetic.c 9.4 KB

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