From 270c7701b4c43faa59b642be5774a0a867a0f1ed Mon Sep 17 00:00:00 2001 From: Hannu Hanhi Date: Mon, 8 Nov 2021 01:17:30 +0200 Subject: [PATCH 1/2] Timestamp function for Lua --- src/i_system.h | 2 +- src/lua_baselib.c | 9 +++++++++ src/sdl/i_system.c | 8 +++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/i_system.h b/src/i_system.h index a3790dca3..a2dd81cca 100644 --- a/src/i_system.h +++ b/src/i_system.h @@ -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); diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 350c1585f..61f0e43cb 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -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" @@ -3876,6 +3877,12 @@ static int lib_gTicsToMilliseconds(lua_State *L) return 1; } +static int lib_iGetTimestamp(lua_State *L) +{ + lua_pushinteger(L, I_PreciseToMicros(I_GetPreciseTime())); + return 1; +} + static luaL_Reg lib[] = { {"print", lib_print}, {"chatprint", lib_chatprint}, @@ -4150,6 +4157,8 @@ static luaL_Reg lib[] = { {"G_TicsToCentiseconds",lib_gTicsToCentiseconds}, {"G_TicsToMilliseconds",lib_gTicsToMilliseconds}, + {"I_GetTimestamp",lib_iGetTimestamp}, + {NULL, NULL} }; diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 2ec28ebc8..ccec37093 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -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)); } // From 56c5a887c858aaf9e1def23327e56d09c546f333 Mon Sep 17 00:00:00 2001 From: Hannu Hanhi Date: Mon, 8 Nov 2021 20:28:35 +0200 Subject: [PATCH 2/2] Call the Lua timestamp function getTimeMicros --- src/lua_baselib.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 61f0e43cb..29de7a05f 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -3877,7 +3877,7 @@ static int lib_gTicsToMilliseconds(lua_State *L) return 1; } -static int lib_iGetTimestamp(lua_State *L) +static int lib_getTimeMicros(lua_State *L) { lua_pushinteger(L, I_PreciseToMicros(I_GetPreciseTime())); return 1; @@ -4157,7 +4157,7 @@ static luaL_Reg lib[] = { {"G_TicsToCentiseconds",lib_gTicsToCentiseconds}, {"G_TicsToMilliseconds",lib_gTicsToMilliseconds}, - {"I_GetTimestamp",lib_iGetTimestamp}, + {"getTimeMicros",lib_getTimeMicros}, {NULL, NULL} };