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