From 2758cceac11c6f0a54fb66f583c72e01b6e4ce91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Gro=C3=9Fmann?= Date: Fri, 7 Aug 2026 18:54:49 +0200 Subject: [PATCH] fix: read mclock64() in os.clock(), not clock64() libc370 #49 (mvslovers/libc370@f6d1888) changed clock64() to seconds resolution, matching what time64.h had documented all along. os_clock() divides its source by CLOCKS_PER_SEC (1000) to split it into whole seconds plus a millisecond remainder, so it needs a millisecond source. Reading clock64() there now yields a value 1000x too small, and so is every elapsed time derived from it. mclock64() stayed at milliseconds through #49, so switching the single call site restores the previous values. The rest of os_clock() is unchanged; clock64() had exactly one production use in this repo. Nothing catches this at build time: clock64_t and mclock64_t are both plain uint64_t, so the resolution mismatch is silent and the module still compiles, links and runs. os.date, os.time and os.difftime are unaffected. #49 moved time64() in the same commit but deliberately kept its value unchanged -- it used to divide clock64()'s milliseconds by CLOCKS_PER_SEC and now returns clock64()'s seconds directly. Refs #8 --- src/loslib.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/loslib.c b/src/loslib.c index ab41e20..2f25350 100644 --- a/src/loslib.c +++ b/src/loslib.c @@ -1287,16 +1287,23 @@ static int os_clock (lua_State *L) { __64 msec; /* miliseconds (1000ms == 1s) */ lua_Number res; - /* Note: The clock() function in /crent370/clib/clock.c is broken - * and always returns -1, so we're going to use clock64() - * in /crent470/time64/tm64clck.c and process it using __64 - * variables before calculating the result as a lua_Number - * (double float) value. + /* Note: libc370's clock() is a stub that always returns -1, so we take + * the time from the 64-bit clock instead and process it using __64 + * variables before calculating the result as a lua_Number (double + * float) value. + * + * Deliberately mclock64(), not clock64(): __64_divmod_u32() below + * splits the value by CLOCKS_PER_SEC (1000) into whole seconds plus a + * millisecond remainder, so it needs a millisecond source. libc370 #49 + * moved clock64() to seconds resolution; mclock64() stays in + * milliseconds. Reading clock64() here now yields a result 1000x too + * small, and nothing catches it: clock64_t and mclock64_t are both + * plain uint64_t, so the resolution mismatch is silent. */ // wtof("%s: CLOCKS_PER_SEC=%u", __func__, CLOCKS_PER_SEC); // wtof("%s: clock()=%u", __func__, clock()); - - c.u64 = clock64(); + + c.u64 = mclock64(); __64_divmod_u32(&c, CLOCKS_PER_SEC, &sec, &msec); /* scale result as seconds.miliseconds value */