]> git.sesse.net Git - ffmpeg/blob - libavutil/softfloat.c
Merge commit '83797da6e36c1aadd85f41ca237dce823fc7bfa1'
[ffmpeg] / libavutil / softfloat.c
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
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 #include <inttypes.h>
22 #include <stdio.h>
23 #include "softfloat.h"
24 #include "common.h"
25 #include "log.h"
26
27 #undef printf
28
29 static const SoftFloat FLOAT_0_017776489257 = {0x1234, 12};
30 static const SoftFloat FLOAT_1374_40625 = {0xabcd, 25};
31 static const SoftFloat FLOAT_0_1249694824218 = {0xFFF, 15};
32
33
34 static av_const double av_sf2double(SoftFloat v) {
35     v.exp -= ONE_BITS +1;
36     if(v.exp > 0) return (double)v.mant * (double)(1 << v.exp);
37     else          return (double)v.mant / (double)(1 << (-v.exp));
38 }
39
40 int main(void){
41     SoftFloat one= av_int2sf(1, 0);
42     SoftFloat sf1, sf2, sf3;
43     double d1, d2, d3;
44     int i, j;
45     av_log_set_level(AV_LOG_DEBUG);
46
47     d1= 1;
48     for(i= 0; i<10; i++){
49         d1= 1/(d1+1);
50     }
51     printf("test1 double=%d\n", (int)(d1 * (1<<24)));
52
53     sf1= one;
54     for(i= 0; i<10; i++){
55         sf1= av_div_sf(one, av_normalize_sf(av_add_sf(one, sf1)));
56     }
57     printf("test1 sf    =%d\n", av_sf2int(sf1, 24));
58
59
60     for(i= 0; i<100; i++){
61         START_TIMER
62         d1= i;
63         d2= i/100.0;
64         for(j= 0; j<1000; j++){
65             d1= (d1+1)*d2;
66         }
67         STOP_TIMER("float add mul")
68     }
69     printf("test2 double=%d\n", (int)(d1 * (1<<24)));
70
71     for(i= 0; i<100; i++){
72         START_TIMER
73         sf1= av_int2sf(i, 0);
74         sf2= av_div_sf(av_int2sf(i, 2), av_int2sf(200, 3));
75         for(j= 0; j<1000; j++){
76             sf1= av_mul_sf(av_add_sf(sf1, one),sf2);
77         }
78         STOP_TIMER("softfloat add mul")
79     }
80     printf("test2 sf    =%d (%d %d)\n", av_sf2int(sf1, 24), sf1.exp, sf1.mant);
81
82     d1 = 0.0177764893;
83     d2 = 1374.40625;
84     d3 = 0.1249694824;
85     d2 += d1;
86     d3 += d2;
87     printf("test3 double: %.10lf\n", d3);
88
89     sf1 = FLOAT_0_017776489257;
90     sf2 = FLOAT_1374_40625;
91     sf3 = FLOAT_0_1249694824218;
92     sf2 = av_add_sf(sf1, sf2);
93     sf3 = av_add_sf(sf3, sf2);
94     printf("test3 softfloat: %.10lf (0x%08x %d)\n", (double)av_sf2double(sf3), sf3.mant, sf3.exp);
95
96     return 0;
97
98 }