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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_value_t *memmap);
  91. /**@brief Deallocate an @ref rpn_if_s and its ressources and close associated
  92. * @ref rpn_expr_s
  93. * @param rif The IF to deallocate
  94. */
  95. void rpn_if_free(rpn_if_t *rif);
  96. /**@brief Run an IF
  97. * @param rif Pointer on IF
  98. * @param pos Input position
  99. * @return new position
  100. */
  101. size_t rpn_if_step(rpn_if_t *rif, size_t pos);
  102. /**@brief Returns the list of RPN expression : allowing to recompile them
  103. * @return A list of RPN expressions
  104. * @note The memory area returned must not be freed !
  105. */
  106. rpn_expr_t **rpn_if_rpn_get(rpn_if_t *rif);
  107. /**@brief New @ref rpn_if_s and partial initialisation
  108. * @param mem_sz memory size in bytes
  109. * @param rpn_argc number of arguments taken by @ref rpn_expr_s
  110. * @param rpn_count number of @ref rpn_expr_s
  111. * @param value_sz the count of rpn_value_t in one memory map value
  112. * @return a pointer on the initialized rif
  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 Iterated function system
  117. * @brief Iterated function system using RPN
  118. *
  119. * Iterated functions system are a composed of Iterated function choosed
  120. * randomly.
  121. *
  122. * @section doc_ifs_if Iterated function general considerations
  123. *
  124. * Iterated functions can be seen as transformations in a given space.
  125. * Functions takes items as argument and set an item as a result.
  126. *
  127. * For the moment, the main goal is to interact with a 2 dimension world with
  128. * 1 to 3 values per items (an image in grayscale or in RGB).
  129. *
  130. * A simple approach can be to use a single expression working with a single
  131. * number later decomposed in multiple composant using bitmasks (basically
  132. * for storage address and stored value).
  133. *
  134. * This can later be decomposed by assigning one (or multiple) expression
  135. * to each composant (one expression for storage address, another one for
  136. * the storage value).
  137. *
  138. * The same consideration can be done on argument number/composition taken
  139. * by the expression.
  140. *
  141. * @subsection doc_ifs_if_io Iterated function generalisation
  142. *
  143. * A generic implementation can be IF as :
  144. * - A generic input transformation system : X arguments transformed in Y
  145. * RPNExpression arguments
  146. * - A generic output system : N results (from N RPNExpr) transformed in X
  147. * results
  148. * - A generic transformation system : N expressions, taking Y arguments
  149. *
  150. * @section doc_ifs_if_api Iterated Function API
  151. *
  152. * Multiple steps are expected in API development :
  153. * - A simple generic API will be defined (something like expecting a
  154. * void* fun(void*) transforming X data in Y result using a blackbox optimized
  155. * behavior associated with a memory map
  156. * - Helper function will be written allowing C and/or Python extensions
  157. */
  158. #endif