Glenn Kasten | 2eafd4a | 2014-03-31 15:58:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <math.h> |
| 18 | #include <audio_utils/minifloat.h> |
| 19 | |
| 20 | #define EXPONENT_BITS 3 |
| 21 | #define EXPONENT_MAX ((1 << EXPONENT_BITS) - 1) |
| 22 | #define EXCESS ((1 << EXPONENT_BITS) - 2) |
| 23 | |
| 24 | #define MANTISSA_BITS 13 |
| 25 | #define MANTISSA_MAX ((1 << MANTISSA_BITS) - 1) |
| 26 | #define HIDDEN_BIT (1 << MANTISSA_BITS) |
| 27 | #define ONE_FLOAT ((float) (1 << (MANTISSA_BITS + 1))) |
| 28 | |
Glenn Kasten | 021f98c | 2014-04-03 12:31:41 -0700 | [diff] [blame] | 29 | #define MINIFLOAT_MAX ((EXPONENT_MAX << MANTISSA_BITS) | MANTISSA_MAX) |
| 30 | |
Glenn Kasten | 2eafd4a | 2014-03-31 15:58:40 -0700 | [diff] [blame] | 31 | #if EXPONENT_BITS + MANTISSA_BITS != 16 |
| 32 | #error EXPONENT_BITS and MANTISSA_BITS must sum to 16 |
| 33 | #endif |
| 34 | |
Glenn Kasten | 021f98c | 2014-04-03 12:31:41 -0700 | [diff] [blame] | 35 | gain_minifloat_t gain_from_float(float v) |
Glenn Kasten | 2eafd4a | 2014-03-31 15:58:40 -0700 | [diff] [blame] | 36 | { |
Glenn Kasten | 021f98c | 2014-04-03 12:31:41 -0700 | [diff] [blame] | 37 | if (isnan(v) || v <= 0.0f) { |
Glenn Kasten | 2eafd4a | 2014-03-31 15:58:40 -0700 | [diff] [blame] | 38 | return 0; |
| 39 | } |
Glenn Kasten | 021f98c | 2014-04-03 12:31:41 -0700 | [diff] [blame] | 40 | if (v >= 2.0f) { |
| 41 | return MINIFLOAT_MAX; |
| 42 | } |
Glenn Kasten | 2eafd4a | 2014-03-31 15:58:40 -0700 | [diff] [blame] | 43 | int exp; |
| 44 | float r = frexpf(v, &exp); |
| 45 | if ((exp += EXCESS) > EXPONENT_MAX) { |
Glenn Kasten | 021f98c | 2014-04-03 12:31:41 -0700 | [diff] [blame] | 46 | return MINIFLOAT_MAX; |
Glenn Kasten | 2eafd4a | 2014-03-31 15:58:40 -0700 | [diff] [blame] | 47 | } |
| 48 | if (-exp >= MANTISSA_BITS) { |
| 49 | return 0; |
| 50 | } |
| 51 | int mantissa = (int) (r * ONE_FLOAT); |
| 52 | return exp > 0 ? (exp << MANTISSA_BITS) | (mantissa & ~HIDDEN_BIT) : |
| 53 | (mantissa >> (1 - exp)) & MANTISSA_MAX; |
| 54 | } |
| 55 | |
Glenn Kasten | 021f98c | 2014-04-03 12:31:41 -0700 | [diff] [blame] | 56 | float float_from_gain(gain_minifloat_t a) |
Glenn Kasten | 2eafd4a | 2014-03-31 15:58:40 -0700 | [diff] [blame] | 57 | { |
| 58 | int mantissa = a & MANTISSA_MAX; |
| 59 | int exponent = (a >> MANTISSA_BITS) & EXPONENT_MAX; |
| 60 | return ldexpf((exponent > 0 ? HIDDEN_BIT | mantissa : mantissa << 1) / ONE_FLOAT, |
| 61 | exponent - EXCESS); |
| 62 | } |