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_ifs.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_ifs__h__
  20. #define __rpn_ifs__h__
  21. #include "config.h"
  22. #include <assert.h>
  23. #include <string.h>
  24. #include "rpn_jit.h"
  25. #include "rpn_if.h"
  26. /**@file rpn_ifs.h
  27. * @brief IFS header
  28. * @ingroup ifs
  29. */
  30. /**@defgroup ifs Iterated function system
  31. * @brief IFS implementation with RPN expressions
  32. *
  33. * IFS are basically a list of @ref ifs_if associated with a probability
  34. * of evaluation.
  35. *
  36. * This implementation aims to :
  37. * - optimize @ref ifs_if calls
  38. * - optimize random number generation and IF random calls
  39. *
  40. * @note It aims to provide an API close to @ref ifs_if API, in order to
  41. * be able to use both IFS and IF almost tansparently via Python API.
  42. */
  43. /**@brief struct @ref rpn_ifs_s shortcut */
  44. typedef struct rpn_ifs_s rpn_ifs_t;
  45. /**@brief Stores IFS informations */
  46. struct rpn_ifs_s
  47. {
  48. /**@brief Parameters shared by all IF of the system */
  49. rpn_if_param_t params;
  50. /**@brief Memory map shared by all iterated functions */
  51. rpn_value_t *mem;
  52. /**@brief If 1 the mem has to be munmap at free time */
  53. short self_mem;
  54. /**@brief Pointers on iterated function structures */
  55. rpn_if_t **rpn_if;
  56. /**@brief Number of iterated functions in the system */
  57. size_t if_sz;
  58. /**@brief Current position in memory map */
  59. size_t pos;
  60. /**@brief Stores the original chance of choosing corresponding IF */
  61. unsigned int *weight;
  62. /** @brief Stores an array of 256 pointers on IF allowing fast
  63. * random choice.*/
  64. rpn_if_t *if_proba[256];
  65. /**@brief Stores the RPN expressions pointer of the IF contained in
  66. * the system */
  67. rpn_expr_t **flat_rpn;
  68. /**Number of rpn expression in the system */
  69. size_t flat_sz;
  70. };
  71. /**@brief Initialize a new IFS struct given params
  72. * @param params Individual, but shared, parameters for iteraed functions
  73. * in the system
  74. * @param memmap A suitable memory map or NULL
  75. * @return NULL if error and set errno
  76. * @note Initialized system is empty. Use @ref rpn_ifs_add_if to add an iterated
  77. * function to the system
  78. */
  79. rpn_ifs_t* rpn_ifs_new(rpn_if_param_t *params, rpn_value_t *memmap);
  80. /**@brief Free ressources of an iterated function system
  81. * @param rifs The iterated function system to free
  82. */
  83. void rpn_ifs_free(rpn_ifs_t *rifs);
  84. /**@brief Set the number and weights of functions in the system
  85. * @note Do not re-init existing function
  86. * @param rifs The iterated function system
  87. * @param count The number of functions in the system
  88. * @param weights The function's weight
  89. * @return The number of function in the system
  90. */
  91. size_t rpn_ifs_set_if_count(rpn_ifs_t *rifs, size_t count, unsigned int *weights);
  92. /**@brief Add a new iterated function to the system
  93. * @param rifs The iterated function system
  94. * @param weight The new expression weight
  95. * @return 0 on error, else returns the new @ref rpn_if_s index
  96. * @note if epxrs is NULL empty RPN expressions are used
  97. * @todo change the exprs argument when if init will be updated
  98. */
  99. size_t rpn_ifs_add_if(rpn_ifs_t *rifs, unsigned int weight);
  100. /**@brief Delete an iterated function from the system given its index
  101. * @param rifs The iterated function system
  102. * @param if_idx The iterated function index in the system
  103. * @return -1 if error else 0
  104. * @note Update sizes but do not calls realloc
  105. */
  106. int rpn_ifs_del_if(rpn_ifs_t *rifs, size_t if_idx);
  107. /**@brief Call IFS n times
  108. * @note Make n random choices and call corresponding IF
  109. * @param rifs The iterated function system
  110. * @param n consecutive IFS calls
  111. * @param seed prepopulated array of random bytes (if NULL one is generated)
  112. * @return -1 if error else 0
  113. */
  114. int rpn_ifs_run(rpn_ifs_t *rifs, size_t n, unsigned char *rnd);
  115. /**@brief Updates the @ref rpn_ifs_s.if_proba array using
  116. * @ref rpn_ifs_s.if_proba values
  117. * @param rifs The iterated function system
  118. * @return -1 if error else 0
  119. */
  120. int rpn_ifs_weight_update(rpn_ifs_t *rifs);
  121. /**@brief Returns an array of pointer on all RPN expressions contained in the
  122. * system, allowing to recompile/update them
  123. * @param rifs The IF system
  124. * @returns An array of pointer on @ref rpn_expr_s
  125. */
  126. rpn_expr_t **rpn_ifs_flat_rpn(rpn_ifs_t *rifs);
  127. /**@brief Randomly choose an IF and make a step updating ifs current posisition
  128. * @param rifs The IF system
  129. * @param seed A random byte
  130. * @return -1 on error else 0
  131. */
  132. int rpn_ifs_step(rpn_ifs_t *rifs, unsigned char rnd);
  133. #endif