Changeset 4525:07dfa64029bc in roaraudio


Ignore:
Timestamp:
10/16/10 22:33:14 (14 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

added generic float handling

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • include/libroar/roarfloat.h

    r4524 r4525  
    3939#include "libroar.h" 
    4040 
     41typedef uint32_t roarfloat32; 
     42 
     43#define ROAR_UFLOAT32_MAX  65535 
     44 
     45#define ROAR_UFLOAT32_ZERO ROAR_HOST2NET32(0x00000000); 
     46#define ROAR_UFLOAT32_PINF ROAR_HOST2NET32(0x00000001); 
     47#define ROAR_UFLOAT32_NINF ROAR_HOST2NET32(0x0000FFFF); 
     48#define ROAR_UFLOAT32_PNAN ROAR_HOST2NET32(0x00007FFF); 
     49#define ROAR_UFLOAT32_NNAN ROAR_HOST2NET32(0x00008000); 
     50 
     51roarfloat32 roar_ufloat32_build(const uint16_t mul, const uint16_t scale); 
     52uint16_t roar_ufloat32_scale(const roarfloat32 f); 
     53uint16_t roar_ufloat32_mul(const roarfloat32 f); 
     54 
     55roarfloat32 roar_ufloat32_from_float(const float       f); 
     56float       roar_ufloat32_to_float  (const roarfloat32 f); 
     57 
     58int roar_float32_iszero(const roarfloat32 f); 
     59int roar_float32_isinf(const roarfloat32 f); 
     60int roar_float32_isnan(const roarfloat32 f); 
     61 
    4162#endif 
    4263 
  • libroar/roarfloat.c

    r4524 r4525  
    3636#include "libroar.h" 
    3737 
     38#ifdef ROAR_HAVE_LIBM 
     39#include <math.h> 
     40#endif 
     41 
     42union c4f { 
     43 unsigned char c[4]; 
     44 roarfloat32 f; 
     45}; 
     46 
     47roarfloat32 roar_ufloat32_build(const uint16_t mul, const uint16_t scale) { 
     48 union c4f ret; 
     49 
     50 ROAR_DBG("roar_ufloat32_build(mul=%u, scale=%u) = ?", (unsigned int)mul, (unsigned int)scale); 
     51 
     52 ret.c[0] = (scale & 0xFF00) >> 8; 
     53 ret.c[1] = (scale & 0x00FF) >> 0; 
     54 ret.c[2] = (mul   & 0xFF00) >> 8; 
     55 ret.c[3] = (mul   & 0x00FF) >> 0; 
     56 
     57 return ret.f; 
     58} 
     59 
     60uint16_t roar_ufloat32_scale(const roarfloat32 f) { 
     61 const union c4f p = {.f = f}; 
     62 
     63 ROAR_DBG("roar_ufloat32_scale(f=?): p.c[] = {%u, %u, %u, %u}", (unsigned int)p.c[0], (unsigned int)p.c[1], (unsigned int)p.c[2], (unsigned int)p.c[3]); 
     64 
     65 return (p.c[0] << 8) | p.c[1]; 
     66} 
     67 
     68uint16_t roar_ufloat32_mul(const roarfloat32 f) { 
     69 const union c4f p = {.f = f}; 
     70 
     71 return (p.c[2] << 8) | p.c[3]; 
     72} 
     73 
     74roarfloat32 roar_ufloat32_from_float(const float       f) { 
     75 uint16_t scale; 
     76 
     77 if ( f < 0 ) 
     78  return ROAR_UFLOAT32_NNAN; 
     79 
     80 if ( f == 0 ) 
     81  return ROAR_UFLOAT32_ZERO; 
     82 
     83 if ( f > ROAR_UFLOAT32_MAX ) 
     84  return ROAR_UFLOAT32_PINF; 
     85 
     86 if ( f <= 1 ) { 
     87  scale = ROAR_UFLOAT32_MAX; 
     88 } else { 
     89  scale = (float)ROAR_UFLOAT32_MAX/f; 
     90 } 
     91 
     92 ROAR_DBG("roar_ufloat32_from_float(f=%f): scale=%u", f, (unsigned int)scale); 
     93 
     94 return roar_ufloat32_build(f*(float)scale, scale); 
     95} 
     96 
     97float       roar_ufloat32_to_float  (const roarfloat32 f) { 
     98 float mul   = roar_ufloat32_mul(f); 
     99 float scale = roar_ufloat32_scale(f); 
     100 
     101 ROAR_DBG("roar_ufloat32_to_float(f=?): mul=%f, scale=%f", mul, scale); 
     102 
     103 if ( roar_float32_iszero(f) ) 
     104  return 0; 
     105 
     106#ifdef ROAR_HAVE_LIBM 
     107 if ( roar_float32_isinf(f) ) 
     108  return INFINITY; 
     109 
     110 if ( roar_float32_isnan(f) ) 
     111  return NAN; 
     112#endif 
     113 
     114 return mul/scale; 
     115} 
     116 
     117int roar_float32_iszero(const roarfloat32 f) { 
     118 const union c4f p = {.f = f}; 
     119 
     120 return p.c[2] == 0 && p.c[3] == 0; 
     121} 
     122 
     123int roar_float32_isinf(const roarfloat32 f) { 
     124 const union c4f p = {.f = f}; 
     125 
     126 if ( p.c[0] != 0 || p.c[1] != 0 ) 
     127  return 0; 
     128 
     129 if ( p.c[2] == 0x00 && p.c[3] == 0x01 ) { 
     130  return  1; 
     131 } else if ( p.c[2] == 0xFF && p.c[3] == 0xFF ) { 
     132  return -1; 
     133 } else { 
     134  return 0; 
     135 } 
     136} 
     137 
     138int roar_float32_isnan(const roarfloat32 f) { 
     139 const union c4f p = {.f = f}; 
     140 
     141 if ( p.c[0] != 0 || p.c[1] != 0 ) 
     142  return 0; 
     143 
     144 if ( p.c[2] == 0x7F && p.c[3] == 0xFF ) { 
     145  return  1; 
     146 } else if ( p.c[2] == 0x80 && p.c[3] == 0x00 ) { 
     147  return -1; 
     148 } else { 
     149  return 0; 
     150 } 
     151} 
     152 
    38153//ll 
Note: See TracChangeset for help on using the changeset viewer.