| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2016 Umair Khan <omerjerk@gmail.com> | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef AVUTIL_SOFTFLOAT_IEEE754_H | ||
| 22 | #define AVUTIL_SOFTFLOAT_IEEE754_H | ||
| 23 | |||
| 24 | #include <stdint.h> | ||
| 25 | |||
| 26 | #define EXP_BIAS 127 | ||
| 27 | #define MANT_BITS 23 | ||
| 28 | |||
| 29 | typedef struct SoftFloat_IEEE754 { | ||
| 30 | int32_t sign; | ||
| 31 | uint64_t mant; | ||
| 32 | int32_t exp; | ||
| 33 | } SoftFloat_IEEE754; | ||
| 34 | |||
| 35 | static const SoftFloat_IEEE754 FLOAT_0 = {0, 0, -126}; | ||
| 36 | static const SoftFloat_IEEE754 FLOAT_1 = {0, 0, 0}; | ||
| 37 | |||
| 38 | /** Normalize the softfloat as defined by IEEE 754 single-recision floating | ||
| 39 | * point specification | ||
| 40 | */ | ||
| 41 | ✗ | static inline SoftFloat_IEEE754 av_normalize_sf_ieee754(SoftFloat_IEEE754 sf) { | |
| 42 | ✗ | while( sf.mant >= 0x1000000UL ) { | |
| 43 | ✗ | sf.exp++; | |
| 44 | ✗ | sf.mant >>= 1; | |
| 45 | } | ||
| 46 | ✗ | sf.mant &= 0x007fffffUL; | |
| 47 | ✗ | return sf; | |
| 48 | } | ||
| 49 | |||
| 50 | /** Convert integer to softfloat. | ||
| 51 | * @return softfloat with value n * 2^e | ||
| 52 | */ | ||
| 53 | ✗ | static inline SoftFloat_IEEE754 av_int2sf_ieee754(int64_t n, int e) { | |
| 54 | ✗ | int sign = 0; | |
| 55 | |||
| 56 | ✗ | if (n < 0) { | |
| 57 | ✗ | sign = 1; | |
| 58 | ✗ | n *= -1; | |
| 59 | } | ||
| 60 | ✗ | return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, n << MANT_BITS, 0 + e}); | |
| 61 | } | ||
| 62 | |||
| 63 | /** Make a softfloat out of the bitstream. Assumes the bits are in the form as defined | ||
| 64 | * by the IEEE 754 spec. | ||
| 65 | */ | ||
| 66 | ✗ | static inline SoftFloat_IEEE754 av_bits2sf_ieee754(uint32_t n) { | |
| 67 | ✗ | return ((SoftFloat_IEEE754) { (n & 0x80000000UL) >> 31, (n & 0x7FFFFFUL), (int8_t)((n & 0x7F800000UL) >> 23)}); | |
| 68 | } | ||
| 69 | |||
| 70 | /** Convert the softfloat to integer | ||
| 71 | */ | ||
| 72 | static inline int av_sf2int_ieee754(SoftFloat_IEEE754 a) { | ||
| 73 | if(a.exp >= 0) return a.mant << a.exp ; | ||
| 74 | else return a.mant >>(-a.exp); | ||
| 75 | } | ||
| 76 | |||
| 77 | /** Divide a by b. b should not be zero. | ||
| 78 | * @return normalized result | ||
| 79 | */ | ||
| 80 | ✗ | static inline SoftFloat_IEEE754 av_div_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b) { | |
| 81 | int32_t mant, exp, sign; | ||
| 82 | ✗ | a = av_normalize_sf_ieee754(a); | |
| 83 | ✗ | b = av_normalize_sf_ieee754(b); | |
| 84 | ✗ | sign = a.sign ^ b.sign; | |
| 85 | ✗ | mant = ((((uint64_t) (a.mant | 0x00800000UL)) << MANT_BITS) / (b.mant| 0x00800000UL)); | |
| 86 | ✗ | exp = a.exp - b.exp; | |
| 87 | ✗ | return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, mant, exp}); | |
| 88 | } | ||
| 89 | |||
| 90 | /** Multiply a with b | ||
| 91 | * #return normalized result | ||
| 92 | */ | ||
| 93 | static inline SoftFloat_IEEE754 av_mul_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b) { | ||
| 94 | int32_t sign, mant, exp; | ||
| 95 | a = av_normalize_sf_ieee754(a); | ||
| 96 | b = av_normalize_sf_ieee754(b); | ||
| 97 | sign = a.sign ^ b.sign; | ||
| 98 | mant = (((uint64_t)(a.mant|0x00800000UL) * (uint64_t)(b.mant|0x00800000UL))>>MANT_BITS); | ||
| 99 | exp = a.exp + b.exp; | ||
| 100 | return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, mant, exp}); | ||
| 101 | } | ||
| 102 | |||
| 103 | /** Compare a with b strictly | ||
| 104 | * @returns 1 if the a and b are equal, 0 otherwise. | ||
| 105 | */ | ||
| 106 | ✗ | static inline int av_cmp_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b) { | |
| 107 | ✗ | a = av_normalize_sf_ieee754(a); | |
| 108 | ✗ | b = av_normalize_sf_ieee754(b); | |
| 109 | ✗ | if (a.sign != b.sign) return 0; | |
| 110 | ✗ | if (a.mant != b.mant) return 0; | |
| 111 | ✗ | if (a.exp != b.exp ) return 0; | |
| 112 | ✗ | return 1; | |
| 113 | } | ||
| 114 | |||
| 115 | #endif /*AVUTIL_SOFTFLOAT_IEEE754_H*/ | ||
| 116 |