Arithmetic seem to overwrite variable

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

Moderators: cheriff, TyRaNiD

Post Reply
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

Arithmetic seem to overwrite variable

Post by anmabagima »

Hi,

i've started PSP developemnt recently and are now faced with a strange issue, which I'm not sure how to call.

I've following situation:

many loops are calculation floats and double values. However, after a certain number of iterations this operation seem to overwrite different values.

Example:

Code: Select all

int i = 0;
int n = 0;
douple d1;

int count = 0;
for &#40;i=0;i<100;i++&#41;&#123;
 for &#40;n=0;n<100;n++&#41;&#123;
   d1 = &#40;i*i + 100&#41; / n;
   count ++;
 &#125;
&#125;
I know this code makes no sence at all.However, if I debug some code which seem to be simmilar to this (a liitle bit more complex ;o) ) there is a very strange behavior:
1. after some runns the count will be set to 0;
2. if this happens the console displays that new thread was started and the debugger does not move through the code line, by line but is jumping more or less randomly and with every step the console display "New Thread"...

Has any one ever faced some simmilar issues ?

I'm using the WINSDK for PSP with eclipse and a psp-c++ compiler.

What worked so far was simple mathematics to draw pixel on the screen with random funtion and some addititions. But it seems as far as I'm introdusing float/double in my code the PSP does strange things. I'm not using gnu..

Thanks for any kinf od advice.

regards
AnMaBaGiMa
Last edited by anmabagima on Fri Oct 02, 2009 3:34 am, edited 2 times in total.
psPea
Posts: 60
Joined: Sat Sep 01, 2007 12:51 pm

Post by psPea »

douple = (i*i + 100) / j; ?
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

There's no such thing as a douple, and j isn't defined either.
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

sorry error in code

Post by anmabagima »

Hi,

sorry - there was an tiny error in the code :o(

Code: Select all

d1 = &#40;i*i + 100&#41; / n ;
sorry...
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

Resuable example

Post by anmabagima »

Hi,

I've tried to simplify my code as much as possible...

Know I give you my code snippets (the usual code which is in any example the same is not shown as I would not expect the issue there)

the main.cpp

Code: Select all

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	pspDebugScreenInit&#40;&#41;;
	// setup of common call backs
	setupCallbacks&#40;&#41;;

	Init&#40; &#41;;
Init() is implemented in engine.cpp:

Code: Select all

struct rayDelta &#123;
	long rayDeltaX, rayDeltaY;
&#125; ;

long   mapSize; //size of the Map &#40;width&#41;
rayDelta preDelta&#91;480&#93;;

void Init&#40; &#41;&#123;
	double rayDeltaX, rayDeltaY;

	double hx;

	mapSize = 512;
	int x;

	for &#40;x=0;x<480;x++&#41;&#123;
		hx = x - 240;
		rayDeltaX = &#40;&#40;hx / sqrt&#40;hx*hx + 400*400&#41;&#41;*1024&#41;;
		rayDeltaY = &#40;400 / sqrt&#40;hx*hx + 400*400&#41;*1024&#41;;
		preDelta&#91;x&#93;.rayDeltaX = rayDeltaX;
		preDelta&#91;x&#93;.rayDeltaY = rayDeltaY;
	&#125;
&#125;
if you compile this and set the breakpoint to the to of the loop than I'm not able to debug into each line. The debugger jumps mainly to the last line. There are each time "New Thread" outputs in the console of the debugger. I've absolutely no clue whats going on.

however, I've done more and more simplification...
And it seem to me that this has nothing really to do with my first assumption that the calculation may the cause...after some tries..also bring all into the main.cpp the debugging console outputs the following:
[New Thread 76458591]
[Switching to Thread 76458591]
Current language: auto; currently c++
[New Thread 76458591]
[New Thread 76458591]
[New Thread 76458591]
[New Thread 76458591]

GDB is unable to find the start of the function at 0x883cb90
and thus can't determine the size of that function's stack frame.
This means that GDB may be unable to access that stack frame, or
the frames below it.
This problem is most likely caused by an invalid program counter or
stack pointer.
However, if you think GDB should simply search farther back
from 0x883cb90 for code which looks like the beginning of a
function, you can increase the range of the search using the `set
heuristic-fence-post' command.
warning: GDB can't find the start of the function at 0x883cb90.

Any further Ideas ? Something seem to change/move the memory addresses/pointers in the stack...but what ?

Many thanks for any kind of advise...
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

That code looks fine, but that's not your real code. It doesn't even compile: at the very least you need to change "rayDelta preDelta[480];" to "struct rayDelta preDelta[480];". If you show us your actual full code, we may be able to help.
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

Code

Post by anmabagima »

Hi,
thanks for the reply. I 've further tried to simpliy and have put all code into main.cpp now. This is not "best practice" and once I know the root cause of the issue I would like to split the whole code accross several files again for better readability...However..It compiles as it is...

Please find the whole code now:

Code: Select all

#include <psptypes.h>
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspgu.h>
#include "../common/callbacks.h"

PSP_MODULE_INFO&#40;"PSP Demo", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

//color generator
#define RGB&#40;r, g, b&#41; &#40;&#40;r&255&#41; | &#40;g&255&#41; << 8 | &#40;b&255&#41; << 16&#41;

#define pg_vramtop &#40;&#40;unsigned char *&#41;0x04000000&#41;

struct rayDelta &#123;
	long rayDeltaX, rayDeltaY;
&#125; ;
long   mapSize; //size of the Map &#40;width&#41;
rayDelta preDelta&#91;480&#93;;

void Init&#40; &#41;&#123;
	double rayDeltaX, rayDeltaY;

	double hx;

	mapSize = 512;
	int x,y;
	for &#40;x=0;x<480;x++&#41;&#123;
		hx = x - 240;
		rayDeltaX = &#40;&#40;hx*1024 / sqrt&#40;hx*hx + 400*400&#41;&#41;&#41;;
		rayDeltaY = &#40;400*1024 / sqrt&#40;hx*hx + 400*400&#41;&#41;;

		preDelta&#91;x&#93;.rayDeltaX = rayDeltaX;
		preDelta&#91;x&#93;.rayDeltaY = rayDeltaY;
	&#125;
&#125;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	// setup of common call backs
	setupCallbacks&#40;&#41;;

	Init&#40; &#41;;

	// run the application
	while&#40;running&#40;&#41;&#41;&#123;
		sceDisplayWaitVblankStart&#40;&#41;;
	&#125;

	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;
Thanks again...
Regards
AnMaBaGiMa
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Code: Select all

struct rayDelta &#123; 
   long rayDeltaX, rayDeltaY; 
&#125; ; 
...
rayDelta preDelta&#91;480&#93;; 
For the second time, how is this going to even compile?

Jim
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

Post by anmabagima »

For the second time, how is this going to even compile?
i do not know, but the compile does not throw any error...

However, I'v changed now to:

Code: Select all

struct rayDelta preDelta&#91;480&#93;;
It still compiles without error ;o) but the behavior is the same...
Here the console output after some debug steps within the main loop:
it's exciting ;o)

Code: Select all

&#91;New Thread 0&#93;
Current language&#58;  auto; currently c
&#91;New Thread 76458563&#93;
&#91;Switching to Thread 76458563&#93;
Current language&#58;  auto; currently c++
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;
&#91;New Thread 76458563&#93;

    GDB is unable to find the start of the function at 0x8808728
and thus can't determine the size of that function's stack frame.
This means that GDB may be unable to access that stack frame, or
the frames below it.
    This problem is most likely caused by an invalid program counter or
stack pointer.
    However, if you think GDB should simply search farther back
from 0x8808728 for code which looks like the beginning of a
function, you can increase the range of the search using the `set
heuristic-fence-post' command.
warning&#58; GDB can't find the start of the function at 0x8808728.
warning&#58; GDB can't find the start of the function at 0x8808727.
Regards
AnMaBaGiMa
ymg545
Posts: 1
Joined: Wed Aug 26, 2009 4:21 pm

Post by ymg545 »

Jim wrote:

Code: Select all

struct rayDelta &#123; 
   long rayDeltaX, rayDeltaY; 
&#125; ; 
...
rayDelta preDelta&#91;480&#93;; 
For the second time, how is this going to even compile?

Jim
In C++, u dont have to insert the keyword "struct" to the variable declaration. So the code should be compiled just fine
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Uh, I've always needed to wrap main() and any callback stuff in extern "C" { } to get it to work in a C++ project. You might at least try that.
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

I still can't compile and test that code.
I don't have your "../common/callbacks.h" and there's no definition of "running()".

If you want us to help, give us a complete example (including the makefile) that we can build and test. Otherwise you're on your own.

How does the code behave when you run it outside gdb? gdb might just be getting confused by some of the types, I don't know how well it's been tested with floating point stuff.
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

Post by anmabagima »

Hi,

you are absolutely right. I should have posted as much as possible right in teh beginning to save some time ;o)

Here is the full stuff. To be able to visualize what the code is doing without debugging I've added some code to draw some pixel on the calculated values.

First of all the ../common/callback.h (+c)

Code: Select all

#ifndef common_callbacks_h
#define common_callbacks_h

#ifdef __cplusplus
extern "C" &#123;
#endif

int running&#40;&#41;;
int setupCallbacks&#40;void&#41;;

#ifdef __cplusplus
&#125;
#endif

#endif

Code: Select all

/*
 * PSP Software Development Kit - http&#58;//www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * Copyright &#40;c&#41; 2005 Jesper Svennevid
 */

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

static int exitRequest = 0;

int running&#40;&#41;
&#123;
	return !exitRequest;
&#125;

int exitCallback&#40;int arg1, int arg2, void *common&#41;
&#123;
	exitRequest = 1;
	return 0;
&#125;

int callbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;

	cbid = sceKernelCreateCallback&#40;"Exit Callback", exitCallback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;

	sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

int setupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", callbackThread, 0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;
now again the main.cpp with the changes:

Code: Select all

#include <psptypes.h>
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspgu.h>
#include "../common/callbacks.h"

PSP_MODULE_INFO&#40;"PSP Demo", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;
#define PIXELSIZE	4				//in short
#define	LINESIZE	512				//in short
//color generator
#define RGB&#40;r, g, b&#41; &#40;&#40;r&255&#41; | &#40;g&255&#41; << 8 | &#40;b&255&#41; << 16&#41;

#define pg_vramtop &#40;&#40;unsigned char *&#41;0x04000000&#41;

struct rayDelta &#123;
   long rayDeltaX, rayDeltaY;
&#125; ;
long   mapSize; //size of the Map &#40;width&#41;
struct rayDelta preDelta&#91;480&#93;;

unsigned char *pgGetVramAddr&#40;unsigned long x,unsigned long y&#41;
&#123;

	return pg_vramtop+x*PIXELSIZE+y*LINESIZE*PIXELSIZE+0x40000000;
&#125;

void setPixel&#40;int x,int y, long color&#41;
&#123;
     unsigned short* vram = &#40;unsigned short*&#41; pgGetVramAddr&#40;x, y&#41;;
     *vram = color;
&#125;

void Init&#40; &#41;&#123;
   double rayDeltaX, rayDeltaY;

   double hx;

   mapSize = 512;
   int x,y;
   for &#40;x=0;x<480;x++&#41;&#123;
      hx = x - 240;
      rayDeltaX = &#40;&#40;hx*1024 / sqrt&#40;hx*hx + 400*400&#41;&#41;&#41;;
      rayDeltaY = &#40;400*1024 / sqrt&#40;hx*hx + 400*400&#41;&#41;;

      preDelta&#91;x&#93;.rayDeltaX = rayDeltaX;
      preDelta&#91;x&#93;.rayDeltaY = rayDeltaY;
   &#125;
&#125;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
   // setup of common call backs
   setupCallbacks&#40;&#41;;

   Init&#40; &#41;;

   // run the application

   // do visualize some stuff to see what the hell the application does
   for &#40;int x=0;x<480;x++&#41;&#123;
	   //make a point on the screen to visiualize the rayDeltaX
	   //normed to a screen height of 272
	   int posY = 272/2 + &#40;&#40;preDelta&#91;x&#93;.rayDeltaX*272/2&#41; >> 10&#41;;
	   setPixel&#40;x, posY, RGB&#40;255,255,255&#41;&#41;;
   &#125;
   while&#40;running&#40;&#41;&#41;&#123;
      sceDisplayWaitVblankStart&#40;&#41;;
   &#125;

   sceKernelExitGame&#40;&#41;;
   return 0;
&#125;
And now the makefile:

Code: Select all

TARGET = pspdemo
OBJS = main.o ../common/callbacks.o

INCDIR =
CFLAGS = -G0 -Wall -g
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;
BUILD_PRX = 1

LIBDIR =
LDFLAGS =
LIBS= -lpspgum -lpspgu -lm

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = PSP Demo

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
Today I was able to test the same stuff on a different PC. I guess the "only" difference here is that Eclipse is running with a different JRE version (1.5.0_11)...
However, the debugging seem to be a bit more stable. There is still many of "new threads" comming on the console during debuging step by step, but the stack issue seem not to occur again. I will try this now in parallel on the PC where this topic was raised for and will see whether the PSP draws a line from upper left to lower right when just deploying the EBOOT to the PSP and run without debugging mode...will update soon.

For now thanks for your help and support and if someone could try this code on his configuration I would be glad to hear about the results ;o)

Best regards
AnMaBaGiMa
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

solved.....somehow

Post by anmabagima »

Hi all,

now I was able to compare an eclipse running on JRE1.4.2_13 and JRE1.5.0_11....

The good news: the result at the end seem to be compiled all right (as EBOOT). However, the strange compiler/debugger issue occures on JRE1.4.2_13 ... the debugger jumps between random code lines and initialize/overwrites variables as it likes...However, I started with a complete new setup of eclipse project on my machine with JRE1.5.0_11 and looking forward that this strange kind of things will not happen again ;o)

Thanks to all again for your support and tipps.
Regards
AnMaBaGiMa
Post Reply