yading@11
|
1 /*
|
yading@11
|
2 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
|
yading@11
|
3 *
|
yading@11
|
4 * This file is part of FFmpeg.
|
yading@11
|
5 *
|
yading@11
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
8 * License as published by the Free Software Foundation; either
|
yading@11
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
10 *
|
yading@11
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
14 * Lesser General Public License for more details.
|
yading@11
|
15 *
|
yading@11
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
19 */
|
yading@11
|
20
|
yading@11
|
21 #ifndef AVUTIL_SOFTFLOAT_H
|
yading@11
|
22 #define AVUTIL_SOFTFLOAT_H
|
yading@11
|
23
|
yading@11
|
24 #include <stdint.h>
|
yading@11
|
25 #include "common.h"
|
yading@11
|
26
|
yading@11
|
27 #define MIN_EXP -126
|
yading@11
|
28 #define MAX_EXP 126
|
yading@11
|
29 #define ONE_BITS 29
|
yading@11
|
30
|
yading@11
|
31 typedef struct SoftFloat{
|
yading@11
|
32 int32_t exp;
|
yading@11
|
33 int32_t mant;
|
yading@11
|
34 }SoftFloat;
|
yading@11
|
35
|
yading@11
|
36 static av_const SoftFloat av_normalize_sf(SoftFloat a){
|
yading@11
|
37 if(a.mant){
|
yading@11
|
38 #if 1
|
yading@11
|
39 while((a.mant + 0x20000000U)<0x40000000U){
|
yading@11
|
40 a.mant += a.mant;
|
yading@11
|
41 a.exp -= 1;
|
yading@11
|
42 }
|
yading@11
|
43 #else
|
yading@11
|
44 int s=ONE_BITS + 1 - av_log2(a.mant ^ (a.mant<<1));
|
yading@11
|
45 a.exp -= s;
|
yading@11
|
46 a.mant <<= s;
|
yading@11
|
47 #endif
|
yading@11
|
48 if(a.exp < MIN_EXP){
|
yading@11
|
49 a.exp = MIN_EXP;
|
yading@11
|
50 a.mant= 0;
|
yading@11
|
51 }
|
yading@11
|
52 }else{
|
yading@11
|
53 a.exp= MIN_EXP;
|
yading@11
|
54 }
|
yading@11
|
55 return a;
|
yading@11
|
56 }
|
yading@11
|
57
|
yading@11
|
58 static inline av_const SoftFloat av_normalize1_sf(SoftFloat a){
|
yading@11
|
59 #if 1
|
yading@11
|
60 if(a.mant + 0x40000000 < 0){
|
yading@11
|
61 a.exp++;
|
yading@11
|
62 a.mant>>=1;
|
yading@11
|
63 }
|
yading@11
|
64 return a;
|
yading@11
|
65 #elif 1
|
yading@11
|
66 int t= a.mant + 0x40000000 < 0;
|
yading@11
|
67 return (SoftFloat){a.exp+t, a.mant>>t};
|
yading@11
|
68 #else
|
yading@11
|
69 int t= (a.mant + 0x40000000U)>>31;
|
yading@11
|
70 return (SoftFloat){a.exp+t, a.mant>>t};
|
yading@11
|
71 #endif
|
yading@11
|
72 }
|
yading@11
|
73
|
yading@11
|
74 /**
|
yading@11
|
75 * @return Will not be more denormalized than a+b. So if either input is
|
yading@11
|
76 * normalized, then the output will not be worse then the other input.
|
yading@11
|
77 * If both are normalized, then the output will be normalized.
|
yading@11
|
78 */
|
yading@11
|
79 static inline av_const SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
|
yading@11
|
80 a.exp += b.exp;
|
yading@11
|
81 a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
|
yading@11
|
82 return av_normalize1_sf(a);
|
yading@11
|
83 }
|
yading@11
|
84
|
yading@11
|
85 /**
|
yading@11
|
86 * b has to be normalized and not zero.
|
yading@11
|
87 * @return Will not be more denormalized than a.
|
yading@11
|
88 */
|
yading@11
|
89 static av_const SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
|
yading@11
|
90 a.exp -= b.exp+1;
|
yading@11
|
91 a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant;
|
yading@11
|
92 return av_normalize1_sf(a);
|
yading@11
|
93 }
|
yading@11
|
94
|
yading@11
|
95 static inline av_const int av_cmp_sf(SoftFloat a, SoftFloat b){
|
yading@11
|
96 int t= a.exp - b.exp;
|
yading@11
|
97 if(t<0) return (a.mant >> (-t)) - b.mant ;
|
yading@11
|
98 else return a.mant - (b.mant >> t);
|
yading@11
|
99 }
|
yading@11
|
100
|
yading@11
|
101 static inline av_const SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
|
yading@11
|
102 int t= a.exp - b.exp;
|
yading@11
|
103 if(t<0) return av_normalize1_sf((SoftFloat){b.exp, b.mant + (a.mant >> (-t))});
|
yading@11
|
104 else return av_normalize1_sf((SoftFloat){a.exp, a.mant + (b.mant >> t )});
|
yading@11
|
105 }
|
yading@11
|
106
|
yading@11
|
107 static inline av_const SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
|
yading@11
|
108 return av_add_sf(a, (SoftFloat){b.exp, -b.mant});
|
yading@11
|
109 }
|
yading@11
|
110
|
yading@11
|
111 //FIXME sqrt, log, exp, pow, sin, cos
|
yading@11
|
112
|
yading@11
|
113 static inline av_const SoftFloat av_int2sf(int v, int frac_bits){
|
yading@11
|
114 return av_normalize_sf((SoftFloat){ONE_BITS-frac_bits, v});
|
yading@11
|
115 }
|
yading@11
|
116
|
yading@11
|
117 /**
|
yading@11
|
118 * Rounding is to -inf.
|
yading@11
|
119 */
|
yading@11
|
120 static inline av_const int av_sf2int(SoftFloat v, int frac_bits){
|
yading@11
|
121 v.exp += frac_bits - ONE_BITS;
|
yading@11
|
122 if(v.exp >= 0) return v.mant << v.exp ;
|
yading@11
|
123 else return v.mant >>(-v.exp);
|
yading@11
|
124 }
|
yading@11
|
125
|
yading@11
|
126 #endif /* AVUTIL_SOFTFLOAT_H */
|