[4524] | 1 | //roarfloat.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
[4524] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
| 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | #include "libroar.h" |
---|
| 37 | |
---|
[4525] | 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 | |
---|
[4524] | 153 | //ll |
---|