AP_Scripting: fixed use of clock and time in lua

not available on stm32
This commit is contained in:
Andrew Tridgell 2022-10-04 20:11:38 +11:00
parent 00d6f94907
commit 2ce936db94
4 changed files with 23 additions and 0 deletions

View File

@ -1044,3 +1044,15 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
(LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v); (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v);
} }
/*
32 bit random number generator for lua internals
*/
uint32_t lua_random32(void)
{
static uint32_t m_z = 1234;
static uint32_t m_w = 76542;
m_z = 36969 * (m_z & 0xFFFFu) + (m_z >> 16);
m_w = 18000 * (m_w & 0xFFFFu) + (m_w >> 16);
return ((m_z << 16) + m_w);
}

View File

@ -36,6 +36,8 @@ typedef struct luaL_Reg {
#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
uint32_t lua_random32(void);
LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz);
#define luaL_checkversion(L) \ #define luaL_checkversion(L) \
luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)

View File

@ -26,6 +26,7 @@
#include "lstring.h" #include "lstring.h"
#include "ltable.h" #include "ltable.h"
#include "ltm.h" #include "ltm.h"
#include "lauxlib.h"
// lua code does lots of casting, these warnings are not helpful // lua code does lots of casting, these warnings are not helpful
#pragma GCC diagnostic ignored "-Wcast-align" #pragma GCC diagnostic ignored "-Wcast-align"
@ -44,10 +45,14 @@
** a macro to help the creation of a unique random seed when a state is ** a macro to help the creation of a unique random seed when a state is
** created; the seed is used to randomize hashes. ** created; the seed is used to randomize hashes.
*/ */
#if defined(ARDUPILOT_BUILD)
#define luai_makeseed() lua_random32()
#else
#if !defined(luai_makeseed) #if !defined(luai_makeseed)
#include <time.h> #include <time.h>
#define luai_makeseed() cast(unsigned int, time(NULL)) #define luai_makeseed() cast(unsigned int, time(NULL))
#endif #endif
#endif

View File

@ -256,6 +256,9 @@ typedef unsigned int IdxT;
** is to copy them to an array of a known type and use the array values. ** is to copy them to an array of a known type and use the array values.
*/ */
static unsigned int l_randomizePivot (void) { static unsigned int l_randomizePivot (void) {
#if defined(ARDUPILOT_BUILD)
return lua_random32();
#else
clock_t c = clock(); clock_t c = clock();
time_t t = time(NULL); time_t t = time(NULL);
unsigned int buff[sof(c) + sof(t)]; unsigned int buff[sof(c) + sof(t)];
@ -265,6 +268,7 @@ static unsigned int l_randomizePivot (void) {
for (i = 0; i < sof(buff); i++) for (i = 0; i < sof(buff); i++)
rnd += buff[i]; rnd += buff[i];
return rnd; return rnd;
#endif
} }
#endif /* } */ #endif /* } */