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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /**@brief Arbitrary data, if set must be freed in one free() call */
  66. void *data;
  67. };
  68. /**@brief Generic Iterated function implementation */
  69. struct rpn_if_s
  70. {
  71. /**@brief IF parameters */
  72. const rpn_if_param_t *params;
  73. /**@brief RPN expression(s) pointer(s) */
  74. rpn_expr_t *rpn;
  75. /**@brief RPN expression(s) result(s) buffer */
  76. rpn_value_t *rpn_res;
  77. /**@brief Arguments given to RPN expression(s) buffer */
  78. rpn_value_t *rpn_args;
  79. /**@brief Memory map in wich data are fetch & stored */
  80. rpn_value_t *mem;
  81. /**@brief If 1 the mmap is called at initialization time, munmap
  82. * should be called by @ref rpn_if_free */
  83. short self_mem;
  84. };
  85. /**@brief Macro fetching a memory pointer given a position
  86. * @return rpn_value_t* values
  87. */
  88. #define rpn_if_getitem(rif, pos) (rif->mem + ((rif->params->value_sz) * pos))
  89. /**@brief Alloc a new @ref rpn_if_s using given parameters
  90. * @param params IF parameters
  91. * @param rpn list of RPN expresions of params->rpn_sz size
  92. * @param memmap A suitable memory map. If NULL given, a new mmap is used
  93. * @return A pointer on an allocated @ref rpn_if_s
  94. */
  95. rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_value_t *memmap);
  96. /**@brief Deallocate an @ref rpn_if_s and its ressources and close associated
  97. * @ref rpn_expr_s
  98. * @param rif The IF to deallocate
  99. */
  100. void rpn_if_free(rpn_if_t *rif);
  101. /**@brief Run an IF
  102. * @param rif Pointer on IF
  103. * @param pos Input position
  104. * @return new position
  105. */
  106. size_t rpn_if_step(rpn_if_t *rif, size_t pos);
  107. /**@brief Returns the list of RPN expression : allowing to recompile them
  108. * @return A list of RPN expressions
  109. * @note The memory area returned must not be freed !
  110. */
  111. rpn_expr_t **rpn_if_rpn_get(rpn_if_t *rif);
  112. /**@brief New @ref rpn_if_s and partial initialisation
  113. * @param mem_sz memory size in bytes
  114. * @param rpn_argc number of arguments taken by @ref rpn_expr_s
  115. * @param rpn_count number of @ref rpn_expr_s
  116. * @param value_sz the count of rpn_value_t in one memory map value
  117. * @return a pointer on the initialized rif
  118. */
  119. rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count,
  120. size_t value_sz);
  121. /**@page doc_ifs Iterated function system
  122. * @brief Iterated function system using RPN
  123. *
  124. * Iterated functions system are a composed of Iterated function choosed
  125. * randomly.
  126. *
  127. * @section doc_ifs_if Iterated function general considerations
  128. *
  129. * Iterated functions can be seen as transformations in a given space.
  130. * Functions takes items as argument and set an item as a result.
  131. *
  132. * For the moment, the main goal is to interact with a 2 dimension world with
  133. * 1 to 3 values per items (an image in grayscale or in RGB).
  134. *
  135. * A simple approach can be to use a single expression working with a single
  136. * number later decomposed in multiple composant using bitmasks (basically
  137. * for storage address and stored value).
  138. *
  139. * This can later be decomposed by assigning one (or multiple) expression
  140. * to each composant (one expression for storage address, another one for
  141. * the storage value).
  142. *
  143. * The same consideration can be done on argument number/composition taken
  144. * by the expression.
  145. *
  146. * @subsection doc_ifs_if_io Iterated function generalisation
  147. *
  148. * A generic implementation can be IF as :
  149. * - A generic input transformation system : X arguments transformed in Y
  150. * RPNExpression arguments
  151. * - A generic output system : N results (from N RPNExpr) transformed in X
  152. * results
  153. * - A generic transformation system : N expressions, taking Y arguments
  154. *
  155. * @section doc_ifs_if_api Iterated Function API
  156. *
  157. * Multiple steps are expected in API development :
  158. * - A simple generic API will be defined (something like expecting a
  159. * void* fun(void*) transforming X data in Y result using a blackbox optimized
  160. * behavior associated with a memory map
  161. * - Helper function will be written allowing C and/or Python extensions
  162. */
  163. #endif