Skip to content

Commit

Permalink
Implements epsilon function
Browse files Browse the repository at this point in the history
  • Loading branch information
modlfo committed Nov 1, 2016
1 parent 3696141 commit 6ceb31a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 58 deletions.
137 changes: 81 additions & 56 deletions runtime/vultin.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,82 +39,106 @@ NOTE: The code for the fixed-point operations is based on the project:
#endif

#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif

typedef int32_t fix16_t;

static const fix16_t fix_pi = 205887;
static const fix16_t fix_pi = 205887;

// Type conversion
static_inline float fix_to_float(fix16_t a) {
return (float)a / 0x00010000;
static_inline float fix_to_float(fix16_t a)
{
return (float)a / 0x00010000;
}
static_inline fix16_t float_to_fix(float a){
float temp = a * 0x00010000;
return (fix16_t)temp;
static_inline fix16_t float_to_fix(float a)
{
float temp = a * 0x00010000;
return (fix16_t)temp;
}

static_inline float int_to_float(int a) {
return (float)a;
static_inline float int_to_float(int a)
{
return (float)a;
}

static_inline int float_to_int(float a){
return (int)a;
static_inline int float_to_int(float a)
{
return (int)a;
}

static_inline fix16_t int_to_fix(int a) {
return a * 0x00010000;
static_inline fix16_t int_to_fix(int a)
{
return a * 0x00010000;
}

static_inline int fix_to_int(fix16_t a)
{
return (a >> 16);
return (a >> 16);
}

// Basic operations for fixed point numbers
static_inline fix16_t fix_add(fix16_t x, fix16_t y){
return x+y;
static_inline fix16_t fix_add(fix16_t x, fix16_t y)
{
return x + y;
}

static_inline fix16_t fix_sub(fix16_t x, fix16_t y){
return x-y;
static_inline fix16_t fix_sub(fix16_t x, fix16_t y)
{
return x - y;
}

static_inline fix16_t fix_mul(fix16_t x, fix16_t y){
int64_t res = (int64_t) x * y;
return (fix16_t) (res >> 16);
static_inline fix16_t fix_mul(fix16_t x, fix16_t y)
{
int64_t res = (int64_t)x * y;
return (fix16_t)(res >> 16);
}

static_inline fix16_t fix_minus(fix16_t x){
return -x;
static_inline fix16_t fix_minus(fix16_t x)
{
return -x;
}

fix16_t fix_div(fix16_t a, fix16_t b);

static_inline fix16_t fix_abs(fix16_t x){
return x<0?(-x):x;
static_inline fix16_t fix_abs(fix16_t x)
{
return x < 0 ? (-x) : x;
}

static_inline fix16_t fix_min(fix16_t a,fix16_t b){
return a<b?a:b;
static_inline fix16_t fix_min(fix16_t a, fix16_t b)
{
return a < b ? a : b;
}

static_inline fix16_t fix_max(fix16_t a,fix16_t b){
return a>b?a:b;
static_inline fix16_t fix_max(fix16_t a, fix16_t b)
{
return a > b ? a : b;
}

static_inline fix16_t fix_clip(fix16_t v,fix16_t minv, fix16_t maxv){
return v>maxv?maxv:(v<minv?minv:v);
static_inline fix16_t fix_clip(fix16_t v, fix16_t minv, fix16_t maxv)
{
return v > maxv ? maxv : (v < minv ? minv : v);
}

static_inline fix16_t fix_floor(fix16_t x)
{
return (x & 0xFFFF0000);
}

static_inline fix16_t fix_not(fix16_t x)
{
return ~x;
}

static_inline fix16_t fix_floor(fix16_t x){
return (x & 0xFFFF0000);
static_inline float float_eps()
{
return 1e-18;
}

static_inline fix16_t fix_not(fix16_t x){
return ~x;
static_inline fix16_t fix_eps()
{
return 1;
}

fix16_t fix_exp(fix16_t inValue);
Expand All @@ -133,37 +157,38 @@ fix16_t fix_tanh(fix16_t inAngle);

fix16_t fix_sqrt(fix16_t inValue);


/* Floating point operations */

static_inline float float_clip(float value, float low, float high){
return value<low?low:(value>high?high:value);
static_inline float float_clip(float value, float low, float high)
{
return value < low ? low : (value > high ? high : value);
}

/* Array get and set */
static_inline void float_set(float a[], int i, float value) { a[i] = value; }
static_inline float float_get(float a[], int i) { return a[i]; }
static_inline void fix_set(fix16_t a[], int i, fix16_t value) { a[i] = value; }
static_inline fix16_t fix_get(fix16_t a[], int i) { return a[i]; }
static_inline void int_set(int a[], int i, int value) { a[i] = value; }
static_inline int int_get(int a[], int i) { return a[i]; }
static_inline void bool_set(uint8_t a[], int i, uint8_t value) { a[i] = value; }
static_inline uint8_t bool_get(uint8_t a[], int i) { return a[i]; }
static_inline void float_set(float a[], int i, float value) { a[i] = value; }
static_inline float float_get(float a[], int i) { return a[i]; }
static_inline void fix_set(fix16_t a[], int i, fix16_t value) { a[i] = value; }
static_inline fix16_t fix_get(fix16_t a[], int i) { return a[i]; }
static_inline void int_set(int a[], int i, int value) { a[i] = value; }
static_inline int int_get(int a[], int i) { return a[i]; }
static_inline void bool_set(uint8_t a[], int i, uint8_t value) { a[i] = value; }
static_inline uint8_t bool_get(uint8_t a[], int i) { return a[i]; }

/* Array initialization */
void float_init_array(int size, float value, float *data);
void int_init_array (int size, int value, int *data);
void bool_init_array (int size, uint8_t value, uint8_t *data);
void fix_init_array (int size, fix16_t value, fix16_t *data);
void int_init_array(int size, int value, int *data);
void bool_init_array(int size, uint8_t value, uint8_t *data);
void fix_init_array(int size, fix16_t value, fix16_t *data);

/* Array copy */
void float_copy_array(int size, float* dest, float *src);
void int_copy_array (int size, int* dest, int *src);
void bool_copy_array (int size, uint8_t* dest, uint8_t *src);
void fix_copy_array (int size, fix16_t* dest, fix16_t *src);
void float_copy_array(int size, float *dest, float *src);
void int_copy_array(int size, int *dest, int *src);
void bool_copy_array(int size, uint8_t *dest, uint8_t *src);
void fix_copy_array(int size, fix16_t *dest, fix16_t *src);

static_inline uint8_t bool_not(uint8_t x){
return !x;
static_inline uint8_t bool_not(uint8_t x)
{
return !x;
}

#ifdef __cplusplus
Expand Down
3 changes: 3 additions & 0 deletions src/core/vEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ let builtin_table =
["not"] , Scope.Function, VType.Constants.bool_bool (), false;
["||"] , Scope.Operator, VType.Constants.bool_bool_bool (), false;
["&&"] , Scope.Operator, VType.Constants.bool_bool_bool (), false;

["eps"] , Scope.Function, VType.Constants.real_type, false;

]

let builtin_functions = List.map (fun (a,_,_,_)-> a ) builtin_table |> IdSet.of_list
Expand Down
2 changes: 2 additions & 0 deletions src/core/vType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,6 @@ module Constants = struct
let array_type = ref (TComposed(["array"],[a;size],None)) in
ref (TArrow(int_type,ref (TArrow(a,array_type,None)),None))

let unit_real () =
ref (TArrow(unit_type,real_type,None))
end
2 changes: 2 additions & 0 deletions src/generators/defaultReplacements.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ module Default = struct
("get", "int"), "int_get";
("get", "uint8_t"), "bool_get";
("not", "uint8_t"), "bool_not";
("eps", "float"), "float_eps";
]

let array_init = Replacements.makeArrayInitializations
Expand Down Expand Up @@ -204,6 +205,7 @@ module FixedPoint = struct
("clip", "fix16_t"), "fix_clip";
("set", "fix16_t"), "fix_set";
("get", "fix16_t"), "fix_get";
("eps", "fix16_t"), "fix_eps";
]

let array_init = Replacements.makeArrayInitializations
Expand Down
4 changes: 3 additions & 1 deletion src/generators/vultJs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ module Templates = struct
let none code = code

let browser module_name code =
{pla|function vultProcess(){
{pla|
function vultProcess(){
this.eps = function() { return 1e-18 };
this.clip = function(x,low,high) { return x<low?low:(x>high?high:x); };
this.not = function(x) { return x==0?1:0; };
this.real = function(x) { return x; };
Expand Down
4 changes: 3 additions & 1 deletion src/generators/vultLua.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ module Templates = struct
let nnoteon_inputs= count_context @@ List.length config.noteon_inputs in
let nnoteoff_inputs = count_context @@ List.length config.noteoff_inputs in
let ncontrolchange_inputs = count_context @@ List.length config.controlchange_inputs in
{pla|local this = {}
{pla|
local this = {}
local ffi = require("ffi")
function this.ternary(cond,then_,else_) if cond then return then_ else return else_ end end
function this.eps() return 1e-18; end
function this.clip(x,low,high) return (this.ternary(x<low,low,this.ternary(x>high,high,x))); end
function this.real(x) return x; end
function this.int(x) return math.floor(x); end
Expand Down

0 comments on commit 6ceb31a

Please sign in to comment.