Skip to content

Commit

Permalink
Adds builtin random number generators
Browse files Browse the repository at this point in the history
  • Loading branch information
modlfo committed Jun 8, 2017
1 parent 8fa60c8 commit a277b99
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 2 deletions.
16 changes: 16 additions & 0 deletions runtime/vultin.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,19 @@ void fix_copy_array(int size, fix16_t *dest, fix16_t *src)
for (i = 0; i < size; i++)
dest[i] = src[i];
}

float float_random()
{
return (float)rand() / RAND_MAX;
}

fix16_t fix_random()
{
float temp = ((float)rand() / RAND_MAX) * 0x00010000;
return (fix16_t)temp;
}

int irandom()
{
return (int)rand();
}
7 changes: 7 additions & 0 deletions runtime/vultin.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ NOTE: The code for the fixed-point operations is based on the project:

#ifndef VULTIN_H
#define VULTIN_H

#include <stdint.h>
#include <stdlib.h>

#ifdef _MSC_VER
#define static_inline static __inline
Expand Down Expand Up @@ -206,6 +208,11 @@ static_inline uint8_t bool_not(uint8_t x)
static_inline fix16_t *fix_wrap_array(const fix16_t x[]) { return (fix16_t *)x; };
static_inline float *float_wrap_array(const float x[]) { return (float *)x; };

/* Random numbers */
float float_random();
fix16_t fix_random();
int irandom();

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 13 additions & 0 deletions src/core/interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ let builtinFunctions env =
| [] -> PReal(1e-18,emptyAttr)
| _ -> failwith "eps: invalid arguments"
in
let random args =
match args with
| [] -> PReal(Random.float 1.0,emptyAttr)
| _ -> failwith "random: invalid arguments"
in
let irandom args =
match args with
| [] -> PInt(Random.int max_int,emptyAttr)
| _ -> failwith "irandom: invalid arguments"
in
let functions =
[
"abs", Env.Builtin(real_real abs_float);
Expand All @@ -319,6 +329,9 @@ let builtinFunctions env =

"not", Env.Builtin(not);
"eps", Env.Builtin(eps);

"random", Env.Builtin(random);
"irandom", Env.Builtin(irandom);
]
in
List.iter (fun (name,body) ->Env.addFunction env [name] body) functions
Expand Down
3 changes: 3 additions & 0 deletions src/core/vEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ let builtin_table =

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

["random"], Scope.Function, VType.Constants.real_type, false;
["irandom"], Scope.Function, VType.Constants.int_type, false;

]

let builtin_functions = List.map (fun (a,_,_,_)-> a ) builtin_table |> IdSet.of_list
Expand Down
6 changes: 6 additions & 0 deletions src/core/vType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ module Constants = struct
let unit_real () =
unit_type |-> real_type

let unit_real () =
unit_type |-> int_type

let int_unit () =
int_type |-> unit_type

let wrap_array () =
let a = ref (TUnbound("'a",None,None)) in
let size = ref (TUnbound("'size",None,None)) in
Expand Down
2 changes: 2 additions & 0 deletions src/generators/defaultReplacements.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ module Default = struct
("get", "uint8_t"), "bool_get";
("not", "uint8_t"), "bool_not";
("eps", "float"), "float_eps";
("random", "float"), "float_random";
("wrap_array", "float"),"float_wrap_array";
]

Expand Down Expand Up @@ -208,6 +209,7 @@ module FixedPoint = struct
("set", "fix16_t"), "fix_set";
("get", "fix16_t"), "fix_get";
("eps", "fix16_t"), "fix_eps";
("random", "fix16_t"), "fix_random";
("wrap_array","fix16_t"), "fix_wrap_array";
]

Expand Down
2 changes: 2 additions & 0 deletions src/generators/vultJs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ module Templates = struct
let common exports module_name code =
{pla|
<#exports#>{
this.random = function() { return Math.random(); };
this.irandom = function() { return Math.floor(Math.random() * 4294967296); };
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; };
Expand Down
2 changes: 2 additions & 0 deletions src/generators/vultLua.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ 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.random() return math.random(); end
function this.irandom() return math.floor(math.random() * 4294967296); 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) local int_part,_ = math.modf(x) return int_part; end
Expand Down
4 changes: 3 additions & 1 deletion src/passes/pass2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ let getInterpEnv state =

module Evaluate = struct

let avoid = IdSet.of_list [["random"]; ["irandom"]]

let isConst exp =
match exp with
| PInt _ | PReal _ | PBool _ -> true
Expand All @@ -42,7 +44,7 @@ module Evaluate = struct
let exp : ('a Env.t,exp) Mapper.mapper_func =
Mapper.make "Simplify.exp" @@ fun state exp ->
match exp with
| PCall(None,_,args,_) when List.for_all isConst args ->
| PCall(None, name, args, _) when List.for_all isConst args && not (IdSet.mem name avoid) ->
let env = getInterpEnv state in
let exp' = Interpreter.evalExp env exp in
state, exp'
Expand Down
2 changes: 1 addition & 1 deletion src/version.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let version = "
v0.3.17-21-gea63d60
v0.3.20
"

0 comments on commit a277b99

Please sign in to comment.