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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * @ingroup ifs
  25. * @brief Iterated RPN expression
  26. *
  27. * A single Iterated Function implemented using an RPN expression.
  28. *
  29. * @note The goal is to optimize iteration writing the code in C and providing
  30. * an high level Python API.
  31. *
  32. * For more details about IF see dedicated page : @ref doc_ifs_if
  33. */
  34. /**@brief Shortcut for struct @ref rpn_if_s */
  35. typedef struct rpn_if_s rpn_if_t;
  36. /**@brief Shortcut for struct @ref rpn_if_param_s */
  37. typedef struct rpn_if_param_s rpn_if_param_t;
  38. /**@brief Stores IF parameters and transformations function pointers */
  39. struct rpn_if_param_s
  40. {
  41. /**@brief Memory map size in items */
  42. size_t mem_sz;
  43. /**@brief Size of a memory item */
  44. size_t value_sz;
  45. /**@brief RPN expression count */
  46. size_t rpn_sz;
  47. /**@brief Number of arguments expected by RPN expressions */
  48. size_t rpn_argc;
  49. /**@brief Sizeof RPN expression stacks */
  50. unsigned char rpn_stack_sz;
  51. /**@brief Set RPN arguments given a position
  52. * @note transform position to arguments and fetch other arguments
  53. * from memory map*/
  54. int (*arg_f)(rpn_if_t *rif, size_t pos, rpn_value_t *args);
  55. /**@brief RPN results to data and position transformation
  56. * @note set memory maps with converted data */
  57. int (*res_f)(rpn_if_t *rif, size_t *pos,
  58. rpn_value_t *data);
  59. void *data;
  60. };
  61. /**@brief Generic Iterated function implementation */
  62. struct rpn_if_s
  63. {
  64. /**@brief IF parameters */
  65. const rpn_if_param_t *params;
  66. /**@brief RPN expression(s) pointer(s) */
  67. rpn_expr_t *rpn;
  68. /**@brief RPN expression(s) result(s) buffer */
  69. rpn_value_t *rpn_res;
  70. /**@brief Arguments given to RPN expression(s) buffer */
  71. rpn_value_t *rpn_args;
  72. /**@brief Memory map in wich data are fetch & stored */
  73. rpn_value_t *mem;
  74. /**@brief If 1 the mmap is called at initialization time, munmap
  75. * should be called by @ref rpn_if_free */
  76. short self_mem;
  77. };
  78. /**@brief Macro fetching a memory pointer given a position
  79. * @return rpn_value_t* values
  80. */
  81. #define rpn_if_getitem(rif, pos) (rif->mem + ((rif->params->value_sz) * pos))
  82. /**@brief Alloc a new @ref rpn_if_s using given parameters
  83. * @param params IF parameters
  84. * @param rpn list of RPN expresions of params->rpn_sz size
  85. * @param memmap A suitable memory map. If NULL given, a new mmap is used
  86. * @return A pointer on an allocated @ref rpn_if_s
  87. * @todo Delete rpn argument : rpn expressions are initialized and
  88. * have to be compiled later
  89. */
  90. rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn,
  91. rpn_value_t *memmap);
  92. /**@brief Deallocate an @ref rpn_if_s and its ressources and close associated
  93. * @ref rpn_expr_s
  94. * @param rif The IF to deallocate
  95. */
  96. void rpn_if_free(rpn_if_t *rif);
  97. /**@brief Run an IF
  98. * @param rif Pointer on IF
  99. * @param pos Input position
  100. * @return new position
  101. */
  102. size_t rpn_if_step(rpn_if_t *rif, size_t pos);
  103. /**@brief Update all RPN expressions
  104. * @param rif The concerned IF
  105. * @param rpns A list of tokenized expressions (must be of rif->rpn_sz size)
  106. * @return 0 if no error else -1
  107. * @note Shortcut for @ref rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
  108. */
  109. int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns);
  110. /**@brief Update a range of RPN expressions
  111. * @param rif The concerned IF
  112. * @param rpns A list of tokenized expressions
  113. * @param sz Number of rpn expression in rpn argument
  114. * @param offset Start updating expressions from this offset
  115. * @return 0 if no error else -1
  116. */
  117. int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
  118. size_t offset);
  119. /**@brief New @ref rpn_if_s and partial initialisation
  120. * @param mem_sz memory size in bytes
  121. * @param rpn_argc number of arguments taken by @ref rpn_expr_s
  122. * @param rpn_count number of @ref rpn_expr_s
  123. * @param value_sz the count of rpn_value_t in one memory map value
  124. * @return a pointer on the initialized rif
  125. */
  126. rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count,
  127. size_t value_sz);
  128. /**@page doc_ifs Iterated function system
  129. * @brief Iterated function system using RPN
  130. *
  131. * Iterated functions system are a composed of Iterated function choosed
  132. * randomly.
  133. *
  134. * @section doc_ifs_if Iterated function general considerations
  135. *
  136. * Iterated functions can be seen as transformations in a given space.
  137. * Functions takes items as argument and set an item as a result.
  138. *
  139. * For the moment, the main goal is to interact with a 2 dimension world with
  140. * 1 to 3 values per items (an image in grayscale or in RGB).
  141. *
  142. * A simple approach can be to use a single expression working with a single
  143. * number later decomposed in multiple composant using bitmasks (basically
  144. * for storage address and stored value).
  145. *
  146. * This can later be decomposed by assigning one (or multiple) expression
  147. * to each composant (one expression for storage address, another one for
  148. * the storage value).
  149. *
  150. * The same consideration can be done on argument number/composition taken
  151. * by the expression.
  152. *
  153. * @subsection doc_ifs_if_io Iterated function generalisation
  154. *
  155. * A generic implementation can be IF as :
  156. * - A generic input transformation system : X arguments transformed in Y
  157. * RPNExpression arguments
  158. * - A generic output system : N results (from N RPNExpr) transformed in X
  159. * results
  160. * - A generic transformation system : N expressions, taking Y arguments
  161. *
  162. * @section doc_ifs_if_api Iterated Function API
  163. *
  164. * Multiple steps are expected in API development :
  165. * - A simple generic API will be defined (something like expecting a
  166. * void* fun(void*) transforming X data in Y result using a blackbox optimized
  167. * behavior associated with a memory map
  168. * - Helper function will be written allowing C and/or Python extensions
  169. */
  170. #endif