Changeset 4525:07dfa64029bc in roaraudio for libroar/roarfloat.c
- Timestamp:
- 10/16/10 22:33:14 (13 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libroar/roarfloat.c
r4524 r4525 36 36 #include "libroar.h" 37 37 38 #ifdef ROAR_HAVE_LIBM 39 #include <math.h> 40 #endif 41 42 union c4f { 43 unsigned char c[4]; 44 roarfloat32 f; 45 }; 46 47 roarfloat32 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 60 uint16_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 68 uint16_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 74 roarfloat32 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 97 float 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 117 int 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 123 int 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 138 int 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 38 153 //ll
Note: See TracChangeset
for help on using the changeset viewer.