love.math.randomNormal can be JIT-compiled

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-04-01 21:19:19 -03:00
parent b205b8a020
commit f8c58c54c8
2 changed files with 25 additions and 0 deletions
+11
View File
@@ -119,6 +119,7 @@ int w_RandomGenerator_getState(lua_State *L)
struct FFI_RandomGenerator
{
double (*random)(Proxy *p);
double (*randomNormal)(Proxy *p, double stddev, double mean);
};
static FFI_RandomGenerator ffifuncs =
@@ -131,6 +132,16 @@ static FFI_RandomGenerator ffifuncs =
RandomGenerator *rng = (RandomGenerator *) p->object;
return rng->random();
},
[](Proxy *p, double stdddev, double mean) -> double // randomNormal
{
// FIXME: We need better type-checking...
if (p == nullptr || p->object == nullptr || p->type == nullptr || !p->type->isa(RandomGenerator::type))
return 0.0;
RandomGenerator *rng = (RandomGenerator *) p->object;
return rng->randomNormal(stdddev) + mean;
}
};
+14
View File
@@ -63,6 +63,7 @@ typedef struct Proxy Proxy;
typedef struct FFI_RandomGenerator
{
double (*random)(Proxy *p);
double (*randomNormal)(Proxy *p, double stddev, double mean);
} FFI_RandomGenerator;
]])
@@ -78,5 +79,18 @@ function RandomGenerator:random(l, u)
return getrandom(r, l, u)
end
function RandomGenerator:randomNormal(stddev, mean)
-- TODO: This should ideally be handled inside ffifuncs.randomNormal
if self == nil then error("bad argument #1 to 'randomNormal' (RandomGenerator expected, got no value)", 2) end
stddev = stddev == nil and 1 or stddev
mean = mean == nil and 0 or mean
if type(stddev) ~= "number" then error("bad argument #1 to 'randomNormal' (number expected)", 2) end
if type(mean) ~= "number" then error("bad argument #2 to 'randomNormal' (number expected)", 2) end
return tonumber(ffifuncs.randomNormal(self, stddev, mean))
end
-- DO NOT REMOVE THE NEXT LINE. It is used to load this file as a C++ string.
--)luastring"--"