Merge branch 'lua-timestamp' into 'next'

Timestamp function for Lua

See merge request STJr/SRB2!1647
This commit is contained in:
LJ Sonic 2021-12-02 22:41:08 +00:00
commit 599abc2a78
3 changed files with 17 additions and 2 deletions

View file

@ -50,7 +50,7 @@ tic_t I_GetTime(void);
*/
precise_t I_GetPreciseTime(void);
/** \brief Returns the difference between precise times as microseconds.
/** \brief Converts a precise_t to microseconds and casts it to a 32 bit integer.
*/
int I_PreciseToMicros(precise_t);

View file

@ -31,6 +31,7 @@
#include "m_misc.h" // M_MapNumber
#include "b_bot.h" // B_UpdateBotleader
#include "d_clisrv.h" // CL_RemovePlayer
#include "i_system.h" // I_GetPreciseTime, I_PreciseToMicros
#include "lua_script.h"
#include "lua_libs.h"
@ -3879,6 +3880,12 @@ static int lib_gTicsToMilliseconds(lua_State *L)
return 1;
}
static int lib_getTimeMicros(lua_State *L)
{
lua_pushinteger(L, I_PreciseToMicros(I_GetPreciseTime()));
return 1;
}
static luaL_Reg lib[] = {
{"print", lib_print},
{"chatprint", lib_chatprint},
@ -4153,6 +4160,8 @@ static luaL_Reg lib[] = {
{"G_TicsToCentiseconds",lib_gTicsToCentiseconds},
{"G_TicsToMilliseconds",lib_gTicsToMilliseconds},
{"getTimeMicros",lib_getTimeMicros},
{NULL, NULL}
};

View file

@ -2163,7 +2163,13 @@ precise_t I_GetPreciseTime(void)
int I_PreciseToMicros(precise_t d)
{
return (int)(d / (timer_frequency / 1000000.0));
// d is going to be converted into a double. So remove the highest bits
// to avoid loss of precision in the lower bits, for the (probably rare) case
// that the higher bits are actually used.
d &= ((precise_t)1 << 53) - 1; // The mantissa of a double can handle 53 bits at most.
// The resulting double from the calculation is converted first to UINT64 to avoid overflow,
// which is undefined behaviour when converting floating point values to integers.
return (int)(UINT64)(d / (timer_frequency / 1000000.0));
}
//