From 6ceb31aad4af31c8a565e865d86239e3e026ea8f Mon Sep 17 00:00:00 2001 From: Leonardo Date: Tue, 1 Nov 2016 20:36:31 +0200 Subject: [PATCH] Implements epsilon function --- runtime/vultin.h | 137 +++++++++++++++----------- src/core/vEnv.ml | 3 + src/core/vType.ml | 2 + src/generators/defaultReplacements.ml | 2 + src/generators/vultJs.ml | 4 +- src/generators/vultLua.ml | 4 +- 6 files changed, 94 insertions(+), 58 deletions(-) diff --git a/runtime/vultin.h b/runtime/vultin.h index 241c617f..4ad469a6 100644 --- a/runtime/vultin.h +++ b/runtime/vultin.h @@ -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 ab?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 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); @@ -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 valuehigh?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 diff --git a/src/core/vEnv.ml b/src/core/vEnv.ml index b552477b..e07d3be5 100644 --- a/src/core/vEnv.ml +++ b/src/core/vEnv.ml @@ -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 diff --git a/src/core/vType.ml b/src/core/vType.ml index f26609f1..09e91d4d 100644 --- a/src/core/vType.ml +++ b/src/core/vType.ml @@ -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 diff --git a/src/generators/defaultReplacements.ml b/src/generators/defaultReplacements.ml index d6a1af5b..7dfe4b12 100644 --- a/src/generators/defaultReplacements.ml +++ b/src/generators/defaultReplacements.ml @@ -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 @@ -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 diff --git a/src/generators/vultJs.ml b/src/generators/vultJs.ml index c59936e0..0bd7b62d 100644 --- a/src/generators/vultJs.ml +++ b/src/generators/vultJs.ml @@ -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 xhigh?high:x); }; this.not = function(x) { return x==0?1:0; }; this.real = function(x) { return x; }; diff --git a/src/generators/vultLua.ml b/src/generators/vultLua.ml index a6404e80..0e55e7f6 100644 --- a/src/generators/vultLua.ml +++ b/src/generators/vultLua.ml @@ -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(xhigh,high,x))); end function this.real(x) return x; end function this.int(x) return math.floor(x); end