/* * Copyright (C) 2020 Weber Yann * * This file is part of pyrpn. * * pyrpn is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * pyrpn is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with pyrpn. If not, see . */ #ifndef __rpn_if__h__ #define __rpn_if__h__ #include "config.h" #include "rpn_jit.h" /**@defgroup ifs_if Iterated function * @brief Iterated RPN expression * * A single Iterated Function implemented using an RPN expression. * * @note The goal is to optimize iteration writing the code in C and providing * an high level Python API. * * For more details about IF see dedicated page : @ref doc_ifs_if */ /**@brief Shortcut for struct @ref rpn_if_s */ typedef struct rpn_if_s rpn_if_t; /**@brief Shortcut for struct @ref rpn_if_param_s */ typedef struct rpn_if_param_s rpn_if_param_t; /**@brief Stores IF parameters and transformations function pointers */ struct rpn_if_param_s { /**@brief Memory map size in items */ size_t mem_sz; /**@brief Size of a memory item */ size_t value_sz; /**@brief RPN expression count */ size_t rpn_sz; /**@brief Number of arguments expected by RPN expressions */ size_t rpn_argc; /**@brief Set RPN arguments given a position * @note transform position to arguments and fetch other arguments * from memory map*/ int (*arg_f)(rpn_if_t *rif, size_t pos, rpn_value_t *args); /**@brief RPN results to data and position transformation * @note set memory maps with converted data */ int (*res_f)(rpn_if_t *rif, size_t *pos, rpn_value_t *data); void *data; }; /**@brief Generic Iterated function implementation */ struct rpn_if_s { /**@brief IF parameters */ rpn_if_param_t params; /**@brief RPN expression(s) pointer(s) */ rpn_expr_t *rpn; /**@brief RPN expression(s) result(s) buffer */ rpn_value_t *rpn_res; /**@brief Arguments given to RPN expression(s) buffer */ rpn_value_t *rpn_args; /**@brief Memory map in wich data are fetch & stored */ rpn_value_t *mem; }; /**@brief Macro fetching a memory pointer given a position * @return rpn_value_t* values */ #define rpn_if_getitem(rif, pos) (rif->mem + (rif->params.value_sz * pos)) /**@brief Alloc a new @ref rpn_if_s from two transformation functions * @param params IF parameters * @param rpn list of RPN expresions of params->rpn_sz size * @return A pointer on an allocated @ref rpn_if_s */ rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn); /**@brief Deallocate an @ref rpn_ifs_s and its ressources and close associated * @ref rpn_expr_s * @param rif The IF to deallocate */ void rpn_if_free(rpn_if_t *rif); /**@brief Run an IF * @param rif Pointer on IF * @param pos Input position * @return new position */ size_t rpn_if_step(rpn_if_t *rif, size_t pos); /**@brief Update all RPN expressions * @param rif The concerned IF * @param rpn A list of tokenized expressions (must be of rif->rpn_sz size) * @return 0 if no error else -1 * @note Shortcut for @ref rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0); */ int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns); /**@brief Update a range of RPN expressions * @param rif The concerned IF * @param rpn A list of tokenized expressions * @param sz Number of rpn expression in rpn argument * @param offset Start updating expressions from this offset * @return 0 if no error else -1 */ int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz, size_t offset); /**@brief New @ref rpn_if_s and partial initialisation * @param mem_sz memory size in bytes * @param argc number of arguments taken by @ref rpn_expr_s * @param rpn_count number of @ref rpn_expr_s */ rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count, size_t value_sz); /**@page doc_ifs * * Iterated functions system are a composed of Iterated function choosed * randomly. * * @section doc_ifs_if Iterated function general considerations * * Iterated functions can be seen as transformations in a given space. * Functions takes items as argument and set an item as a result. * * For the moment, the main goal is to interact with a 2 dimension world with * 1 to 3 values per items (an image in grayscale or in RGB). * * A simple approach can be to use a single expression working with a single * number later decomposed in multiple composant using bitmasks (basically * for storage address and stored value). * * This can later be decomposed by assigning one (or multiple) expression * to each composant (one expression for storage address, another one for * the storage value). * * The same consideration can be done on argument number/composition taken * by the expression. * * @subsection doc_ifs_if_io Iterated function generalisation * * A generic implementation can be IF as : * - A generic input transformation system : X arguments transformed in Y * RPNExpression arguments * - A generic output system : N results (from N RPNExpr) transformed in X * results * - A generic transformation system : N expressions, taking Y arguments * * @section doc_ifs_if_api Iterated Function API * * Multiple steps are expected in API development : * - A simple generic API will be defined (something like expecting a * void* fun(void*) transforming X data in Y result using a blackbox optimized * behavior associated with a memory map * - Helper function will be written allowing C and/or Python extensions */ #endif