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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <assert.h>
  23. #include "rpn_jit.h"
  24. /**@file rpn_if.h
  25. * @ingroup ifs_if
  26. * @brief Iterated functions headers
  27. *
  28. * Iterated functions structure + API definition
  29. */
  30. /**@defgroup ifs_if Iterated function
  31. * @ingroup ifs
  32. * @brief Iterated RPN expression
  33. *
  34. * @note For more details about Iterated function see dedicated
  35. * section : @ref doc_ifs_if
  36. *
  37. * Iterated function are function that can be iterated on successive position
  38. * in a memory map. The function takes its arguments from a position in the
  39. * memory map and output its results at a resulting position in the memory map.
  40. *
  41. * The iterated function can be seen as a function taking a position as argument
  42. * and outputing another position with borders effects from data stored at given
  43. * positions.
  44. *
  45. * The implementation try to be as generic as possible, allowing to implement
  46. * the transformation using a @ref rpn_expr_t system. The iterated function
  47. * has 2 functions as parameters :
  48. * - The first one @ref rpn_if_param_s.getarg_f transform a position
  49. * to argument(s) and fetch the other arguments from the memory map
  50. * - The second @ref rpn_if_param_s.setres_f takes the results from the
  51. * @ref rpn_expr_t system, transform it in position and results and set
  52. * the memory map at resulting position with results.
  53. *
  54. * Based on this generic implementation @ref ifs_if_default are implemented for
  55. * various common cases : mutli-dimentionnal positions and constants, boolean,
  56. * colors as results.
  57. */
  58. /**@brief Shortcut for struct @ref rpn_if_s */
  59. typedef struct rpn_if_s rpn_if_t;
  60. /**@brief Shortcut for struct @ref rpn_if_param_s */
  61. typedef struct rpn_if_param_s rpn_if_param_t;
  62. /**@brief Stores IF parameters and transformations function pointers */
  63. struct rpn_if_param_s
  64. {
  65. /**@brief Memory map size in items */
  66. size_t mem_sz;
  67. /**@brief Size of a memory item in bytes (should be a multiple of
  68. sizeof(rpn_value_t))*/
  69. size_t value_sz;
  70. /**@brief RPN expression count */
  71. size_t rpn_sz;
  72. /**@brief Number of arguments expected by RPN expressions */
  73. size_t rpn_argc;
  74. /**@brief Sizeof RPN expression stacks */
  75. unsigned char rpn_stack_sz;
  76. /**@brief Set RPN arguments given a position
  77. * @note transform position to arguments and fetch other arguments
  78. * from memory map*/
  79. int (*getarg_f)(rpn_if_t *rif, size_t pos);
  80. /**@brief RPN results to data and position transformation
  81. * @note set memory maps with converted data */
  82. int (*setres_f)(rpn_if_t *rif, size_t *pos);
  83. /**@brief Arbitrary data, if set must be freed in one free() call */
  84. void *data;
  85. };
  86. /**@brief Generic Iterated function implementation */
  87. struct rpn_if_s
  88. {
  89. /**@brief IF parameters */
  90. const rpn_if_param_t *params;
  91. /**@brief RPN expression(s) pointer(s) */
  92. rpn_expr_t *rpn;
  93. /**@brief RPN expression(s) result(s) buffer */
  94. rpn_value_t *rpn_res;
  95. /**@brief Arguments given to RPN expression(s) buffer */
  96. rpn_value_t *rpn_args;
  97. /**@brief Memory map in wich data are fetch & stored */
  98. rpn_value_t *mem;
  99. /**@brief If 1 the mmap is called at initialization time, munmap
  100. * should be called by @ref rpn_if_free */
  101. short self_mem;
  102. };
  103. /**@brief Macro fetching a memory pointer given a position
  104. * @return rpn_value_t* values
  105. */
  106. #define rpn_if_getitem(rif, pos) (rpn_value_t*)(((unsigned char*)rif->mem + ((rif->params->value_sz) * pos)))
  107. /**@brief Alloc a new @ref rpn_if_s using given parameters
  108. * @param params IF parameters
  109. * @param memmap A suitable memory map. If NULL given, a new mmap is used
  110. * @param init_exprs If no NULL uses this expression (steal refs), else uses
  111. * new empty expressions
  112. * @return A pointer on an allocated @ref rpn_if_s
  113. */
  114. rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_value_t *memmap,
  115. rpn_expr_t *init_exprs);
  116. /**@brief Deallocate an @ref rpn_if_s and its ressources and close associated
  117. * @ref rpn_expr_s
  118. * @param rif The IF to deallocate
  119. */
  120. void rpn_if_free(rpn_if_t *rif);
  121. /**@brief Run an IF
  122. * @param rif Pointer on IF
  123. * @param pos Input position
  124. * @return new position
  125. */
  126. size_t rpn_if_step(rpn_if_t *rif, size_t pos);
  127. /**@brief Returns the list of RPN expression : allowing to recompile them
  128. * @param rif Pointer on IF
  129. * @return A list of RPN expressions
  130. * @note The memory area returned must not be freed !
  131. */
  132. rpn_expr_t **rpn_if_rpn_get(rpn_if_t *rif);
  133. /**@brief New @ref rpn_if_s and partial initialisation
  134. * @param mem_sz memory size in bytes
  135. * @param rpn_argc number of arguments taken by @ref rpn_expr_s
  136. * @param rpn_count number of @ref rpn_expr_s
  137. * @param value_sz the count of rpn_value_t in one memory map value
  138. * @return a pointer on the initialized rif
  139. */
  140. rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count,
  141. size_t value_sz);
  142. /**@page doc_ifs Iterated function system
  143. * @brief Iterated function system using RPN
  144. *
  145. * Iterated functions system are a composed of Iterated function choosed
  146. * randomly.
  147. *
  148. * @section doc_ifs_if Iterated function general considerations
  149. *
  150. * Iterated functions can be seen as transformations in a given space.
  151. * Functions takes items as argument and set an item as a result.
  152. *
  153. * For the moment, the main goal is to interact with a 2 dimension world with
  154. * 1 to 3 values per items (an image in grayscale or in RGB).
  155. *
  156. * A simple approach can be to use a single expression working with a single
  157. * number later decomposed in multiple composant using bitmasks (basically
  158. * for storage address and stored value).
  159. *
  160. * This can later be decomposed by assigning one (or multiple) expression
  161. * to each composant (one expression for storage address, another one for
  162. * the storage value).
  163. *
  164. * The same consideration can be done on argument number/composition taken
  165. * by the expression.
  166. *
  167. * @subsection doc_ifs_if_io Iterated function generalisation
  168. *
  169. * A generic implementation can be IF as :
  170. * - A generic input transformation system : X arguments transformed in Y
  171. * RPNExpression arguments
  172. * - A generic output system : N results (from N RPNExpr) transformed in X
  173. * results
  174. * - A generic transformation system : N expressions, taking Y arguments
  175. *
  176. * @section doc_ifs_if_api Iterated Function API
  177. *
  178. * Multiple steps are expected in API development :
  179. * - A simple generic API will be defined (something like expecting a
  180. * void* fun(void*) transforming X data in Y result using a blackbox optimized
  181. * behavior associated with a memory map
  182. * - Helper function will be written allowing C and/or Python extensions
  183. */
  184. #endif