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