gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > calling sin/cos lut for arcsin/arccos

#99661 - Rajveer - Wed Aug 23, 2006 2:17 am

hi, a while ago i asked about arc sin/cos functions for libnds and somebody said you can use the existing sin/cos luts and go backwards. for future reference, can somebody tell me how to do this?

cheers

#99678 - kevinc - Wed Aug 23, 2006 6:27 am

I'm not sure if the code actually works, I'm creating it as I type, but I hope you'll get the idea. It assumes that COS is an array of shorts and that it uses a circle with 512 degrees.
Of course, considering it's 8AM here and I haven't slept, it'll be probably horribly broken.

Code:
// comparation function for COS
// uses the fact that for [0, 180], cos is ordered from bigger to smaller,
// so a binary search can be used.
// par = function parameter; lut = position inside the COS lut
// because the order is from higher to lower, comparisons are the opposite
// of what you'd normally use (lut[x + 1] <= lut[x])
int arccomp(const void *a, const void *b) {
    short *par = (short*)a, *lut = (short*)b;
    if (*par == *lut ||
        (*(lut + 1) < *par && *par < *lut)) return 0;
    else if (*par <= *lut) return 1;
    else return -1;
}

// arccos: returns a value in [0, 256]
short arccos(short par) {
    short *ac = (short*)bsearch(&par, COS, 257, sizeof(short), arccomp); // 257 = size of [0, 256]
    if (!ac) ErrorOutOfRange(); // not a real function, do some error handling instead
    return ac - COS; // return index of ac in relation to COS, which equals the resulting angle
}

// arcsin: returns a value in [-128, 128]
// uses the identity sin(x) = cos(90 - x) => asin(x) = 90 - acos(x)
short arcsin(short par) {
    return 128 - arccos(par);
}



EDIT: added some comments
_________________
http://akzeac.blogspot.com


Last edited by kevinc on Thu Aug 24, 2006 6:39 pm; edited 5 times in total

#99689 - Rajveer - Wed Aug 23, 2006 9:30 am

ahh cool, so thats how its done! i like to learn as much as i can :)

thanks again kevinc!

#99693 - kevinc - Wed Aug 23, 2006 10:58 am

Rajveer wrote:
ahh cool, so thats how its done! i like to learn as much as i can :)

thanks again kevinc!


You're welcome :)




/damn, that code is unreadable, I'll comment it later :D
_________________
http://akzeac.blogspot.com

#99702 - Rajveer - Wed Aug 23, 2006 1:01 pm

when trying to compile the bsearch and erroroutoufrange commands, it gives an implicit declaration of function error. i wasnt sure if i needed any c++ libraries or anything installed as the compiler compiled things like arrays e.t.c. which libraries do i need to install and where can i get them from? do i need something such as visual studio 2005? (which my brother has but i didnt really want to install)

#99704 - kevinc - Wed Aug 23, 2006 1:15 pm

Rajveer wrote:
when trying to compile the bsearch and erroroutoufrange commands, it gives an implicit declaration of function error. i wasnt sure if i needed any c++ libraries or anything installed as the compiler compiled things like arrays e.t.c. which libraries do i need to install and where can i get them from? do i need something such as visual studio 2005? (which my brother has but i didnt really want to install)


bsearch a standard C function, in stdlib.h. Most of the libraries you'll need already come with a standard compiler installation :)
If you're using DevkitPro, a text editor is the only extra thing you'll need. You can install an IDE, for example Eclipse, to make programming easier, but it's optional.

ErrorOutOfRange is not a real function, of course. I meant to say "put there some code that handles an out-of-range error".
_________________
http://akzeac.blogspot.com

#99712 - Rajveer - Wed Aug 23, 2006 2:35 pm

oh right, cheers! i thought i may not need to install libraries with devkitpro when i first started, but ill install a standard compiler for c++ functions :)

any compilers you recommend?

#99725 - kevinc - Wed Aug 23, 2006 4:32 pm

Rajveer wrote:
oh right, cheers! i thought i may not need to install libraries with devkitpro when i first started, but ill install a standard compiler for c++ functions :)

any compilers you recommend?


Um... the compiler that comes with devkitpro? o_o
(the compiler that is devkitpro?)
_________________
http://akzeac.blogspot.com

#99737 - Rajveer - Wed Aug 23, 2006 6:00 pm

oh, i thought you meant that i had to install a standard c++ compiler to get the c++ libraries that include code such as bsearch. so once i get a c++ library, how do i include it in my work, with a normal include call? or do i have to set up some environmental variables? (sorry im being such a newb :( )

#99797 - tepples - Thu Aug 24, 2006 1:30 am

The bsearch() function (along with the rest of <stdlib.h> and <string.h>) is part of Newlib, which comes with each devkitPro architecture package.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#99902 - Rajveer - Thu Aug 24, 2006 6:25 pm

ahh cool thats what i meant, i didnt know that c libraries were included with devkitpro :)

#99905 - Rajveer - Thu Aug 24, 2006 6:29 pm

kevinc wrote:
return ac - COS; // return index of ac in relation to COS, which equals the resulting angle


hmm im not sure i understand this part, wont i just return ac? also ac - COS gives a binary error :S

#99908 - kevinc - Thu Aug 24, 2006 6:36 pm

Rajveer wrote:
kevinc wrote:
return ac - COS; // return index of ac in relation to COS, which equals the resulting angle


hmm im not sure i understand this part, wont i just return ac? also ac - COS gives a binary error :S


Both ac and COS are pointers. ac points to COS[angle], so that ac - COS = angle.

What's the error that it's giving you? I'm assuming that COS is an array of ints, I may be wrong...

EDIT: Now that I've checked it, it's actually an array of shorts. I've changed the code to correct that.
_________________
http://akzeac.blogspot.com

#99976 - Rajveer - Fri Aug 25, 2006 1:30 am

ah cool, cheers for the help. i understand the code now, thanks :)

#100016 - hellfire - Fri Aug 25, 2006 1:16 pm

consider that a binary-search on the 256-entry table requires 8 memory-accesses, with the first 4 most probably missing cache.
_________________
"The three chief virtues of a programmer are: Laziness, Impatience and Hubris"

#100125 - Rajveer - Sat Aug 26, 2006 12:20 pm

yeah but 8 memory accesses is surely less intensive than calculating and angle with an arccos function? (involving multiple multiplications)

#100676 - hellfire - Tue Aug 29, 2006 2:59 pm

"The ARM9E core sustains one 16x16 or 16x32 multiply-accumulate (MAC) per cycle"
but the situation is not as good with ram.
so at the end a good integer-approximation could actually be faster.
_________________
"The three chief virtues of a programmer are: Laziness, Impatience and Hubris"