• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavutil/intfloat_readwrite.c

Go to the documentation of this file.
00001 /*
00002  * portable IEEE float/double read/write functions
00003  *
00004  * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "common.h"
00029 #include "intfloat_readwrite.h"
00030 
00031 double av_int2dbl(int64_t v){
00032     if(v+v > 0xFFEULL<<52)
00033         return 0.0/0.0;
00034     return ldexp(((v&((1LL<<52)-1)) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
00035 }
00036 
00037 float av_int2flt(int32_t v){
00038     if(v+v > 0xFF000000U)
00039         return 0.0/0.0;
00040     return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
00041 }
00042 
00043 double av_ext2dbl(const AVExtFloat ext){
00044     uint64_t m = 0;
00045     int e, i;
00046 
00047     for (i = 0; i < 8; i++)
00048         m = (m<<8) + ext.mantissa[i];
00049     e = (((int)ext.exponent[0]&0x7f)<<8) | ext.exponent[1];
00050     if (e == 0x7fff && m)
00051         return 0.0/0.0;
00052     e -= 16383 + 63;        /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
00053                              * mantissa bit is written as opposed to the
00054                              * single and double precision formats. */
00055     if (ext.exponent[0]&0x80)
00056         m= -m;
00057     return ldexp(m, e);
00058 }
00059 
00060 int64_t av_dbl2int(double d){
00061     int e;
00062     if     ( !d) return 0;
00063     else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
00064     d= frexp(d, &e);
00065     return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
00066 }
00067 
00068 int32_t av_flt2int(float d){
00069     int e;
00070     if     ( !d) return 0;
00071     else if(d-d) return 0x7F800000 + ((d<0)<<31) + (d!=d);
00072     d= frexp(d, &e);
00073     return (d<0)<<31 | (e+126)<<23 | (int64_t)((fabs(d)-0.5)*(1<<24));
00074 }
00075 
00076 AVExtFloat av_dbl2ext(double d){
00077     struct AVExtFloat ext= {{0}};
00078     int e, i; double f; uint64_t m;
00079 
00080     f = fabs(frexp(d, &e));
00081     if (f >= 0.5 && f < 1) {
00082         e += 16382;
00083         ext.exponent[0] = e>>8;
00084         ext.exponent[1] = e;
00085         m = (uint64_t)ldexp(f, 64);
00086         for (i=0; i < 8; i++)
00087             ext.mantissa[i] = m>>(56-(i<<3));
00088     } else if (f != 0.0) {
00089         ext.exponent[0] = 0x7f; ext.exponent[1] = 0xff;
00090         if (f != 1/0.0)
00091             ext.mantissa[0] = ~0;
00092     }
00093     if (d < 0)
00094         ext.exponent[0] |= 0x80;
00095     return ext;
00096 }
00097 

Generated on Sat Feb 16 2013 09:23:15 for ffmpeg by  doxygen 1.7.1