Haptic: Explicitly avoid floating point arithmetic if it's not needed.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 16 Aug 2014 16:42:55 -0400
changeset 906916b5dda99337
parent 9068 a57a1d70ae07
child 9070 8973a237f360
Haptic: Explicitly avoid floating point arithmetic if it's not needed.

Thanks, Elias!

Partially fixes Bugzilla #2686.
src/haptic/linux/SDL_syshaptic.c
     1.1 --- a/src/haptic/linux/SDL_syshaptic.c	Sat Aug 16 16:41:25 2014 -0400
     1.2 +++ b/src/haptic/linux/SDL_syshaptic.c	Sat Aug 16 16:42:55 2014 -0400
     1.3 @@ -658,7 +658,6 @@
     1.4  SDL_SYS_ToDirection(Uint16 *dest, SDL_HapticDirection * src)
     1.5  {
     1.6      Uint32 tmp;
     1.7 -    float f;                    /* Ideally we'd use fixed point math instead of floats... */
     1.8  
     1.9      switch (src->type) {
    1.10      case SDL_HAPTIC_POLAR:
    1.11 @@ -690,7 +689,12 @@
    1.12          break;
    1.13  
    1.14      case SDL_HAPTIC_CARTESIAN:
    1.15 -        f = atan2(src->dir[1], src->dir[0]);
    1.16 +        if (!src->dir[1])
    1.17 +            *dest = (src->dir[0] >= 0 ? 0x4000 : 0xC000);
    1.18 +        else if (!src->dir[0])
    1.19 +            *dest = (src->dir[1] >= 0 ? 0x8000 : 0);
    1.20 +        else {
    1.21 +            float f = atan2(src->dir[1], src->dir[0]);    /* Ideally we'd use fixed point math instead of floats... */
    1.22                      /*
    1.23                        atan2 takes the parameters: Y-axis-value and X-axis-value (in that order)
    1.24                         - Y-axis-value is the second coordinate (from center to SOUTH)
    1.25 @@ -702,8 +706,9 @@
    1.26                        --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
    1.27                      */
    1.28                  tmp = (((Sint32) (f * 18000. / M_PI)) + 45000) % 36000;
    1.29 -        tmp = (tmp * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
    1.30 -        *dest = (Uint16) tmp;
    1.31 +            tmp = (tmp * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
    1.32 +            *dest = (Uint16) tmp;
    1.33 +        }
    1.34          break;
    1.35  
    1.36      default: