Fast Math - DC - Questions

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Fast Math - DC - Questions

Post by PH3NOM »

I am posting because I have found inaccuracies in the DC Fast Math operations.

I have linked to -lm, and included the following:

Code: Select all

#include <math.h>
#include <dc/fmath.h>
#include <dc/matrix.h>
#include <dc/matrix3d.h>
Using KGLX, I have written a function to render a cylinder based on a single point and its center point.

Code: Select all

void DCE_glVectorRotate( vector4f vector, vector4f origin, float angle, int plane1, int plane2 )
{
    float r[2];
    r[0] = (origin[plane1] + ((vector[plane1] - origin[plane1]) * cos(angle)) - ((vector[plane2] - origin[plane2]) * sin(angle)));
    r[1] = (origin[plane2] + ((vector[plane1] - origin[plane1]) * sin(angle)) + ((vector[plane2] - origin[plane2]) * cos(angle)));
    vector[plane1]=r[0];
    vector[plane2]=r[1];
}

#define RANGLE 1.0f         // Rotation Angle
#define RSD 360.0f/RANGLE    // Rotated Side
#define RADIAN 0.0174532925  // Convert Degrees to Radians

// Radius is determined by the distance between p and c ( point and center )
// With RANGLE set to 15, this will draw an object with 24 sides ( faster )
// Decrease RANGLE down to 1.0f to draw an object with 360 sides ( slower )
void DCE_glRenderCylinder( vector4f p, vector4f c, vector4f rot, vector4f pos,
                           vector4f campos, vector4f camrot,
                           GLuint *txr, float u, float v, float height, float scale )
{
     int i;
     float ust=0, uend, utx = u/RSD; 
     matrix4f s;
          
     for( i=0; i< RSD; i++ )
     {    
          s[0][x]=p[x]; s[0][y]=p[y]; s[0][z]=p[z];
          p[y]+=height; c[y]+=height;
          s[1][x]=p[x]; s[1][y]=p[y]; s[1][z]=p[z];
          DCE_glVectorRotate( p, c, RADIAN*RANGLE, x, z ); 
          s[2][x]=p[x]; s[2][y]=p[y]; s[2][z]=p[z];
          p[y]-=height; c[y]-=height;          
          s[3][x]=p[x]; s[3][y]=p[y]; s[3][z]=p[z];
          
          uend = ust + utx;    
          DCE_glRender4fvuuvScale( &s, rot, pos, txr,
                                   campos, camrot, ust, uend, v, scale );    
          ust += utx;
     }
}
I know this is not optimal, simply a starting point.
Anyway, this works fine ( texture disabled for clarity of error in rotation )
Image

However, when I try to optimize this routine using the kos dc fast math operations, it is not correct.
The object is continually spinning, and we can see the final point does not reach the original point:

Code: Select all

void DCE_glVectorRotate( vector4f vector, vector4f origin, float angle, int plane1, int plane2 )
{
    float r[2];
    r[0] = (origin[plane1] + ((vector[plane1] - origin[plane1]) * fcos(angle)) - ((vector[plane2] - origin[plane2]) * fsin(angle)));
    r[1] = (origin[plane2] + ((vector[plane1] - origin[plane1]) * fsin(angle)) + ((vector[plane2] - origin[plane2]) * fcos(angle)));
    vector[plane1]=r[0];
    vector[plane2]=r[1];
}
Image
Ayla
DC Developer
DC Developer
Posts: 142
Joined: Thu Apr 03, 2008 7:01 am
Has thanked: 0
Been thanked: 4 times
Contact:

Re: Fast Math - DC - Questions

Post by Ayla »

On the first example, shouldn't you use cosf/sinf instead of cos/sin? The latter take doubles as their argument.
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 366
Joined: Wed Mar 30, 2011 12:13 pm
Has thanked: 2 times
Been thanked: 0

Re: Fast Math - DC - Questions

Post by RyoDC »

By the way yes, I read somewhere in WinCe docs, that sh4 works 20 times slowly with double precision floats, then single precision.
How do I try to build a Dreamcast toolchain:
Image
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Fast Math - DC - Questions

Post by BlueCrab »

The normal way that code is compiled for the Dreamcast (with the -m4-single-only flag to GCC), doubles and floats are essentially the same thing (double gets internally mapped to float).
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: Fast Math - DC - Questions

Post by PH3NOM »

Thank you all for responding.

To make things clear, I have posted video on youtube.

http://www.youtube.com/watch?v=fi_QiUTl ... e=youtu.be
Post Reply