Using sin and rand functions

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
C-Be
Posts: 5
Joined: Thu Apr 15, 2004 7:03 am

Using sin and rand functions

Post by C-Be »

Hi,

i have a probably very easy starter question, but i just can't figure it out on my own.

I have installed the ps2 dev environment and have writen some code, that runs fine on the ps2 emulator (haven't gotten it to run on my ps2 for now) but now i want to use some functions such as sin and rand but including math.h and stdlib.h doesn't work. Do i have to install newlib or use the compiler flag -lm -lc etc?

Because that doen't seem to work. I just used the code from the "Dreamtime's PS2DEV Tutorial #1" and had hoped i could just use rand() etc.

If someone could help me out i really appreciate it.

tnx,
Siebe
User avatar
Saotome
Posts: 182
Joined: Sat Apr 03, 2004 3:45 am

Post by Saotome »

instead of rand() you could use the "mersenne twister"
its faster (4 times faster than the standard rand()),
and you get better "random" values (longer period) than with the standart function (read in the link above).
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

Yes, you need to install newlib and pass -lm and -lc on the linker command line. Also, it's better to use the single-precision versions of the math routines (sinf() instead of sin()), as the compiler has to emulate double-precision floats for the PS2.
C-Be
Posts: 5
Joined: Thu Apr 15, 2004 7:03 am

Post by C-Be »

Thanks for the help.

i actually am using another random function somewhat like mersenne twister, but i need a first really random number or else every time i run the program al randoms are the same for every run. So i want 1x rand() and then 8000x the other random().

I got the sinf() working with newlib and the -lm -lc linker commands but now when i use rand() i get the following:

c:/ps2dev/newlib/lib/libc.a(rand.o): In function `rand':
/home/Nickvv/source/ps2-build/newlib-build/mips64r5900-ps2-elf/newlib/libc/stdli
b/../../../../../../ps2/newlib-1.10.0/newlib/libc/stdlib/rand.c:84: undefined re
ference to `__muldi3'


Does anybody know how to fix this or what i am doing wrong?

Thanks,
Siebe
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post by pixel »

Add -lgcc to your linking phase.
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
blackdroid
Posts: 564
Joined: Sat Jan 17, 2004 10:22 am
Location: Sweden
Contact:

Post by blackdroid »

just for the record, the vector units have rinit, rnext, rget.. ( prepend a v if you want cop2 to use them ).
Kung VU
C-Be
Posts: 5
Joined: Thu Apr 15, 2004 7:03 am

Post by C-Be »

I had tried -lgcc already but it doesn't help.

I put it in front of the -lm and -lc, thats where it should be right?
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

No, it needs to be last.
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

Also, you can use the PS2's RTC as an intital random seed. See libcdvd in ps2lib for the function (cdReadClock(), IIRC).
C-Be
Posts: 5
Joined: Thu Apr 15, 2004 7:03 am

Post by C-Be »

Hey, that worked, amazing:
mrbrown, you the man! :)

I have got one other small question.
I tested the code i wrote on the ps2 emulator only (i don't have my ps2 at hand all the time :( ) and it worked just fine on it. But when i tried to upload it with InLink using Pukklink on my ps2 it gave the following error:

Exception handler: Address store exception
\par
\par Cause 60008014 BadVAddr 003f46d4 Status 70030c13 EPC 002040b4
\par
\par zero: 00000000000000000000000000000000 t8: 00000000000000000000000000000000
\par at: 00000000000000000000000000000001 t9: 00000000000000000000000000000000
\par v0: 00000000000000000000000000204740 s0: 00000000000000000000000000000000
\par v1: 00000000000000000000000000000190 s1: 00000000000000000000000000000000
\par a0: 00000000000000000000000000000000 s2: 00000000000000000000000000000000
\par a1: 00000000000000000000000000204744 s3: 00000000000000000000000000000000
\par a2: 0000000000000000FFFFFFFF80017B88 s4: 00000000000000000000000000000000
\par a3: 00000000000000000000000000000000 s5: 00000000000000000000000000000000
\par t0: 00000000000000000000000000001000 s6: 00000000000000000000000000000000
\par t1: 00000000000000000000000000000000 s7: 00000000000000000000000000000000
\par t2: 00000000000000000000000000000000 k0: 00000000000000000000000000000000
\par t3: 00000000000000000000000000000000 k1: 00000020810200040000000000000000
\par t4: 00000000000000000000000000000000 gp: 0000000000000000000000000020C6F0
\par t5: 00000000000000000000000000000000 sp: 000000000000000000000000003F46A4
\par t6: 00000000000000000000000000000000 fp: 000000000000000000000000000D6B60
\par t7: 00000000000000000000000000000000 ra: 0000000000000000000000000020425C

I havent got a clue as to what i did wrong. Must be some programming error, but what? Or should i change some stuff to make it work with pukklink etc?
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

Those types of errors usually occur from trying to write to memory that either doesn't exist (i.e. just past end of RAM) or you don't have permission to write to (i.e. kernel memory)
Shoot Pixels Not People!
Makeshift Development
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

One of the best methods of tracking down bugs is to get someone else to look at your code. Try to track down where your program breaks by sprinkling printf() around to get a basic trace of your program's execution. Once you pinpoint the line (or lines) that are broken, respond in this thread with the code or post it somewhere.

If the line(s) refer to other variables or functions then you should post those too.

Other than that, we can't help you if we don't know what's wrong :P.
mharris
Posts: 155
Joined: Sun Jan 25, 2004 2:26 pm
Location: Annapolis, MD, USA

Post by mharris »

mrbrown wrote:Try to track down where your program breaks by sprinkling printf() around to get a basic trace of your program's execution.
This is OT, but that reminded me of a problem I've run into...

Since ps2link apparently uses UDP to send its prints to the host, I've run into some issues where print statements come out in the wrong order (and some packets are disappearing entirely). That's an additional challenge...

Has anyone else run into the same thing? I probably don't have the latest of anything (ps2lib, ps2link, pksh, etc.) so maybe it's already been addressed.
blackdroid
Posts: 564
Joined: Sat Jan 17, 2004 10:22 am
Location: Sweden
Contact:

Post by blackdroid »

looks like stack trashing ( sp register is the closest to BadVAddr ) .. the address is not dividable by 8, and storing a doubleworld will cause an exception. you can do an ee-objdump on your elf to see where the exception is generated ( check the EPC address ).
Kung VU
C-Be
Posts: 5
Joined: Thu Apr 15, 2004 7:03 am

Post by C-Be »

Using printf() is a good idea. They will show up in the InLink log window i presume. I was going to commend out code until it started working, but i will try the printf() first.

Unfortunally I am stuck to the emulator for a few days before i can test it on a real ps2. But when i narrowed it down but can't solve it (i hope i can offcourse) i will post it up here.

I tried to remove/avoid all use of doubles, i hope this helps (?)

And thanks again for all the help :)
Post Reply