Browse Source

love.math.randomNormal can be JIT-compiled

--HG--
branch : minor
Alex Szpakowski 8 years ago
parent
commit
f8c58c54c8

+ 11 - 0
src/modules/math/wrap_RandomGenerator.cpp

@@ -119,6 +119,7 @@ int w_RandomGenerator_getState(lua_State *L)
 struct FFI_RandomGenerator
 struct FFI_RandomGenerator
 {
 {
 	double (*random)(Proxy *p);
 	double (*random)(Proxy *p);
+	double (*randomNormal)(Proxy *p, double stddev, double mean);
 };
 };
 
 
 static FFI_RandomGenerator ffifuncs =
 static FFI_RandomGenerator ffifuncs =
@@ -131,6 +132,16 @@ static FFI_RandomGenerator ffifuncs =
 
 
 		RandomGenerator *rng = (RandomGenerator *) p->object;
 		RandomGenerator *rng = (RandomGenerator *) p->object;
 		return rng->random();
 		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 - 0
src/modules/math/wrap_RandomGenerator.lua

@@ -63,6 +63,7 @@ typedef struct Proxy Proxy;
 typedef struct FFI_RandomGenerator
 typedef struct FFI_RandomGenerator
 {
 {
 	double (*random)(Proxy *p);
 	double (*random)(Proxy *p);
+	double (*randomNormal)(Proxy *p, double stddev, double mean);
 } FFI_RandomGenerator;
 } FFI_RandomGenerator;
 ]])
 ]])
 
 
@@ -78,5 +79,18 @@ function RandomGenerator:random(l, u)
 	return getrandom(r, l, u)
 	return getrandom(r, l, u)
 end
 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.
 -- DO NOT REMOVE THE NEXT LINE. It is used to load this file as a C++ string.
 --)luastring"--"
 --)luastring"--"