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

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