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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * @brief Iterated RPN expression
  25. *
  26. * A single Iterated Function implemented using an RPN expression.
  27. *
  28. * @note The goal is to optimize iteration writing the code in C and providing
  29. * an high level Python API.
  30. *
  31. * For more details about IF see dedicated page : @ref doc_ifs_if
  32. */
  33. typedef void* rpn_if_arg_t;
  34. typedef unsigned long rpn_arg_t;
  35. typedef struct rpn_if_s rpn_if_t;
  36. typedef struct rpn_if_res_s rpn_if_res_t;
  37. typedef struct rpn_if_state_s rpn_if_state_t;
  38. typedef struct rpn_if_transfo_s rpn_if_transfo_t;
  39. typedef enum rpn_if_transfo_type_e rpn_if_transfo_type_t;
  40. /**@brief IF state to RPN arguments transformation */
  41. typedef void (*if2rpn_f)(rpn_if_t *rif, rpn_if_state_t, rpn_arg_t*);
  42. /**@brief RPN arguments to IF state transformation */
  43. typedef rpn_if_res_t (*rpn2if_f)(rpn_if_t *rif, rpn_arg_t, rpn_if_state_t);
  44. /**@brief IF state
  45. *
  46. * Will always be something like :
  47. * - memory adress/offset/index
  48. * - value
  49. */
  50. struct rpn_if_state_s
  51. {
  52. /**@brief Data address */
  53. size_t i;
  54. /**@brief Data value(s) */
  55. void *val;
  56. };
  57. /**@brief Indicate function type for @ref rpn_if_transfo_s */
  58. enum rpn_if_transfo_type_e
  59. {
  60. /**@brief No transformation
  61. *
  62. * Default behavior is to copy data in args directly assuming
  63. * argc * sizeof(unsigned long) = data_sz */
  64. RPN_f_null,
  65. /**@brief Transform RPN result into data */
  66. RPN_f_rpn2if,
  67. /**@brief Transform data into RPN result */
  68. RPN_f_if2rpn
  69. };
  70. /**@brief Represent an IF transformation function
  71. *
  72. * Can transform data into arguments or arguments into data, depending
  73. * on function type.
  74. */
  75. struct rpn_if_transfo_s
  76. {
  77. /**@brief Function pointer type
  78. * @note optionnal, for type checking
  79. */
  80. rpn_if_transfo_type_t type;
  81. /**@brief Data size (a @ref rpn_if_s::mem item ) in bytes */
  82. size_t data_sz;
  83. /**@brief Memory size in byte */
  84. size_t mem_sz;
  85. /**@brief RPN arg/result size
  86. *
  87. * - if type is RPN_if2rpn argc is the rpn expression argc
  88. * - if type is RPN_rpn2if argc is the rpn expression count (results
  89. * count)
  90. */
  91. size_t argc;
  92. union {
  93. /**@brief IF state to RPN arguments transformation */
  94. if2rpn_f if2rpn;
  95. /**@brief RPN arguments to IF state transformation */
  96. rpn2if_f rpn2if;
  97. };
  98. };
  99. /**@brief Generic Iterated function implementation */
  100. struct rpn_if_s
  101. {
  102. /**@brief Memory map in wich data are fetch & stored */
  103. void *mem;
  104. /**@brief Memory map size in bytes */
  105. size_t mem_sz;
  106. /**@brief Size of a memory item */
  107. size_t value_sz;
  108. /**@brief IF last position + value buffer */
  109. rpn_if_state_t state;
  110. /**@brief RPN expression(s) result(s) buffer */
  111. unsigned long *rpn_res;
  112. /**@brief Arguments given to RPN expression(s) buffer */
  113. rpn_arg_t *rpn_args;
  114. /**@brief Number of arguments expected by RPN expressions */
  115. size_t rpn_argc;
  116. /**@brief RPN expression(s) pointer(s) */
  117. rpn_expr_t *rpn;
  118. /**@brief RPN expression count */
  119. size_t rpn_sz;
  120. /**@brief IF state to RPN arguments transformation */
  121. if2rpn_f if2rpn;
  122. /**@brief RPN arguments to IF state transformation */
  123. rpn2if_f rpn2if;
  124. };
  125. /**@brief Alloc a new @ref rpn_if_s from two transformation functions
  126. * @param if2rpn informations about data to rpn args transformation
  127. * @param rpn2if informations about rpn args to data transformation
  128. * @param rpn list of RPN expresions ( must be of rpn2if->argc size !!!)
  129. * @return A pointer on an allocated @ref rpn_if_s
  130. */
  131. rpn_if_t* rpn_if_new(size_t mem_sz, rpn_if_transfo_t *if2rpn,
  132. rpn_if_transfo_t *rpn2if, rpn_expr_t *rpn);
  133. /**@brief Deallocate an @ref rpn_ifs_s and close associated @ref rpn_expr_s
  134. * @param rif The IF to deallocate
  135. */
  136. void rpn_if_free(rpn_if_t *rif);
  137. /**@brief Update all RPN expressions
  138. * @param rif The concerned IF
  139. * @param rpn A list of tokenized expressions (must be of rif->rpn_sz size)
  140. * @return 0 if no error else -1
  141. * @note Shortcut for @ref rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
  142. */
  143. int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns);
  144. /**@brief Update a range of RPN expressions
  145. * @param rif The concerned IF
  146. * @param rpn A list of tokenized expressions
  147. * @param sz Number of rpn expression in rpn argument
  148. * @param offset Start updating expressions from this offset
  149. * @return 0 if no error else -1
  150. */
  151. int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
  152. size_t offset);
  153. /**@brief New @ref rpn_if_s and partial initialisation
  154. * @param mem_sz memory size in bytes
  155. * @param argc number of arguments taken by @ref rpn_expr_s
  156. * @param rpn_count number of @ref rpn_expr_s
  157. */
  158. rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count,
  159. size_t value_sz);
  160. /**@page doc_ifs
  161. *
  162. * Iterated functions system are a composed of Iterated function choosed
  163. * randomly.
  164. *
  165. * @section doc_ifs_if Iterated function general considerations
  166. *
  167. * Iterated functions can be seen as transformations in a given space.
  168. * Functions takes items as argument and set an item as a result.
  169. *
  170. * For the moment, the main goal is to interact with a 2 dimension world with
  171. * 1 to 3 values per items (an image in grayscale or in RGB).
  172. *
  173. * A simple approach can be to use a single expression working with a single
  174. * number later decomposed in multiple composant using bitmasks (basically
  175. * for storage address and stored value).
  176. *
  177. * This can later be decomposed by assigning one (or multiple) expression
  178. * to each composant (one expression for storage address, another one for
  179. * the storage value).
  180. *
  181. * The same consideration can be done on argument number/composition taken
  182. * by the expression.
  183. *
  184. * @subsection doc_ifs_if_io Iterated function generalisation
  185. *
  186. * A generic implementation can be IF as :
  187. * - A generic input transformation system : X arguments transformed in Y
  188. * RPNExpression arguments
  189. * - A generic output system : N results (from N RPNExpr) transformed in X
  190. * results
  191. * - A generic transformation system : N expressions, taking Y arguments
  192. *
  193. * @section doc_ifs_if_api Iterated Function API
  194. *
  195. * Multiple steps are expected in API development :
  196. * - A simple generic API will be defined (something like expecting a
  197. * void* fun(void*) transforming X data in Y result using a blackbox optimized
  198. * behavior associated with a memory map
  199. * - Helper function will be written allowing C and/or Python extensions
  200. */
  201. #endif