 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
MrMr[iCE]
Joined: 03 Oct 2005 Posts: 43
|
Posted: Wed Jan 24, 2007 9:35 pm Post subject: VFPU math lib |
|
|
I've been working on a general purpose math lib to replace most of libm's implementations of major math functions. I'm almost done with it but here's a taste of what to expect, in cycle times:
| Code: |
let v = 0.4f
- sinf(v) = 0.389418, cycles: 856
- vfpu_sinf(v) = 0.389418, cycles: 160
- cosf(v) = 0.921061, cycles: 990
- vfpu_cosf(v) = 0.921061, cycles: 154
- tanf(v) = 0.422793, cycles: 1632
- vfpu_tanf(v) = 0.422793, cycles: 121
- asinf(v) = 0.411517, cycles: 1265
- vfpu_asinf(v) = 0.411517, cycles: 154
- acosf(v) = 1.159279, cycles: 1433
- vfpu_acosf(v) = 1.159280, cycles: 107
- atanf(v) = 0.380506, cycles: 692
- vfpu_atanf(v) = 0.380506, cycles: 126
- sinhf(v) = 0.410752, cycles: 1898
- vfpu_sinhf(v) = 0.410752, cycles: 356
- coshf(v) = 1.081072, cycles: 1885
- vfpu_coshf(v) = 1.081072, cycles: 246
- tanhf(v) = 0.379949, cycles: 1525
- vfpu_tanhf(v) = 0.379949, cycles: 208
- expf(v) = 1.491825, cycles: 1351
- vfpu_expf(v) = 1.491824, cycles: 212
- logf(v) = -0.916291, cycles: 1409
- vfpu_logf(v) = -0.916292, cycles: 210
- fabsf(v) = 0.400000, cycles: 7
- vfpu_fabsf(v) = 0.400000, cycles: 93
- sqrtf(v) = 0.632456, cycles: 40
- vfpu_sqrtf(v) = 0.632455, cycles: 240
- powf(v, v) = 0.693145, cycles: 3488
- vfpu_powf(v, v) = 0.693145, cycles: 412
|
The only clear losers in my lib are fabsf and sqrtf, since the floating point ops on the allegrex already do those functions..I'll prolly axe those, let the allegrex deal with those since it seems to do well in that area.
My lib will have other functions not in libm though, some related to vector operations (like vfpu_normalize_vector), some matrix ops (like vfpu_perspective_matrix, libpspgum_vfpu didnt have a proper asm version at the time), and some quaternion math as well. Stay tuned as I make progress.
As I said, this is a work in progress, as such, dont expect much help from me using it =) Here's the link, you should know what do with the files: http://mrmrice.fx-world.org/libpspmath.zip |
|
| Back to top |
|
 |
Tinnus
Joined: 29 Jul 2006 Posts: 67
|
Posted: Thu Jan 25, 2007 12:30 am Post subject: |
|
|
I think it should be useful to add functions like sinfd, sinft and sinfq (for all functions) to speedup calculations in the case the user wants/can calculate 2, 3 or 4 values at the same time. Should be faster than calling the function multiple times :)
I know the main purpose is to just replace libm, but why not enable a little (more) optimization as well? :) _________________ Let's see what the PSP reserves... well, I'd say anything is better than Palm OS. |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Thu Jan 25, 2007 1:53 am Post subject: |
|
|
| well if you were on #pspdev, you should probably know that to enable more optimization you need a gcc "aware" of vfpu registers (single or vector ones). So such optimizations for sinfd/t/q would not really be efficient and very cumbersome to use. |
|
| Back to top |
|
 |
MrMr[iCE]
Joined: 03 Oct 2005 Posts: 43
|
Posted: Fri Jan 26, 2007 4:54 am Post subject: |
|
|
v2 is up for grabs
http://mrmrice.fx-world.org/files/libpspmath_v2.zip
This one adds quite a few quaternion functions, check pspmath.h for the full list. atan2f is available as well.
to install:
make install will copy the lib and header files into the proper sdk folders, so just do #include <pspmath.h> and add -lpspmath to your LIBS line in your makefiles |
|
| Back to top |
|
 |
cools
Joined: 04 Mar 2006 Posts: 46
|
Posted: Fri Jan 26, 2007 1:02 pm Post subject: |
|
|
| This is pretty cool! It can make using the vfpu a lot easier for people who dont know how to use the vfpu... |
|
| Back to top |
|
 |
MrMr[iCE]
Joined: 03 Oct 2005 Posts: 43
|
Posted: Fri Jan 26, 2007 5:12 pm Post subject: |
|
|
v3 is up now
http://mrmrice.fx-world.org/files/libpspmath_v3.zip
This adds one new function: vfpu_ease_in_out. This function is mainly for animation control, when you want to smoothly interpolate between 2 points with an accleration and deceleration curve.
Ive included a sample in the zip that shows how to use the quaternion functions, including how to interpolate between 2 quaternions, how to make a random 3d starfield and some other features. like the vfpu random number generator.
enjoy =) |
|
| Back to top |
|
 |
Bytrix
Joined: 14 Sep 2005 Posts: 72 Location: England
|
Posted: Mon Jan 29, 2007 8:01 pm Post subject: |
|
|
| Great work MrMr[iCE]. It'll be interesting to see how these functions speed up my games. Especially in collision detection. |
|
| Back to top |
|
 |
pegasus2000
Joined: 12 Jul 2006 Posts: 160
|
Posted: Fri Feb 02, 2007 4:23 am Post subject: what is the license ? BSD ? |
|
|
| MrMr[iCE] wrote: | v3 is up now
http://mrmrice.fx-world.org/files/libpspmath_v3.zip
This adds one new function: vfpu_ease_in_out. This function is mainly for animation control, when you want to smoothly interpolate between 2 points with an accleration and deceleration curve.
Ive included a sample in the zip that shows how to use the quaternion functions, including how to interpolate between 2 quaternions, how to make a random 3d starfield and some other features. like the vfpu random number generator.
enjoy =) |
What is the license ? BSD ? |
|
| Back to top |
|
 |
Xfacter
Joined: 28 Feb 2007 Posts: 9
|
Posted: Wed Feb 28, 2007 10:24 am Post subject: |
|
|
| Great lib, but there's a problem with vfpu_atan2f. An x and y of zero will cause a crash (divide by zero). Most libraries I've seen return 0 when this is encountered, easy fix. |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Wed Feb 28, 2007 4:13 pm Post subject: |
|
|
| Xfacter wrote: | | Great lib, but there's a problem with vfpu_atan2f. An x and y of zero will cause a crash (divide by zero). Most libraries I've seen return 0 when this is encountered, easy fix. |
add something like "vcmp.s NS, S000; vcmovt.s S000, S000[0], 0" at the end of the function, and any situation where a NaN would occur would turn the result in 0.0. You may need to adapt S000 to the right vfpu register being used as a result in this function. |
|
| Back to top |
|
 |
KickinAezz
Joined: 03 Jun 2007 Posts: 328
|
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Thu Jun 14, 2007 8:41 pm Post subject: |
|
|
| KickinAezz wrote: | http://mrmrice.fx-world.org/files/libpspmath_v4.zip -<<< Is also available
Version 4!?? |
I have the libpspmath v4 given by MrMrIce the last time I "saw" him. So yes, the last version is 4. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Jun 18, 2007 1:26 pm Post subject: |
|
|
This is a noob question, but if you define floats in your app, and perform
math operations on them, does that automatically mean that the FPU is used
to calculate them in the PSP? |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Mon Jun 18, 2007 4:59 pm Post subject: |
|
|
Yes. Else it would have to be done in software emulation (as IS done with doubles) and this would be hella slow. _________________ <Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Jun 18, 2007 7:11 pm Post subject: |
|
|
| Thanks, I suspected so. |
|
| Back to top |
|
 |
Xfacter
Joined: 28 Feb 2007 Posts: 9
|
Posted: Mon Jul 02, 2007 8:02 am Post subject: |
|
|
I don't know if you meant to do this or what, but vfpu_logf is actually natural log... Here's log:
| Code: | float vfpu_log(float x) {
float result;
__asm__ volatile (
"mtv %1, S000\n"
"vcst.s S001, VFPU_LOG2TEN\n"
"vrcp.s S001, S001\n"
"vlog2.s S000, S000\n"
"vmul.s S000, S000, S001\n"
"mfv %0, S000\n"
: "=r"(result) : "r"(x));
return result;
} |
Edit: Oops, don't mind me. Guess I don't know much about libm's naming scheme (generally when I see "log" I assume log10). Sorry! |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Thu Aug 30, 2007 2:16 pm Post subject: |
|
|
DELETED
Last edited by hlide on Thu Aug 30, 2007 2:18 pm; edited 1 time in total |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Thu Aug 30, 2007 2:17 pm Post subject: |
|
|
| Xfacter wrote: |
Edit: Oops, don't mind me. Guess I don't know much about libm's naming scheme (generally when I see "log" I assume log10). Sorry! |
log(x) = log10(x) = log2(x)/log2(10);
this is what the code is doing, so there's no problem here. |
|
| Back to top |
|
 |
Xfacter
Joined: 28 Feb 2007 Posts: 9
|
Posted: Thu Aug 30, 2007 2:37 pm Post subject: |
|
|
| hlide wrote: | | Xfacter wrote: |
Edit: Oops, don't mind me. Guess I don't know much about libm's naming scheme (generally when I see "log" I assume log10). Sorry! |
log(x) = log10(x) = log2(x)/log2(10);
this is what the code is doing, so there's no problem here. |
That's what the revised code I posted does, the one in the library is natural log. |
|
| Back to top |
|
 |
headness13
Joined: 21 Oct 2007 Posts: 2
|
Posted: Sun Oct 21, 2007 6:25 pm Post subject: libpspmath |
|
|
very nice library, but there are a few functions missing, like:
rotate_matrixX(M44, angle)
rotate_matrixY(M44, angle)
rotate_matrixZ(M44, angle)
rotate_matrixXYZ(M44, anglex, angley, anglez)
multiply_matrix(M44, M44, M44)
vector_cross_multiply (for FVector3 and FVector4)
could you help me with this things ?! - i can make them in C code, but asm is so much faster - and speed is a ishue at what i-m doing
thanx if you decide to help me MrMr[iCE] |
|
| Back to top |
|
 |
snowsquirrel
Joined: 24 Feb 2008 Posts: 51
|
Posted: Sun Apr 06, 2008 12:27 am Post subject: |
|
|
hello,
I am interested in the vector proecessing funciton in the lb. But I notice that they are v4's and not v3's. To use this with v3's, do I have to assign x,y,z in my v4 to xyz in my v3. Or do most people just use v4's for everything, ignoring the 'w' when only xyz are needed?
~S |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Sun Apr 06, 2008 1:34 am Post subject: |
|
|
well, as an example :
| Code: | void vfpu_add_vector(ScePspFVector4 *vout, ScePspFVector4 *va, ScePspFVector4 *vb) {
__asm__ volatile (
"lv.q C000, %1\n"
"lv.q C010, %2\n"
"vadd.t C020, C000, C010\n"
"sv.q C020, %0\n"
: "+m"(*vout): "m"(*va), "m"(*vb));
}
|
vadd.t adds two 3D vectors, not 4D vectors
there is a reason why we use ScePspFVector4 instead of ScePspFVector3 : "lv.q ..."
vfpu can read a 4-component vector in one instruction instead of 4 single "lv.s"
but it cannot load or store a 2D or 3D vectors in memory.
but if you really want that bad a ScePspFVector3 instead, you need to convert it into :
| Code: | void vfpu_add_vector3(ScePspFVector3 *vout, ScePspFVector3 *va, ScePspFVector3 *vb) {
__asm__ volatile (
//"lv.q C000, %1\n"
"lv.s S000, %4\n"
"lv.s S001, %5\n"
"lv.s S002, %6\n"
//"lv.q C010, %2\n"
"lv.s S010, %7\n"
"lv.s S011, %8\n"
"lv.s S012, %9\n"
"vadd.t C020, C000, C010\n"
//"sv.q C020, %0\n"
"sv.s S020, %0\n"
"sv.s S021, %1\n"
"sv.s S022, %2\n"
: "+m"(vout->x), "+m"(vout->y), "+m"(vout->z)
: "m"(va->x), "m"(va->y), "m"(va->z),
"m"(vb->x), "m"(vb->y), "m"(vb->z));
}
|
|
|
| Back to top |
|
 |
snowsquirrel
Joined: 24 Feb 2008 Posts: 51
|
Posted: Sun Apr 06, 2008 1:50 am Post subject: |
|
|
yes, I have looked at the code, but it really means nothing too me.
I suspected that the instructions were optimized for v4's not v3's. I think that is pretty common.
But I am not 100% sure what you are saying?
| Code: | "lv.q C000, %1\n"
"lv.q C010, %2\n" |
loading va and vb into registers
| Code: | | "vadd.t C020, C000, C010\n" |
add those two registers. you said vadd.t adds 3d vectors? so what is happening to 'w' here?
save result to vout.
| Code: | | : "+m"(*vout): "m"(*va), "m"(*vb) |
what is this doing?
I assume if I used this exact code with v3's memory stomping would occur where it expected the last 4 bytes of the v4.
sorry to be daft, I have just never worked with assember much, other than some trivial stuff years ago.
~S |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Sun Apr 06, 2008 2:16 am Post subject: |
|
|
| snowsquirrel wrote: | yes, I have looked at the code, but it really means nothing too me.
I suspected that the instructions were optimized for v4's not v3's. I think that is pretty common.
But I am not 100% sure what you are saying?
| Code: | "lv.q C000, %1\n"
"lv.q C010, %2\n" |
loading va and vb into registers
| Code: | | "vadd.t C020, C000, C010\n" |
add those two registers. you said vadd.t adds 3d vectors? so what is happening to 'w' here?
save result to vout.
| Code: | | : "+m"(*vout): "m"(*va), "m"(*vb) |
what is this doing?
I assume if I used this exact code with v3's memory stomping would occur where it expected the last 4 bytes of the v4.
sorry to be daft, I have just never worked with assember much, other than some trivial stuff years ago.
~S |
vfpu instructions use suffixes like .s (single/scalar), .p (pair, 2D), .t(triple, 3D), .q(quad, 4D)
with .q, x, y, z and w are touched
with .t, only x, y and z are touched, w is left untouched
with .p, only x and y are touched, z and w are left untouched
with .s, you can access any element in a matrix
so any instructions with .t suffix would ignore your w component.
load and store operations :
lv = load vector
sv = store vector
lv.s/sv.s = load/store a scalar element in matrix (Smcr = element in matrix m, at column c, at row r).
lv.q/sv.q = load/store a 4D vector (Cmcr = column c vector in matrix m, starting from element at row r; Rmcr = row r vector in matrix m, starting from element at column c).
there is no lv.p/sv.p/lv.t/sv.t, so you need to split into lv.s/sv.s
inline_asm : 'asm' '(' asm_sequence_string [':' [ output_regs ] [ ':' [ input_regs ] [ ':' clobbered_regs ] ] ] ')' ';'
output_registers : output_register [ output_registers ] ;
input_registers : input_register [ input_registers ] ;
output_register :
"=m"(var) --> this a memory place just for output
"+m"(var) --> this a memory place for input and output
input_register :
"m"(var) --> this a memory place just for input
%0, %1, ... : if you have in this order : "=m"(*vout) : "m"(*va), "m"(*vb), gcc would substitute %0 with a machine address to access vout, %1 to access va and %2 to access vb. Seem them as macro parameters. |
|
| Back to top |
|
 |
snowsquirrel
Joined: 24 Feb 2008 Posts: 51
|
Posted: Sun Apr 06, 2008 3:14 am Post subject: |
|
|
Ok I understand everything but this line:
| Code: | | : "+m"(*vout): "m"(*va), "m"(*vb)" |
So this line is mapping the value in registers to value in memory? If so, shouldn't this mapping be done at the beginning? Or does it take effect in the beginning?
So if my C code uses v3's, would I be better to manually add the 3 elements, or create temporary v4's with v3 values, and then use vfpu_vector_add function? Or use the 3x lv.s method?
Thanks,
~S |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Sun Apr 06, 2008 6:06 am Post subject: |
|
|
| snowsquirrel wrote: | So if my C code uses v3's, would I be better to manually add the 3 elements, or create temporary v4's with v3 values, and then use vfpu_vector_add function? Or use the 3x lv.s method?
~S |
3xlv.s method is probably best for this case, so long as your vector only has 3 components. |
|
| Back to top |
|
 |
snowsquirrel
Joined: 24 Feb 2008 Posts: 51
|
Posted: Sun Apr 06, 2008 1:02 pm Post subject: |
|
|
Extended Asm. I get it now. Thanks.
~S |
|
| Back to top |
|
 |
Heimdall
Joined: 10 Nov 2005 Posts: 259 Location: Netherlands
|
Posted: Thu Apr 10, 2008 4:35 pm Post subject: |
|
|
Hi guys,
From v3 to v4 I see that there was a change on the API and the demo on v3 doesn't build, does anyone know what changed from v3 to v4? (so i can be lazzy and don't diff the code ;)) |
|
| Back to top |
|
 |
Heimdall
Joined: 10 Nov 2005 Posts: 259 Location: Netherlands
|
Posted: Thu Apr 10, 2008 5:44 pm Post subject: |
|
|
| nevermind it was straightforward! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|