arithmetic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <ncd/NCDModule.h>
  71. #include <ncd/static_strings.h>
  72. #include <ncd/value_utils.h>
  73. #include <generated/blog_channel_ncd_arithmetic.h>
  74. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  75. struct boolean_instance {
  76. NCDModuleInst *i;
  77. int value;
  78. };
  79. typedef int (*boolean_compute_func) (uintmax_t n1, uintmax_t n2);
  80. struct number_instance {
  81. NCDModuleInst *i;
  82. char value[25];
  83. };
  84. typedef int (*number_compute_func) (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *out_str);
  85. static int compute_lesser (uintmax_t n1, uintmax_t n2)
  86. {
  87. return n1 < n2;
  88. }
  89. static int compute_greater (uintmax_t n1, uintmax_t n2)
  90. {
  91. return n1 > n2;
  92. }
  93. static int compute_lesser_equal (uintmax_t n1, uintmax_t n2)
  94. {
  95. return n1 <= n2;
  96. }
  97. static int compute_greater_equal (uintmax_t n1, uintmax_t n2)
  98. {
  99. return n1 >= n2;
  100. }
  101. static int compute_equal (uintmax_t n1, uintmax_t n2)
  102. {
  103. return n1 == n2;
  104. }
  105. static int compute_different (uintmax_t n1, uintmax_t n2)
  106. {
  107. return n1 != n2;
  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_boolean_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, boolean_compute_func cfunc)
  160. {
  161. struct boolean_instance *o = vo;
  162. o->i = i;
  163. NCDValRef n1_arg;
  164. NCDValRef n2_arg;
  165. if (!NCDVal_ListRead(params->args, 2, &n1_arg, &n2_arg)) {
  166. ModuleLog(i, BLOG_ERROR, "wrong arity");
  167. goto fail0;
  168. }
  169. if (!NCDVal_IsString(n1_arg) || !NCDVal_IsString(n2_arg)) {
  170. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  171. goto fail0;
  172. }
  173. uintmax_t n1;
  174. if (!ncd_read_uintmax(n1_arg, &n1)) {
  175. ModuleLog(o->i, BLOG_ERROR, "wrong first argument");
  176. goto fail0;
  177. }
  178. uintmax_t n2;
  179. if (!ncd_read_uintmax(n2_arg, &n2)) {
  180. ModuleLog(o->i, BLOG_ERROR, "wrong second argument");
  181. goto fail0;
  182. }
  183. o->value = cfunc(n1, n2);
  184. NCDModuleInst_Backend_Up(i);
  185. return;
  186. fail0:
  187. NCDModuleInst_Backend_SetError(i);
  188. NCDModuleInst_Backend_Dead(i);
  189. }
  190. static int boolean_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  191. {
  192. struct boolean_instance *o = vo;
  193. if (name == NCD_STRING_EMPTY) {
  194. *out = ncd_make_boolean(mem, o->value, o->i->params->iparams->string_index);
  195. if (NCDVal_IsInvalid(*out)) {
  196. ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
  197. }
  198. return 1;
  199. }
  200. return 0;
  201. }
  202. static void new_number_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, number_compute_func cfunc)
  203. {
  204. struct number_instance *o = vo;
  205. o->i = i;
  206. NCDValRef n1_arg;
  207. NCDValRef n2_arg;
  208. if (!NCDVal_ListRead(params->args, 2, &n1_arg, &n2_arg)) {
  209. ModuleLog(i, BLOG_ERROR, "wrong arity");
  210. goto fail0;
  211. }
  212. if (!NCDVal_IsString(n1_arg) || !NCDVal_IsString(n2_arg)) {
  213. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  214. goto fail0;
  215. }
  216. uintmax_t n1;
  217. if (!ncd_read_uintmax(n1_arg, &n1)) {
  218. ModuleLog(o->i, BLOG_ERROR, "wrong first argument");
  219. goto fail0;
  220. }
  221. uintmax_t n2;
  222. if (!ncd_read_uintmax(n2_arg, &n2)) {
  223. ModuleLog(o->i, BLOG_ERROR, "wrong second argument");
  224. goto fail0;
  225. }
  226. if (!cfunc(i, n1, n2, o->value)) {
  227. goto fail0;
  228. }
  229. NCDModuleInst_Backend_Up(i);
  230. return;
  231. fail0:
  232. NCDModuleInst_Backend_SetError(i);
  233. NCDModuleInst_Backend_Dead(i);
  234. }
  235. static int number_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  236. {
  237. struct number_instance *o = vo;
  238. if (name == NCD_STRING_EMPTY) {
  239. *out = NCDVal_NewString(mem, o->value);
  240. if (NCDVal_IsInvalid(*out)) {
  241. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  242. }
  243. return 1;
  244. }
  245. return 0;
  246. }
  247. static void func_new_lesser (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  248. {
  249. new_boolean_templ(vo, i, params, compute_lesser);
  250. }
  251. static void func_new_greater (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  252. {
  253. new_boolean_templ(vo, i, params, compute_greater);
  254. }
  255. static void func_new_lesser_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  256. {
  257. new_boolean_templ(vo, i, params, compute_lesser_equal);
  258. }
  259. static void func_new_greater_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  260. {
  261. new_boolean_templ(vo, i, params, compute_greater_equal);
  262. }
  263. static void func_new_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  264. {
  265. new_boolean_templ(vo, i, params, compute_equal);
  266. }
  267. static void func_new_different (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  268. {
  269. new_boolean_templ(vo, i, params, compute_different);
  270. }
  271. static void func_new_add (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  272. {
  273. new_number_templ(vo, i, params, compute_add);
  274. }
  275. static void func_new_subtract (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  276. {
  277. new_number_templ(vo, i, params, compute_subtract);
  278. }
  279. static void func_new_multiply (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  280. {
  281. new_number_templ(vo, i, params, compute_multiply);
  282. }
  283. static void func_new_divide (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  284. {
  285. new_number_templ(vo, i, params, compute_divide);
  286. }
  287. static void func_new_modulo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  288. {
  289. new_number_templ(vo, i, params, compute_modulo);
  290. }
  291. static struct NCDModule modules[] = {
  292. {
  293. .type = "num_lesser",
  294. .func_new2 = func_new_lesser,
  295. .func_getvar2 = boolean_func_getvar2,
  296. .alloc_size = sizeof(struct boolean_instance)
  297. }, {
  298. .type = "num_greater",
  299. .func_new2 = func_new_greater,
  300. .func_getvar2 = boolean_func_getvar2,
  301. .alloc_size = sizeof(struct boolean_instance)
  302. }, {
  303. .type = "num_lesser_equal",
  304. .func_new2 = func_new_lesser_equal,
  305. .func_getvar2 = boolean_func_getvar2,
  306. .alloc_size = sizeof(struct boolean_instance)
  307. }, {
  308. .type = "num_greater_equal",
  309. .func_new2 = func_new_greater_equal,
  310. .func_getvar2 = boolean_func_getvar2,
  311. .alloc_size = sizeof(struct boolean_instance)
  312. }, {
  313. .type = "num_equal",
  314. .func_new2 = func_new_equal,
  315. .func_getvar2 = boolean_func_getvar2,
  316. .alloc_size = sizeof(struct boolean_instance)
  317. }, {
  318. .type = "num_different",
  319. .func_new2 = func_new_different,
  320. .func_getvar2 = boolean_func_getvar2,
  321. .alloc_size = sizeof(struct boolean_instance)
  322. }, {
  323. .type = "num_add",
  324. .func_new2 = func_new_add,
  325. .func_getvar2 = number_func_getvar2,
  326. .alloc_size = sizeof(struct number_instance)
  327. }, {
  328. .type = "num_subtract",
  329. .func_new2 = func_new_subtract,
  330. .func_getvar2 = number_func_getvar2,
  331. .alloc_size = sizeof(struct number_instance)
  332. }, {
  333. .type = "num_multiply",
  334. .func_new2 = func_new_multiply,
  335. .func_getvar2 = number_func_getvar2,
  336. .alloc_size = sizeof(struct number_instance)
  337. }, {
  338. .type = "num_divide",
  339. .func_new2 = func_new_divide,
  340. .func_getvar2 = number_func_getvar2,
  341. .alloc_size = sizeof(struct number_instance)
  342. }, {
  343. .type = "num_modulo",
  344. .func_new2 = func_new_modulo,
  345. .func_getvar2 = number_func_getvar2,
  346. .alloc_size = sizeof(struct number_instance)
  347. }, {
  348. .type = NULL
  349. }
  350. };
  351. const struct NCDModuleGroup ncdmodule_arithmetic = {
  352. .modules = modules
  353. };