wrong CLOCKS_PER_SEC?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

wrong CLOCKS_PER_SEC?

Post by Shine »

I've used this Lua Player script:

Code: Select all

white = Color.new(255, 255, 255) 
while true do 
	screen:clear()
	screen:print(0, 0, "clock: " .. os.clock(), white) 
	screen.waitVblankStart() 
	screen.flip() 
	if Controls.read():start() then break end 
end 
and every second 1000 is added. os.clock() is implemented like this in the lua lib:

Code: Select all

static int io_clock (lua_State *L) {
  lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  return 1;
}
so looks like CLOCKS_PER_SEC needs to be multiplied by 1000.
cooleyandy
Posts: 41
Joined: Sat Jul 02, 2005 10:12 am

Post by cooleyandy »

heh, looks like the same problem I have had. I asked this question also like a month ago with no response. Yes, it's 1,000,000 clock ticks per second and CLOCK_PER_SEC should be 1,000,000.
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

LUA problems should be in the LUA forum...

Moved.
Lego of my Ago!
chaos
Posts: 135
Joined: Sun Apr 10, 2005 5:05 pm

Post by chaos »

Agoln wrote:LUA problems should be in the LUA forum...

Moved.
gotta love the moderation around here ;)
Chaosmachine Studios: High Quality Homebrew.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

Agoln wrote:LUA problems should be in the LUA forum...
This is not a Lua Player problem, moved back to PSPSDK Support and Development. Try this code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <time.h>

/* Define the module info section */
PSP_MODULE_INFO&#40;"TEST", 0, 1, 1&#41;;

/* Define the main thread's attribute value &#40;optional&#41; */
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

int main&#40;void&#41;
&#123;
	pspDebugScreenInit&#40;&#41;;

	int c = 0;
	clock_t t0 = clock&#40;&#41;;
	while &#40;1&#41; &#123;
		clock_t t1 = clock&#40;&#41;;
		pspDebugScreenClear&#40;&#41;;
		int delta = t1 - t0;
		pspDebugScreenPrintf&#40;"%i", delta / CLOCKS_PER_SEC&#41;;
		sceDisplayWaitVblankStart&#40;&#41;;
		sceDisplayWaitVblankStart&#40;&#41;;
		if &#40;c++ == 300&#41; break;
	&#125;
	sceKernelExitGame&#40;&#41;;

	return 0;
&#125;
clock()/CLOCKS_PER_SEC is defined to give seconds, not milliseconds, but I don't know where to change it in PSPSDK.
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

You wont find it in pspsdk because it is a newlib thing, seems Sony's clock function follows the POSIX standard by making clock in 1e6 units, while newlib err doesn't :P

I'll try and get around to adjusting it at some point (note: it shouldn't require a rebuild of newlib if you have it, just change it in /usr/local/pspdev/psp/include/time.h)
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

TyRaNiD wrote:I'll try and get around to adjusting it at some point (note: it shouldn't require a rebuild of newlib if you have it, just change it in /usr/local/pspdev/psp/include/time.h)
Thanks, now it works.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

The latest PSPSDK with GCC 4.0.2 has introduced the bug again, please patch it again.
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

It was broken back in r1096 when the newlib patch as regenerated. It should be fixed (for good) in r1218.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

jimparis wrote:It was broken back in r1096 when the newlib patch as regenerated. It should be fixed (for good) in r1218.
Thanks, it works again.
Post Reply