intfloat_readwrite.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * portable IEEE float/double read/write functions
  3. *
  4. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * portable IEEE float/double read/write functions
  25. */
  26. #include <stdint.h>
  27. #include "common.h"
  28. #include "mathematics.h"
  29. #include "intfloat_readwrite.h"
  30. #include "version.h"
  31. #if FF_API_INTFLOAT
  32. double av_int2dbl(int64_t v){
  33. if((uint64_t)v+v > 0xFFEULL<<52)
  34. return NAN;
  35. return ldexp(((v&((1LL<<52)-1)) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
  36. }
  37. float av_int2flt(int32_t v){
  38. if((uint32_t)v+v > 0xFF000000U)
  39. return NAN;
  40. return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
  41. }
  42. double av_ext2dbl(const AVExtFloat ext){
  43. uint64_t m = 0;
  44. int e, i;
  45. for (i = 0; i < 8; i++)
  46. m = (m<<8) + ext.mantissa[i];
  47. e = (((int)ext.exponent[0]&0x7f)<<8) | ext.exponent[1];
  48. if (e == 0x7fff && m)
  49. return NAN;
  50. e -= 16383 + 63; /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
  51. * mantissa bit is written as opposed to the
  52. * single and double precision formats. */
  53. if (ext.exponent[0]&0x80)
  54. m= -m;
  55. return ldexp(m, e);
  56. }
  57. int64_t av_dbl2int(double d){
  58. int e;
  59. if ( !d) return 0;
  60. else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
  61. d= frexp(d, &e);
  62. return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
  63. }
  64. int32_t av_flt2int(float d){
  65. int e;
  66. if ( !d) return 0;
  67. else if(d-d) return 0x7F800000 + ((d<0)<<31) + (d!=d);
  68. d= frexp(d, &e);
  69. return (d<0)<<31 | (e+126)<<23 | (int64_t)((fabs(d)-0.5)*(1<<24));
  70. }
  71. AVExtFloat av_dbl2ext(double d){
  72. struct AVExtFloat ext= {{0}};
  73. int e, i; double f; uint64_t m;
  74. f = fabs(frexp(d, &e));
  75. if (f >= 0.5 && f < 1) {
  76. e += 16382;
  77. ext.exponent[0] = e>>8;
  78. ext.exponent[1] = e;
  79. m = (uint64_t)ldexp(f, 64);
  80. for (i=0; i < 8; i++)
  81. ext.mantissa[i] = m>>(56-(i<<3));
  82. } else if (f != 0.0) {
  83. ext.exponent[0] = 0x7f; ext.exponent[1] = 0xff;
  84. if (!isinf(f))
  85. ext.mantissa[0] = ~0;
  86. }
  87. if (d < 0)
  88. ext.exponent[0] |= 0x80;
  89. return ext;
  90. }
  91. #endif /* FF_API_INTFLOAT */