intfloat_readwrite.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. double av_int2dbl(int64_t v){
  31. if((uint64_t)v+v > 0xFFEULL<<52)
  32. return NAN;
  33. return ldexp(((v&((1LL<<52)-1)) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
  34. }
  35. float av_int2flt(int32_t v){
  36. if((uint32_t)v+v > 0xFF000000U)
  37. return NAN;
  38. return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
  39. }
  40. double av_ext2dbl(const AVExtFloat ext){
  41. uint64_t m = 0;
  42. int e, i;
  43. for (i = 0; i < 8; i++)
  44. m = (m<<8) + ext.mantissa[i];
  45. e = (((int)ext.exponent[0]&0x7f)<<8) | ext.exponent[1];
  46. if (e == 0x7fff && m)
  47. return NAN;
  48. e -= 16383 + 63; /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
  49. * mantissa bit is written as opposed to the
  50. * single and double precision formats. */
  51. if (ext.exponent[0]&0x80)
  52. m= -m;
  53. return ldexp(m, e);
  54. }
  55. int64_t av_dbl2int(double d){
  56. int e;
  57. if ( !d) return 0;
  58. else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
  59. d= frexp(d, &e);
  60. return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
  61. }
  62. int32_t av_flt2int(float d){
  63. int e;
  64. if ( !d) return 0;
  65. else if(d-d) return 0x7F800000 + ((d<0)<<31) + (d!=d);
  66. d= frexp(d, &e);
  67. return (d<0)<<31 | (e+126)<<23 | (int64_t)((fabs(d)-0.5)*(1<<24));
  68. }
  69. AVExtFloat av_dbl2ext(double d){
  70. struct AVExtFloat ext= {{0}};
  71. int e, i; double f; uint64_t m;
  72. f = fabs(frexp(d, &e));
  73. if (f >= 0.5 && f < 1) {
  74. e += 16382;
  75. ext.exponent[0] = e>>8;
  76. ext.exponent[1] = e;
  77. m = (uint64_t)ldexp(f, 64);
  78. for (i=0; i < 8; i++)
  79. ext.mantissa[i] = m>>(56-(i<<3));
  80. } else if (f != 0.0) {
  81. ext.exponent[0] = 0x7f; ext.exponent[1] = 0xff;
  82. if (!isinf(f))
  83. ext.mantissa[0] = ~0;
  84. }
  85. if (d < 0)
  86. ext.exponent[0] |= 0x80;
  87. return ext;
  88. }