jit_blocks 0.1.0
Loading...
Searching...
No Matches
Modules | Data Structures | Typedefs | Functions
Efficient floating point arithmetic expression engine

Creates a stack-based expression evaluation function. More...

Collaboration diagram for Efficient floating point arithmetic expression engine:

Modules

 Predefined arithmetic operations
 expr engine has special support for these operations by inlining, leading to much faster execution.
 

Data Structures

struct  jit_blocks_expr_context
 Context of calculation. More...
 

Typedefs

typedef struct jit_blocks_expr_context jit_blocks_expr_context
 Context of calculation.
 
typedef void(* jit_blocks_expr_func_t) (jit_blocks_expr_context *ctx)
 Users could pass arbitrary functions to process the expression.
 

Functions

JIT_BLOCKS_EXPORT jit_blocks_expr_contextjit_blocks_expr_context_new (int initial_stack_capacity)
 Creates a new context with the given initial stack capacity.
 
JIT_BLOCKS_EXPORT void jit_blocks_expr_context_release (jit_blocks_expr_context *ctx)
 Destroys the context and frees all associated resources.
 
JIT_BLOCKS_EXPORT bool jit_blocks_expr_context_push (jit_blocks_expr_context *ctx, double value)
 Pushes a double onto the stack.
 
JIT_BLOCKS_EXPORT bool jit_blocks_expr_context_pop (jit_blocks_expr_context *ctx, double *out_value)
 Pops a double from the stack.
 
JIT_BLOCKS_EXPORT jit_blocks_expr_func_t jit_blocks_expr_build (const jit_blocks_expr_func_t *ops, int num_ops, gcc_jit_result **out_res)
 Creates a function that performs the given arithmetic operations on the initial_ctx stack, and pushes the result back onto the stack.
 
JIT_BLOCKS_EXPORT jit_blocks_expr_func_t jit_blocks_expr_build_aux (const jit_blocks_expr_func_t *ops, int num_ops, gcc_jit_context *ctx, gcc_jit_result **out_res)
 

Detailed Description

Creates a stack-based expression evaluation function.

Users could pass an array of jit_blocks_expr_func_t to jit_blocks_expr_build to create a function that accepts a jit_blocks_expr_context (which is essentially a stack of doubles),

Then, users could call jit_blocks_expr_context_new to create a context, push doubles into the context via jit_blocks_expr_context_push (Operands evaluated first should be pushed last so that they could popped out during calculation first). After users build a successful jit_blocks_expr_context, they can call the generation function and extract result from the stack via jit_blocks_expr_context_pop.

Example usage:

// stack_top *= 22.5
void custom_op(jit_blocks_expr_context* ctx)
{
double v = 0.;
}
// -((20 + 10) * 5 / 2 - 3)
// Note that operands are pushed in reverse order:
&custom_op};
gcc_jit_result* result = NULL;
assert(out != NULL);
out(ctx);
assert(ctx->stack_size == 1);
assert(fabs(ctx->stack[0] - -1620.) < 1e-5);
JIT_BLOCKS_EXPORT void jit_blocks_expr_op_divide(jit_blocks_expr_context *ctx)
Pops two doubles from the stack, divides the first by the second, and pushes the result back.
JIT_BLOCKS_EXPORT void jit_blocks_expr_op_multiply(jit_blocks_expr_context *ctx)
Pops two doubles from the stack, multiplies them, and pushes the result back.
JIT_BLOCKS_EXPORT void jit_blocks_expr_op_plus(jit_blocks_expr_context *ctx)
Pops two doubles from the stack, adds them, and pushes the result back.
JIT_BLOCKS_EXPORT void jit_blocks_expr_op_negate(jit_blocks_expr_context *ctx)
Pops a double from the stack, negates it, and pushes the result back.
JIT_BLOCKS_EXPORT void jit_blocks_expr_op_minus(jit_blocks_expr_context *ctx)
Pops two doubles from the stack, subtracts the second from the first, and pushes the result back.
void(* jit_blocks_expr_func_t)(jit_blocks_expr_context *ctx)
Users could pass arbitrary functions to process the expression.
Definition jit_blocks.h:180
JIT_BLOCKS_EXPORT jit_blocks_expr_context * jit_blocks_expr_context_new(int initial_stack_capacity)
Creates a new context with the given initial stack capacity.
JIT_BLOCKS_EXPORT bool jit_blocks_expr_context_pop(jit_blocks_expr_context *ctx, double *out_value)
Pops a double from the stack.
JIT_BLOCKS_EXPORT jit_blocks_expr_func_t jit_blocks_expr_build(const jit_blocks_expr_func_t *ops, int num_ops, gcc_jit_result **out_res)
Creates a function that performs the given arithmetic operations on the initial_ctx stack,...
JIT_BLOCKS_EXPORT bool jit_blocks_expr_context_push(jit_blocks_expr_context *ctx, double value)
Pushes a double onto the stack.
Context of calculation.
Definition jit_blocks.h:155
int stack_size
Definition jit_blocks.h:157
double * stack
Definition jit_blocks.h:156

jit_blocks_expr_op_* functions are predefined operators and got specially handled for faster calculation. See Predefined arithmetic operations for more details.

Typedef Documentation

◆ jit_blocks_expr_context

Context of calculation.

Under the hood it's a fixed capacity stack for doubles. Users should use jit_blocks_expr_context_new to create one.

◆ jit_blocks_expr_func_t

typedef void(* jit_blocks_expr_func_t) (jit_blocks_expr_context *ctx)

Users could pass arbitrary functions to process the expression.

Function Documentation

◆ jit_blocks_expr_build()

JIT_BLOCKS_EXPORT jit_blocks_expr_func_t jit_blocks_expr_build ( const jit_blocks_expr_func_t ops,
int  num_ops,
gcc_jit_result **  out_res 
)

Creates a function that performs the given arithmetic operations on the initial_ctx stack, and pushes the result back onto the stack.

Returns
the built function. NULL if the function creation failed.

◆ jit_blocks_expr_build_aux()

JIT_BLOCKS_EXPORT jit_blocks_expr_func_t jit_blocks_expr_build_aux ( const jit_blocks_expr_func_t ops,
int  num_ops,
gcc_jit_context *  ctx,
gcc_jit_result **  out_res 
)

◆ jit_blocks_expr_context_new()

JIT_BLOCKS_EXPORT jit_blocks_expr_context * jit_blocks_expr_context_new ( int  initial_stack_capacity)

Creates a new context with the given initial stack capacity.

◆ jit_blocks_expr_context_pop()

JIT_BLOCKS_EXPORT bool jit_blocks_expr_context_pop ( jit_blocks_expr_context ctx,
double *  out_value 
)

Pops a double from the stack.

Returns
true if the pop operation was successful, false otherwise.

◆ jit_blocks_expr_context_push()

JIT_BLOCKS_EXPORT bool jit_blocks_expr_context_push ( jit_blocks_expr_context ctx,
double  value 
)

Pushes a double onto the stack.

Returns
true if the push operation was successful, false otherwise.

◆ jit_blocks_expr_context_release()

JIT_BLOCKS_EXPORT void jit_blocks_expr_context_release ( jit_blocks_expr_context ctx)

Destroys the context and frees all associated resources.