Fast IFS using RPN notation
python
c
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpn_if.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2020 Weber Yann
  3. *
  4. * This file is part of pyrpn.
  5. *
  6. * pyrpn is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * pyrpn is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __rpn_if__h__
  20. #define __rpn_if__h__
  21. #include "config.h"
  22. #include "rpn_jit.h"
  23. /**@defgroup ifs_if Iterated function
  24. * @brief Iterated RPN expression
  25. *
  26. * A single Iterated Function implemented using an RPN expression.
  27. *
  28. * @note The goal is to optimize iteration writing the code in C and providing
  29. * an high level Python API.
  30. *
  31. * For more details about IF see dedicated page : @ref doc_ifs_if
  32. */
  33. /**@brief Shortcut for struct @ref rpn_if_s */
  34. typedef struct rpn_if_s rpn_if_t;
  35. /**@brief Shortcut for struct @ref rpn_if_param_s */
  36. typedef struct rpn_if_param_s rpn_if_param_t;
  37. /**@brief Stores IF parameters and transformations function pointers */
  38. struct rpn_if_param_s
  39. {
  40. /**@brief Memory map size in items */
  41. size_t mem_sz;
  42. /**@brief Size of a memory item */
  43. size_t value_sz;
  44. /**@brief RPN expression count */
  45. size_t rpn_sz;
  46. /**@brief Number of arguments expected by RPN expressions */
  47. size_t rpn_argc;
  48. /**@brief Set RPN arguments given a position
  49. * @note transform position to arguments and fetch other arguments
  50. * from memory map*/
  51. int (*arg_f)(rpn_if_t *rif, size_t pos, rpn_value_t *args);
  52. /**@brief RPN results to data and position transformation
  53. * @note set memory maps with converted data */
  54. int (*res_f)(rpn_if_t *rif, size_t *pos,
  55. rpn_value_t *data);
  56. void *data;
  57. };
  58. /**@brief Generic Iterated function implementation */
  59. struct rpn_if_s
  60. {
  61. /**@brief IF parameters */
  62. rpn_if_param_t params;
  63. /**@brief RPN expression(s) pointer(s) */
  64. rpn_expr_t *rpn;
  65. /**@brief RPN expression(s) result(s) buffer */
  66. rpn_value_t *rpn_res;
  67. /**@brief Arguments given to RPN expression(s) buffer */
  68. rpn_value_t *rpn_args;
  69. /**@brief Memory map in wich data are fetch & stored */
  70. rpn_value_t *mem;
  71. };
  72. /**@brief Macro fetching a memory pointer given a position
  73. * @return rpn_value_t* values
  74. */
  75. #define rpn_if_getitem(rif, pos) (rif->mem + (rif->params.value_sz * pos))
  76. /**@brief Alloc a new @ref rpn_if_s from two transformation functions
  77. * @param params IF parameters
  78. * @param rpn list of RPN expresions of params->rpn_sz size
  79. * @return A pointer on an allocated @ref rpn_if_s
  80. */
  81. rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn);
  82. /**@brief Deallocate an @ref rpn_ifs_s and its ressources and close associated
  83. * @ref rpn_expr_s
  84. * @param rif The IF to deallocate
  85. */
  86. void rpn_if_free(rpn_if_t *rif);
  87. /**@brief Run an IF
  88. * @param rif Pointer on IF
  89. * @param pos Input position
  90. * @return new position
  91. */
  92. size_t rpn_if_step(rpn_if_t *rif, size_t pos);
  93. /**@brief Update all RPN expressions
  94. * @param rif The concerned IF
  95. * @param rpn A list of tokenized expressions (must be of rif->rpn_sz size)
  96. * @return 0 if no error else -1
  97. * @note Shortcut for @ref rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
  98. */
  99. int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns);
  100. /**@brief Update a range of RPN expressions
  101. * @param rif The concerned IF
  102. * @param rpn A list of tokenized expressions
  103. * @param sz Number of rpn expression in rpn argument
  104. * @param offset Start updating expressions from this offset
  105. * @return 0 if no error else -1
  106. */
  107. int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
  108. size_t offset);
  109. /**@brief New @ref rpn_if_s and partial initialisation
  110. * @param mem_sz memory size in bytes
  111. * @param argc number of arguments taken by @ref rpn_expr_s
  112. * @param rpn_count number of @ref rpn_expr_s
  113. */
  114. rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count,
  115. size_t value_sz);
  116. /**@page doc_ifs
  117. *
  118. * Iterated functions system are a composed of Iterated function choosed
  119. * randomly.
  120. *
  121. * @section doc_ifs_if Iterated function general considerations
  122. *
  123. * Iterated functions can be seen as transformations in a given space.
  124. * Functions takes items as argument and set an item as a result.
  125. *
  126. * For the moment, the main goal is to interact with a 2 dimension world with
  127. * 1 to 3 values per items (an image in grayscale or in RGB).
  128. *
  129. * A simple approach can be to use a single expression working with a single
  130. * number later decomposed in multiple composant using bitmasks (basically
  131. * for storage address and stored value).
  132. *
  133. * This can later be decomposed by assigning one (or multiple) expression
  134. * to each composant (one expression for storage address, another one for
  135. * the storage value).
  136. *
  137. * The same consideration can be done on argument number/composition taken
  138. * by the expression.
  139. *
  140. * @subsection doc_ifs_if_io Iterated function generalisation
  141. *
  142. * A generic implementation can be IF as :
  143. * - A generic input transformation system : X arguments transformed in Y
  144. * RPNExpression arguments
  145. * - A generic output system : N results (from N RPNExpr) transformed in X
  146. * results
  147. * - A generic transformation system : N expressions, taking Y arguments
  148. *
  149. * @section doc_ifs_if_api Iterated Function API
  150. *
  151. * Multiple steps are expected in API development :
  152. * - A simple generic API will be defined (something like expecting a
  153. * void* fun(void*) transforming X data in Y result using a blackbox optimized
  154. * behavior associated with a memory map
  155. * - Helper function will be written allowing C and/or Python extensions
  156. */
  157. #endif